Skip to content
Snippets Groups Projects
Commit 35533eb2 authored by rswindell's avatar rswindell
Browse files

Introduces getdisksize() which reports the total number of bytes (or units,

e.g. kilobytes)  of the disk (not just the "free space").
parent b4024bf4
No related branches found
No related tags found
No related merge requests found
...@@ -680,8 +680,8 @@ static int bit_num(ulong val) ...@@ -680,8 +680,8 @@ static int bit_num(ulong val)
} }
#endif #endif
/* Unit should be a power-of-2 (e.g. 1024 to report kilobytes) */ /* Unit should be a power-of-2 (e.g. 1024 to report kilobytes) or 1 (to report bytes) */
ulong DLLCALL getfreediskspace(const char* path, ulong unit) static ulong getdiskspace(const char* path, ulong unit, BOOL freespace)
{ {
#if defined(_WIN32) #if defined(_WIN32)
char root[16]; char root[16];
...@@ -707,20 +707,23 @@ ulong DLLCALL getfreediskspace(const char* path, ulong unit) ...@@ -707,20 +707,23 @@ ulong DLLCALL getfreediskspace(const char* path, ulong unit)
NULL)) /* receives the free bytes on disk */ NULL)) /* receives the free bytes on disk */
return(0); return(0);
if(freespace)
size=avail;
if(unit>1) if(unit>1)
avail.QuadPart=Int64ShrlMod32(avail.QuadPart,bit_num(unit)); size.QuadPart=Int64ShrlMod32(size.QuadPart,bit_num(unit));
#if defined(_ANONYMOUS_STRUCT) #if defined(_ANONYMOUS_STRUCT)
if(avail.HighPart) if(size.HighPart)
#else #else
if(avail.u.HighPart) if(size.u.HighPart)
#endif #endif
return(0xffffffff); /* 4GB max */ return(0xffffffff); /* 4GB max */
#if defined(_ANONYMOUS_STRUCT) #if defined(_ANONYMOUS_STRUCT)
return(avail.LowPart); return(size.LowPart);
#else #else
return(avail.u.LowPart); return(size.u.LowPart);
#endif #endif
} }
...@@ -735,33 +738,47 @@ ulong DLLCALL getfreediskspace(const char* path, ulong unit) ...@@ -735,33 +738,47 @@ ulong DLLCALL getfreediskspace(const char* path, ulong unit)
)) ))
return(0); return(0);
if(freespace)
TotalNumberOfClusters = NumberOfFreeClusters;
if(unit>1) if(unit>1)
NumberOfFreeClusters/=unit; TotalNumberOfClusters/=unit;
return(NumberOfFreeClusters*SectorsPerCluster*BytesPerSector); return(TotalNumberOfClusters*SectorsPerCluster*BytesPerSector);
#elif defined(__solaris__) || (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 300000000 /* NetBSD 3.0 */)) #elif defined(__solaris__) || (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 300000000 /* NetBSD 3.0 */))
struct statvfs fs; struct statvfs fs;
unsigned long blocks;
if (statvfs(path, &fs) < 0) if (statvfs(path, &fs) < 0)
return 0; return 0;
if(freespace)
blocks=fs.f_bavail;
else
blocks=fs.f_blocks;
if(unit>1) if(unit>1)
fs.f_bavail/=unit; blocks/=unit;
return fs.f_bsize * fs.f_bavail; return fs.f_bsize * blocks;
/* statfs is also used under FreeBSD (Though it *supports* statvfs() now too) */ /* statfs is also used under FreeBSD (Though it *supports* statvfs() now too) */
#elif defined(__GLIBC__) || defined(BSD) #elif defined(__GLIBC__) || defined(BSD)
struct statfs fs; struct statfs fs;
unsigned long blocks;
if (statfs(path, &fs) < 0) if (statfs(path, &fs) < 0)
return 0; return 0;
if(freespace)
blocks=fs.f_bavail;
else
blocks=fs.f_blocks;
if(unit>1) if(unit>1)
fs.f_bavail/=unit; blocks/=unit;
return fs.f_bsize * fs.f_bavail; return fs.f_bsize * blocks;
#else #else
...@@ -771,6 +788,16 @@ ulong DLLCALL getfreediskspace(const char* path, ulong unit) ...@@ -771,6 +788,16 @@ ulong DLLCALL getfreediskspace(const char* path, ulong unit)
#endif #endif
} }
ulong DLLCALL getfreediskspace(const char* path, ulong unit)
{
return getdiskspace(path, unit, /* freespace? */TRUE);
}
ulong DLLCALL getdisksize(const char* path, ulong unit)
{
return getdiskspace(path, unit, /* freespace? */FALSE);
}
/****************************************************************************/ /****************************************************************************/
/* Resolves //, /./, and /../ in a path. Should work indetically to Windows */ /* Resolves //, /./, and /../ in a path. Should work indetically to Windows */
/****************************************************************************/ /****************************************************************************/
......
...@@ -227,6 +227,7 @@ DLLEXPORT BOOL DLLCALL isfullpath(const char* filename); ...@@ -227,6 +227,7 @@ DLLEXPORT BOOL DLLCALL isfullpath(const char* filename);
DLLEXPORT char* DLLCALL getfname(const char* path); DLLEXPORT char* DLLCALL getfname(const char* path);
DLLEXPORT char* DLLCALL getfext(const char* path); DLLEXPORT char* DLLCALL getfext(const char* path);
DLLEXPORT int DLLCALL getfattr(const char* filename); DLLEXPORT int DLLCALL getfattr(const char* filename);
DLLEXPORT ulong DLLCALL getdisksize(const char* path, ulong unit);
DLLEXPORT ulong DLLCALL getfreediskspace(const char* path, ulong unit); DLLEXPORT ulong DLLCALL getfreediskspace(const char* path, ulong unit);
DLLEXPORT ulong DLLCALL delfiles(char *inpath, char *spec); DLLEXPORT ulong DLLCALL delfiles(char *inpath, char *spec);
DLLEXPORT char* DLLCALL backslash(char* path); DLLEXPORT char* DLLCALL backslash(char* path);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment