Wrapped popen calls in HAVE_POPEN - needed if we are to add the
[jra/samba/.git] / source3 / smbd / dfree.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    functions to calculate the free disk space
5    Copyright (C) Andrew Tridgell 1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 extern int DEBUGLEVEL;
26
27 /****************************************************************************
28 normalise for DOS usage 
29 ****************************************************************************/
30 static void disk_norm(BOOL small_query, SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
31 {
32         /* check if the disk is beyond the max disk size */
33         SMB_BIG_UINT maxdisksize = lp_maxdisksize();
34         if (maxdisksize) {
35                 /* convert to blocks - and don't overflow */
36                 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
37                 if (*dsize > maxdisksize) *dsize = maxdisksize;
38                 if (*dfree > maxdisksize) *dfree = maxdisksize-1; 
39                 /* the -1 should stop applications getting div by 0
40                    errors */
41         }  
42
43         while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
44                 *dfree /= 2;
45                 *dsize /= 2;
46                 *bsize *= 2;
47                 if(small_query) {       
48                         /*
49                          * Force max to fit in 16 bit fields.
50                          */
51                         if (*bsize > (WORDMAX*512)) {
52                                 *bsize = (WORDMAX*512);
53                                 if (*dsize > WORDMAX)
54                                         *dsize = WORDMAX;
55                                 if (*dfree >  WORDMAX)
56                                         *dfree = WORDMAX;
57                                 break;
58                         }
59                 }
60         }
61 }
62
63
64 /* Return the number of TOSIZE-byte blocks used by
65    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
66 */
67 static SMB_BIG_UINT adjust_blocks(SMB_BIG_UINT blocks, SMB_BIG_UINT fromsize, SMB_BIG_UINT tosize)
68 {
69         if (fromsize == tosize) /* e.g., from 512 to 512 */
70                 return blocks;
71         else if (fromsize > tosize)     /* e.g., from 2048 to 512 */
72                 return blocks * (fromsize / tosize);
73         else                            /* e.g., from 256 to 512 */
74                 return (blocks + 1) / (tosize / fromsize);
75 }
76
77 /* this does all of the system specific guff to get the free disk space.
78    It is derived from code in the GNU fileutils package, but has been
79    considerably mangled for use here 
80
81    results are returned in *dfree and *dsize, in 512 byte units
82 */
83 static int fsusage(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
84 {
85 #ifdef STAT_STATFS3_OSF1
86 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
87         struct statfs fsd;
88
89         if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
90                 return -1;
91 #endif /* STAT_STATFS3_OSF1 */
92         
93 #ifdef STAT_STATFS2_FS_DATA     /* Ultrix */
94 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)1024, (SMB_BIG_UINT)512)      
95         struct fs_data fsd;
96         
97         if (statfs (path, &fsd) != 1)
98                 return -1;
99         
100         (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot);
101         (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen);
102 #endif /* STAT_STATFS2_FS_DATA */
103         
104 #ifdef STAT_STATFS2_BSIZE       /* 4.3BSD, SunOS 4, HP-UX, AIX */
105 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
106         struct statfs fsd;
107         
108         if (statfs (path, &fsd) < 0)
109                 return -1;
110         
111 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
112         /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
113            struct statfs are truncated to 2GB.  These conditions detect that
114            truncation, presumably without botching the 4.1.1 case, in which
115            the values are not truncated.  The correct counts are stored in
116            undocumented spare fields.  */
117         if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0) {
118                 fsd.f_blocks = fsd.f_spare[0];
119                 fsd.f_bfree = fsd.f_spare[1];
120                 fsd.f_bavail = fsd.f_spare[2];
121         }
122 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
123 #endif /* STAT_STATFS2_BSIZE */
124         
125
126 #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD */
127 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
128         
129         struct statfs fsd;
130         
131         if (statfs (path, &fsd) < 0)
132                 return -1;
133 #endif /* STAT_STATFS2_FSIZE */
134         
135 #ifdef STAT_STATFS4             /* SVR3, Dynix, Irix, AIX */
136 # if _AIX || defined(_CRAY)
137 #  define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
138 #  ifdef _CRAY
139 #   define f_bavail f_bfree
140 #  endif
141 # else
142 #  define CONVERT_BLOCKS(B) ((SMB_BIG_UINT)B)
143 #  ifndef _SEQUENT_             /* _SEQUENT_ is DYNIX/ptx */
144 #   ifndef DOLPHIN              /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
145 #    define f_bavail f_bfree
146 #   endif
147 #  endif
148 # endif
149         
150         struct statfs fsd;
151
152         if (statfs (path, &fsd, sizeof fsd, 0) < 0)
153                 return -1;
154         /* Empirically, the block counts on most SVR3 and SVR3-derived
155            systems seem to always be in terms of 512-byte blocks,
156            no matter what value f_bsize has.  */
157
158 #endif /* STAT_STATFS4 */
159
160 #if defined(STAT_STATVFS) || defined(STAT_STATVFS64)            /* SVR4 */
161 # define CONVERT_BLOCKS(B) \
162         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)
163
164 #ifdef STAT_STATVFS64
165         struct statvfs64 fsd;
166         if (statvfs64(path, &fsd) < 0) return -1;
167 #else
168         struct statvfs fsd;
169         if (statvfs(path, &fsd) < 0) return -1;
170 #endif
171
172         /* f_frsize isn't guaranteed to be supported.  */
173
174 #endif /* STAT_STATVFS */
175
176 #ifndef CONVERT_BLOCKS
177         /* we don't have any dfree code! */
178         return -1;
179 #else
180 #if !defined(STAT_STATFS2_FS_DATA)
181         /* !Ultrix */
182         (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
183         (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
184 #endif /* not STAT_STATFS2_FS_DATA */
185 #endif
186
187         return 0;
188 }
189
190 /****************************************************************************
191   return number of 1K blocks available on a path and total number 
192 ****************************************************************************/
193
194 static SMB_BIG_UINT disk_free(char *path, BOOL small_query, 
195                               SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
196 {
197         int dfree_retval;
198         SMB_BIG_UINT dfree_q = 0;
199         SMB_BIG_UINT bsize_q = 0;
200         SMB_BIG_UINT dsize_q = 0;
201         char *dfree_command;
202
203         (*dfree) = (*dsize) = 0;
204         (*bsize) = 512;
205
206
207         /*
208          * If external disk calculation specified, use it.
209          */
210
211 #ifdef HAVE_POPEN
212         dfree_command = lp_dfree_command();
213         if (dfree_command && *dfree_command) {
214                 pstring line;
215                 char *p;
216                 FILE *pp;
217
218                 slprintf (line, sizeof(pstring) - 1, "%s %s", dfree_command, path);
219                 pp = popen(line, "r");
220                 if (pp) {
221                         fgets(line, sizeof(pstring), pp);
222                         line[sizeof(pstring)-1] = '\0';
223                         if (strlen(line) > 0)
224                                 line[strlen(line)-1] = '\0';
225
226                         DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
227
228                         *dsize = (SMB_BIG_UINT)strtoul(line, &p, 10);
229                         while (p && *p & isspace(*p))
230                                 p++;
231                         if (p && *p)
232                                 *dfree = (SMB_BIG_UINT)strtoul(p, &p, 10);
233                         while (p && *p & isspace(*p))
234                                 p++;
235                         if (p && *p)
236                                 *bsize = (SMB_BIG_UINT)strtoul(p, NULL, 10);
237                         else
238                                 *bsize = 1024;
239                         pclose (pp);
240                         DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
241                                 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
242
243                         if (!*dsize)
244                                 *dsize = 2048;
245                         if (!*dfree)
246                                 *dfree = 1024;
247                 } else {
248                         DEBUG (0, ("disk_free: popen() failed for command %s. Error was : %s\n",
249                                 line, strerror(errno) ));
250                         fsusage(path, dfree, dsize);
251                 }
252         } else
253 #endif /* HAVE_POPEN */
254                 fsusage(path, dfree, dsize);
255
256         if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
257                 (*bsize) = bsize_q;
258                 (*dfree) = MIN(*dfree,dfree_q);
259                 (*dsize) = MIN(*dsize,dsize_q);
260         }
261
262         /* FIXME : Any reason for this assumption ? */
263         if (*bsize < 256) {
264                 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
265                 *bsize = 512;
266         }
267
268         if ((*dsize)<1) {
269                 static int done;
270                 if (!done) {
271                         DEBUG(0,("WARNING: dfree is broken on this system\n"));
272                         done=1;
273                 }
274                 *dsize = 20*1024*1024/(*bsize);
275                 *dfree = MAX(1,*dfree);
276         }
277
278         disk_norm(small_query,bsize,dfree,dsize);
279
280         if ((*bsize) < 1024) {
281                 dfree_retval = (*dfree)/(1024/(*bsize));
282         } else {
283                 dfree_retval = ((*bsize)/1024)*(*dfree);
284         }
285
286         return(dfree_retval);
287 }
288
289
290 /****************************************************************************
291 wrap it to get filenames right
292 ****************************************************************************/
293 SMB_BIG_UINT sys_disk_free(char *path, BOOL small_query, 
294                            SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
295 {
296         return(disk_free(dos_to_unix(path,False),small_query, bsize,dfree,dsize));
297 }