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>
33 #ifdef HAVE_SYS_STATVFS_H
34 #include <sys/statvfs.h>
39 #include "guestfs_protocol.h"
44 do_statvfs (const char *path)
48 guestfs_int_statvfs *ret;
49 struct statvfs statbuf;
52 r = statvfs (path, &statbuf);
56 reply_with_perror ("statvfs");
60 ret = malloc (sizeof *ret);
62 reply_with_perror ("malloc");
66 ret->bsize = statbuf.f_bsize;
67 ret->frsize = statbuf.f_frsize;
68 ret->blocks = statbuf.f_blocks;
69 ret->bfree = statbuf.f_bfree;
70 ret->bavail = statbuf.f_bavail;
71 ret->files = statbuf.f_files;
72 ret->ffree = statbuf.f_ffree;
73 ret->favail = statbuf.f_favail;
74 ret->fsid = statbuf.f_fsid;
75 ret->flag = statbuf.f_flag;
76 ret->namemax = statbuf.f_namemax;
80 #else /* !HAVE_STATVFS */
84 guestfs_int_statvfs *ret;
85 ULONGLONG free_bytes_available; /* for user - similar to bavail */
86 ULONGLONG total_number_of_bytes;
87 ULONGLONG total_number_of_free_bytes; /* for everyone - bfree */
89 disk = sysroot_path (path);
91 reply_with_perror ("malloc");
95 if (!GetDiskFreeSpaceEx (disk,
96 (PULARGE_INTEGER) &free_bytes_available,
97 (PULARGE_INTEGER) &total_number_of_bytes,
98 (PULARGE_INTEGER) &total_number_of_free_bytes)) {
99 reply_with_perror ("GetDiskFreeSpaceEx");
105 ret = malloc (sizeof *ret);
107 reply_with_perror ("malloc");
111 /* XXX I couldn't determine how to get block size. MSDN has a
112 * unhelpful hard-coded list here:
113 * http://support.microsoft.com/kb/140365
114 * but this depends on the filesystem type, the size of the disk and
115 * the version of Windows. So this code assumes the disk is NTFS
116 * and the version of Windows is >= Win2K.
118 if (total_number_of_bytes < UINT64_C(16) * 1024 * 1024 * 1024 * 1024)
120 else if (total_number_of_bytes < UINT64_C(32) * 1024 * 1024 * 1024 * 1024)
122 else if (total_number_of_bytes < UINT64_C(64) * 1024 * 1024 * 1024 * 1024)
124 else if (total_number_of_bytes < UINT64_C(128) * 1024 * 1024 * 1024 * 1024)
129 /* As with stat, -1 indicates a field is not known. */
130 ret->frsize = ret->bsize;
131 ret->blocks = total_number_of_bytes / ret->bsize;
132 ret->bfree = total_number_of_free_bytes / ret->bsize;
133 ret->bavail = free_bytes_available / ret->bsize;
139 ret->namemax = FILENAME_MAX;
147 guestfs_int_statvfs *ret;
150 disk = sysroot_path (path);
152 reply_with_perror ("malloc");
156 r = get_fs_usage (disk, disk, &fsu);
160 reply_with_perror ("get_fs_usage: %s", path);
164 ret = malloc (sizeof *ret);
166 reply_with_perror ("malloc");
170 /* As with stat, -1 indicates a field is not known. */
171 ret->bsize = fsu.f_bsize;
173 ret->blocks = fsu.f_blocks;
174 ret->bfree = fsu.f_bfree;
175 ret->bavail = fsu.f_bavail;
176 ret->files = fsu.f_files;
177 ret->ffree = fsu.f_ffree;
186 #endif /* !HAVE_STATVFS */