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
|
|
|
//
|
|
|
|
// The base class for all mass storage devices with image file support
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "shared/piscsi_util.h"
|
2022-10-23 19:51:39 +00:00
|
|
|
#include "mode_page_device.h"
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <filesystem>
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class StorageDevice : public ModePageDevice
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
StorageDevice(PbDeviceType, int);
|
|
|
|
~StorageDevice() override = default;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void CleanUp() override;
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
virtual void Open() = 0;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string GetFilename() const { return filename.string(); }
|
|
|
|
void SetFilename(string_view);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
uint64_t GetBlockCount() const { return blocks; }
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void ReserveFile() const;
|
2022-10-23 19:51:39 +00:00
|
|
|
void UnreserveFile();
|
2023-10-15 06:38:15 +00:00
|
|
|
// TODO Remove this method, it is only used by the unit tests
|
2022-10-23 19:51:39 +00:00
|
|
|
static void UnreserveAll();
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
static bool FileExists(string_view);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
void SetMediumChanged(bool b) { medium_changed = b; }
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
static auto GetReservedFiles() { return reserved_files; }
|
|
|
|
static void SetReservedFiles(const unordered_map<string, id_set, piscsi_util::StringHash, equal_to<>>& r)
|
|
|
|
{ reserved_files = r; }
|
2022-11-02 14:36:19 +00:00
|
|
|
static id_set GetIdsForReservedFile(const string&);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
void ValidateFile();
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
bool IsMediumChanged() const { return medium_changed; }
|
|
|
|
|
|
|
|
void SetBlockCount(uint64_t b) { blocks = b; }
|
|
|
|
|
|
|
|
off_t GetFileSize() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool IsReadOnlyFile() const;
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
uint64_t blocks = 0;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
filesystem::path filename;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
bool medium_changed = false;
|
|
|
|
|
|
|
|
// The list of image files in use and the IDs and LUNs using these files
|
2023-10-15 06:38:15 +00:00
|
|
|
static inline unordered_map<string, id_set, piscsi_util::StringHash, equal_to<>> reserved_files;
|
2022-10-23 19:51:39 +00:00
|
|
|
};
|