Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use C++ filesystem library/iostreams, added unit tests (#911) #934

Merged
merged 8 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/raspberrypi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ SRC_SHARED = \
protobuf_serializer.cpp

SRC_RASCSI_CORE = \
bus.cpp \
filepath.cpp \
fileio.cpp
bus.cpp
SRC_RASCSI_CORE += $(shell find ./rascsi -name '*.cpp')
SRC_RASCSI_CORE += $(shell find ./controllers -name '*.cpp')
SRC_RASCSI_CORE += $(shell find ./devices -name '*.cpp')
Expand All @@ -114,8 +112,7 @@ SRC_RASCTL = rasctl.cpp
SRC_RASDUMP = \
rasdump.cpp \
bus.cpp \
filepath.cpp \
fileio.cpp \
rasdump_fileio.cpp \
rascsi_version.cpp
SRC_RASDUMP += $(shell find ./hal -name '*.cpp')

Expand Down
7 changes: 3 additions & 4 deletions src/raspberrypi/devices/cd_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void CDTrack::Init(int track, uint32_t first, uint32_t last)
last_lba = last;
}

void CDTrack::SetPath(bool cdda, const Filepath& path)
void CDTrack::SetPath(bool cdda, string_view path)
{
assert(valid);

Expand All @@ -41,12 +41,11 @@ void CDTrack::SetPath(bool cdda, const Filepath& path)
imgpath = path;
}

void CDTrack::GetPath(Filepath& path) const
string CDTrack::GetPath() const
{
assert(valid);

// Return the path (by reference)
path = imgpath;
return imgpath;
}

//---------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions src/raspberrypi/devices/cd_track.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#pragma once

#include "filepath.h"
#include <string>

using namespace std;

class CDTrack final
{
Expand All @@ -26,8 +28,8 @@ class CDTrack final
void Init(int track, uint32_t first, uint32_t last);

// Properties
void SetPath(bool cdda, const Filepath& path); // Set the path
void GetPath(Filepath& path) const; // Get the path
void SetPath(bool, string_view); // Set the path
string GetPath() const; // Get the path
uint32_t GetFirst() const; // Get the start LBA
uint32_t GetLast() const; // Get the last LBA
uint32_t GetBlocks() const; // Get the number of blocks
Expand All @@ -41,5 +43,6 @@ class CDTrack final
uint32_t first_lba = 0; // First LBA
uint32_t last_lba = 0; // Last LBA
bool audio = false; // Audio track flag
Filepath imgpath; // Image file path

string imgpath; // Image file path
};
1 change: 0 additions & 1 deletion src/raspberrypi/devices/cfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "os.h"
#include "log.h"
#include "filepath.h"
#include "cfilesystem.h"
#include <dirent.h>
#include <iconv.h>
Expand Down
4 changes: 4 additions & 0 deletions src/raspberrypi/devices/cfilesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

#pragma once

using TCHAR = char;

static const int FILEPATH_MAX = 260;

//---------------------------------------------------------------------------
//
// Status code definitions
Expand Down
6 changes: 3 additions & 3 deletions src/raspberrypi/devices/ctapdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,20 @@ bool CTapDriver::Init(const unordered_map<string, string>& const_params)
#endif
}

