mirror of
https://github.com/akuker/RASCSI.git
synced 2024-12-11 18:49:24 +00:00
41bdcd4aed
* Update logging * Remove duplicate code * Update unit tests * Clean up includes * Merge ProtobufSerializer into protobuf_util namespace * Precompile regex * Add const * Add Split() convenience method, update log level/ID parsing * Move log.h to legacy folder * Elimininate gotos * Fixes for gcc 13 * Update compiler flags * Update default folder handling * Use references instead of pointers * Move code for better encapsulation * Move code * Remove unused method argument * Move device logger * Remove redundant to_string * Rename for consistency * Update handling of protobuf pointers * Simplify protobuf usage * Memory handling update * Add hasher
120 lines
2.4 KiB
C++
120 lines
2.4 KiB
C++
//---------------------------------------------------------------------------
|
|
//
|
|
// SCSI Target Emulator PiSCSI
|
|
// for Raspberry Pi
|
|
//
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
|
//
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "shared/piscsi_exceptions.h"
|
|
#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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
void StorageDevice::ValidateFile()
|
|
{
|
|
if (blocks == 0) {
|
|
throw io_exception(string(GetTypeString()) + " device has 0 blocks");
|
|
}
|
|
|
|
if (!exists(filename)) {
|
|
throw file_not_found_exception("Image file '" + filename.string() + "' for " + GetTypeString() + " device does not exist");
|
|
}
|
|
|
|
if (GetFileSize() > 2LL * 1024 * 1024 * 1024 * 1024) {
|
|
throw io_exception("Image files > 2 TiB are not supported");
|
|
}
|
|
|
|
// TODO Check for duplicate handling of these properties (-> piscsi_executor.cpp)
|
|
if (IsReadOnlyFile()) {
|
|
// Permanently write-protected
|
|
SetReadOnly(true);
|
|
SetProtectable(false);
|
|
SetProtected(false);
|
|
}
|
|
|
|
SetStopped(false);
|
|
SetRemoved(false);
|
|
SetLocked(false);
|
|
SetReady(true);
|
|
}
|
|
|
|
void StorageDevice::ReserveFile() const
|
|
{
|
|
assert(!filename.empty());
|
|
assert(!reserved_files.contains(filename.string()));
|
|
|
|
reserved_files[filename.string()] = { GetId(), GetLun() };
|
|
}
|
|
|
|
void StorageDevice::UnreserveFile()
|
|
{
|
|
reserved_files.erase(filename.string());
|
|
|
|
filename.clear();
|
|
}
|
|
|
|
id_set StorageDevice::GetIdsForReservedFile(const string& file)
|
|
{
|
|
if (const auto& it = reserved_files.find(file); it != reserved_files.end()) {
|
|
return { it->second.first, it->second.second };
|
|
}
|
|
|
|
return { -1, -1 };
|
|
}
|
|
|
|
void StorageDevice::UnreserveAll()
|
|
{
|
|
reserved_files.clear();
|
|
}
|
|
|
|
bool StorageDevice::FileExists(string_view file)
|
|
{
|
|
return exists(path(file));
|
|
}
|
|
|
|
bool StorageDevice::IsReadOnlyFile() const
|
|
{
|
|
return access(filename.c_str(), W_OK);
|
|
}
|
|
|
|
off_t StorageDevice::GetFileSize() const
|
|
{
|
|
try {
|
|
return file_size(filename);
|
|
}
|
|
catch (const filesystem_error& e) {
|
|
throw io_exception("Can't get size of '" + filename.string() + "': " + e.what());
|
|
}
|
|
}
|