Commit a2fdbfca authored by rswindell's avatar rswindell
Browse files

getfreediskspace() can now report larger-then-byte units (e.g. kilobytes,

megabytes, etc) by specifying an optional "unit" parameter.
parent b9d4ba32
......@@ -448,7 +448,12 @@ char* sbbs_t::atcode(char* sp, char* str)
}
if(!strcmp(sp,"FREESPACE")) {
sprintf(str,"%lu",getfreediskspace(cfg.temp_dir));
sprintf(str,"%lu",getfreediskspace(cfg.temp_dir,0));
return(str);
}
if(!strcmp(sp,"FREESPACEK")) {
sprintf(str,"%lu",getfreediskspace(cfg.temp_dir,1024));
return(str);
}
......
......@@ -2822,9 +2822,9 @@ static void ctrl_thread(void* arg)
else
l=0;
if(local_fsys)
avail=getfreediskspace(local_dir);
avail=getfreediskspace(local_dir,0);
else
avail=getfreediskspace(scfg.data_dir); /* Change to temp_dir? */
avail=getfreediskspace(scfg.data_dir,0); /* Change to temp_dir? */
if(l && l>avail)
sockprintf(sock,"504 Only %lu bytes available.",avail);
else
......
......@@ -156,7 +156,7 @@ static JSBool js_system_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
p=lastuseron;
break;
case SYS_PROP_FREEDISKSPACE:
val = getfreediskspace(cfg->temp_dir);
val = getfreediskspace(cfg->temp_dir,0);
if(INT_FITS_IN_JSVAL(val) && !(val&0x80000000))
*vp = INT_TO_JSVAL(val);
else
......
......@@ -105,7 +105,7 @@ void sbbs_t::temp_xfer()
switch(ch) {
case 'A': /* add to temp file */
/* free disk space */
space=getfreediskspace(cfg.temp_dir);
space=getfreediskspace(cfg.temp_dir,0);
if(space<(ulong)cfg.min_dspace*1024L) {
bputs(text[LowDiskSpace]);
sprintf(str,"Diskspace is low: %s (%lu bytes)"
......@@ -289,7 +289,7 @@ void sbbs_t::extract(uint dirnum)
intmp=1;
/* get free disk space */
space=getfreediskspace(cfg.temp_dir);
space=getfreediskspace(cfg.temp_dir,0);
if(space<(ulong)cfg.min_dspace*1024L) {
bputs(text[LowDiskSpace]);
sprintf(str,"Diskspace is low: %s (%lu bytes)",cfg.temp_dir,space);
......
......@@ -260,10 +260,10 @@ bool sbbs_t::upload(uint dirnum)
/* get free disk space */
space=getfreediskspace(path);
if(space<(ulong)cfg.min_dspace*1024L) {
space=getfreediskspace(path,1024);
if(space<(ulong)cfg.min_dspace) {
bputs(text[LowDiskSpace]);
sprintf(str,"Diskspace is low: %s (%lu bytes)",path,space);
sprintf(str,"Diskspace is low: %s (%lu kilobytes)",path,space);
errorlog(str);
if(!dir_op(dirnum))
return(false);
......
......@@ -577,11 +577,23 @@ ulong DLLCALL delfiles(char *inpath, char *spec)
/* Return free disk space in bytes (up to a maximum of 4GB) */
/****************************************************************************/
#if defined(_WIN32)
typedef BOOL(WINAPI * GetDiskFreeSpaceEx_t)
(LPCTSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
typedef BOOL(WINAPI * GetDiskFreeSpaceEx_t)
(LPCTSTR,PULARGE_INTEGER,PULARGE_INTEGER,PULARGE_INTEGER);
static int bit_num(ulong val)
{
int i;
for(i=31;i>=0;i--)
if(val&(1<<i))
return(i);
return(-1);
}
#endif
ulong DLLCALL getfreediskspace(const char* path)
/* Unit should be a power-of-2 (e.g. 1024 to report kilobytes) */
ulong DLLCALL getfreediskspace(const char* path, ulong unit)
{
#if defined(_WIN32)
char root[16];
......@@ -606,6 +618,10 @@ ulong DLLCALL getfreediskspace(const char* path)
&size, // receives the number of bytes on disk
NULL)) // receives the free bytes on disk
return(0);
if(unit)
avail.QuadPart=Int64ShrlMod32(avail.QuadPart,bit_num(unit));
#if defined(_ANONYMOUS_STRUCT)
if(avail.HighPart)
#else
......@@ -631,6 +647,8 @@ ulong DLLCALL getfreediskspace(const char* path)
))
return(0);
if(unit)
NumberOfFreeClusters/=unit;
return(NumberOfFreeClusters*SectorsPerCluster*BytesPerSector);
......@@ -642,6 +660,8 @@ ulong DLLCALL getfreediskspace(const char* path)
if (statfs(path, &fs) < 0)
return 0;
if(unit)
fs.f_bavail/=unit;
return fs.f_bsize * fs.f_bavail;
#else
......
......@@ -197,7 +197,7 @@ DLLEXPORT time_t DLLCALL fdate(const char *filename);
DLLEXPORT BOOL DLLCALL isdir(const char *filename);
DLLEXPORT char* DLLCALL getfname(const char* path);
DLLEXPORT int DLLCALL getfattr(const char* filename);
DLLEXPORT ulong DLLCALL getfreediskspace(const char* path);
DLLEXPORT ulong DLLCALL getfreediskspace(const char* path, ulong unit);
DLLEXPORT ulong DLLCALL delfiles(char *inpath, char *spec);
#if defined(__unix__)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment