diff --git a/src/raspberrypi/devices/scsicd.cpp b/src/raspberrypi/devices/scsicd.cpp index 9d654d0c..e5fa05c9 100644 --- a/src/raspberrypi/devices/scsicd.cpp +++ b/src/raspberrypi/devices/scsicd.cpp @@ -728,7 +728,7 @@ int SCSICD::ReadToc(const DWORD *cdb, BYTE *buf) void SCSICD::GetEventStatusNotification(SASIDEV *controller) { - if (!ctrl->cmd[1] & 0x01) { + if (!(ctrl->cmd[1] & 0x01)) { // Asynchronous notification is optional and not supported by rascsi controller->Error(ERROR_CODES::sense_key::ILLEGAL_REQUEST, ERROR_CODES::asc::INVALID_FIELD_IN_CDB); return; diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index e0cdd799..047cab32 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -619,22 +619,6 @@ bool Attach(int fd, const PbDeviceDefinition& pb_device, Device *map[], bool dry filepath.SetPath(filename.c_str()); string initial_filename = filepath.GetPath(); - try { - try { - file_support->Open(filepath); - } - catch(const file_not_found_exception&) { - // If the file does not exist search for it in the default image folder - filepath.SetPath(string(rascsi_image.GetDefaultImageFolder() + "/" + filename).c_str()); - file_support->Open(filepath); - } - } - catch(const io_exception& e) { - delete device; - - return ReturnStatus(fd, false, "Tried to open an invalid or non-existing file '" + initial_filename + "': " + e.getmsg()); - } - int id; int unit; if (FileSupport::GetIdsForReservedFile(filepath, id, unit)) { @@ -644,6 +628,30 @@ bool Attach(int fd, const PbDeviceDefinition& pb_device, Device *map[], bool dry return ReturnStatus(fd, false, error); } + try { + try { + file_support->Open(filepath); + } + catch(const file_not_found_exception&) { + // If the file does not exist search for it in the default image folder + filepath.SetPath(string(rascsi_image.GetDefaultImageFolder() + "/" + filename).c_str()); + + if (FileSupport::GetIdsForReservedFile(filepath, id, unit)) { + delete device; + + error << "Image file '" << filename << "' is already used by ID " << id << ", unit " << unit; + return ReturnStatus(fd, false, error); + } + + file_support->Open(filepath); + } + } + catch(const io_exception& e) { + delete device; + + return ReturnStatus(fd, false, "Tried to open an invalid or non-existing file '" + initial_filename + "': " + e.getmsg()); + } + file_support->ReserveFile(filepath, device->GetId(), device->GetLun()); } @@ -737,17 +745,6 @@ bool Insert(int fd, const PbDeviceDefinition& pb_device, Device *device, bool dr LOGINFO("Insert %sfile '%s' requested into %s ID %d, unit %d", pb_device.protected_() ? "protected " : "", filename.c_str(), device->GetType().c_str(), pb_device.id(), pb_device.unit()); - int id; - int unit; - Filepath filepath; - filepath.SetPath(filename.c_str()); - string initial_filename = filepath.GetPath(); - 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); - } - if (pb_device.block_size()) { Disk *disk = dynamic_cast(device); if (disk && disk->IsSectorSizeConfigurable()) { @@ -762,6 +759,18 @@ bool Insert(int fd, const PbDeviceDefinition& pb_device, Device *device, bool dr } } + int id; + int unit; + Filepath filepath; + filepath.SetPath(filename.c_str()); + string initial_filename = filepath.GetPath(); + + 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); + } + FileSupport *file_support = dynamic_cast(device); try { try { @@ -770,12 +779,20 @@ bool Insert(int fd, const PbDeviceDefinition& pb_device, Device *device, bool dr catch(const file_not_found_exception&) { // If the file does not exist search for it in the default image folder filepath.SetPath((rascsi_image.GetDefaultImageFolder() + "/" + filename).c_str()); + + 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); + } + file_support->Open(filepath); } } catch(const io_exception& e) { return ReturnStatus(fd, false, "Tried to open an invalid or non-existing file '" + initial_filename + "': " + e.getmsg()); } + file_support->ReserveFile(filepath, device->GetId(), device->GetLun()); // Only non read-only devices support protect/unprotect.