diff --git a/Foundation/include/Poco/File.h b/Foundation/include/Poco/File.h index 4dc6d775c..00ba0a32d 100644 --- a/Foundation/include/Poco/File.h +++ b/Foundation/include/Poco/File.h @@ -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; diff --git a/Foundation/include/Poco/File_UNIX.h b/Foundation/include/Poco/File_UNIX.h index 06a64d07b..3642ebad0 100644 --- a/Foundation/include/Poco/File_UNIX.h +++ b/Foundation/include/Poco/File_UNIX.h @@ -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: diff --git a/Foundation/include/Poco/File_VMS.h b/Foundation/include/Poco/File_VMS.h index 2e8395a1b..024ac453c 100644 --- a/Foundation/include/Poco/File_VMS.h +++ b/Foundation/include/Poco/File_VMS.h @@ -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: diff --git a/Foundation/include/Poco/File_VX.h b/Foundation/include/Poco/File_VX.h index a6470edc6..926c15f13 100644 --- a/Foundation/include/Poco/File_VX.h +++ b/Foundation/include/Poco/File_VX.h @@ -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: diff --git a/Foundation/include/Poco/File_WIN32.h b/Foundation/include/Poco/File_WIN32.h index 2f06683c2..dad370f0f 100644 --- a/Foundation/include/Poco/File_WIN32.h +++ b/Foundation/include/Poco/File_WIN32.h @@ -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: diff --git a/Foundation/include/Poco/File_WIN32U.h b/Foundation/include/Poco/File_WIN32U.h index 387751b4b..fe0c7499f 100644 --- a/Foundation/include/Poco/File_WIN32U.h +++ b/Foundation/include/Poco/File_WIN32U.h @@ -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: diff --git a/Foundation/include/Poco/File_WINCE.h b/Foundation/include/Poco/File_WINCE.h index 05f75e2a8..9a8dd5dd1 100644 --- a/Foundation/include/Poco/File_WINCE.h +++ b/Foundation/include/Poco/File_WINCE.h @@ -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: diff --git a/Foundation/src/File.cpp b/Foundation/src/File.cpp index 5ecb84edf..06ba25b6a 100644 --- a/Foundation/src/File.cpp +++ b/Foundation/src/File.cpp @@ -316,6 +316,24 @@ void File::list(std::vector& 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& files) const { files.clear(); diff --git a/Foundation/src/File_UNIX.cpp b/Foundation/src/File_UNIX.cpp index 84a606a65..9546b8e2b 100644 --- a/Foundation/src/File_UNIX.cpp +++ b/Foundation/src/File_UNIX.cpp @@ -20,6 +20,12 @@ #include #include #include +#if defined(POCO_OS_FAMILY_BSD) +#include +#include +#else +#include +#endif #include #include #include @@ -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) diff --git a/Foundation/src/File_VMS.cpp b/Foundation/src/File_VMS.cpp index 40880ed29..a8b640e47 100644 --- a/Foundation/src/File_VMS.cpp +++ b/Foundation/src/File_VMS.cpp @@ -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) diff --git a/Foundation/src/File_VX.cpp b/Foundation/src/File_VX.cpp index 13878f2a6..4a59082b9 100644 --- a/Foundation/src/File_VX.cpp +++ b/Foundation/src/File_VX.cpp @@ -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) diff --git a/Foundation/src/File_WIN32.cpp b/Foundation/src/File_WIN32.cpp index 343f80ca0..9cb92e3ea 100644 --- a/Foundation/src/File_WIN32.cpp +++ b/Foundation/src/File_WIN32.cpp @@ -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(); diff --git a/Foundation/src/File_WIN32U.cpp b/Foundation/src/File_WIN32U.cpp index 94bc2e553..3bdff0171 100644 --- a/Foundation/src/File_WIN32U.cpp +++ b/Foundation/src/File_WIN32U.cpp @@ -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(); diff --git a/Foundation/src/File_WINCE.cpp b/Foundation/src/File_WINCE.cpp index c321a76ee..a11b1c822 100644 --- a/Foundation/src/File_WINCE.cpp +++ b/Foundation/src/File_WINCE.cpp @@ -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()) diff --git a/Foundation/testsuite/src/FileTest.cpp b/Foundation/testsuite/src/FileTest.cpp index fc0810e41..3334159b9 100644 --- a/Foundation/testsuite/src/FileTest.cpp +++ b/Foundation/testsuite/src/FileTest.cpp @@ -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); diff --git a/Foundation/testsuite/src/FileTest.h b/Foundation/testsuite/src/FileTest.h index 0177a9eca..6560a0492 100644 --- a/Foundation/testsuite/src/FileTest.h +++ b/Foundation/testsuite/src/FileTest.h @@ -33,6 +33,7 @@ public: void testCompare(); void testSwap(); void testSize(); + void testSpace(); void testDirectory(); void testCopy(); void testMove();