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