Copy/rename/create

This commit is contained in:
Uwe Seimet 2021-12-20 18:31:55 +01:00
parent b969d3990a
commit 5afce5f50f
2 changed files with 32 additions and 20 deletions

View File

@ -46,6 +46,23 @@ RascsiImage::RascsiImage()
} }
} }
bool RascsiImage::CreateImageFolder(int fd, const string& filename)
{
size_t filename_start = filename.rfind('/');
if (filename_start != string::npos) {
string folder = filename.substr(0, filename_start);
std::error_code error;
filesystem::create_directories(folder, error);
if (error) {
ReturnStatus(fd, false, "Can't create image folder '" + folder + "': " + strerror(errno));
return false;
}
}
return true;
}
string RascsiImage::SetDefaultImageFolder(const string& f) string RascsiImage::SetDefaultImageFolder(const string& f)
{ {
string folder = f; string folder = f;
@ -131,14 +148,8 @@ bool RascsiImage::CreateImage(int fd, const PbCommand& command)
return ReturnStatus(fd, false, error.str()); return ReturnStatus(fd, false, error.str());
} }
size_t slash_position = filename.find('/'); if (!CreateImageFolder(fd, full_filename)) {
if (slash_position != string::npos) { return false;
string folder = default_image_folder + "/" + filename.substr(0, slash_position);
struct stat st;
if (stat(folder.c_str(), &st) && mkdir(folder.c_str(), 0777)) {
return ReturnStatus(fd, false, "Can't create folder '" + folder + "': " + strerror(errno));
}
} }
string permission = GetParam(command, "read_only"); string permission = GetParam(command, "read_only");
@ -210,9 +221,7 @@ bool RascsiImage::RenameImage(int fd, const PbCommand& command)
if (from.empty()) { if (from.empty()) {
return ReturnStatus(fd, false, "Can't rename image file: Missing source filename"); return ReturnStatus(fd, false, "Can't rename image file: Missing source filename");
} }
if (from.find('/') != string::npos) {
return ReturnStatus(fd, false, "The source filename '" + from + "' must not contain a path");
}
from = default_image_folder + "/" + from; from = default_image_folder + "/" + from;
if (!IsValidSrcFilename(from)) { if (!IsValidSrcFilename(from)) {
return ReturnStatus(fd, false, "Can't rename image file: '" + from + "': Invalid name or type"); return ReturnStatus(fd, false, "Can't rename image file: '" + from + "': Invalid name or type");
@ -222,14 +231,16 @@ bool RascsiImage::RenameImage(int fd, const PbCommand& command)
if (to.empty()) { if (to.empty()) {
return ReturnStatus(fd, false, "Can't rename image file '" + from + "': Missing destination filename"); return ReturnStatus(fd, false, "Can't rename image file '" + from + "': Missing destination filename");
} }
if (to.find('/') != string::npos) {
return ReturnStatus(fd, false, "The destination filename '" + to + "' must not contain a path");
}
to = default_image_folder + "/" + to; to = default_image_folder + "/" + to;
if (!IsValidDstFilename(to)) { if (!IsValidDstFilename(to)) {
return ReturnStatus(fd, false, "Can't rename image file '" + from + "' to '" + to + "': File already exists"); return ReturnStatus(fd, false, "Can't rename image file '" + from + "' to '" + to + "': File already exists");
} }
if (!CreateImageFolder(fd, to)) {
return false;
}
if (rename(from.c_str(), to.c_str())) { if (rename(from.c_str(), to.c_str())) {
return ReturnStatus(fd, false, "Can't rename image file '" + from + "' to '" + to + "': " + string(strerror(errno))); return ReturnStatus(fd, false, "Can't rename image file '" + from + "' to '" + to + "': " + string(strerror(errno)));
} }
@ -245,9 +256,7 @@ bool RascsiImage::CopyImage(int fd, const PbCommand& command)
if (from.empty()) { if (from.empty()) {
return ReturnStatus(fd, false, "Can't copy image file: Missing source filename"); return ReturnStatus(fd, false, "Can't copy image file: Missing source filename");
} }
if (from.find('/') != string::npos) {
return ReturnStatus(fd, false, "The source filename '" + from + "' must not contain a path");
}
from = default_image_folder + "/" + from; from = default_image_folder + "/" + from;
if (!IsValidSrcFilename(from)) { if (!IsValidSrcFilename(from)) {
return ReturnStatus(fd, false, "Can't copy image file: '" + from + "': Invalid name or type"); return ReturnStatus(fd, false, "Can't copy image file: '" + from + "': Invalid name or type");
@ -257,9 +266,7 @@ bool RascsiImage::CopyImage(int fd, const PbCommand& command)
if (to.empty()) { if (to.empty()) {
return ReturnStatus(fd, false, "Can't copy image file '" + from + "': Missing destination filename"); return ReturnStatus(fd, false, "Can't copy image file '" + from + "': Missing destination filename");
} }
if (to.find('/') != string::npos) {
return ReturnStatus(fd, false, "The destination filename '" + to + "' must not contain a path");
}
to = default_image_folder + "/" + to; to = default_image_folder + "/" + to;
if (!IsValidDstFilename(to)) { if (!IsValidDstFilename(to)) {
return ReturnStatus(fd, false, "Can't copy image file '" + from + "' to '" + to + "': File already exists"); return ReturnStatus(fd, false, "Can't copy image file '" + from + "' to '" + to + "': File already exists");
@ -270,6 +277,10 @@ bool RascsiImage::CopyImage(int fd, const PbCommand& command)
return ReturnStatus(fd, false, "Can't access source image file '" + from + "': " + string(strerror(errno))); return ReturnStatus(fd, false, "Can't access source image file '" + from + "': " + string(strerror(errno)));
} }
if (!CreateImageFolder(fd, to)) {
return false;
}
// Symbolic links need a special handling // Symbolic links need a special handling
if ((st.st_mode & S_IFMT) == S_IFLNK) { if ((st.st_mode & S_IFMT) == S_IFLNK) {
if (symlink(filesystem::read_symlink(from).c_str(), to.c_str())) { if (symlink(filesystem::read_symlink(from).c_str(), to.c_str())) {

View File

@ -22,6 +22,7 @@ public:
RascsiImage(); RascsiImage();
~RascsiImage() {}; ~RascsiImage() {};
bool CreateImageFolder(int, const string&);
string GetDefaultImageFolder() const { return default_image_folder; } string GetDefaultImageFolder() const { return default_image_folder; }
string SetDefaultImageFolder(const string&); string SetDefaultImageFolder(const string&);
bool IsValidSrcFilename(const string&); bool IsValidSrcFilename(const string&);