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