1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/types.h>
32 #ifdef HAVE_SYS_STATVFS_H
33 #include <sys/statvfs.h>
38 #include "../src/guestfs_protocol.h"
43 do_statvfs (const char *path)
47 guestfs_int_statvfs *ret;
48 struct statvfs statbuf;
51 r = statvfs (path, &statbuf);
55 reply_with_perror ("statvfs");
59 ret = malloc (sizeof *ret);
61 reply_with_perror ("malloc");
65 ret->bsize = statbuf.f_bsize;
66 ret->frsize = statbuf.f_frsize;
67 ret->blocks = statbuf.f_blocks;
68 ret->bfree = statbuf.f_bfree;
69 ret->bavail = statbuf.f_bavail;
70 ret->files = statbuf.f_files;
71 ret->ffree = statbuf.f_ffree;
72 ret->favail = statbuf.f_favail;
73 ret->fsid = statbuf.f_fsid;
74 ret->flag = statbuf.f_flag;
75 ret->namemax = statbuf.f_namemax;
79 #else /* !HAVE_STATVFS */
83 guestfs_int_statvfs *ret;
84 ULONGLONG free_bytes_available; /* for user - similar to bavail */
85 ULONGLONG total_number_of_bytes;
86 ULONGLONG total_number_of_free_bytes; /* for everyone - bfree */
88 disk = sysroot_path (path);
90 reply_with_perror ("malloc");
94 if (!GetDiskFreeSpaceEx (disk,
95 (PULARGE_INTEGER) &free_bytes_available,
96 (PULARGE_INTEGER) &total_number_of_bytes,
97 (PULARGE_INTEGER) &total_number_of_free_bytes)) {
98 reply_with_perror ("GetDiskFreeSpaceEx");
104 ret = malloc (sizeof *ret);
106 reply_with_perror ("malloc");
110 /* XXX I couldn't determine how to get block size. MSDN has a
111 * unhelpful hard-coded list here:
112 * http://support.microsoft.com/kb/140365
113 * but this depends on the filesystem type, the size of the disk and
114 * the version of Windows. So this code assumes the disk is NTFS
115 * and the version of Windows is >= Win2K.
117 if (total_number_of_bytes < 16ULL * 1024 * 1024 * 1024 * 1024)
119 else if (total_number_of_bytes < 32ULL * 1024 * 1024 * 1024 * 1024)
121 else if (total_number_of_bytes < 64ULL * 1024 * 1024 * 1024 * 1024)
123 else if (total_number_of_bytes < 128ULL * 1024 * 1024 * 1024 * 1024)
128 /* As with stat, -1 indicates a field is not known. */
129 ret->frsize = ret->bsize;
130 ret->blocks = total_number_of_bytes / ret->bsize;
131 ret->bfree = total_number_of_free_bytes / ret->bsize;
132 ret->bavail = free_bytes_available / ret->bsize;
138 ret->namemax = FILENAME_MAX;
146 guestfs_int_statvfs *ret;
149 disk = sysroot_path (path);
151 reply_with_perror ("malloc");
155 r = get_fs_usage (disk, disk, &fsu);
159 reply_with_perror ("get_fs_usage: %s", path);
163 ret = malloc (sizeof *ret);
165 reply_with_perror ("malloc");
169 /* As with stat, -1 indicates a field is not known. */
170 ret->bsize = fsu.f_bsize;
172 ret->blocks = fsu.f_blocks;
173 ret->bfree = fsu.f_bfree;
174 ret->bavail = fsu.f_bavail;
175 ret->files = fsu.f_files;
176 ret->ffree = fsu.f_ffree;
185 #endif /* !HAVE_STATVFS */