#1545: Merge partition space information methods from develop

This commit is contained in:
Günter Obiltschnig 2018-03-06 18:46:48 +01:00
parent 0d3c3ce4d4
commit 3f75fd539f
12 changed files with 230 additions and 0 deletions

View File

@ -232,6 +232,15 @@ public:
/// Fills the vector with the names of all
/// files in the directory.
FileSize totalSpace() const;
/// Returns the total size in bytes of the partition containing this path.
FileSize usableSpace() const;
/// Returns the number of usable free bytes on the partition containing this path.
FileSize freeSpace() const;
/// Returns the number of free bytes on the partition containing this path.
bool operator == (const File& file) const;
bool operator != (const File& file) const;
bool operator < (const File& file) const;

View File

@ -57,6 +57,9 @@ protected:
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();
FileSizeImpl totalSpaceImpl() const;
FileSizeImpl usableSpaceImpl() const;
FileSizeImpl freeSpaceImpl() const;
static void handleLastErrorImpl(const std::string& path);
private:

View File

@ -57,6 +57,9 @@ protected:
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();
FileSizeImpl totalSpaceImpl() const;
FileSizeImpl usableSpaceImpl() const;
FileSizeImpl freeSpaceImpl() const;
static void handleLastErrorImpl(const std::string& path);
private:

View File

@ -58,6 +58,9 @@ protected:
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();
FileSizeImpl totalSpaceImpl() const;
FileSizeImpl usableSpaceImpl() const;
FileSizeImpl freeSpaceImpl() const;
static void handleLastErrorImpl(const std::string& path);
private:

View File

@ -58,6 +58,9 @@ protected:
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();
FileSizeImpl totalSpaceImpl() const;
FileSizeImpl usableSpaceImpl() const;
FileSizeImpl freeSpaceImpl() const;
static void handleLastErrorImpl(const std::string& path);
static void convertPath(const std::string& utf8Path, std::wstring& utf16Path);

View File

@ -58,6 +58,9 @@ protected:
void removeImpl();
bool createFileImpl();
bool createDirectoryImpl();
FileSizeImpl totalSpaceImpl() const;
FileSizeImpl usableSpaceImpl() const;
FileSizeImpl freeSpaceImpl() const;
static void handleLastErrorImpl(const std::string& path);
static void convertPath(const std::string& utf8Path, std::wstring& utf16Path);

View File

@ -354,6 +354,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

@ -19,6 +19,15 @@
#include <algorithm>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#if defined(POCO_OS_FAMILY_BSD)
#include <sys/param.h>
#include <sys/mount.h>
#elif (POCO_OS == POCO_OS_SOLARIS)
#include <sys/statvfs.h>
#else
#include <sys/statfs.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
@ -26,6 +35,14 @@
#include <utime.h>
#include <cstring>
#if (POCO_OS == POCO_OS_SOLARIS)
#define STATFSFN statvfs
#define STATFSSTRUCT statvfs
#else
#define STATFSFN statfs
#define STATFSSTRUCT statfs
#endif
namespace Poco {
@ -428,6 +445,42 @@ bool FileImpl::createDirectoryImpl()
}
FileImpl::FileSizeImpl FileImpl::totalSpaceImpl() const
{
poco_assert(!_path.empty());
struct STATFSSTRUCT stats;
if (STATFSFN(const_cast<char*>(_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 STATFSSTRUCT stats;
if (STATFSFN(const_cast<char*>(_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 STATFSSTRUCT stats;
if (STATFSFN(const_cast<char*>(_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

@ -334,6 +334,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

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

View File

@ -389,6 +389,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

@ -352,6 +352,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())