From 91fd46b65e0b04de4f072222d914037da49c0f2a Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sun, 29 Aug 2021 14:41:37 +0200 Subject: [PATCH] Save list of files in use before dry-run --- src/raspberrypi/rascsi.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index 85fe1e5e..f08eccd7 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -66,7 +66,8 @@ static void *MonThread(void *param); vector log_levels; string current_log_level; // Some versions of spdlog do not support get_log_level() string default_image_folder; -map> files_in_use; +typedef pair id_set; +map files_in_use; DeviceFactory& device_factory = DeviceFactory::instance(); //--------------------------------------------------------------------------- @@ -902,6 +903,13 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation op LOGINFO("Insert file '%s' requested into %s ID %d, unit %d", filename.c_str(), device->GetType().c_str(), id, unit); + const string path = filepath.GetPath(); + if (files_in_use.find(path) != files_in_use.end()) { + const auto& full_id = files_in_use[path]; + error << "Image file '" << filename << "' is already used by ID " << full_id.first << ", unit " << full_id.second; + return ReturnStatus(fd, false, error); + } + try { try { fileSupport->Open(filepath); @@ -916,13 +924,6 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation op return ReturnStatus(fd, false, "Tried to open an invalid file '" + string(filepath.GetPath()) + "': " + e.getmsg()); } - const string path = filepath.GetPath(); - if (files_in_use.find(path) != files_in_use.end()) { - const auto& full_id = files_in_use[path]; - error << "Image file '" << filename << "' is already used by ID " << full_id.first << ", unit " << full_id.second; - return ReturnStatus(fd, false, error); - } - files_in_use[path] = make_pair(device->GetId(), device->GetLun()); } break; @@ -982,15 +983,15 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation op bool ProcessCmd(const int fd, const PbCommand& command) { // Dry run first + const auto files = files_in_use; for (int i = 0; i < command.devices().devices_size(); i++) { if (!ProcessCmd(fd, command.devices().devices(i), command.operation(), command.params(), true)) { return false; } } - files_in_use.clear(); - // Execute + files_in_use = files; for (int i = 0; i < command.devices().devices_size(); i++) { if (!ProcessCmd(fd, command.devices().devices(i), command.operation(), command.params(), false)) { return false;