2022-10-23 19:51:39 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-23 19:51:39 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-10-23 19:51:39 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-10-23 19:51:39 +00:00
|
|
|
#include "storage_device.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace filesystem;
|
|
|
|
|
|
|
|
StorageDevice::StorageDevice(PbDeviceType type, int lun) : ModePageDevice(type, lun)
|
|
|
|
{
|
|
|
|
SupportsFile(true);
|
|
|
|
SetStoppable(true);
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void StorageDevice::CleanUp()
|
|
|
|
{
|
|
|
|
UnreserveFile();
|
|
|
|
|
|
|
|
ModePageDevice::CleanUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageDevice::SetFilename(string_view f)
|
|
|
|
{
|
|
|
|
filename = filesystem::path(f);
|
|
|
|
|
|
|
|
// Permanently write-protected
|
|
|
|
SetReadOnly(IsReadOnlyFile());
|
|
|
|
|
|
|
|
SetProtectable(!IsReadOnlyFile());
|
|
|
|
|
|
|
|
if (IsReadOnlyFile()) {
|
|
|
|
SetProtected(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
void StorageDevice::ValidateFile()
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
|
|
|
if (blocks == 0) {
|
|
|
|
throw io_exception(string(GetTypeString()) + " device has 0 blocks");
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
if (!exists(filename)) {
|
|
|
|
throw file_not_found_exception("Image file '" + filename.string() + "' for " + GetTypeString() + " device does not exist");
|
2022-10-25 08:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GetFileSize() > 2LL * 1024 * 1024 * 1024 * 1024) {
|
2023-10-15 06:38:15 +00:00
|
|
|
throw io_exception("Image files > 2 TiB are not supported");
|
2022-10-25 08:29:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
// TODO Check for duplicate handling of these properties (-> piscsi_executor.cpp)
|
2023-10-15 06:38:15 +00:00
|
|
|
if (IsReadOnlyFile()) {
|
2022-10-23 19:51:39 +00:00
|
|
|
// Permanently write-protected
|
|
|
|
SetReadOnly(true);
|
|
|
|
SetProtectable(false);
|
|
|
|
SetProtected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStopped(false);
|
|
|
|
SetRemoved(false);
|
|
|
|
SetLocked(false);
|
|
|
|
SetReady(true);
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void StorageDevice::ReserveFile() const
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
assert(!filename.empty());
|
|
|
|
assert(!reserved_files.contains(filename.string()));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
reserved_files[filename.string()] = { GetId(), GetLun() };
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageDevice::UnreserveFile()
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
reserved_files.erase(filename.string());
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
filename.clear();
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
id_set StorageDevice::GetIdsForReservedFile(const string& file)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
|
|
|
if (const auto& it = reserved_files.find(file); it != reserved_files.end()) {
|
2023-10-15 06:38:15 +00:00
|
|
|
return { it->second.first, it->second.second };
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
return { -1, -1 };
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageDevice::UnreserveAll()
|
|
|
|
{
|
|
|
|
reserved_files.clear();
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool StorageDevice::FileExists(string_view file)
|
2022-10-23 19:51:39 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
return exists(path(file));
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StorageDevice::IsReadOnlyFile() const
|
|
|
|
{
|
|
|
|
return access(filename.c_str(), W_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t StorageDevice::GetFileSize() const
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
try {
|
|
|
|
return file_size(filename);
|
|
|
|
}
|
|
|
|
catch (const filesystem_error& e) {
|
|
|
|
throw io_exception("Can't get size of '" + filename.string() + "': " + e.what());
|
2022-10-23 19:51:39 +00:00
|
|
|
}
|
|
|
|
}
|