* fstab.c: Added; wrap <fstab.h> functions: getfsent(3), getfsfile(3),
[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 Jonathan Pryor
8  */
9
10 #include <errno.h>
11
12 #include "mph.h"
13
14 #ifdef HAVE_SYS_STATVFS_H
15 #include <sys/statvfs.h>
16 #endif /* def HAVE_SYS_STATVFS_H */
17
18 #ifdef HAVE_GETFSSTAT
19 #include <sys/param.h>
20 #include <sys/ucred.h>
21 #include <sys/mount.h>
22 #include <unistd.h>     /* for pathconf */
23 #endif /* def HAVE_GETFSSTAT */
24
25 G_BEGIN_DECLS
26
27 struct Mono_Posix_Statvfs {
28         guint64         f_bsize;    /* file system block size */
29         guint64         f_frsize;   /* fragment size */
30         mph_fsblkcnt_t  f_blocks;   /* size of fs in f_frsize units */
31         mph_fsblkcnt_t  f_bfree;    /* # free blocks */
32         mph_fsblkcnt_t  f_bavail;   /* # free blocks for non-root */
33         mph_fsfilcnt_t  f_files;    /* # inodes */
34         mph_fsfilcnt_t  f_ffree;    /* # free inodes */
35         mph_fsfilcnt_t  f_favail;   /* # free inodes for non-root */
36         guint64         f_fsid;     /* file system id */
37         guint64         f_flag;     /* mount flags */
38         guint64         f_namemax;  /* maximum filename length */
39 };
40
41 #ifdef HAVE_SYS_STATVFS_H
42 static void
43 copy_statvfs (struct Mono_Posix_Statvfs *to, struct statvfs *from)
44 {
45   to->f_bsize   = from->f_bsize;
46   to->f_frsize  = from->f_frsize;
47   to->f_blocks  = from->f_blocks;
48   to->f_bfree   = from->f_bfree;
49   to->f_bavail  = from->f_bavail;
50   to->f_files   = from->f_files;
51   to->f_ffree   = from->f_ffree;
52   to->f_favail  = from->f_favail;
53   to->f_fsid    = from->f_fsid;
54   to->f_flag    = from->f_flag;
55   to->f_namemax =       from->f_namemax;
56 }
57 #endif /* ndef HAVE_SYS_STATVFS_H */
58
59 /*
60  * System V-compatible definitions
61  */
62
63 #ifdef HAVE_STATVFS
64 gint32
65 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
66 {
67         struct statvfs s;
68         int r;
69
70         if (buf == NULL) {
71                 errno = EFAULT;
72                 return -1;
73         }
74
75         if ((r = statvfs (path, &s)) == 0)
76                 copy_statvfs (buf, &s);
77
78         return r;
79 }
80 #endif /* ndef HAVA_STATVFS */
81
82 #ifdef HAVE_FSTATVFS
83 gint32
84 Mono_Posix_Syscall_fstatvfs (gint32 fd, 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 = fstatvfs (fd, &s)) == 0)
95                 copy_statvfs (buf, &s);
96
97         return r;
98 }
99 #endif /* ndef HAVA_FSTATVFS */
100
101 /*
102  * BSD-compatible definitions.
103  *
104  * Linux also provides these, but are deprecated in favor of (f)statvfs.
105  */
106
107 #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS)
108 static void
109 copy_statfs (struct Mono_Posix_Statvfs *to, struct statfs *from)
110 {
111   to->f_bsize   = from->f_bsize;
112   to->f_frsize  = from->f_frsize;
113   to->f_blocks  = from->f_blocks;
114   to->f_bfree   = from->f_bfree;
115   to->f_bavail  = from->f_bavail;
116   to->f_files   = from->f_files;
117   to->f_ffree   = from->f_ffree;
118   to->f_favail  = from->f_ffree; /* OSX doesn't have f_avail */
119   to->f_fsid    = from->f_fsid;
120   to->f_flag    = from->f_flags;
121 }
122
123 static void
124 set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
125 {
126   buf->f_namemax = pathconf (path, _PC_NAME_MAX);
127 }
128 #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
129
130 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
131 gint32
132 Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
133 {
134         struct statfs s;
135         int r;
136
137         if (buf == NULL) {
138                 errno = EFAULT;
139                 return -1;
140         }
141
142         if ((r = statfs (path, &s)) == 0) {
143                 copy_statfs (buf, &s);
144                 set_namemax (path, buf);
145         }
146
147         return r;
148 }
149 #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
150
151 #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
152 gint32
153 Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
154 {
155         struct statfs s;
156         int r;
157
158         if (buf == NULL) {
159                 errno = EFAULT;
160                 return -1;
161         }
162
163         if ((r = fstatfs (fd, &s)) == 0) {
164                 copy_statvfs (buf, &s);
165                 set_namemax (path, buf);
166         }
167
168         return r;
169 }
170 #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
171
172 G_END_DECLS
173
174 /*
175  * vim: noexpandtab
176  */