Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sys-statvfs.c « support - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2f8486017235b4031129b39cadc68237cd2300b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * <sys/sendfile.h> wrapper functions.
 *
 * Authors:
 *   Jonathan Pryor (jonpryor@vt.edu)
 *
 * Copyright (C) 2004 Jonathan Pryor
 */

#include <errno.h>

#include <string.h>

#include "mph.h"

#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif /* def HAVE_SYS_STATVFS_H */

#ifdef HAVE_GETFSSTAT
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <unistd.h>     /* for pathconf */
#endif /* def HAVE_GETFSSTAT */

G_BEGIN_DECLS

struct Mono_Posix_Statvfs {
	guint64         f_bsize;    /* file system block size */
	guint64         f_frsize;   /* fragment size */
	mph_fsblkcnt_t  f_blocks;   /* size of fs in f_frsize units */
	mph_fsblkcnt_t  f_bfree;    /* # free blocks */
	mph_fsblkcnt_t  f_bavail;   /* # free blocks for non-root */
	mph_fsfilcnt_t  f_files;    /* # inodes */
	mph_fsfilcnt_t  f_ffree;    /* # free inodes */
	mph_fsfilcnt_t  f_favail;   /* # free inodes for non-root */
	guint64         f_fsid;     /* file system id */
	guint64         f_flag;     /* mount flags */
	guint64         f_namemax;  /* maximum filename length */
};

#ifdef HAVE_SYS_STATVFS_H
static void
copy_statvfs (struct Mono_Posix_Statvfs *to, struct statvfs *from)
{
  to->f_bsize   = from->f_bsize;
  to->f_frsize  = from->f_frsize;
  to->f_blocks  = from->f_blocks;
  to->f_bfree   = from->f_bfree;
  to->f_bavail  = from->f_bavail;
  to->f_files   = from->f_files;
  to->f_ffree   = from->f_ffree;
  to->f_favail  = from->f_favail;
  to->f_fsid    = from->f_fsid;
  to->f_flag    = from->f_flag;
  to->f_namemax =	from->f_namemax;
}
#endif /* ndef HAVE_SYS_STATVFS_H */

/*
 * System V-compatible definitions
 */

#ifdef HAVE_STATVFS
gint32
Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
{
	struct statvfs s;
	int r;

	if (buf == NULL) {
		errno = EFAULT;
		return -1;
	}

	if ((r = statvfs (path, &s)) == 0)
		copy_statvfs (buf, &s);

	return r;
}
#endif /* ndef HAVA_STATVFS */

#ifdef HAVE_FSTATVFS
gint32
Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
{
	struct statvfs s;
	int r;

	if (buf == NULL) {
		errno = EFAULT;
		return -1;
	}

	if ((r = fstatvfs (fd, &s)) == 0)
		copy_statvfs (buf, &s);

	return r;
}
#endif /* ndef HAVA_FSTATVFS */

/*
 * BSD-compatible definitions.
 *
 * Linux also provides these, but are deprecated in favor of (f)statvfs.
 */

#if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS)
static void
copy_statfs (struct Mono_Posix_Statvfs *to, struct statfs *from)
{
  to->f_bsize   = from->f_bsize;
  to->f_frsize  = from->f_bsize;
  to->f_blocks  = from->f_blocks;
  to->f_bfree   = from->f_bfree;
  to->f_bavail  = from->f_bavail;
  to->f_files   = from->f_files;
  to->f_ffree   = from->f_ffree;
  to->f_favail  = from->f_ffree; /* OSX doesn't have f_avail */
  to->f_flag    = from->f_flags;
	// from->f_fsid is an int32[2], to->f_fsid is a uint64, 
	// so this shouldn't lose anything.
	memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
}

static void
set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
{
  buf->f_namemax = pathconf (path, _PC_NAME_MAX);
}

static void
set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
{
  buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
}
#endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */

#if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
gint32
Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
{
	struct statfs s;
	int r;

	if (buf == NULL) {
		errno = EFAULT;
		return -1;
	}

	if ((r = statfs (path, &s)) == 0) {
		copy_statfs (buf, &s);
		set_namemax (path, buf);
	}

	return r;
}
#endif /* !def HAVE_STATVFS && def HAVE_STATFS */

#if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
gint32
Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
{
	struct statfs s;
	int r;

	if (buf == NULL) {
		errno = EFAULT;
		return -1;
	}

	if ((r = fstatfs (fd, &s)) == 0) {
		copy_statfs (buf, &s);
		set_fnamemax (fd, buf);
	}

	return r;
}
#endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */

G_END_DECLS

/*
 * vim: noexpandtab
 */