void CTapDriver::OpenDump(const Filepath& path) {
void CTapDriver::OpenDump(const string& path) {
if (m_pcap == nullptr) {
m_pcap = pcap_open_dead(DLT_EN10MB, 65535);
}
if (m_pcap_dumper != nullptr) {
pcap_dump_close(m_pcap_dumper);
}
m_pcap_dumper = pcap_dump_open(m_pcap, path.GetPath());
m_pcap_dumper = pcap_dump_open(m_pcap, path.c_str());
if (m_pcap_dumper == nullptr) {
LOGERROR("Can't open pcap file: %s", pcap_geterr(m_pcap))
throw io_exception("Can't open pcap file");
}

LOGTRACE("%s Opened %s for dumping", __PRETTY_FUNCTION__, path.GetPath())
LOGTRACE("%s Opened %s for dumping", __PRETTY_FUNCTION__, path.c_str())
}

bool CTapDriver::Enable() const
Expand Down
3 changes: 1 addition & 2 deletions src/raspberrypi/devices/ctapdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <pcap/pcap.h>
#include <net/ethernet.h>
#include "filepath.h"
#include <unordered_map>
#include <list>
#include <string>
Expand All @@ -33,7 +32,7 @@ class CTapDriver
CTapDriver& operator=(const CTapDriver&) = default;

bool Init(const unordered_map<string, string>&);
void OpenDump(const Filepath& path); // Capture packets
void OpenDump(const string& path); // Capture packets
void GetMacAddr(BYTE *mac) const;
int Receive(BYTE *buf);
int Send(const BYTE *buf, int len);
Expand Down
22 changes: 6 additions & 16 deletions src/raspberrypi/devices/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
//
//---------------------------------------------------------------------------

#include "fileio.h"
#include "rascsi_exceptions.h"
#include "dispatcher.h"
#include "scsi_command_util.h"
Expand Down Expand Up @@ -79,17 +78,13 @@ bool Disk::Dispatch(scsi_command cmd)

void Disk::SetUpCache(off_t image_offset, bool raw)
{
Filepath path;
path.SetPath(GetFilename().c_str());
cache = make_unique<DiskCache>(path, size_shift_count, (uint32_t)GetBlockCount(), image_offset);
cache = make_unique<DiskCache>(GetFilename(), size_shift_count, (uint32_t)GetBlockCount(), image_offset);
cache->SetRawMode(raw);
}

void Disk::ResizeCache(const string& filename, bool raw)
void Disk::ResizeCache(const string& path, bool raw)
{
Filepath path;
path.SetPath(filename.c_str());
cache.reset(new DiskCache(path, GetSectorSizeShiftCount(), (uint32_t)GetBlockCount()));
cache.reset(new DiskCache(path, size_shift_count, (uint32_t)GetBlockCount()));
cache->SetRawMode(raw);
}

Expand Down Expand Up @@ -501,8 +496,7 @@ int Disk::Read(const vector<int>&, vector<BYTE>& buf, uint64_t block)
throw scsi_exception(sense_key::MEDIUM_ERROR, asc::READ_FAULT);
}

// Success
return 1 << size_shift_count;
return GetSectorSizeInBytes();
}

int Disk::WriteCheck(uint64_t block)
Expand All @@ -519,18 +513,14 @@ int Disk::WriteCheck(uint64_t block)
throw scsi_exception(sense_key::DATA_PROTECT, asc::WRITE_PROTECTED);
}

// Success
return 1 << size_shift_count;
return GetSectorSizeInBytes();
}

