few edits
[Samba.git] / source / lib / fsusage.c
blob1bff1f953e0802bd04b0ddeb31ab6fb720ad84df
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0.
4 functions to calculate the free disk space
5 Copyright (C) Andrew Tridgell 1998-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
25 /* Return the number of TOSIZE-byte blocks used by
26 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
28 static SMB_BIG_UINT adjust_blocks(SMB_BIG_UINT blocks, SMB_BIG_UINT fromsize, SMB_BIG_UINT tosize)
30 if (fromsize == tosize) /* e.g., from 512 to 512 */
31 return blocks;
32 else if (fromsize > tosize) /* e.g., from 2048 to 512 */
33 return blocks * (fromsize / tosize);
34 else /* e.g., from 256 to 512 */
35 return (blocks + 1) / (tosize / fromsize);
38 /* this does all of the system specific guff to get the free disk space.
39 It is derived from code in the GNU fileutils package, but has been
40 considerably mangled for use here
42 results are returned in *dfree and *dsize, in 512 byte units
44 int sys_fsusage(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
46 #ifdef STAT_STATFS3_OSF1
47 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
48 struct statfs fsd;
50 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
51 return -1;
52 #endif /* STAT_STATFS3_OSF1 */
54 #ifdef STAT_STATFS2_FS_DATA /* Ultrix */
55 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)1024, (SMB_BIG_UINT)512)
56 struct fs_data fsd;
58 if (statfs (path, &fsd) != 1)
59 return -1;
61 (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot);
62 (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen);
63 #endif /* STAT_STATFS2_FS_DATA */
65 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
66 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
67 struct statfs fsd;
69 if (statfs (path, &fsd) < 0)
70 return -1;
72 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
73 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
74 struct statfs are truncated to 2GB. These conditions detect that
75 truncation, presumably without botching the 4.1.1 case, in which
76 the values are not truncated. The correct counts are stored in
77 undocumented spare fields. */
78 if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0) {
79 fsd.f_blocks = fsd.f_spare[0];
80 fsd.f_bfree = fsd.f_spare[1];
81 fsd.f_bavail = fsd.f_spare[2];
83 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
84 #endif /* STAT_STATFS2_BSIZE */
87 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD */
88 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
90 struct statfs fsd;
92 if (statfs (path, &fsd) < 0)
93 return -1;
94 #endif /* STAT_STATFS2_FSIZE */
96 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
97 # if _AIX || defined(_CRAY)
98 # define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
99 # ifdef _CRAY
100 # define f_bavail f_bfree
101 # endif
102 # else
103 # define CONVERT_BLOCKS(B) ((SMB_BIG_UINT)B)
104 # ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx */
105 # ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
106 # define f_bavail f_bfree
107 # endif
108 # endif
109 # endif
111 struct statfs fsd;
113 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
114 return -1;
115 /* Empirically, the block counts on most SVR3 and SVR3-derived
116 systems seem to always be in terms of 512-byte blocks,
117 no matter what value f_bsize has. */
119 #endif /* STAT_STATFS4 */
121 #if defined(STAT_STATVFS) || defined(STAT_STATVFS64) /* SVR4 */
122 # define CONVERT_BLOCKS(B) \
123 adjust_blocks ((SMB_BIG_UINT)(B), fsd.f_frsize ? (SMB_BIG_UINT)fsd.f_frsize : (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
125 #ifdef STAT_STATVFS64
126 struct statvfs64 fsd;
127 if (statvfs64(path, &fsd) < 0) return -1;
128 #else
129 struct statvfs fsd;
130 if (statvfs(path, &fsd) < 0) return -1;
131 #endif
133 /* f_frsize isn't guaranteed to be supported. */
135 #endif /* STAT_STATVFS */
137 #ifndef CONVERT_BLOCKS
138 /* we don't have any dfree code! */
139 return -1;
140 #else
141 #if !defined(STAT_STATFS2_FS_DATA)
142 /* !Ultrix */
143 (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
144 (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
145 #endif /* not STAT_STATFS2_FS_DATA */
146 #endif
148 return 0;