Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[tprouty/samba.git] / source / 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 normalise for DOS usage 
26 ****************************************************************************/
27 static void disk_norm(BOOL small_query, SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
28 {
29         /* check if the disk is beyond the max disk size */
30         SMB_BIG_UINT maxdisksize = lp_maxdisksize();
31         if (maxdisksize) {
32                 /* convert to blocks - and don't overflow */
33                 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
34                 if (*dsize > maxdisksize) *dsize = maxdisksize;
35                 if (*dfree > maxdisksize) *dfree = maxdisksize-1; 
36                 /* the -1 should stop applications getting div by 0
37                    errors */
38         }  
39
40         while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
41                 *dfree /= 2;
42                 *dsize /= 2;
43                 *bsize *= 2;
44                 if(small_query) {       
45                         /*
46                          * Force max to fit in 16 bit fields.
47                          */
48                         if (*bsize > (WORDMAX*512)) {
49                                 *bsize = (WORDMAX*512);
50                                 if (*dsize > WORDMAX)
51                                         *dsize = WORDMAX;
52                                 if (*dfree >  WORDMAX)
53                                         *dfree = WORDMAX;
54                                 break;
55                         }
56                 }
57         }
58 }
59
60
61
62 /****************************************************************************
63   return number of 1K blocks available on a path and total number 
64 ****************************************************************************/
65
66 static SMB_BIG_UINT disk_free(const char *path, BOOL small_query, 
67                               SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
68 {
69         int dfree_retval;
70         SMB_BIG_UINT dfree_q = 0;
71         SMB_BIG_UINT bsize_q = 0;
72         SMB_BIG_UINT dsize_q = 0;
73         char *dfree_command;
74
75         (*dfree) = (*dsize) = 0;
76         (*bsize) = 512;
77
78         /*
79          * If external disk calculation specified, use it.
80          */
81
82         dfree_command = lp_dfree_command();
83         if (dfree_command && *dfree_command) {
84                 char *p;
85                 char **lines;
86                 pstring syscmd;
87
88                 slprintf(syscmd, sizeof(syscmd)-1, "%s %s", dfree_command, path);
89                 DEBUG (3, ("disk_free: Running command %s\n", syscmd));
90
91                 lines = file_lines_pload(syscmd, NULL);
92                 if (lines) {
93                         char *line = lines[0];
94
95                         DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
96
97                         *dsize = (SMB_BIG_UINT)strtoul(line, &p, 10);
98                         while (p && *p & isspace(*p))
99                                 p++;
100                         if (p && *p)
101                                 *dfree = (SMB_BIG_UINT)strtoul(p, &p, 10);
102                         while (p && *p & isspace(*p))
103                                 p++;
104                         if (p && *p)
105                                 *bsize = (SMB_BIG_UINT)strtoul(p, NULL, 10);
106                         else
107                                 *bsize = 1024;
108                         file_lines_free(lines);
109                         DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
110                                 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
111
112                         if (!*dsize)
113                                 *dsize = 2048;
114                         if (!*dfree)
115                                 *dfree = 1024;
116                 } else {
117                         DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
118                                 syscmd, strerror(errno) ));
119                         sys_fsusage(path, dfree, dsize);
120                 }
121         } else
122                 sys_fsusage(path, dfree, dsize);
123
124         if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
125                 (*bsize) = bsize_q;
126                 (*dfree) = MIN(*dfree,dfree_q);
127                 (*dsize) = MIN(*dsize,dsize_q);
128         }
129
130         /* FIXME : Any reason for this assumption ? */
131         if (*bsize < 256) {
132                 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
133                 *bsize = 512;
134         }
135
136         if ((*dsize)<1) {
137                 static int done;
138                 if (!done) {
139                         DEBUG(0,("WARNING: dfree is broken on this system\n"));
140                         done=1;
141                 }
142                 *dsize = 20*1024*1024/(*bsize);
143                 *dfree = MAX(1,*dfree);
144         }
145
146         disk_norm(small_query,bsize,dfree,dsize);
147
148         if ((*bsize) < 1024) {
149                 dfree_retval = (*dfree)/(1024/(*bsize));
150         } else {
151                 dfree_retval = ((*bsize)/1024)*(*dfree);
152         }
153
154         return(dfree_retval);
155 }
156
157
158 /****************************************************************************
159 wrap it to get filenames right
160 ****************************************************************************/
161 SMB_BIG_UINT sys_disk_free(const char *path, BOOL small_query, 
162                            SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
163 {
164         return disk_free(path,small_query, bsize,dfree,dsize);
165 }