Postfix3.3.1
fsspace.c
[詳解]
1 /*++
2 /* NAME
3 /* fsspace 3
4 /* SUMMARY
5 /* determine available file system space
6 /* SYNOPSIS
7 /* #include <fsspace.h>
8 /*
9 /* struct fsspace {
10 /* .in +4
11 /* unsigned long block_size;
12 /* unsigned long block_free;
13 /* .in -4
14 /* };
15 /*
16 /* void fsspace(path, sp)
17 /* const char *path;
18 /* struct fsspace *sp;
19 /* DESCRIPTION
20 /* fsspace() returns the amount of available space in the file
21 /* system specified in \fIpath\fR, in terms of the block size and
22 /* of the number of available blocks.
23 /* DIAGNOSTICS
24 /* All errors are fatal.
25 /* BUGS
26 /* Use caution when doing computations with the result from fsspace().
27 /* It is easy to cause overflow (by multiplying large numbers) or to
28 /* cause underflow (by subtracting unsigned numbers).
29 /* LICENSE
30 /* .ad
31 /* .fi
32 /* The Secure Mailer license must be distributed with this software.
33 /* AUTHOR(S)
34 /* Wietse Venema
35 /* IBM T.J. Watson Research
36 /* P.O. Box 704
37 /* Yorktown Heights, NY 10598, USA
38 /*--*/
39 
40 /* System library. */
41 
42 #include <sys_defs.h>
43 
44 #if defined(STATFS_IN_SYS_MOUNT_H)
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #elif defined(STATFS_IN_SYS_VFS_H)
48 #include <sys/vfs.h>
49 #elif defined(STATVFS_IN_SYS_STATVFS_H)
50 #include <sys/statvfs.h>
51 #elif defined(STATFS_IN_SYS_STATFS_H)
52 #include <sys/statfs.h>
53 #else
54 #ifdef USE_STATFS
55 #error "please specify the include file with `struct statfs'"
56 #else
57 #error "please specify the include file with `struct statvfs'"
58 #endif
59 #endif
60 
61 /* Utility library. */
62 
63 #include <msg.h>
64 #include <fsspace.h>
65 
66 /* fsspace - find amount of available file system space */
67 
68 void fsspace(const char *path, struct fsspace * sp)
69 {
70  const char *myname = "fsspace";
71 
72 #ifdef USE_STATFS
73 #ifdef USE_STRUCT_FS_DATA /* Ultrix */
74  struct fs_data fsbuf;
75 
76  if (statfs(path, &fsbuf) < 0)
77  msg_fatal("statfs %s: %m", path);
78  sp->block_size = 1024;
79  sp->block_free = fsbuf.fd_bfreen;
80 #else
81  struct statfs fsbuf;
82 
83  if (statfs(path, &fsbuf) < 0)
84  msg_fatal("statfs %s: %m", path);
85  sp->block_size = fsbuf.f_bsize;
86  sp->block_free = fsbuf.f_bavail;
87 #endif
88 #endif
89 #ifdef USE_STATVFS
90  struct statvfs fsbuf;
91 
92  if (statvfs(path, &fsbuf) < 0)
93  msg_fatal("statvfs %s: %m", path);
94  sp->block_size = fsbuf.f_frsize;
95  sp->block_free = fsbuf.f_bavail;
96 #endif
97  if (msg_verbose)
98  msg_info("%s: %s: block size %lu, blocks free %lu",
99  myname, path, sp->block_size, sp->block_free);
100 }
101 
102 #ifdef TEST
103 
104  /*
105  * Proof-of-concept test program: print free space unit and count for all
106  * listed file systems.
107  */
108 
109 #include <vstream.h>
110 
111 int main(int argc, char **argv)
112 {
113  struct fsspace sp;
114 
115  if (argc == 1)
116  msg_fatal("usage: %s filesystem...", argv[0]);
117 
118  while (--argc && *++argv) {
119  fsspace(*argv, &sp);
120  vstream_printf("%10s: block size %lu, blocks free %lu\n",
121  *argv, sp.block_size, sp.block_free);
123  }
124  return (0);
125 }
126 
127 #endif
int msg_verbose
Definition: msg.c:177
unsigned long block_free
Definition: fsspace.h:17
#define VSTREAM_OUT
Definition: vstream.h:67
int main(int argc, char **argv)
Definition: anvil.c:1010
unsigned long block_size
Definition: fsspace.h:16
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
void fsspace(const char *path, struct fsspace *sp)
Definition: fsspace.c:68
void msg_info(const char *fmt,...)
Definition: msg.c:199