Move python modules from source4/scripting/python/ to python/.
[Samba.git] / source3 / smbd / dfree.c
blobe6a0af218592b9902b1ffd0b51638e832f8374af
1 /*
2 Unix SMB/CIFS implementation.
3 functions to calculate the free disk space
4 Copyright (C) Andrew Tridgell 1998
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 "smbd/smbd.h"
22 #include "smbd/globals.h"
24 /****************************************************************************
25 Normalise for DOS usage.
26 ****************************************************************************/
28 void disk_norm(bool small_query, uint64_t *bsize,uint64_t *dfree,uint64_t *dsize)
30 /* check if the disk is beyond the max disk size */
31 uint64_t maxdisksize = lp_maxdisksize();
32 if (maxdisksize) {
33 /* convert to blocks - and don't overflow */
34 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
35 if (*dsize > maxdisksize) *dsize = maxdisksize;
36 if (*dfree > maxdisksize) *dfree = maxdisksize-1;
37 /* the -1 should stop applications getting div by 0
38 errors */
41 if(small_query) {
42 while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
43 *dfree /= 2;
44 *dsize /= 2;
45 *bsize *= 2;
47 * Force max to fit in 16 bit fields.
49 if (*bsize > (WORDMAX*512)) {
50 *bsize = (WORDMAX*512);
51 if (*dsize > WORDMAX)
52 *dsize = WORDMAX;
53 if (*dfree > WORDMAX)
54 *dfree = WORDMAX;
55 break;
63 /****************************************************************************
64 Return number of 1K blocks available on a path and total number.
65 ****************************************************************************/
67 uint64_t sys_disk_free(connection_struct *conn, const char *path, bool small_query,
68 uint64_t *bsize,uint64_t *dfree,uint64_t *dsize)
70 uint64_t dfree_retval;
71 uint64_t dfree_q = 0;
72 uint64_t bsize_q = 0;
73 uint64_t dsize_q = 0;
74 const char *dfree_command;
76 (*dfree) = (*dsize) = 0;
77 (*bsize) = 512;
80 * If external disk calculation specified, use it.
83 dfree_command = lp_dfree_command(talloc_tos(), SNUM(conn));
84 if (dfree_command && *dfree_command) {
85 const char *p;
86 char **lines = NULL;
87 char *syscmd = NULL;
89 syscmd = talloc_asprintf(talloc_tos(),
90 "%s %s",
91 dfree_command,
92 path);
94 if (!syscmd) {
95 return (uint64_t)-1;
98 DEBUG (3, ("disk_free: Running command '%s'\n", syscmd));
100 lines = file_lines_pload(syscmd, NULL);
101 if (lines) {
102 char *line = lines[0];
104 DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
106 *dsize = STR_TO_SMB_BIG_UINT(line, &p);
107 while (p && *p && isspace(*p))
108 p++;
109 if (p && *p)
110 *dfree = STR_TO_SMB_BIG_UINT(p, &p);
111 while (p && *p && isspace(*p))
112 p++;
113 if (p && *p)
114 *bsize = STR_TO_SMB_BIG_UINT(p, NULL);
115 else
116 *bsize = 1024;
117 TALLOC_FREE(lines);
118 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
119 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
121 if (!*dsize)
122 *dsize = 2048;
123 if (!*dfree)
124 *dfree = 1024;
125 } else {
126 DEBUG (0, ("disk_free: file_lines_load() failed for "
127 "command '%s'. Error was : %s\n",
128 syscmd, strerror(errno) ));
129 if (sys_fsusage(path, dfree, dsize) != 0) {
130 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
131 strerror(errno) ));
132 return (uint64_t)-1;
135 } else {
136 if (sys_fsusage(path, dfree, dsize) != 0) {
137 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
138 strerror(errno) ));
139 return (uint64_t)-1;
143 if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
144 (*bsize) = bsize_q;
145 (*dfree) = MIN(*dfree,dfree_q);
146 (*dsize) = MIN(*dsize,dsize_q);
149 /* FIXME : Any reason for this assumption ? */
150 if (*bsize < 256) {
151 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
152 *bsize = 512;
155 if ((*dsize)<1) {
156 if (!dfree_broken) {
157 DEBUG(0,("WARNING: dfree is broken on this system\n"));
158 dfree_broken=true;
160 *dsize = 20*1024*1024/(*bsize);
161 *dfree = MAX(1,*dfree);
164 disk_norm(small_query,bsize,dfree,dsize);
166 if ((*bsize) < 1024) {
167 dfree_retval = (*dfree)/(1024/(*bsize));
168 } else {
169 dfree_retval = ((*bsize)/1024)*(*dfree);
172 return(dfree_retval);
175 /****************************************************************************
176 Potentially returned cached dfree info.
177 ****************************************************************************/
179 uint64_t get_dfree_info(connection_struct *conn,
180 const char *path,
181 bool small_query,
182 uint64_t *bsize,
183 uint64_t *dfree,
184 uint64_t *dsize)
186 int dfree_cache_time = lp_dfree_cache_time(SNUM(conn));
187 struct dfree_cached_info *dfc = conn->dfree_info;
188 uint64_t dfree_ret;
190 if (!dfree_cache_time) {
191 return SMB_VFS_DISK_FREE(conn,path,small_query,bsize,dfree,dsize);
194 if (dfc && (conn->lastused - dfc->last_dfree_time < dfree_cache_time)) {
195 /* Return cached info. */
196 *bsize = dfc->bsize;
197 *dfree = dfc->dfree;
198 *dsize = dfc->dsize;
199 return dfc->dfree_ret;
202 dfree_ret = SMB_VFS_DISK_FREE(conn,path,small_query,bsize,dfree,dsize);
204 if (dfree_ret == (uint64_t)-1) {
205 /* Don't cache bad data. */
206 return dfree_ret;
209 /* No cached info or time to refresh. */
210 if (!dfc) {
211 dfc = talloc(conn, struct dfree_cached_info);
212 if (!dfc) {
213 return dfree_ret;
215 conn->dfree_info = dfc;
218 dfc->bsize = *bsize;
219 dfc->dfree = *dfree;
220 dfc->dsize = *dsize;
221 dfc->dfree_ret = dfree_ret;
222 dfc->last_dfree_time = conn->lastused;
224 return dfree_ret;