From ed492e3eb0c3ac419f94bb0f61e47a796b21a197 Mon Sep 17 00:00:00 2001 From: Saurabh Banethia <90389428+sbanethia@users.noreply.github.com> Date: Tue, 26 Dec 2023 17:06:11 +0530 Subject: [PATCH] ISSUE #608 Provide better reasoning in the log for failures to write to file system Added functions to check the file writing permissions and also check the free disk size before writing the file on the disk. --- .../fileservice/repo_file_handler_fs.cpp | 32 ++++++++++++++++--- bouncer/src/repo/lib/repo_utils.h | 20 ++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/bouncer/src/repo/core/handler/fileservice/repo_file_handler_fs.cpp b/bouncer/src/repo/core/handler/fileservice/repo_file_handler_fs.cpp index fb92ad660..834ac73cc 100644 --- a/bouncer/src/repo/core/handler/fileservice/repo_file_handler_fs.cpp +++ b/bouncer/src/repo/core/handler/fileservice/repo_file_handler_fs.cpp @@ -133,15 +133,37 @@ std::string FSFileHandler::uploadFile( path /= keyName; ss << keyName; int retries = 0; - bool failed; + bool failed = false; do { std::ofstream outs(path.string(), std::ios::out | std::ios::binary); - outs.write((char*)bin.data(), bin.size()); - outs.close(); - if (failed = (!outs || !repo::lib::doesFileExist(path))) { - repoError << "Failed to write to file " << path.string() << ((retries + 1) < 3 ? ". Retrying... " : ""); + if (outs.good()) + { + outs.write((char*)bin.data(), bin.size()); + outs.close(); + } + else if(outs.bad() || outs.fail()) + { + failed = true; + if (repo::lib::doesFileExist(path)) { + repoError << "Failed to write to file " << path.filename() <<" as file is already present. "; + } + + if (repo::lib::hasFileWritePermissions(path)) { + repoError << "Failed to write to file " << path.filename() << " as file writting permission is not there."; + } + + if (repo::lib::isFileWritingSpaceAvailable(path)) { + repoError << "Failed to write to file " << path.filename() << " as there is insufficient space on the disk."; + } + + repoError << ((retries + 1) < 3 ? ".Retrying... " : ""); boost::this_thread::sleep(boost::posix_time::seconds(5)); } + + /*if (failed = (!outs || !repo::lib::doesFileExist(path))) { + repoError << "Failed to write to file " << path.string() << ((retries + 1) < 3 ? ". Retrying... " : ""); + boost::this_thread::sleep(boost::posix_time::seconds(5)); + }*/ } while (failed && ++retries < 3); return failed ? "" : ss.str(); diff --git a/bouncer/src/repo/lib/repo_utils.h b/bouncer/src/repo/lib/repo_utils.h index 1209e1773..f86c59fc6 100644 --- a/bouncer/src/repo/lib/repo_utils.h +++ b/bouncer/src/repo/lib/repo_utils.h @@ -60,5 +60,25 @@ namespace repo { char* value = getenv(envVarName.c_str()); return (value && strlen(value) > 0) ? value : ""; } + + static bool hasFileWritePermissions(const boost::filesystem::path& inputPath) + { + + boost::filesystem::file_status s = boost::filesystem::status(inputPath); + if (boost::filesystem::perms::owner_write == s.permissions()) { + return true; + } + return false; + } + + static bool isFileWritingSpaceAvailable(const boost::filesystem::path& inputPath) + { + boost::filesystem::path pathWithoutFileName = inputPath; + boost::filesystem::space_info spaceInfo = boost::filesystem::space(pathWithoutFileName.remove_filename()); + if (spaceInfo.free > (boost::filesystem::file_size(inputPath) + 1)) { + return true; + } + return false; + } } } \ No newline at end of file