diff --git a/common/utility.hpp b/common/utility.hpp index 575272e..85be83e 100644 --- a/common/utility.hpp +++ b/common/utility.hpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include class Utility @@ -16,6 +18,7 @@ class Utility static bool fmod(double dividend,double divisor); static bool fmod(double dividend, double divisor,double epsilon); static String byteCountToString(double bytesTransferred, bool perSecond = true); + static size_t getAvailableDiskSpace(const String &path); private: static String pad(String theString,char padChar,int toLength); static String addCommas(String& theString); @@ -172,4 +175,23 @@ bool Utility::fmod(double dividend, double divisor) // Check if the absolute remainder is less than epsilon return std::fabs(remainder) < epsilon; } + +// inline +// size_t Utility::getAvailableDiskSpace(const String& path) +// { +// struct statvfs stat; +// if (::statvfs(path, &stat) != 0) +// { +// throw new Exception("Error calling statvfs to get available disk space."); +// } +// // Calculate available space in bytes. Multiply Number of free inodes * Fragment size +// return (size_t)stat.f_bavail * stat.f_frsize; +// } + +inline +size_t Utility::getAvailableDiskSpace(const String& path) +{ + std::filesystem::space_info si = std::filesystem::space(path.str()); + return si.available; +} #endif \ No newline at end of file diff --git a/sstp/.vscode/settings.json b/sstp/.vscode/settings.json index f46726b..24036bd 100644 --- a/sstp/.vscode/settings.json +++ b/sstp/.vscode/settings.json @@ -53,6 +53,8 @@ "stop_token": "cpp", "thread": "cpp", "*.tpp": "cpp", - "csignal": "cpp" + "csignal": "cpp", + "codecvt": "cpp", + "iomanip": "cpp" } } \ No newline at end of file diff --git a/sstp/main.cpp b/sstp/main.cpp index 40956b4..5f0e009 100644 --- a/sstp/main.cpp +++ b/sstp/main.cpp @@ -15,8 +15,6 @@ void signalHandler(int signal); // The Control-C handler SmartPointer sstp; -// **************************************************************************************************************************************************************** - /// @brief /// @param argc /// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName} @@ -25,7 +23,7 @@ int main(int argc, char **argv) { int returnCode(0); Profiler profiler; - String version = "0.1.0.4"; // major version, minor version, patch, build + String version = "0.1.0.5"; // major version, minor version, patch, build std::cout << "sstp version " << version.str() << std::endl; if(!registerSignalHandler()) diff --git a/sstp/socketconnectionreceiver.cpp b/sstp/socketconnectionreceiver.cpp index 0d395c8..380ac9f 100644 --- a/sstp/socketconnectionreceiver.cpp +++ b/sstp/socketconnectionreceiver.cpp @@ -94,10 +94,20 @@ bool SocketConnectionReceiver::handlePut(Block &commands) size_t fileLength; size_t totalBytesRead=0; size_t totalPacketsRead=0; + size_t availableDiskSpace=0; fileName = commands[1]; fileLength = commands[commands.size()-1].toLong(); - std::cout << "PUT" << " " << fileName << " " << fileLength << std::endl; + std::cout << "PUT" << " " << fileName << " " << Utility::byteCountToString(fileLength) << std::endl; + + availableDiskSpace=Utility::getAvailableDiskSpace(String("./")); + std::cout << Utility::byteCountToString(availableDiskSpace,false) << " available space on disk" << std::endl; + if(fileLength > availableDiskSpace) + { + std::cout << "Unsufficient space on disk. Required space " << Utility::byteCountToString(availableDiskSpace,false) << " , available space" << Utility::byteCountToString(availableDiskSpace,false) << std::endl; + return false; + } + FileIO writeFile(fileName, FileIO::ByteOrder::LittleEndian, FileIO::Mode::ReadWrite,FileIO::CreationFlags::CreateAlways); if(!writeFile.isOkay())