Prepare Mono for Android NDK with unified headers (#5680)
[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_PATHCONF_H
18 #include <pathconf.h>
19 #endif
20
21 #ifdef HAVE_SYS_STATVFS_H
22 #include <sys/statvfs.h>
23 #elif defined (HAVE_STATFS) || defined (HAVE_FSTATFS)
24 #include <sys/vfs.h>
25 #endif /* def HAVE_SYS_STATVFS_H */
26
27 #ifdef HAVE_GETFSSTAT
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
30 #endif
31 #include <sys/ucred.h>
32 #include <sys/mount.h>
33 #include <unistd.h>     /* for pathconf */
34 #endif /* def HAVE_GETFSSTAT */
35
36 #include "mono/utils/mono-compiler.h"
37
38 G_BEGIN_DECLS
39
40 #ifdef HAVE_SYS_STATVFS_H
41 int
42 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
43 {
44         struct statvfs *from = _from;
45
46         to->f_bsize   = from->f_bsize;
47         to->f_frsize  = from->f_frsize;
48         to->f_blocks  = from->f_blocks;
49         to->f_bfree   = from->f_bfree;
50         to->f_bavail  = from->f_bavail;
51         to->f_files   = from->f_files;
52         to->f_ffree   = from->f_ffree;
53         to->f_favail  = from->f_favail;
54         to->f_fsid    = from->f_fsid;
55         to->f_namemax = from->f_namemax;
56
57         if (Mono_Posix_ToMountFlags (from->f_flag, &to->f_flag) != 0)
58                 return -1;
59
60         return 0;
61 }
62
63 int
64 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
65 {
66         struct statvfs *to = _to;
67         guint64 flag;
68
69         to->f_bsize   = from->f_bsize;
70         to->f_frsize  = from->f_frsize;
71         to->f_blocks  = from->f_blocks;
72         to->f_bfree   = from->f_bfree;
73         to->f_bavail  = from->f_bavail;
74         to->f_files   = from->f_files;
75         to->f_ffree   = from->f_ffree;
76         to->f_favail  = from->f_favail;
77         to->f_fsid    = from->f_fsid;
78         to->f_namemax = from->f_namemax;
79
80         if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
81                 return -1;
82         to->f_flag = flag;
83
84         return 0;
85 }
86 #endif /* ndef HAVE_SYS_STATVFS_H */
87
88 /*
89  * System V-compatible definitions
90  */
91
92 #ifdef HAVE_STATVFS
93 gint32
94 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
95 {
96         struct statvfs s;
97         int r;
98
99         if (buf == NULL) {
100                 errno = EFAULT;
101                 return -1;
102         }
103
104         if ((r = statvfs (path, &s)) == 0)
105                 r = Mono_Posix_ToStatvfs (&s, buf);
106
107         return r;
108 }
109 #endif /* ndef HAVA_STATVFS */
110
111 #ifdef HAVE_FSTATVFS
112 gint32
113 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
114 {
115         struct statvfs s;
116         int r;
117
118         if (buf == NULL) {
119                 errno = EFAULT;
120                 return -1;
121         }
122
123         if ((r = fstatvfs (fd, &s)) == 0)
124                 r = Mono_Posix_ToStatvfs (&s, buf);
125
126         return r;
127 }
128 #endif /* ndef HAVA_FSTATVFS */
129
130 /*
131  * BSD-compatible definitions.
132  *
133  * Linux also provides these, but are deprecated in favor of (f)statvfs.
134  * Android NDK unified headers define HAVE_FSTATFS but also HAVE_SYS_STATVFS_H
135  * which makes these duplicates of the functions defined above
136  */
137
138 #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS) && !defined(ANDROID_UNIFIED_HEADERS)
139 int
140 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
141 {
142         struct statfs *from = _from;
143
144         to->f_bsize   = from->f_bsize;
145         to->f_frsize  = from->f_bsize;
146         to->f_blocks  = from->f_blocks;
147         to->f_bfree   = from->f_bfree;
148         to->f_bavail  = from->f_bavail;
149         to->f_files   = from->f_files;
150         to->f_ffree   = from->f_ffree;
151         to->f_favail  = from->f_ffree; /* OSX doesn't have f_avail */
152
153         // from->f_fsid is an int32[2], to->f_fsid is a uint64, 
154         // so this shouldn't lose anything.
155         memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
156
157 #if HAVE_STRUCT_STATFS_F_FLAGS
158         if (Mono_Posix_ToMountFlags (from->f_flags, &to->f_flag) != 0)
159                 return -1;
160 #endif  /* def HAVE_STRUCT_STATFS_F_FLAGS */
161
162         return 0;
163 }
164
165 int
166 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
167 {
168         struct statfs *to = _to;
169         guint64 flag;
170
171         to->f_bsize   = from->f_bsize;
172         to->f_blocks  = from->f_blocks;
173         to->f_bfree   = from->f_bfree;
174         to->f_bavail  = from->f_bavail;
175         to->f_files   = from->f_files;
176         to->f_ffree   = from->f_ffree;
177
178         // from->f_fsid is an int32[2], to->f_fsid is a uint64, 
179         // so this shouldn't lose anything.
180         memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
181
182 #if HAVE_STRUCT_STATFS_F_FLAGS
183         if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
184                 return -1;
185         to->f_flags = flag;
186 #endif  /* def HAVE_STRUCT_STATFS_F_FLAGS */
187
188         return 0;
189 }
190
191 static void
192 set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
193 {
194   buf->f_namemax = pathconf (path, _PC_NAME_MAX);
195 }
196
197 static void
198 set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
199 {
200   buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
201 }
202 #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
203
204 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS) && (!defined(ANDROID_UNIFIED_HEADERS) || __ANDROID_API__ >= 19)
205 gint32
206 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
207 {
208         struct statfs s;
209         int r;
210
211         if (buf == NULL) {
212                 errno = EFAULT;
213                 return -1;
214         }
215
216         if ((r = statfs (path, &s)) == 0 &&
217                         (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
218                 set_namemax (path, buf);
219         }
220
221         return r;
222 }
223 #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
224
225 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS) && (!defined(ANDROID_UNIFIED_HEADERS) || __ANDROID_API__ >= 19)
226 gint32
227 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
228 {
229         struct statfs s;
230         int r;
231
232         if (buf == NULL) {
233                 errno = EFAULT;
234                 return -1;
235         }
236
237         if ((r = fstatfs (fd, &s)) == 0 &&
238                         (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
239                 set_fnamemax (fd, buf);
240         }
241
242         return r;
243 }
244 #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
245
246 G_END_DECLS
247
248 /*
249  * vim: noexpandtab
250  */