Prevent image files in use from being deleted

This commit is contained in:
Uwe Seimet 2021-09-15 15:17:26 +02:00
parent f6f572ee9e
commit da110c4e8e
3 changed files with 16 additions and 5 deletions

View File

@ -25,7 +25,7 @@ void FileSupport::UnreserveFile()
reserved_files.erase(diskpath.GetPath());
}
bool FileSupport::GetIdsForReservedFile(const Filepath& path, int& id, int& unit) const
bool FileSupport::GetIdsForReservedFile(const Filepath& path, int& id, int& unit)
{
if (reserved_files.find(path.GetPath()) != reserved_files.end()) {
const id_set ids = reserved_files[path.GetPath()];

View File

@ -38,8 +38,8 @@ public:
static void SetReservedFiles(const map<string, id_set>& files_in_use) { FileSupport::reserved_files = files_in_use; }
void ReserveFile(const Filepath&, int, int);
void UnreserveFile();
bool GetIdsForReservedFile(const Filepath&, int&, int&) const;
static bool GetIdsForReservedFile(const Filepath&, int&, int&);
static void UnreserveAll();
virtual void Open(const Filepath&) = 0;

View File

@ -785,6 +785,16 @@ bool DeleteImage(int fd, const PbCommand& command)
filename = default_image_folder + "/" + filename;
int id;
int unit;
Filepath filepath;
filepath.SetPath(filename.c_str());
if (FileSupport::GetIdsForReservedFile(filepath, id, unit)) {
ostringstream msg;
msg << "Can't delete image file '" << filename << "', it is in use by device ID " << id << ", unit " << unit;
return ReturnStatus(fd, false, msg.str());
}
if (unlink(filename.c_str())) {
return ReturnStatus(fd, false, "Can't delete image file '" + filename + "': " + string(strerror(errno)));
}
@ -945,7 +955,7 @@ bool Attach(int fd, const PbDeviceDefinition& pb_device, Device *map[], bool dry
int id;
int unit;
if (file_support->GetIdsForReservedFile(filepath, id, unit)) {
if (FileSupport::GetIdsForReservedFile(filepath, id, unit)) {
delete device;
error << "Image file '" << filename << "' is already used by ID " << id << ", unit " << unit;
@ -1049,8 +1059,7 @@ bool Insert(int fd, const PbDeviceDefinition& pb_device, Device *device, bool dr
Filepath filepath;
filepath.SetPath(filename.c_str());
string initial_filename = filepath.GetPath();
FileSupport *file_support = dynamic_cast<FileSupport *>(device);
if (file_support->GetIdsForReservedFile(filepath, id, unit)) {
if (FileSupport::GetIdsForReservedFile(filepath, id, unit)) {
ostringstream error;
error << "Image file '" << filename << "' is already used by ID " << id << ", unit " << unit;
return ReturnStatus(fd, false, error);
@ -1070,6 +1079,8 @@ bool Insert(int fd, const PbDeviceDefinition& pb_device, Device *device, bool dr
}
}
FileSupport *file_support = dynamic_cast<FileSupport *>(device);
try {
try {
file_support->Open(filepath);