Skip to content

Commit

Permalink
ISSUE #608 Provide better reasoning in the log for failures to write …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
sbanethia committed Dec 26, 2023
1 parent 5755809 commit ed492e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
32 changes: 27 additions & 5 deletions bouncer/src/repo/core/handler/fileservice/repo_file_handler_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions bouncer/src/repo/lib/repo_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit ed492e3

Please sign in to comment.