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)) while (!f_eof(&fil))
{ {
uint8_t buffer[4096]; uint8_t buffer[4096];
UINT done; UINT done;
res = f_read(&fil, buffer, sizeof(buffer), &done); res = f_read(&fil, buffer, sizeof(buffer), &done);
throwError(res); throwError(res);
bw += Bytes(buffer, done); bw += Bytes(buffer, done);
} }
f_close(&fil); f_close(&fil);
@@ -167,7 +167,8 @@ private:
throw BadFilesystemException(); throw BadFilesystemException();
default: 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 std::string Path::to_str() const
{ {
return join(*this, "/"); return join(*this, "/");
} }
Filesystem::Filesystem(std::shared_ptr<SectorInterface> sectors): Filesystem::Filesystem(std::shared_ptr<SectorInterface> sectors):

View File

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

View File

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

View File

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

View File

@@ -15,7 +15,7 @@
#include <google/protobuf/text_format.h> #include <google/protobuf/text_format.h>
#include <fstream> #include <fstream>
static FlagGroup flags({ &fileFlags }); static FlagGroup flags({&fileFlags});
static StringFlag directory({"-p", "--path"}, "path to list", ""); static StringFlag directory({"-p", "--path"}, "path to list", "");
@@ -40,24 +40,31 @@ int mainLs(int argc, const char* argv[])
showProfiles("ls", formats); showProfiles("ls", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats); flags.parseFlagsWithConfigFiles(argc, argv, formats);
auto filesystem = createFilesystemFromConfig(); try
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", auto filesystem = createFilesystemFromConfig();
fileTypeChar(dirent->file_type), auto files = filesystem->list(Path(directory));
dirent->filename,
maxlen, int maxlen = 0;
dirent->length); for (const auto& dirent : files)
total += dirent->length; 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; return 0;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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