[interp] Non-recursive newobj. (#18931)
[mono-project.git] / support / sys-statvfs.c
blobd75a658f925686c245f6ccfc008c84c0c986e76d
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_PATHCONF_H
18 #include <pathconf.h>
19 #endif
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 */
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 */
36 #include "mono/utils/mono-compiler.h"
38 G_BEGIN_DECLS
40 #ifdef HAVE_SYS_STATVFS_H
41 int
42 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
44 struct statvfs *from = _from;
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;
55 * On AIX with -D_ALL_SOURCE, fsid_t is a struct
56 * See: github.com/python/cpython/pull/4972
58 #if defined(_AIX) && defined(_ALL_SOURCE)
59 to->f_fsid = from->f_fsid.val[0];
60 #else
61 to->f_fsid = from->f_fsid;
62 #endif
63 to->f_namemax = from->f_namemax;
65 if (Mono_Posix_ToMountFlags (from->f_flag, &to->f_flag) != 0)
66 return -1;
68 return 0;
71 int
72 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
74 struct statvfs *to = _to;
75 guint64 flag;
77 to->f_bsize = from->f_bsize;
78 to->f_frsize = from->f_frsize;
79 to->f_blocks = from->f_blocks;
80 to->f_bfree = from->f_bfree;
81 to->f_bavail = from->f_bavail;
82 to->f_files = from->f_files;
83 to->f_ffree = from->f_ffree;
84 to->f_favail = from->f_favail;
85 #if defined(_AIX) && defined(_ALL_SOURCE)
86 to->f_fsid.val[0] = from->f_fsid;
87 #else
88 to->f_fsid = from->f_fsid;
89 #endif
90 to->f_namemax = from->f_namemax;
92 if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
93 return -1;
94 to->f_flag = flag;
96 return 0;
98 #endif /* ndef HAVE_SYS_STATVFS_H */
101 * System V-compatible definitions
104 #ifdef HAVE_STATVFS
105 gint32
106 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
108 struct statvfs s;
109 int r;
111 if (buf == NULL) {
112 errno = EFAULT;
113 return -1;
116 if ((r = statvfs (path, &s)) == 0)
117 r = Mono_Posix_ToStatvfs (&s, buf);
119 return r;
121 #endif /* ndef HAVA_STATVFS */
123 #ifdef HAVE_FSTATVFS
124 gint32
125 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
127 struct statvfs s;
128 int r;
130 if (buf == NULL) {
131 errno = EFAULT;
132 return -1;
135 if ((r = fstatvfs (fd, &s)) == 0)
136 r = Mono_Posix_ToStatvfs (&s, buf);
138 return r;
140 #endif /* ndef HAVA_FSTATVFS */
143 * BSD-compatible definitions.
145 * Linux also provides these, but are deprecated in favor of (f)statvfs.
146 * Android NDK unified headers define HAVE_FSTATFS but also HAVE_SYS_STATVFS_H
147 * which makes these duplicates of the functions defined above
150 #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS) && !defined(ANDROID_UNIFIED_HEADERS)
152 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
154 struct statfs *from = _from;
156 to->f_bsize = from->f_bsize;
157 to->f_frsize = 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 to->f_favail = from->f_ffree; /* OSX doesn't have f_avail */
165 // from->f_fsid is an int32[2], to->f_fsid is a uint64,
166 // so this shouldn't lose anything.
167 memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
169 #if HAVE_STRUCT_STATFS_F_FLAGS
170 if (Mono_Posix_ToMountFlags (from->f_flags, &to->f_flag) != 0)
171 return -1;
172 #endif /* def HAVE_STRUCT_STATFS_F_FLAGS */
174 return 0;
178 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
180 struct statfs *to = _to;
181 guint64 flag;
183 to->f_bsize = from->f_bsize;
184 to->f_blocks = from->f_blocks;
185 to->f_bfree = from->f_bfree;
186 to->f_bavail = from->f_bavail;
187 to->f_files = from->f_files;
188 to->f_ffree = from->f_ffree;
190 // from->f_fsid is an int32[2], to->f_fsid is a uint64,
191 // so this shouldn't lose anything.
192 memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
194 #if HAVE_STRUCT_STATFS_F_FLAGS
195 if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
196 return -1;
197 to->f_flags = flag;
198 #endif /* def HAVE_STRUCT_STATFS_F_FLAGS */
200 return 0;
203 static void
204 set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
206 buf->f_namemax = pathconf (path, _PC_NAME_MAX);
209 static void
210 set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
212 buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
214 #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
216 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS) && (!defined(ANDROID_UNIFIED_HEADERS) || __ANDROID_API__ >= 19)
217 gint32
218 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
220 struct statfs s;
221 int r;
223 if (buf == NULL) {
224 errno = EFAULT;
225 return -1;
228 if ((r = statfs (path, &s)) == 0 &&
229 (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
230 set_namemax (path, buf);
233 return r;
235 #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
237 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS) && (!defined(ANDROID_UNIFIED_HEADERS) || __ANDROID_API__ >= 19)
238 gint32
239 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
241 struct statfs s;
242 int r;
244 if (buf == NULL) {
245 errno = EFAULT;
246 return -1;
249 if ((r = fstatfs (fd, &s)) == 0 &&
250 (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
251 set_fnamemax (fd, buf);
254 return r;
256 #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
258 G_END_DECLS
261 * vim: noexpandtab