e71b19a78e399aa486d286e90999b1450f0648c3
[libguestfs.git] / daemon / statvfs.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #ifdef HAVE_WINDOWS_H
22 #include <windows.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #ifdef HAVE_SYS_STATVFS_H
33 #include <sys/statvfs.h>
34 #endif
35
36 #include <fsusage.h>
37
38 #include "../src/guestfs_protocol.h"
39 #include "daemon.h"
40 #include "actions.h"
41
42 guestfs_int_statvfs *
43 do_statvfs (const char *path)
44 {
45 #ifdef HAVE_STATVFS
46   int r;
47   guestfs_int_statvfs *ret;
48   struct statvfs statbuf;
49
50   CHROOT_IN;
51   r = statvfs (path, &statbuf);
52   CHROOT_OUT;
53
54   if (r == -1) {
55     reply_with_perror ("statvfs");
56     return NULL;
57   }
58
59   ret = malloc (sizeof *ret);
60   if (ret == NULL) {
61     reply_with_perror ("malloc");
62     return NULL;
63   }
64
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;
76
77   return ret;
78
79 #else /* !HAVE_STATVFS */
80 #  if WIN32
81
82   char *disk;
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 */
87
88   disk = sysroot_path (path);
89   if (!disk) {
90     reply_with_perror ("malloc");
91     return NULL;
92   }
93
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");
99     free (disk);
100     return NULL;
101   }
102   free (disk);
103
104   ret = malloc (sizeof *ret);
105   if (ret == NULL) {
106     reply_with_perror ("malloc");
107     return NULL;
108   }
109
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.
116    */
117   if (total_number_of_bytes < 16ULL * 1024 * 1024 * 1024 * 1024)
118     ret->bsize = 4096;
119   else if (total_number_of_bytes < 32ULL * 1024 * 1024 * 1024 * 1024)
120     ret->bsize = 8192;
121   else if (total_number_of_bytes < 64ULL * 1024 * 1024 * 1024 * 1024)
122     ret->bsize = 16384;
123   else if (total_number_of_bytes < 128ULL * 1024 * 1024 * 1024 * 1024)
124     ret->bsize = 32768;
125   else
126     ret->bsize = 65536;
127
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;
133   ret->files = -1;
134   ret->ffree = -1;
135   ret->favail = -1;
136   ret->fsid = -1;
137   ret->flag = -1;
138   ret->namemax = FILENAME_MAX;
139
140   return ret;
141
142 #  else /* !WIN32 */
143
144   char *disk;
145   int r;
146   guestfs_int_statvfs *ret;
147   struct fs_usage fsu;
148
149   disk = sysroot_path (path);
150   if (!disk) {
151     reply_with_perror ("malloc");
152     return NULL;
153   }
154
155   r = get_fs_usage (disk, disk, &fsu);
156   free (disk);
157
158   if (r == -1) {
159     reply_with_perror ("get_fs_usage: %s", path);
160     return NULL;
161   }
162
163   ret = malloc (sizeof *ret);
164   if (ret == NULL) {
165     reply_with_perror ("malloc");
166     return NULL;
167   }
168
169   /* As with stat, -1 indicates a field is not known. */
170   ret->bsize = fsu.f_bsize;
171   ret->frsize = -1;
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;
177   ret->favail = -1;
178   ret->fsid = -1;
179   ret->flag = -1;
180   ret->namemax = -1;
181
182   return ret;
183
184 #  endif /* !WIN32 */
185 #endif /* !HAVE_STATVFS */
186 }