|
Boost Users : |
From: Thomas Matelich (matelich_at_[hidden])
Date: 2005-06-21 15:23:04
On 6/21/05, remi.chateauneu_at_[hidden] <remi.chateauneu_at_[hidden]> wrote:
> I am, thanks in advance !
> If this implementation is not deemed to go with Boost,
> why not a new project on Sourceforge ?
> I may try to port it to Solaris, if you wish to.
>
>
> Thomas Matelich wrote:
>
> >On 6/21/05, Steve Hartmann <steven_hartmann_at_[hidden]> wrote:
> >
> >
> >>Hello all,
> >>
> >>I was browsing boost to find some portable means for getting
> >>file system statistics such as file system (disk) size, free
> >>space, etc. I kind of expected to see this in the boost
> >>filesystem library, but they don't appear to be there.
> >>Doesn't even seem to be in the "Do-list".
> >>
> >>Am I missing something? Looking in the wrong place?
> >>
> >>
> >
> >
> >I've got an implementation (independent of FileSystem) that works on
> >windows, hp and linux (others I'm sure, but I don't use them) if
> >anyone's interested.
> >
Its really not that complicated. just a wrapper around statfs on unix
and GetDiskFreeSpaceEx on windows. Its not terribly long, I'll post
it (copied from a longer .cpp file, sorry if I miss an include).
#ifndef _WIN32
#include <sys/vfs.h>
#else
#include <winsock2.h> // I don't want winsock.h included ever
#include <Windows.h>
#endif
#include <boost/cstdint.hpp>
namespace
{
bool getDeviceStuff(std::string const& path, boost::intmax_t&
num_bytes, boost::intmax_t& available_bytes)
{
if(path.empty())
{
//ZLOG("diskspace") << "Cannot get device info for empty directory
path" << std::endl;
return false;
}
#ifndef _WIN32
struct statfs buf;
if(statfs(path.c_str(), &buf) < 0)
{
//ZLOG("diskspace") << "statfs failed. " << strerror(errno) << std::endl;
return false;
}
//ZLOG("diskspace") << " f_blocks=" << buf.f_blocks << " f_bsize="
<< buf.f_bsize << " f_bavail=" << buf.f_bavail << std::endl;
num_bytes = buf.f_blocks * buf.f_bsize;
available_bytes = buf.f_bavail * buf.f_bsize;
#else
__int64 freebytestocaller, totalbytes, freebytes;
if(!GetDiskFreeSpaceEx (path.c_str(),
(PULARGE_INTEGER)&freebytestocaller,
(PULARGE_INTEGER)&totalbytes,
(PULARGE_INTEGER)&freebytes))
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
//ZLOG("diskspace") << "GetDiskFreeSpaceEx failed: " <<
GetLastError() << " - " << (LPCTSTR)lpMsgBuf << std::endl;
LocalFree( lpMsgBuf );
return false;
}
num_bytes = totalbytes;
available_bytes = freebytestocaller;
#endif
return true;
}
}
double XPlat::getDeviceFreeSpace(std::string const& path)
{
boost::intmax_t num_bytes, available_bytes;
if(!getDeviceStuff(path, num_bytes, available_bytes))
return -1.0;
double retval = double(available_bytes) / (1 << 20);
//ZLOG("diskspace") << retval << " meg free for " << path << std::endl;
return retval;
}
double XPlat::getDeviceFreePercent(std::string const& path)
{
boost::intmax_t num_bytes, available_bytes;
if(!getDeviceStuff(path, num_bytes, available_bytes))
return -1.0;
double retval = (double(available_bytes) / num_bytes) * 100.0;
//ZLOG("diskspace") << retval << " {(double(" << available_bytes <<
") / " << num_bytes << ") * 100.0} percent free for " << path <<
std::endl;
return retval;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net