New test.
[mono.git] / support / sys-statvfs.c
1 /*
2  * <sys/sendfile.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004-2006 Jonathan Pryor
8  */
9
10 #include <errno.h>
11
12 #include <string.h>
13
14 #include "mph.h"
15 #include "map.h"
16
17 #ifdef HAVE_SYS_STATVFS_H
18 #include <sys/statvfs.h>
19 #endif /* def HAVE_SYS_STATVFS_H */
20
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 */
27
28 G_BEGIN_DECLS
29
30 #ifdef HAVE_SYS_STATVFS_H
31 int
32 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
33 {
34         struct statvfs *from = _from;
35
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;
46
47         if (Mono_Posix_ToMountFlags (from->f_flag, &to->f_flag) != 0)
48                 return -1;
49
50         return 0;
51 }
52
53 int
54 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
55 {
56         struct statvfs *to = _to;
57         guint64 flag;
58
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;
69
70         if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
71                 return -1;
72         to->f_flag = flag;
73
74         return 0;
75 }
76 #endif /* ndef HAVE_SYS_STATVFS_H */
77
78 /*
79  * System V-compatible definitions
80  */
81
82 #ifdef HAVE_STATVFS
83 gint32
84 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
85 {
86         struct statvfs s;
87         int r;
88
89         if (buf == NULL) {
90                 errno = EFAULT;
91                 return -1;
92         }
93
94         if ((r = statvfs (path, &s)) == 0)
95                 r = Mono_Posix_ToStatvfs (&s, buf);
96
97         return r;
98 }
99 #endif /* ndef HAVA_STATVFS */
100
101 #ifdef HAVE_FSTATVFS
102 gint32
103 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
104 {
105         struct statvfs s;
106         int r;
107
108         if (buf == NULL) {
109                 errno = EFAULT;
110                 return -1;
111         }
112
113         if ((r = fstatvfs (fd, &s)) == 0)
114                 r = Mono_Posix_ToStatvfs (&s, buf);
115
116         return r;
117 }
118 #endif /* ndef HAVA_FSTATVFS */
119
120 /*
121  * BSD-compatible definitions.
122  *
123  * Linux also provides these, but are deprecated in favor of (f)statvfs.
124  */
125
126 #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS)
127 int
128 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
129 {
130         struct statfs *from = _from;
131
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 */
140
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));
144
145         if (Mono_Posix_ToMountFlags (from->f_flags, &to->f_flag) != 0)
146                 return -1;
147
148         return 0;
149 }
150
151 int
152 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
153 {
154         struct statfs *to = _to;
155         guint64 flag;
156
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;
163
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));
167
168         if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
169                 return -1;
170         to->f_flags = flag;
171
172         return 0;
173 }
174
175 static void
176 set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
177 {
178   buf->f_namemax = pathconf (path, _PC_NAME_MAX);
179 }
180
181 static void
182 set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
183 {
184   buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
185 }
186 #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
187
188 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
189 gint32
190 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
191 {
192         struct statfs s;
193         int r;
194
195         if (buf == NULL) {
196                 errno = EFAULT;
197                 return -1;
198         }
199
200         if ((r = statfs (path, &s)) == 0 &&
201                         (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
202                 set_namemax (path, buf);
203         }
204
205         return r;
206 }
207 #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
208
209 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
210 gint32
211 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
212 {
213         struct statfs s;
214         int r;
215
216         if (buf == NULL) {
217                 errno = EFAULT;
218                 return -1;
219         }
220
221         if ((r = fstatfs (fd, &s)) == 0 &&
222                         (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
223                 set_fnamemax (fd, buf);
224         }
225
226         return r;
227 }
228 #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
229
230 G_END_DECLS
231
232 /*
233  * vim: noexpandtab
234  */