void Disk::Write(const vector<int>&, const vector<BYTE>& buf, uint64_t block)
{
LOGTRACE("%s", __PRETTY_FUNCTION__)

// Error if not ready
if (!IsReady()) {
throw scsi_exception(sense_key::NOT_READY);
}
CheckReady();

// Error if the total number of blocks is exceeded
if (block >= GetBlockCount()) {
Expand Down
7 changes: 3 additions & 4 deletions src/raspberrypi/devices/disk_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
//
//---------------------------------------------------------------------------

#include "os.h"
#include "disk_track.h"
#include "disk_cache.h"
#include <cstdlib>
#include <cassert>

DiskCache::DiskCache(const Filepath& path, int size, uint32_t blocks, off_t imgoff)
: sec_size(size), sec_blocks(blocks), imgoffset(imgoff)
DiskCache::DiskCache(const string& path, int size, uint32_t blocks, off_t imgoff)
: sec_path(path), sec_size(size), sec_blocks(blocks), imgoffset(imgoff)
{
assert(blocks > 0);
assert(imgoff >= 0);

sec_path = path;
}

bool DiskCache::Save() const
Expand Down
6 changes: 3 additions & 3 deletions src/raspberrypi/devices/disk_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

#pragma once

#include "filepath.h"
#include <array>
#include <memory>
#include <string>

using namespace std;

Expand All @@ -34,7 +34,7 @@ class DiskCache
uint32_t serial; // Serial
};

DiskCache(const Filepath& path, int size, uint32_t blocks, off_t imgoff = 0);
DiskCache(const string&, int, uint32_t, off_t = 0);
~DiskCache() = default;

void SetRawMode(bool b) { cd_raw = b; } // CD-ROM raw mode setting
Expand All @@ -55,7 +55,7 @@ class DiskCache
// Internal data
array<cache_t, CACHE_MAX> cache = {}; // Cache management
uint32_t serial = 0; // Last serial number
Filepath sec_path; // Path
string sec_path; // Path
int sec_size; // Sector Size (8=256, 9=512, 10=1024, 11=2048, 12=4096)
int sec_blocks; // Blocks per sector
bool cd_raw = false; // CD-ROM RAW mode
Expand Down
47 changes: 19 additions & 28 deletions src/raspberrypi/devices/disk_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
//---------------------------------------------------------------------------

#include "log.h"
#include "fileio.h"
#include "disk_track.h"
#include <fstream>

DiskTrack::~DiskTrack()
{
Expand Down Expand Up @@ -46,7 +46,7 @@ void DiskTrack::Init(int track, int size, int sectors, bool raw, off_t imgoff)
dt.imgoffset = imgoff;
}

bool DiskTrack::Load(const Filepath& path)
bool DiskTrack::Load(const string& path)
{
// Not needed if already loaded
if (dt.init) {
Expand Down Expand Up @@ -97,24 +97,21 @@ bool DiskTrack::Load(const Filepath& path)
dt.changemap.resize(dt.sectors);
fill(dt.changemap.begin(), dt.changemap.end(), false);

// Read from File
Fileio fio;
if (!fio.OpenDIO(path, Fileio::OpenMode::ReadOnly)) {
ifstream in(path, ios::binary);
if (in.fail()) {
return false;
}

if (dt.raw) {
// Split Reading
for (int i = 0; i < dt.sectors; i++) {
// Seek
if (!fio.Seek(offset)) {
fio.Close();
in.seekg(offset);
if (in.fail()) {
return false;
}

// Read
if (!fio.Read(&dt.buffer[i << dt.size], 1 << dt.size)) {
fio.Close();
in.read((char *)&dt.buffer[i << dt.size], 1 << dt.size);
if (in.fail()) {
return false;
}

Expand All @@ -123,24 +120,23 @@ bool DiskTrack::Load(const Filepath& path)
}
} else {
// Continuous reading
if (!fio.Seek(offset)) {
fio.Close();
in.seekg(offset);
if (in.fail()) {
return false;
}
if (!fio.Read(dt.buffer, length)) {
fio.Close();
in.read((char *)dt.buffer, length);
if (in.fail()) {
return false;
}
}
fio.Close();

// Set a flag and end normally
dt.init = true;
dt.changed = false;
return true;
}

bool DiskTrack::Save(const Filepath& path)
bool DiskTrack::Save(const string& path)
{
// Not needed if not initialized
if (!dt.init) {
Expand Down Expand Up @@ -169,9 +165,8 @@ bool DiskTrack::Save(const Filepath& path)
// Calculate length per sector
const int length = 1 << dt.size;

// Open file
Fileio fio;
if (!fio.Open(path, Fileio::OpenMode::ReadWrite)) {
ofstream out(path, ios::in | ios::out | ios::binary);
if (out.fail()) {
return false;
}

Expand All @@ -183,9 +178,8 @@ bool DiskTrack::Save(const Filepath& path)
// Initialize write size
total = 0;

// Seek
if (!fio.Seek(offset + ((off_t)i << dt.size))) {
fio.Close();
out.seekp(offset + ((off_t)i << dt.size));
if (out.fail()) {
return false;
}

Expand All @@ -201,9 +195,8 @@ bool DiskTrack::Save(const Filepath& path)
total += length;
}

// Write
if (!fio.Write(&dt.buffer[i << dt.size], total)) {
fio.Close();
out.write((const char *)&dt.buffer[i << dt.size], total);
if (out.fail()) {
return false;
}

Expand All @@ -215,8 +208,6 @@ bool DiskTrack::Save(const Filepath& path)
}
}

fio.Close();

// Drop the change flag and exit
fill(dt.changemap.begin(), dt.changemap.end(), false);
dt.changed = false;
Expand Down
Loading