Enable optional anonymization of user names,
[Samba/ekacnet.git] / lib / util / fsusage.c
blob43c87872164d8ef08cb3650267d6aa834b4a86c2
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
23 /**
24 * @file
25 * @brief Utility functions for getting the amount of free disk space
28 /* Return the number of TOSIZE-byte blocks used by
29 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
31 static uint64_t adjust_blocks(uint64_t blocks, uint64_t fromsize, uint64_t tosize)
33 if (fromsize == tosize) /* e.g., from 512 to 512 */
34 return blocks;
35 else if (fromsize > tosize) /* e.g., from 2048 to 512 */
36 return blocks * (fromsize / tosize);
37 else /* e.g., from 256 to 512 */
38 return (blocks + 1) / (tosize / fromsize);
41 /**
42 * Retrieve amount of free disk space.
43 * this does all of the system specific guff to get the free disk space.
44 * It is derived from code in the GNU fileutils package, but has been
45 * considerably mangled for use here
47 * results are returned in *dfree and *dsize, in 512 byte units
49 _PUBLIC_ int sys_fsusage(const char *path, uint64_t *dfree, uint64_t *dsize)
51 #ifdef STAT_STATFS3_OSF1
52 #define CONVERT_BLOCKS(B) adjust_blocks ((uint64_t)(B), (uint64_t)fsd.f_fsize, (uint64_t)512)
53 struct statfs fsd;
55 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
56 return -1;
57 #endif /* STAT_STATFS3_OSF1 */
59 #ifdef STAT_STATFS2_FS_DATA /* Ultrix */
60 #define CONVERT_BLOCKS(B) adjust_blocks ((uint64_t)(B), (uint64_t)1024, (uint64_t)512)
61 struct fs_data fsd;
63 if (statfs (path, &fsd) != 1)
64 return -1;
66 (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot);
67 (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen);
68 #endif /* STAT_STATFS2_FS_DATA */
70 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
71 #define CONVERT_BLOCKS(B) adjust_blocks ((uint64_t)(B), (uint64_t)fsd.f_bsize, (uint64_t)512)
72 struct statfs fsd;
74 if (statfs (path, &fsd) < 0)
75 return -1;
77 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
78 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
79 struct statfs are truncated to 2GB. These conditions detect that
80 truncation, presumably without botching the 4.1.1 case, in which
81 the values are not truncated. The correct counts are stored in
82 undocumented spare fields. */
83 if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0) {
84 fsd.f_blocks = fsd.f_spare[0];
85 fsd.f_bfree = fsd.f_spare[1];
86 fsd.f_bavail = fsd.f_spare[2];
88 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
89 #endif /* STAT_STATFS2_BSIZE */
92 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD */
93 #define CONVERT_BLOCKS(B) adjust_blocks ((uint64_t)(B), (uint64_t)fsd.f_fsize, (uint64_t)512)
95 struct statfs fsd;
97 if (statfs (path, &fsd) < 0)
98 return -1;
99 #endif /* STAT_STATFS2_FSIZE */
101 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
102 # if _AIX || defined(_CRAY)
103 # define CONVERT_BLOCKS(B) adjust_blocks ((uint64_t)(B), (uint64_t)fsd.f_bsize, (uint64_t)512)
104 # ifdef _CRAY
105 # define f_bavail f_bfree
106 # endif
107 # else
108 # define CONVERT_BLOCKS(B) ((uint64_t)B)
109 # ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx */
110 # ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
111 # define f_bavail f_bfree
112 # endif
113 # endif
114 # endif
116 struct statfs fsd;
118 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
119 return -1;
120 /* Empirically, the block counts on most SVR3 and SVR3-derived
121 systems seem to always be in terms of 512-byte blocks,
122 no matter what value f_bsize has. */
124 #endif /* STAT_STATFS4 */
126 #if defined(STAT_STATVFS) || defined(STAT_STATVFS64) /* SVR4 */
127 # define CONVERT_BLOCKS(B) \
128 adjust_blocks ((uint64_t)(B), fsd.f_frsize ? (uint64_t)fsd.f_frsize : (uint64_t)fsd.f_bsize, (uint64_t)512)
130 #ifdef STAT_STATVFS64
131 struct statvfs64 fsd;
132 if (statvfs64(path, &fsd) < 0) return -1;
133 #else
134 struct statvfs fsd;
135 if (statvfs(path, &fsd) < 0) return -1;
136 #endif
138 /* f_frsize isn't guaranteed to be supported. */
140 #endif /* STAT_STATVFS */
142 #ifndef CONVERT_BLOCKS
143 /* we don't have any dfree code! */
144 return -1;
145 #else
146 #if !defined(STAT_STATFS2_FS_DATA)
147 /* !Ultrix */
148 (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
149 (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
150 #endif /* not STAT_STATFS2_FS_DATA */
151 #endif
153 return 0;