From 68aa20257fc8556330b2c08cfa76d7f2ab9fe68d Mon Sep 17 00:00:00 2001 From: kervala Date: Mon, 4 Jan 2016 15:39:31 +0100 Subject: [PATCH] Changed: Use new code to determine free disk space --HG-- branch : develop --- code/nel/include/nel/misc/system_info.h | 4 +- code/nel/src/misc/system_info.cpp | 56 ++++++++----------------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/code/nel/include/nel/misc/system_info.h b/code/nel/include/nel/misc/system_info.h index 4395277a1..747ff10d6 100644 --- a/code/nel/include/nel/misc/system_info.h +++ b/code/nel/include/nel/misc/system_info.h @@ -72,9 +72,9 @@ public: */ static bool isNT(); - /** Returns the space left on the hard drive that contains the filename + /** Returns the space left on the hard drive that contains the filename in bytes */ - static std::string availableHDSpace (const std::string &filename); + static uint64 availableHDSpace (const std::string &filename); /** Returns all the physical memory available on the computer (in bytes) */ diff --git a/code/nel/src/misc/system_info.cpp b/code/nel/src/misc/system_info.cpp index 0aa559e89..c891f97e2 100644 --- a/code/nel/src/misc/system_info.cpp +++ b/code/nel/src/misc/system_info.cpp @@ -34,6 +34,11 @@ # include # define nlcpuid(regs, idx) __cpuid(idx, regs[0], regs[1], regs[2], regs[3]) # endif // NL_CPU_INTEL +# ifdef NL_OS_MAC +# include +# else +# include +# endif #endif // NL_OS_WINDOWS #include "nel/misc/system_info.h" @@ -1118,49 +1123,24 @@ bool CSystemInfo::isNT() #endif } -string CSystemInfo::availableHDSpace (const string &filename) +uint64 CSystemInfo::availableHDSpace (const string &filename) { -#ifdef NL_OS_UNIX - string cmd = "df "; - if(filename.empty()) - cmd += "."; - else - cmd += filename; - cmd += " >/tmp/nelhdfs"; - sint error = system (cmd.c_str()); - if (error) - nlwarning("'%s' failed with error code %d", cmd.c_str(), error); - - int fd = open("/tmp/nelhdfs", O_RDONLY); - if (fd == -1) - { - return 0; - } - else - { - char buffer[4096+1]; - int len = read(fd, buffer, sizeof(buffer)-1); - close(fd); - buffer[len] = '\0'; - - vector splitted; - explode(string(buffer), string("\n"), splitted, true); + std::string path = CFile::getPath(filename); - if(splitted.size() < 2) - return "NoInfo"; - - vector sline; - explode(splitted[1], string(" "), sline, true); +#ifdef NL_OS_UNIX + struct stat stst; + struct statfs stfs; - if(sline.size() < 5) - return splitted[1]; + if (::stat(path.c_str(), &stst) == -1) return 0; + if (::statfs(c_str(), &stfs) == -1) return 0; - string space = sline[3] + "000"; - return bytesToHumanReadable(space); - } + return (uint64)(stfs.f_bavail * stst.st_blksize); #else - nlunreferenced(filename); - return "NoInfo"; + ULARGE_INTEGER freeSpace = {0}; + BOOL bRes = ::GetDiskFreeSpaceExA(path.c_str(), &freeSpace, NULL, NULL); + if (!bRes) return 0; + + return (uint64)freeSpace.QuadPart; #endif }