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
commit 5a1920c935
16 changed files with 290 additions and 0 deletions

View File

@ -208,6 +208,15 @@ public:
/// Fills the vector with the names of all /// Fills the vector with the names of all
/// files in the directory. /// 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; bool operator != (const File& file) const;
bool operator < (const File& file) const; bool operator < (const File& file) const;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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 void File::list(std::vector<File>& files) const
{ {
files.clear(); files.clear();

View File

@ -20,6 +20,12 @@
#include <algorithm> #include <algorithm>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.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 <fcntl.h>
#include <errno.h> #include <errno.h>
#include <unistd.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) void FileImpl::handleLastErrorImpl(const std::string& path)
{ {
switch (errno) 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) void FileImpl::handleLastErrorImpl(const std::string& path)
{ {
switch (errno) 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) void FileImpl::handleLastErrorImpl(const std::string& path)
{ {
switch (errno) 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) void FileImpl::handleLastErrorImpl(const std::string& path)
{ {
DWORD err = GetLastError(); 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) void FileImpl::handleLastErrorImpl(const std::string& path)
{ {
DWORD err = GetLastError(); 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) void FileImpl::handleLastErrorImpl(const std::string& path)
{ {
switch (GetLastError()) switch (GetLastError())

View File

@ -180,6 +180,33 @@ void FileTest::testFileAttributes1()
catch (Exception&) 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() void FileTest::testDirectory()
{ {
File d("testdir"); File d("testdir");
@ -525,6 +561,7 @@ CppUnit::Test* FileTest::suite()
CppUnit_addTest(pSuite, FileTest, testCompare); CppUnit_addTest(pSuite, FileTest, testCompare);
CppUnit_addTest(pSuite, FileTest, testSwap); CppUnit_addTest(pSuite, FileTest, testSwap);
CppUnit_addTest(pSuite, FileTest, testSize); CppUnit_addTest(pSuite, FileTest, testSize);
CppUnit_addTest(pSuite, FileTest, testSpace);
CppUnit_addTest(pSuite, FileTest, testDirectory); CppUnit_addTest(pSuite, FileTest, testDirectory);
CppUnit_addTest(pSuite, FileTest, testCopy); CppUnit_addTest(pSuite, FileTest, testCopy);
CppUnit_addTest(pSuite, FileTest, testMove); CppUnit_addTest(pSuite, FileTest, testMove);

View File

@ -33,6 +33,7 @@ public:
void testCompare(); void testCompare();
void testSwap(); void testSwap();
void testSize(); void testSize();
void testSpace();
void testDirectory(); void testDirectory();
void testCopy(); void testCopy();
void testMove(); void testMove();