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