mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-15 14:43:32 +02:00
Merge pull request #1132 from TcT2k/FileDiskSpace
Add partition space information to File class
This commit is contained in:
commit
5a1920c935
@ -208,6 +208,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;
|
||||
|
@ -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:
|
||||
|
@ -59,6 +59,9 @@ protected:
|
||||
void removeImpl();
|
||||
bool createFileImpl();
|
||||
bool createDirectoryImpl();
|
||||
FileSizeImpl totalSpaceImpl() const;
|
||||
FileSizeImpl usableSpaceImpl() const;
|
||||
FileSizeImpl freeSpaceImpl() const;
|
||||
static void handleLastError(const std::string& path);
|
||||
|
||||
private:
|
||||
|
@ -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:
|
||||
|
@ -59,6 +59,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:
|
||||
|
@ -59,6 +59,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:
|
||||
|
@ -59,6 +59,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:
|
||||
|
@ -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();
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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())
|
||||
|
@ -180,6 +180,33 @@ void FileTest::testFileAttributes1()
|
||||
catch (Exception&)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
f.totalSpace();
|
||||
failmsg("file does not exist - must throw exception");
|
||||
}
|
||||
catch (Exception&)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
f.usableSpace();
|
||||
failmsg("file does not exist - must throw exception");
|
||||
}
|
||||
catch (Exception&)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
f.freeSpace();
|
||||
failmsg("file does not exist - must throw exception");
|
||||
}
|
||||
catch (Exception&)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -317,6 +344,15 @@ void FileTest::testSize()
|
||||
}
|
||||
|
||||
|
||||
void FileTest::testSpace()
|
||||
{
|
||||
File f(Path::home());
|
||||
assert(f.totalSpace() > 0);
|
||||
assert(f.usableSpace() > 0);
|
||||
assert(f.freeSpace() > 0);
|
||||
}
|
||||
|
||||
|
||||
void FileTest::testDirectory()
|
||||
{
|
||||
File d("testdir");
|
||||
@ -525,6 +561,7 @@ CppUnit::Test* FileTest::suite()
|
||||
CppUnit_addTest(pSuite, FileTest, testCompare);
|
||||
CppUnit_addTest(pSuite, FileTest, testSwap);
|
||||
CppUnit_addTest(pSuite, FileTest, testSize);
|
||||
CppUnit_addTest(pSuite, FileTest, testSpace);
|
||||
CppUnit_addTest(pSuite, FileTest, testDirectory);
|
||||
CppUnit_addTest(pSuite, FileTest, testCopy);
|
||||
CppUnit_addTest(pSuite, FileTest, testMove);
|
||||
|
@ -33,6 +33,7 @@ public:
|
||||
void testCompare();
|
||||
void testSwap();
|
||||
void testSize();
|
||||
void testSpace();
|
||||
void testDirectory();
|
||||
void testCopy();
|
||||
void testMove();
|
||||
|
Loading…
x
Reference in New Issue
Block a user