Image file deletion: Fixed error handling (#944)

* Fixed ID/LUN check when deleting image

* Updated error handling when deleting image file

* Prevent renaming of image file in use
This commit is contained in:
Uwe Seimet 2022-10-26 17:43:11 +02:00 committed by GitHub
parent 77a32bd828
commit 2cecf4b35c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -169,19 +169,20 @@ bool RascsiImage::DeleteImage(const CommandContext& context, const PbCommand& co
return context.ReturnStatus(false, ("Invalid folder hierarchy depth '" + filename + "'").c_str()); return context.ReturnStatus(false, ("Invalid folder hierarchy depth '" + filename + "'").c_str());
} }
const string full_filename = GetFullName(filename); const auto full_filename = path(GetFullName(filename));
if (!exists(full_filename)) {
return context.ReturnStatus(false, "Image file '" + string(full_filename) + "' does not exist");
}
const auto [id, lun] = StorageDevice::GetIdsForReservedFile(full_filename); const auto [id, lun] = StorageDevice::GetIdsForReservedFile(full_filename);
if (id == -1 || lun == -1) { if (id != -1 || lun != -1) {
return context.ReturnStatus(false, "Can't delete image file '" + full_filename + return context.ReturnStatus(false, "Can't delete image file '" + string(full_filename) +
"', it is currently being used by device ID " + to_string(id) + ", LUN " + to_string(lun)); "', it is currently being used by device ID " + to_string(id) + ", LUN " + to_string(lun));
} }
try { if (error_code error; !remove(full_filename, error)) {
remove(path(full_filename)); return context.ReturnStatus(false, "Can't delete image file '" + string(full_filename) + "'");
}
catch(const filesystem_error& e) {
return context.ReturnStatus(false, "Can't delete image file '" + full_filename + "': " + e.what());
} }
// Delete empty subfolders // Delete empty subfolders
@ -194,12 +195,8 @@ bool RascsiImage::DeleteImage(const CommandContext& context, const PbCommand& co
break; break;
} }
try { if (error_code error; !remove(full_folder)) {
remove(full_folder); return context.ReturnStatus(false, "Can't delete empty image folder '" + string(full_folder) + "'");
}
catch(const filesystem_error& e) {
return context.ReturnStatus(false, "Can't delete empty image folder '" + string(full_folder)
+ "': " + e.what());
} }
last_slash = folder.rfind('/'); last_slash = folder.rfind('/');
@ -218,6 +215,12 @@ bool RascsiImage::RenameImage(const CommandContext& context, const PbCommand& co
return false; return false;
} }
const auto [id, lun] = StorageDevice::GetIdsForReservedFile(from);
if (id != -1 || lun != -1) {
return context.ReturnStatus(false, "Can't rename/move image file '" + from +
"', it is currently being used by device ID " + to_string(id) + ", LUN " + to_string(lun));
}
if (!CreateImageFolder(context, to)) { if (!CreateImageFolder(context, to)) {
return false; return false;
} }