2010-04-09 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / support / sys-statvfs.c
blob60fecdd92c5a11832f12ae1ac9010aebf6831068
1 /*
2 * <sys/sendfile.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
8 */
10 #include <errno.h>
12 #include <string.h>
14 #include "mph.h"
15 #include "map.h"
17 #ifdef HAVE_SYS_STATVFS_H
18 #include <sys/statvfs.h>
19 #endif /* def HAVE_SYS_STATVFS_H */
21 #ifdef HAVE_GETFSSTAT
22 #include <sys/param.h>
23 #include <sys/ucred.h>
24 #include <sys/mount.h>
25 #include <unistd.h> /* for pathconf */
26 #endif /* def HAVE_GETFSSTAT */
28 G_BEGIN_DECLS
30 #ifdef HAVE_SYS_STATVFS_H
31 int
32 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
34 struct statvfs *from = _from;
36 to->f_bsize = from->f_bsize;
37 to->f_frsize = from->f_frsize;
38 to->f_blocks = from->f_blocks;
39 to->f_bfree = from->f_bfree;
40 to->f_bavail = from->f_bavail;
41 to->f_files = from->f_files;
42 to->f_ffree = from->f_ffree;
43 to->f_favail = from->f_favail;
44 to->f_fsid = from->f_fsid;
45 to->f_namemax = from->f_namemax;
47 if (Mono_Posix_ToMountFlags (from->f_flag, &to->f_flag) != 0)
48 return -1;
50 return 0;
53 int
54 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
56 struct statvfs *to = _to;
57 guint64 flag;
59 to->f_bsize = from->f_bsize;
60 to->f_frsize = from->f_frsize;
61 to->f_blocks = from->f_blocks;
62 to->f_bfree = from->f_bfree;
63 to->f_bavail = from->f_bavail;
64 to->f_files = from->f_files;
65 to->f_ffree = from->f_ffree;
66 to->f_favail = from->f_favail;
67 to->f_fsid = from->f_fsid;
68 to->f_namemax = from->f_namemax;
70 if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
71 return -1;
72 to->f_flag = flag;
74 return 0;
76 #endif /* ndef HAVE_SYS_STATVFS_H */
79 * System V-compatible definitions
82 #ifdef HAVE_STATVFS
83 gint32
84 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
86 struct statvfs s;
87 int r;
89 if (buf == NULL) {
90 errno = EFAULT;
91 return -1;
94 if ((r = statvfs (path, &s)) == 0)
95 r = Mono_Posix_ToStatvfs (&s, buf);
97 return r;
99 #endif /* ndef HAVA_STATVFS */
101 #ifdef HAVE_FSTATVFS
102 gint32
103 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
105 struct statvfs s;
106 int r;
108 if (buf == NULL) {
109 errno = EFAULT;
110 return -1;
113 if ((r = fstatvfs (fd, &s)) == 0)
114 r = Mono_Posix_ToStatvfs (&s, buf);
116 return r;
118 #endif /* ndef HAVA_FSTATVFS */
121 * BSD-compatible definitions.
123 * Linux also provides these, but are deprecated in favor of (f)statvfs.
126 #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS)
128 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
130 struct statfs *from = _from;
132 to->f_bsize = from->f_bsize;
133 to->f_frsize = from->f_bsize;
134 to->f_blocks = from->f_blocks;
135 to->f_bfree = from->f_bfree;
136 to->f_bavail = from->f_bavail;
137 to->f_files = from->f_files;
138 to->f_ffree = from->f_ffree;
139 to->f_favail = from->f_ffree; /* OSX doesn't have f_avail */
141 // from->f_fsid is an int32[2], to->f_fsid is a uint64,
142 // so this shouldn't lose anything.
143 memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
145 if (Mono_Posix_ToMountFlags (from->f_flags, &to->f_flag) != 0)
146 return -1;
148 return 0;
152 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
154 struct statfs *to = _to;
155 guint64 flag;
157 to->f_bsize = from->f_bsize;
158 to->f_blocks = from->f_blocks;
159 to->f_bfree = from->f_bfree;
160 to->f_bavail = from->f_bavail;
161 to->f_files = from->f_files;
162 to->f_ffree = from->f_ffree;
164 // from->f_fsid is an int32[2], to->f_fsid is a uint64,
165 // so this shouldn't lose anything.
166 memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
168 if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
169 return -1;
170 to->f_flags = flag;
172 return 0;
175 static void
176 set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
178 buf->f_namemax = pathconf (path, _PC_NAME_MAX);
181 static void
182 set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
184 buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
186 #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
188 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
189 gint32
190 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
192 struct statfs s;
193 int r;
195 if (buf == NULL) {
196 errno = EFAULT;
197 return -1;
200 if ((r = statfs (path, &s)) == 0 &&
201 (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
202 set_namemax (path, buf);
205 return r;
207 #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
209 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
210 gint32
211 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
213 struct statfs s;
214 int r;
216 if (buf == NULL) {
217 errno = EFAULT;
218 return -1;
221 if ((r = fstatfs (fd, &s)) == 0 &&
222 (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
223 set_fnamemax (fd, buf);
226 return r;
228 #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
230 G_END_DECLS
233 * vim: noexpandtab