Welcome to mirror list, hosted at ThFree Co, Russian Federation.

disk.c « zbxwin32 « libs « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da2bdebe1be665d459f2ba8ed5bfcde57af2579c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "zbxwin32.h"

#include "zbxstr.h"
#include "log.h"

/******************************************************************************
 *                                                                            *
 * Purpose: get file system cluster size for specified path (for cases when   *
 *          the file system is mounted on empty NTFS directory)               *
 *                                                                            *
 * Parameters: path  - [IN] file system path                                  *
 *             error - [OUT] error message                                    *
 *                                                                            *
 * Return value: On success, nonzero cluster size is returned                 *
 *               On error, 0 is returned.                                     *
 *                                                                            *
 ******************************************************************************/
zbx_uint64_t	zbx_get_cluster_size(const char *path, char **error)
{
	wchar_t 	*disk = NULL, *wpath = NULL;
	unsigned long	sectors_per_cluster, bytes_per_sector, path_length;
	zbx_uint64_t	res = 0;
	char		*err_msg = "Cannot obtain file system cluster size:";

	wpath = zbx_utf8_to_unicode(path);

	/* Here GetFullPathName() is used in multithreaded application. */
	/* We assume it is safe because: */
	/*   - only file names with absolute paths are used (i.e. no relative paths) and */
	/*   - SetCurrentDirectory() is not used in Zabbix agent. */

	if (0 == (path_length = GetFullPathName(wpath, 0, NULL, NULL) + 1))
	{
		*error = zbx_dsprintf(*error, "%s GetFullPathName() failed: %s", err_msg,
				strerror_from_system(GetLastError()));
		goto err;
	}

	disk = (wchar_t *)zbx_malloc(NULL, path_length * sizeof(wchar_t));

	if (0 == GetVolumePathName(wpath, disk, path_length))
	{
		*error = zbx_dsprintf(*error, "%s GetVolumePathName() failed: %s", err_msg,
				strerror_from_system(GetLastError()));
		goto err;
	}

	if (0 == GetDiskFreeSpace(disk, &sectors_per_cluster, &bytes_per_sector, NULL, NULL))
	{
		*error = zbx_dsprintf(*error, "%s GetDiskFreeSpace() failed: %s", err_msg,
				strerror_from_system(GetLastError()));
		goto err;
	}

	res = (zbx_uint64_t)sectors_per_cluster * bytes_per_sector;
err:
	zbx_free(disk);
	zbx_free(wpath);

	return res;
}