Merge pull request #1132 from TcT2k/FileDiskSpace

Add partition space information to File class
This commit is contained in:
Günter Obiltschnig
2016-03-05 15:01:28 +01:00
16 changed files with 290 additions and 0 deletions

View File

@@ -316,6 +316,24 @@ void File::list(std::vector<std::string>& files) const
}
File::FileSize File::totalSpace() const
{
return totalSpaceImpl();
}
File::FileSize File::usableSpace() const
{
return usableSpaceImpl();
}
File::FileSize File::freeSpace() const
{
return freeSpaceImpl();
}
void File::list(std::vector<File>& files) const
{
files.clear();

View File

@@ -20,6 +20,12 @@
#include <algorithm>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(POCO_OS_FAMILY_BSD)
#include <sys/param.h>
#include <sys/mount.h>
#else
#include <sys/statfs.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
@@ -408,6 +414,42 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
struct statfs stats;
if (statfs(_path.c_str(), &stats) != 0)
handleLastErrorImpl(_path);
return (FileSizeImpl)stats.f_blocks * (FileSizeImpl)stats.f_bsize;
}
FileImpl::FileSizeImpl FileImpl::usableSpaceImpl() const
{
poco_assert(!_path.empty());
struct statfs stats;
if (statfs(_path.c_str(), &stats) != 0)
handleLastErrorImpl(_path);
return (FileSizeImpl)stats.f_bavail * (FileSizeImpl)stats.f_bsize;
}
FileImpl::FileSizeImpl FileImpl::freeSpaceImpl() const
{
poco_assert(!_path.empty());
struct statfs stats;
if (statfs(_path.c_str(), &stats) != 0)
handleLastErrorImpl(_path);
return (FileSizeImpl)stats.f_bfree * (FileSizeImpl)stats.f_bsize;
}
void FileImpl::handleLastErrorImpl(const std::string& path)
{
switch (errno)

View File

@@ -350,6 +350,36 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
// TODO: implement
return -1;
}
FileImpl::FileSizeImpl FileImpl::usableSpaceImpl() const
{
poco_assert(!_path.empty());
// TODO: implement
return -1;
}
FileImpl::FileSizeImpl FileImpl::freeSpaceImpl() const
{
poco_assert(!_path.empty());
// TODO: implement
return -1;
}
void FileImpl::handleLastErrorImpl(const std::string& path)
{
switch (errno)

View File

@@ -330,6 +330,42 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
struct statfs stats;
if (statfs(_path.c_str(), &stats) != 0)
handleLastErrorImpl(_path);
return (FileSizeImpl)stats.f_blocks * (FileSizeImpl)stats.f_bsize;
}
FileImpl::FileSizeImpl FileImpl::usableSpaceImpl() const
{
poco_assert(!_path.empty());
struct statfs stats;
if (statfs(_path.c_str(), &stats) != 0)
handleLastErrorImpl(_path);
return (FileSizeImpl)stats.f_bavail * (FileSizeImpl)stats.f_bsize;
}
FileImpl::FileSizeImpl FileImpl::freeSpaceImpl() const
{
poco_assert(!_path.empty());
struct statfs stats;
if (statfs(_path.c_str(), &stats) != 0)
handleLastErrorImpl(_path);
return (FileSizeImpl)stats.f_bfree * (FileSizeImpl)stats.f_bsize;
}
void FileImpl::handleLastErrorImpl(const std::string& path)
{
switch (errno)

View File

@@ -349,6 +349,39 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceEx(_path.c_str(), NULL, &space, NULL))
handleLastErrorImpl(_path);
return space.QuadPart;
}
FileImpl::FileSizeImpl FileImpl::usableSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceEx(_path.c_str(), &space, NULL, NULL))
handleLastErrorImpl(_path);
return space.QuadPart;
}
FileImpl::FileSizeImpl FileImpl::freeSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceEx(_path.c_str(), NULL, NULL, &space))
handleLastErrorImpl(_path);
return space.QuadPart;
}
void FileImpl::handleLastErrorImpl(const std::string& path)
{
DWORD err = GetLastError();

View File

@@ -357,6 +357,39 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceExW(_upath.c_str(), NULL, &space, NULL))
handleLastErrorImpl(_path);
return space.QuadPart;
}
FileImpl::FileSizeImpl FileImpl::usableSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceExW(_upath.c_str(), &space, NULL, NULL))
handleLastErrorImpl(_path);
return space.QuadPart;
}
FileImpl::FileSizeImpl FileImpl::freeSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceExW(_upath.c_str(), NULL, NULL, &space))
handleLastErrorImpl(_path);
return space.QuadPart;
}
void FileImpl::handleLastErrorImpl(const std::string& path)
{
DWORD err = GetLastError();

View File

@@ -348,6 +348,39 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceExW(_upath.c_str(), NULL, &space, NULL))
handleLastErrorImpl(_path);
return space.QuadPart;
}
FileImpl::FileSizeImpl FileImpl::usableSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceExW(_upath.c_str(), &space, NULL, NULL))
handleLastErrorImpl(_path);
return space.QuadPart;
}
FileImpl::FileSizeImpl FileImpl::freeSpaceImpl() const
{
poco_assert(!_path.empty());
ULARGE_INTEGER space;
if (!GetDiskFreeSpaceExW(_upath.c_str(), NULL, NULL, &space))
handleLastErrorImpl(_path);
return space.QuadPart;
}
void FileImpl::handleLastErrorImpl(const std::string& path)
{
switch (GetLastError())