aboutsummaryrefslogtreecommitdiff
path: root/win32/statfs.c
blob: a35c9adeaaa25f9a888de6faa636c4b7844cb013 (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
#include <sys/vfs.h>
#include "libbb.h"

/*
 * Code from libguestfs (with addition of GetVolumeInformation call)
 */
int statfs(const char *file, struct statfs *buf)
{
	ULONGLONG free_bytes_available;         /* for user - similar to bavail */
	ULONGLONG total_number_of_bytes;
	ULONGLONG total_number_of_free_bytes;   /* for everyone - bfree */
	DWORD serial, namelen, flags;
	char fsname[100];
	struct mntent *mnt;

	if ( (mnt=find_mount_point(file, 0)) == NULL ) {
		return -1;
	}

	file = mnt->mnt_dir;
	if ( !GetDiskFreeSpaceEx(file, (PULARGE_INTEGER) &free_bytes_available,
			(PULARGE_INTEGER) &total_number_of_bytes,
			(PULARGE_INTEGER) &total_number_of_free_bytes) ) {
		errno = err_win_to_posix(GetLastError());
		return -1;
	}

	if ( !GetVolumeInformation(file, NULL, 0, &serial, &namelen, &flags,
								fsname, 100) ) {
		errno = err_win_to_posix(GetLastError());
		return -1;
	}

	/* XXX I couldn't determine how to get block size.  MSDN has a
	 * unhelpful hard-coded list here:
	 * http://support.microsoft.com/kb/140365
	 * but this depends on the filesystem type, the size of the disk and
	 * the version of Windows.  So this code assumes the disk is NTFS
	 * and the version of Windows is >= Win2K.
	 */
	if (total_number_of_bytes < UINT64_C(16) * 1024 * 1024 * 1024 * 1024)
		buf->f_bsize = 4096;
	else if (total_number_of_bytes < UINT64_C(32) * 1024 * 1024 * 1024 * 1024)
		buf->f_bsize = 8192;
	else if (total_number_of_bytes < UINT64_C(64) * 1024 * 1024 * 1024 * 1024)
		buf->f_bsize = 16384;
	else if (total_number_of_bytes < UINT64_C(128) * 1024 * 1024 * 1024 * 1024)
		buf->f_bsize = 32768;
	else
		buf->f_bsize = 65536;

	/*
	 * Valid filesystem names don't seem to be documented.  The following
	 * are present in Wine.
	 */
	if ( strcmp(fsname, "NTFS") == 0 ) {
		buf->f_type = 0x5346544e;
	}
	else if ( strcmp(fsname, "FAT") == 0 || strcmp(fsname, "FAT32") == 0 ) {
		buf->f_type = 0x4006;
	}
	else if ( strcmp(fsname, "CDFS") == 0 ) {
		buf->f_type = 0x9660;
	}
	else {
		buf->f_type = 0;
	}

	buf->f_frsize = buf->f_bsize;
	buf->f_blocks = total_number_of_bytes / buf->f_bsize;
	buf->f_bfree = total_number_of_free_bytes / buf->f_bsize;
	buf->f_bavail = free_bytes_available / buf->f_bsize;
	buf->f_files = UINT32_MAX;
	buf->f_ffree = UINT32_MAX;
	buf->f_fsid = serial;
	buf->f_flag = UINT64_MAX;
	buf->f_namelen = namelen;

	return 0;
}