Enable FatFS on a bunch of formats; better error reporting (i.e., some).

This commit is contained in:
David Given
2022-08-27 18:07:04 +02:00
parent 983f6caf46
commit 917d5d2dd2
20 changed files with 214 additions and 72 deletions

View File

@@ -103,12 +103,12 @@ public:
while (!f_eof(&fil))
{
uint8_t buffer[4096];
UINT done;
res = f_read(&fil, buffer, sizeof(buffer), &done);
throwError(res);
uint8_t buffer[4096];
UINT done;
res = f_read(&fil, buffer, sizeof(buffer), &done);
throwError(res);
bw += Bytes(buffer, done);
bw += Bytes(buffer, done);
}
f_close(&fil);
@@ -167,7 +167,8 @@ private:
throw BadFilesystemException();
default:
throw FilesystemException();
throw FilesystemException(
fmt::format("unknown fatfs error {}", res));
}
}

View File

@@ -30,7 +30,7 @@ Path::Path(const std::string& path)
std::string Path::to_str() const
{
return join(*this, "/");
return join(*this, "/");
}
Filesystem::Filesystem(std::shared_ptr<SectorInterface> sectors):

View File

@@ -21,7 +21,7 @@ struct Dirent
std::string filename;
FileType file_type;
uint64_t length;
std::string mode;
std::string mode;
};
enum FilesystemStatus
@@ -33,60 +33,112 @@ enum FilesystemStatus
FS_BAD
};
class FilesystemException : public ErrorException {};
class BadPathException : public FilesystemException {};
class FileNotFoundException : public FilesystemException {};
class BadFilesystemException : public FilesystemException {};
class ReadOnlyFilesystemException : public FilesystemException {};
class UnimplementedFilesystemException : public FilesystemException {};
class FilesystemException
{
public:
FilesystemException(const std::string& message): message(message) {}
public:
std::string message;
};
class BadPathException : public FilesystemException
{
public:
BadPathException(): FilesystemException("Bad path") {}
};
class FileNotFoundException : public FilesystemException
{
public:
FileNotFoundException(): FilesystemException("File not found") {}
};
class BadFilesystemException : public FilesystemException
{
public:
BadFilesystemException(): FilesystemException("Invalid filesystem") {}
};
class ReadOnlyFilesystemException : public FilesystemException
{
public:
ReadOnlyFilesystemException(): FilesystemException("Read only filesystem")
{
}
};
class UnimplementedFilesystemException : public FilesystemException
{
public:
UnimplementedFilesystemException():
FilesystemException("Unimplemented operation")
{
}
};
class Path : public std::vector<std::string>
{
public:
Path() {}
Path(const std::string& text);
Path() {}
Path(const std::string& text);
public:
std::string to_str() const;
std::string to_str() const;
};
class Filesystem
{
public:
virtual void create()
{ throw UnimplementedFilesystemException(); }
virtual void create()
{
throw UnimplementedFilesystemException();
}
virtual FilesystemStatus check()
{ throw UnimplementedFilesystemException(); }
{
throw UnimplementedFilesystemException();
}
virtual std::vector<std::unique_ptr<Dirent>> list(const Path& path)
{ throw UnimplementedFilesystemException(); }
{
throw UnimplementedFilesystemException();
}
virtual Bytes getFile(const Path& path)
{ throw UnimplementedFilesystemException(); }
{
throw UnimplementedFilesystemException();
}
virtual void putFile(const Path& path, const Bytes& data)
{ throw UnimplementedFilesystemException(); }
{
throw UnimplementedFilesystemException();
}
virtual std::map<std::string, std::string> getMetadata(const Path& path)
{ throw UnimplementedFilesystemException(); }
virtual std::map<std::string, std::string> getMetadata(const Path& path)
{
throw UnimplementedFilesystemException();
}
virtual void putMetadata(const Path& path, const std::map<std::string, std::string>& metadata)
{ throw UnimplementedFilesystemException(); }
virtual void putMetadata(
const Path& path, const std::map<std::string, std::string>& metadata)
{
throw UnimplementedFilesystemException();
}
protected:
Filesystem(std::shared_ptr<SectorInterface> sectors);
Filesystem(std::shared_ptr<SectorInterface> sectors);
Bytes getLogicalSector(uint32_t number, uint32_t count = 1);
void putLogicalSector(uint32_t number, const Bytes& data);
Bytes getLogicalSector(uint32_t number, uint32_t count = 1);
void putLogicalSector(uint32_t number, const Bytes& data);
unsigned getLogicalSectorCount();
unsigned getLogicalSectorSize();
unsigned getLogicalSectorCount();
unsigned getLogicalSectorSize();
private:
typedef std::tuple<unsigned, unsigned, unsigned> location_t;
std::vector<location_t> _locations;
std::shared_ptr<SectorInterface> _sectors;
typedef std::tuple<unsigned, unsigned, unsigned> location_t;
std::vector<location_t> _locations;
std::shared_ptr<SectorInterface> _sectors;
public:
static std::unique_ptr<Filesystem> createBrother120Filesystem(
@@ -96,8 +148,8 @@ public:
static std::unique_ptr<Filesystem> createFatFsFilesystem(
const FilesystemProto& config, std::shared_ptr<SectorInterface> image);
static std::unique_ptr<Filesystem> createFilesystem(
const FilesystemProto& config, std::shared_ptr<SectorInterface> image);
static std::unique_ptr<Filesystem> createFilesystem(
const FilesystemProto& config, std::shared_ptr<SectorInterface> image);
};
#endif

View File

@@ -16,7 +16,7 @@
#include <google/protobuf/text_format.h>
#include <fstream>
static FlagGroup flags({ &fileFlags });
static FlagGroup flags({&fileFlags});
static StringFlag directory({"-p", "--path"}, "path to work on", "");
static StringFlag output({"-o", "--output"}, "local filename to write to", "");
@@ -27,18 +27,25 @@ int mainGetFile(int argc, const char* argv[])
showProfiles("getfile", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
Path inputFilename(directory);
if (inputFilename.size() == 0)
Error() << "you must supply a filename to read";
try
{
Path inputFilename(directory);
if (inputFilename.size() == 0)
Error() << "you must supply a filename to read";
std::string outputFilename = output;
if (outputFilename.empty())
outputFilename = inputFilename.back();
fmt::print("{}\n", outputFilename);
auto filesystem = createFilesystemFromConfig();
auto data = filesystem->getFile(inputFilename);
data.writeToFile(outputFilename);
std::string outputFilename = output;
if (outputFilename.empty())
outputFilename = inputFilename.back();
fmt::print("{}\n", outputFilename);
auto filesystem = createFilesystemFromConfig();
auto data = filesystem->getFile(inputFilename);
data.writeToFile(outputFilename);
}
catch (const FilesystemException& e)
{
Error() << e.message;
}
return 0;
}

View File

@@ -16,7 +16,7 @@
#include <google/protobuf/text_format.h>
#include <fstream>
static FlagGroup flags({ &fileFlags });
static FlagGroup flags({&fileFlags});
static StringFlag directory({"-p", "--path"}, "path to work on", "");
@@ -26,11 +26,18 @@ int mainGetFileInfo(int argc, const char* argv[])
showProfiles("getfileinfo", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
auto filesystem = createFilesystemFromConfig();
auto attributes = filesystem->getMetadata(Path(directory));
try
{
auto filesystem = createFilesystemFromConfig();
auto attributes = filesystem->getMetadata(Path(directory));
for (const auto& e : attributes)
fmt::print("{} = {}\n", e.first, e.second);
for (const auto& e : attributes)
fmt::print("{} = {}\n", e.first, e.second);
}
catch (const FilesystemException& e)
{
Error() << e.message;
}
return 0;
}

View File

@@ -15,7 +15,7 @@
#include <google/protobuf/text_format.h>
#include <fstream>
static FlagGroup flags({ &fileFlags });
static FlagGroup flags({&fileFlags});
static StringFlag directory({"-p", "--path"}, "path to list", "");
@@ -40,24 +40,31 @@ int mainLs(int argc, const char* argv[])
showProfiles("ls", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
auto filesystem = createFilesystemFromConfig();
auto files = filesystem->list(Path(directory));
int maxlen = 0;
for (const auto& dirent : files)
maxlen = std::max(maxlen, (int)dirent->filename.size());
uint32_t total = 0;
for (const auto& dirent : files)
try
{
fmt::print("{} {:{}} {:6}\n",
fileTypeChar(dirent->file_type),
dirent->filename,
maxlen,
dirent->length);
total += dirent->length;
auto filesystem = createFilesystemFromConfig();
auto files = filesystem->list(Path(directory));
int maxlen = 0;
for (const auto& dirent : files)
maxlen = std::max(maxlen, (int)dirent->filename.size());
uint32_t total = 0;
for (const auto& dirent : files)
{
fmt::print("{} {:{}} {:6}\n",
fileTypeChar(dirent->file_type),
dirent->filename,
maxlen,
dirent->length);
total += dirent->length;
}
fmt::print("({} files, {} bytes)\n", files.size(), total);
}
catch (const FilesystemException& e)
{
Error() << e.message;
}
fmt::print("({} files, {} bytes)\n", files.size(), total);
return 0;
}

View File

@@ -69,3 +69,7 @@ heads {
end: 0
}
filesystem {
fatfs {}
}

View File

@@ -68,3 +68,8 @@ heads {
end: 0
}
filesystem {
fatfs {}
}

View File

@@ -69,3 +69,8 @@ heads {
end: 0
}
filesystem {
fatfs {}
}

View File

@@ -69,3 +69,8 @@ heads {
end: 0
}
filesystem {
fatfs {}
}

View File

@@ -68,3 +68,8 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -68,3 +68,8 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -69,3 +69,8 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -69,3 +69,8 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -73,3 +73,8 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -67,3 +67,8 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -72,3 +72,7 @@ heads {
end: 1
}
filesystem {
fatfs {}
}

View File

@@ -68,3 +68,9 @@ heads {
}
tpi: 48
filesystem {
fatfs {}
}

View File

@@ -65,3 +65,8 @@ heads {
tpi: 48
filesystem {
fatfs {}
}

View File

@@ -69,3 +69,7 @@ heads {
end: 1
}
filesystem {
fatfs {}
}