mirror of
https://github.com/akuker/RASCSI.git
synced 2024-12-21 23:29:39 +00:00
Code cleanup, initial support for creating nested file
This commit is contained in:
parent
4ae273ccbd
commit
6a9ac4d0da
@ -104,17 +104,15 @@ bool RascsiImage::CreateImage(int fd, const PbCommand& command)
|
|||||||
if (filename.empty()) {
|
if (filename.empty()) {
|
||||||
return ReturnStatus(fd, false, "Can't create image file: Missing image filename");
|
return ReturnStatus(fd, false, "Can't create image file: Missing image filename");
|
||||||
}
|
}
|
||||||
if (filename.find('/') != string::npos) {
|
|
||||||
return ReturnStatus(fd, false, "Can't create image file '" + filename + "': Filename must not contain a path");
|
string full_filename = default_image_folder + "/" + filename;
|
||||||
}
|
if (!IsValidDstFilename(full_filename)) {
|
||||||
filename = default_image_folder + "/" + filename;
|
return ReturnStatus(fd, false, "Can't create image file: '" + full_filename + "': File already exists");
|
||||||
if (!IsValidDstFilename(filename)) {
|
|
||||||
return ReturnStatus(fd, false, "Can't create image file: '" + filename + "': File already exists");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const string size = GetParam(command, "size");
|
const string size = GetParam(command, "size");
|
||||||
if (size.empty()) {
|
if (size.empty()) {
|
||||||
return ReturnStatus(fd, false, "Can't create image file '" + filename + "': Missing image size");
|
return ReturnStatus(fd, false, "Can't create image file '" + full_filename + "': Missing image size");
|
||||||
}
|
}
|
||||||
|
|
||||||
off_t len;
|
off_t len;
|
||||||
@ -122,10 +120,10 @@ bool RascsiImage::CreateImage(int fd, const PbCommand& command)
|
|||||||
len = stoull(size);
|
len = stoull(size);
|
||||||
}
|
}
|
||||||
catch(const invalid_argument& e) {
|
catch(const invalid_argument& e) {
|
||||||
return ReturnStatus(fd, false, "Can't create image file '" + filename + "': Invalid file size " + size);
|
return ReturnStatus(fd, false, "Can't create image file '" + full_filename + "': Invalid file size " + size);
|
||||||
}
|
}
|
||||||
catch(const out_of_range& e) {
|
catch(const out_of_range& e) {
|
||||||
return ReturnStatus(fd, false, "Can't create image file '" + filename + "': Invalid file size " + size);
|
return ReturnStatus(fd, false, "Can't create image file '" + full_filename + "': Invalid file size " + size);
|
||||||
}
|
}
|
||||||
if (len < 512 || (len & 0x1ff)) {
|
if (len < 512 || (len & 0x1ff)) {
|
||||||
ostringstream error;
|
ostringstream error;
|
||||||
@ -133,9 +131,12 @@ bool RascsiImage::CreateImage(int fd, const PbCommand& command)
|
|||||||
return ReturnStatus(fd, false, error.str());
|
return ReturnStatus(fd, false, error.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat st;
|
size_t slash_position = filename.find('/');
|
||||||
if (!stat(filename.c_str(), &st)) {
|
if (slash_position != string::npos) {
|
||||||
return ReturnStatus(fd, false, "Can't create image file '" + filename + "': File already exists");
|
string folder = default_image_folder + "/" + filename.substr(0, slash_position);
|
||||||
|
if (mkdir(folder.c_str(), 0777) == -1) {
|
||||||
|
return ReturnStatus(fd, false, "Can't create folder '" + folder + "': " + strerror(errno));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string permission = GetParam(command, "read_only");
|
string permission = GetParam(command, "read_only");
|
||||||
@ -143,23 +144,23 @@ bool RascsiImage::CreateImage(int fd, const PbCommand& command)
|
|||||||
int permissions = !strcasecmp(permission.c_str(), "true") ?
|
int permissions = !strcasecmp(permission.c_str(), "true") ?
|
||||||
S_IRUSR | S_IRGRP | S_IROTH : S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
S_IRUSR | S_IRGRP | S_IROTH : S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
||||||
|
|
||||||
int image_fd = open(filename.c_str(), O_CREAT|O_WRONLY, permissions);
|
int image_fd = open(full_filename.c_str(), O_CREAT|O_WRONLY, permissions);
|
||||||
if (image_fd == -1) {
|
if (image_fd == -1) {
|
||||||
return ReturnStatus(fd, false, "Can't create image file '" + filename + "': " + string(strerror(errno)));
|
return ReturnStatus(fd, false, "Can't create image file '" + full_filename + "': " + string(strerror(errno)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fallocate(image_fd, 0, 0, len) == -1) {
|
if (fallocate(image_fd, 0, 0, len) == -1) {
|
||||||
close(image_fd);
|
close(image_fd);
|
||||||
|
|
||||||
unlink(filename.c_str());
|
unlink(full_filename.c_str());
|
||||||
|
|
||||||
return ReturnStatus(fd, false, "Can't allocate space for image file '" + filename + "': " + string(strerror(errno)));
|
return ReturnStatus(fd, false, "Can't allocate space for image file '" + full_filename + "': " + string(strerror(errno)));
|
||||||
}
|
}
|
||||||
|
|
||||||
close(image_fd);
|
close(image_fd);
|
||||||
|
|
||||||
ostringstream msg;
|
ostringstream msg;
|
||||||
msg << "Created " << (permissions & S_IWUSR ? "": "read-only ") << "image file '" << filename + "' with a size of " << len << " bytes";
|
msg << "Created " << (permissions & S_IWUSR ? "": "read-only ") << "image file '" << full_filename + "' with a size of " << len << " bytes";
|
||||||
LOGINFO("%s", msg.str().c_str());
|
LOGINFO("%s", msg.str().c_str());
|
||||||
|
|
||||||
return ReturnStatus(fd);
|
return ReturnStatus(fd);
|
||||||
|
@ -127,10 +127,10 @@ void RasctlCommands::CommandReserveIds(const string& reserved_ids)
|
|||||||
|
|
||||||
void RasctlCommands::CommandCreateImage(const string& image_params)
|
void RasctlCommands::CommandCreateImage(const string& image_params)
|
||||||
{
|
{
|
||||||
size_t separatorPos = image_params.find(COMPONENT_SEPARATOR);
|
size_t separator_pos = image_params.find(COMPONENT_SEPARATOR);
|
||||||
if (separatorPos != string::npos) {
|
if (separator_pos != string::npos) {
|
||||||
AddParam(command, "file", image_params.substr(0, separatorPos));
|
AddParam(command, "file", image_params.substr(0, separator_pos));
|
||||||
AddParam(command, "size", image_params.substr(separatorPos + 1));
|
AddParam(command, "size", image_params.substr(separator_pos + 1));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cerr << "Error: Invalid file descriptor '" << image_params << "', format is NAME:SIZE" << endl;
|
cerr << "Error: Invalid file descriptor '" << image_params << "', format is NAME:SIZE" << endl;
|
||||||
@ -151,10 +151,10 @@ void RasctlCommands::CommandDeleteImage(const string& filename)
|
|||||||
|
|
||||||
void RasctlCommands::CommandRenameImage(const string& image_params)
|
void RasctlCommands::CommandRenameImage(const string& image_params)
|
||||||
{
|
{
|
||||||
size_t separatorPos = image_params.find(COMPONENT_SEPARATOR);
|
size_t separator_pos = image_params.find(COMPONENT_SEPARATOR);
|
||||||
if (separatorPos != string::npos) {
|
if (separator_pos != string::npos) {
|
||||||
AddParam(command, "from", image_params.substr(0, separatorPos));
|
AddParam(command, "from", image_params.substr(0, separator_pos));
|
||||||
AddParam(command, "to", image_params.substr(separatorPos + 1));
|
AddParam(command, "to", image_params.substr(separator_pos + 1));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cerr << "Error: Invalid file descriptor '" << image_params << "', format is CURRENT_NAME:NEW_NAME" << endl;
|
cerr << "Error: Invalid file descriptor '" << image_params << "', format is CURRENT_NAME:NEW_NAME" << endl;
|
||||||
@ -166,10 +166,10 @@ void RasctlCommands::CommandRenameImage(const string& image_params)
|
|||||||
|
|
||||||
void RasctlCommands::CommandCopyImage(const string& image_params)
|
void RasctlCommands::CommandCopyImage(const string& image_params)
|
||||||
{
|
{
|
||||||
size_t separatorPos = image_params.find(COMPONENT_SEPARATOR);
|
size_t separator_pos = image_params.find(COMPONENT_SEPARATOR);
|
||||||
if (separatorPos != string::npos) {
|
if (separator_pos != string::npos) {
|
||||||
AddParam(command, "from", image_params.substr(0, separatorPos));
|
AddParam(command, "from", image_params.substr(0, separator_pos));
|
||||||
AddParam(command, "to", image_params.substr(separatorPos + 1));
|
AddParam(command, "to", image_params.substr(separator_pos + 1));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cerr << "Error: Invalid file descriptor '" << image_params << "', format is CURRENT_NAME:NEW_NAME" << endl;
|
cerr << "Error: Invalid file descriptor '" << image_params << "', format is CURRENT_NAME:NEW_NAME" << endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user