implemented rename function
All checks were successful
EIVE/eive-obsw/pipeline/head This commit looks good

This commit is contained in:
2021-11-25 21:38:13 +01:00
parent 0f89282dae
commit 5b412bf072
4 changed files with 73 additions and 63 deletions

View File

@ -136,13 +136,11 @@ ReturnValue_t FileSystemHandler::initialize() {
ReturnValue_t FileSystemHandler::appendToFile(const char* repositoryPath,
const char* filename, const uint8_t* data, size_t size,
uint16_t packetNumber, FileSystemArgsIF* args) {
// A double slash between repo and filename should not be an issue, so add it in any case
std::string fullPath = currentMountPrefix + std::string(repositoryPath) + "/" +
std::string(filename);
if(not std::filesystem::exists(fullPath)) {
auto path = getInitPath(args) / repositoryPath / filename;
if(not std::filesystem::exists(path)) {
return FILE_DOES_NOT_EXIST;
}
std::ofstream file(fullPath, std::ios_base::app|std::ios_base::out);
std::ofstream file(path, std::ios_base::app|std::ios_base::out);
file.write(reinterpret_cast<const char*>(data), size);
if(not file.good()) {
return GENERIC_FILE_ERROR;
@ -152,19 +150,11 @@ ReturnValue_t FileSystemHandler::appendToFile(const char* repositoryPath,
ReturnValue_t FileSystemHandler::createFile(const char* repositoryPath,
const char* filename, const uint8_t* data, size_t size, FileSystemArgsIF* args) {
std::string fullPath;
bool useMountPrefix = true;
parseCfg(reinterpret_cast<FsCommandCfg*>(args), useMountPrefix);
if(useMountPrefix) {
fullPath += currentMountPrefix;
}
// A double slash between repo and filename should not be an issue, so add it in any case
fullPath += std::string(repositoryPath) + "/" + std::string(filename);
if(std::filesystem::exists(fullPath)) {
auto path = getInitPath(args) / filename;
if(std::filesystem::exists(path)) {
return FILE_ALREADY_EXISTS;
}
std::ofstream file(fullPath);
std::ofstream file(path);
file.write(reinterpret_cast<const char*>(data), size);
if(not file.good()) {
return GENERIC_FILE_ERROR;
@ -174,19 +164,11 @@ ReturnValue_t FileSystemHandler::createFile(const char* repositoryPath,
ReturnValue_t FileSystemHandler::removeFile(const char* repositoryPath,
const char* filename, FileSystemArgsIF* args) {
std::string fullPath;
bool useMountPrefix = true;
parseCfg(reinterpret_cast<FsCommandCfg*>(args), useMountPrefix);
if(useMountPrefix) {
fullPath += currentMountPrefix;
}
// A double slash between repo and filename should not be an issue, so add it in any case
fullPath += std::string(repositoryPath) + "/" + std::string(filename);
if(not std::filesystem::exists(fullPath)) {
auto path = getInitPath(args) / repositoryPath / filename;
if(not std::filesystem::exists(path)) {
return FILE_DOES_NOT_EXIST;
}
int result = std::remove(fullPath.c_str());
int result = std::remove(path.c_str());
if(result != 0) {
sif::warning << "FileSystemHandler::deleteFile: Failed with code " << result << std::endl;
return GENERIC_FILE_ERROR;
@ -196,42 +178,26 @@ ReturnValue_t FileSystemHandler::removeFile(const char* repositoryPath,
ReturnValue_t FileSystemHandler:: createDirectory(const char* repositoryPath, const char* dirname,
bool createParentDirs, FileSystemArgsIF* args) {
std::string fullPath;
bool useMountPrefix = true;
parseCfg(reinterpret_cast<FsCommandCfg*>(args), useMountPrefix);
if(useMountPrefix) {
fullPath += currentMountPrefix;
}
fullPath += std::string(repositoryPath);
fullPath += "/" + std::string(dirname);
if(std::filesystem::exists(fullPath)) {
auto path = getInitPath(args) / repositoryPath / dirname;
if(std::filesystem::exists(path)) {
return DIRECTORY_ALREADY_EXISTS;
}
if(std::filesystem::create_directory(fullPath)) {
if(std::filesystem::create_directory(path)) {
return HasReturnvaluesIF::RETURN_OK;
}
sif::warning << "Creating directory " << fullPath << " failed" << std::endl;
sif::warning << "Creating directory " << path << " failed" << std::endl;
return GENERIC_FILE_ERROR;
}
ReturnValue_t FileSystemHandler::removeDirectory(const char* repositoryPath, const char* dirname,
bool deleteRecurively, FileSystemArgsIF* args) {
std::string fullPath;
bool useMountPrefix = true;
parseCfg(reinterpret_cast<FsCommandCfg*>(args), useMountPrefix);
if(useMountPrefix) {
fullPath += currentMountPrefix;
}
fullPath += std::string(repositoryPath);
fullPath += "/" + std::string(dirname);
if(not std::filesystem::exists(fullPath)) {
auto path = getInitPath(args) / repositoryPath / dirname;
if(not std::filesystem::exists(path)) {
return DIRECTORY_DOES_NOT_EXIST;
}
std::error_code err;
if(not deleteRecurively) {
if(std::filesystem::remove(fullPath, err)) {
if(std::filesystem::remove(path, err)) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
@ -248,7 +214,7 @@ ReturnValue_t FileSystemHandler::removeDirectory(const char* repositoryPath, con
}
}
else {
if(std::filesystem::remove_all(fullPath, err)) {
if(std::filesystem::remove_all(path, err)) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
@ -267,14 +233,25 @@ ReturnValue_t FileSystemHandler::removeDirectory(const char* repositoryPath, con
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t FileSystemHandler::renameFile(const char *repositoryPath, const char *oldFilename,
const char *newFilename, FileSystemArgsIF *args) {
auto basepath = getInitPath(args) / repositoryPath;
std::filesystem::rename(basepath / oldFilename, basepath / newFilename);
return HasReturnvaluesIF::RETURN_OK;
}
void FileSystemHandler::parseCfg(FsCommandCfg *cfg, bool& useMountPrefix) {
if(cfg != nullptr) {
useMountPrefix = cfg->useMountPrefix;
}
}
ReturnValue_t FileSystemHandler::renameFile(const char *repositoryPath, const char *oldFilename,
const char *newFilename, FileSystemArgsIF *args) {
// TODO: Implement
return HasReturnvaluesIF::RETURN_OK;
std::filesystem::path FileSystemHandler::getInitPath(FileSystemArgsIF* args) {
bool useMountPrefix = true;
parseCfg(reinterpret_cast<FsCommandCfg*>(args), useMountPrefix);
std::string path;
if(useMountPrefix) {
path = currentMountPrefix;
}
return std::filesystem::path(path);
}