[utils] Add fallback TLS offset scanning to X86 and AMD64.
[mono-project.git] / support / sys-statvfs.c
blobe03152a773d95d4f12fe1fc7ad78b240b1cb8f60
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 G_BEGIN_DECLS
38 #ifdef HAVE_SYS_STATVFS_H
39 int
40 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
42 struct statvfs *from = _from;
44 to->f_bsize = from->f_bsize;
45 to->f_frsize = from->f_frsize;
46 to->f_blocks = from->f_blocks;
47 to->f_bfree = from->f_bfree;
48 to->f_bavail = from->f_bavail;
49 to->f_files = from->f_files;
50 to->f_ffree = from->f_ffree;
51 to->f_favail = from->f_favail;
52 to->f_fsid = from->f_fsid;
53 to->f_namemax = from->f_namemax;
55 if (Mono_Posix_ToMountFlags (from->f_flag, &to->f_flag) != 0)
56 return -1;
58 return 0;
61 int
62 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
64 struct statvfs *to = _to;
65 guint64 flag;
67 to->f_bsize = from->f_bsize;
68 to->f_frsize = from->f_frsize;
69 to->f_blocks = from->f_blocks;
70 to->f_bfree = from->f_bfree;
71 to->f_bavail = from->f_bavail;
72 to->f_files = from->f_files;
73 to->f_ffree = from->f_ffree;
74 to->f_favail = from->f_favail;
75 to->f_fsid = from->f_fsid;
76 to->f_namemax = from->f_namemax;
78 if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
79 return -1;
80 to->f_flag = flag;
82 return 0;
84 #endif /* ndef HAVE_SYS_STATVFS_H */
87 * System V-compatible definitions
90 #ifdef HAVE_STATVFS
91 gint32
92 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
94 struct statvfs s;
95 int r;
97 if (buf == NULL) {
98 errno = EFAULT;
99 return -1;
102 if ((r = statvfs (path, &s)) == 0)
103 r = Mono_Posix_ToStatvfs (&s, buf);
105 return r;
107 #endif /* ndef HAVA_STATVFS */
109 #ifdef HAVE_FSTATVFS
110 gint32
111 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
113 struct statvfs s;
114 int r;
116 if (buf == NULL) {
117 errno = EFAULT;
118 return -1;
121 if ((r = fstatvfs (fd, &s)) == 0)
122 r = Mono_Posix_ToStatvfs (&s, buf);
124 return r;
126 #endif /* ndef HAVA_FSTATVFS */
129 * BSD-compatible definitions.
131 * Linux also provides these, but are deprecated in favor of (f)statvfs.
134 #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS)
136 Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
138 struct statfs *from = _from;
140 to->f_bsize = from->f_bsize;
141 to->f_frsize = from->f_bsize;
142 to->f_blocks = from->f_blocks;
143 to->f_bfree = from->f_bfree;
144 to->f_bavail = from->f_bavail;
145 to->f_files = from->f_files;
146 to->f_ffree = from->f_ffree;
147 to->f_favail = from->f_ffree; /* OSX doesn't have f_avail */
149 // from->f_fsid is an int32[2], to->f_fsid is a uint64,
150 // so this shouldn't lose anything.
151 memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
153 #if HAVE_STRUCT_STATFS_F_FLAGS
154 if (Mono_Posix_ToMountFlags (from->f_flags, &to->f_flag) != 0)
155 return -1;
156 #endif /* def HAVE_STRUCT_STATFS_F_FLAGS */
158 return 0;
162 Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
164 struct statfs *to = _to;
165 guint64 flag;
167 to->f_bsize = from->f_bsize;
168 to->f_blocks = from->f_blocks;
169 to->f_bfree = from->f_bfree;
170 to->f_bavail = from->f_bavail;
171 to->f_files = from->f_files;
172 to->f_ffree = from->f_ffree;
174 // from->f_fsid is an int32[2], to->f_fsid is a uint64,
175 // so this shouldn't lose anything.
176 memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
178 #if HAVE_STRUCT_STATFS_F_FLAGS
179 if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
180 return -1;
181 to->f_flags = flag;
182 #endif /* def HAVE_STRUCT_STATFS_F_FLAGS */
184 return 0;
187 static void
188 set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
190 buf->f_namemax = pathconf (path, _PC_NAME_MAX);
193 static void
194 set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
196 buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
198 #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
200 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
201 gint32
202 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
204 struct statfs s;
205 int r;
207 if (buf == NULL) {
208 errno = EFAULT;
209 return -1;
212 if ((r = statfs (path, &s)) == 0 &&
213 (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
214 set_namemax (path, buf);
217 return r;
219 #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
221 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
222 gint32
223 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
225 struct statfs s;
226 int r;
228 if (buf == NULL) {
229 errno = EFAULT;
230 return -1;
233 if ((r = fstatfs (fd, &s)) == 0 &&
234 (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
235 set_fnamemax (fd, buf);
238 return r;
240 #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
242 G_END_DECLS
245 * vim: noexpandtab