lib: Fix CID 1034723 Explicit null dereferenced
[Samba.git] / source3 / smbd / dfree.c
blobbcefea5b274648ddd29bc45a9fd8a270eff05cf7
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(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_max_disk_size();
32 if (maxdisksize) {
33 /* convert to blocks - and don't overflow */
34 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
35 if (*dsize > maxdisksize) {
36 *dsize = maxdisksize;
38 if (*dfree > maxdisksize) {
39 *dfree = maxdisksize - 1;
41 /* the -1 should stop applications getting div by 0
42 errors */
48 /****************************************************************************
49 Return number of 1K blocks available on a path and total number.
50 ****************************************************************************/
52 uint64_t sys_disk_free(connection_struct *conn, const char *path,
53 uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
55 uint64_t dfree_retval;
56 uint64_t dfree_q = 0;
57 uint64_t bsize_q = 0;
58 uint64_t dsize_q = 0;
59 const char *dfree_command;
61 (*dfree) = (*dsize) = 0;
62 (*bsize) = 512;
65 * If external disk calculation specified, use it.
68 dfree_command = lp_dfree_command(talloc_tos(), SNUM(conn));
69 if (dfree_command && *dfree_command) {
70 const char *p;
71 char **lines = NULL;
72 char *syscmd = NULL;
74 syscmd = talloc_asprintf(talloc_tos(),
75 "%s %s",
76 dfree_command,
77 path);
79 if (!syscmd) {
80 return (uint64_t)-1;
83 DEBUG (3, ("disk_free: Running command '%s'\n", syscmd));
85 lines = file_lines_pload(syscmd, NULL);
86 if (lines) {
87 char *line = lines[0];
89 DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
91 *dsize = STR_TO_SMB_BIG_UINT(line, &p);
92 while (p && *p && isspace(*p))
93 p++;
94 if (p && *p)
95 *dfree = STR_TO_SMB_BIG_UINT(p, &p);
96 while (p && *p && isspace(*p))
97 p++;
98 if (p && *p)
99 *bsize = STR_TO_SMB_BIG_UINT(p, NULL);
100 else
101 *bsize = 1024;
102 TALLOC_FREE(lines);
103 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
104 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
106 if (!*dsize)
107 *dsize = 2048;
108 if (!*dfree)
109 *dfree = 1024;
110 } else {
111 DEBUG (0, ("disk_free: file_lines_load() failed for "
112 "command '%s'. Error was : %s\n",
113 syscmd, strerror(errno) ));
114 if (sys_fsusage(path, dfree, dsize) != 0) {
115 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
116 strerror(errno) ));
117 return (uint64_t)-1;
120 } else {
121 if (sys_fsusage(path, dfree, dsize) != 0) {
122 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
123 strerror(errno) ));
124 return (uint64_t)-1;
128 if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
129 (*bsize) = bsize_q;
130 (*dfree) = MIN(*dfree,dfree_q);
131 (*dsize) = MIN(*dsize,dsize_q);
134 /* FIXME : Any reason for this assumption ? */
135 if (*bsize < 256) {
136 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
137 *bsize = 512;
140 if ((*dsize)<1) {
141 if (!dfree_broken) {
142 DEBUG(0,("WARNING: dfree is broken on this system\n"));
143 dfree_broken=true;
145 *dsize = 20*1024*1024/(*bsize);
146 *dfree = MAX(1,*dfree);
149 disk_norm(bsize, dfree, dsize);
151 if ((*bsize) < 1024) {
152 dfree_retval = (*dfree)/(1024/(*bsize));
153 } else {
154 dfree_retval = ((*bsize)/1024)*(*dfree);
157 return(dfree_retval);
160 /****************************************************************************
161 Potentially returned cached dfree info.
162 ****************************************************************************/
164 uint64_t get_dfree_info(connection_struct *conn,
165 const char *path,
166 uint64_t *bsize,
167 uint64_t *dfree,
168 uint64_t *dsize)
170 int dfree_cache_time = lp_dfree_cache_time(SNUM(conn));
171 struct dfree_cached_info *dfc = conn->dfree_info;
172 uint64_t dfree_ret;
174 if (!dfree_cache_time) {
175 return SMB_VFS_DISK_FREE(conn, path, bsize, dfree, dsize);
178 if (dfc && (conn->lastused - dfc->last_dfree_time < dfree_cache_time)) {
179 /* Return cached info. */
180 *bsize = dfc->bsize;
181 *dfree = dfc->dfree;
182 *dsize = dfc->dsize;
183 return dfc->dfree_ret;
186 dfree_ret = SMB_VFS_DISK_FREE(conn, path, bsize, dfree, dsize);
188 if (dfree_ret == (uint64_t)-1) {
189 /* Don't cache bad data. */
190 return dfree_ret;
193 /* No cached info or time to refresh. */
194 if (!dfc) {
195 dfc = talloc(conn, struct dfree_cached_info);
196 if (!dfc) {
197 return dfree_ret;
199 conn->dfree_info = dfc;
202 dfc->bsize = *bsize;
203 dfc->dfree = *dfree;
204 dfc->dsize = *dsize;
205 dfc->dfree_ret = dfree_ret;
206 dfc->last_dfree_time = conn->lastused;
208 return dfree_ret;