diff --git a/lib/vfs/fatfs.cc b/lib/vfs/fatfs.cc index 9b521f4a..2a2c57dd 100644 --- a/lib/vfs/fatfs.cc +++ b/lib/vfs/fatfs.cc @@ -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)); } } diff --git a/lib/vfs/vfs.cc b/lib/vfs/vfs.cc index b93e2760..1b102715 100644 --- a/lib/vfs/vfs.cc +++ b/lib/vfs/vfs.cc @@ -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 sectors): diff --git a/lib/vfs/vfs.h b/lib/vfs/vfs.h index 38357c6a..96be1427 100644 --- a/lib/vfs/vfs.h +++ b/lib/vfs/vfs.h @@ -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 { 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> 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 getMetadata(const Path& path) - { throw UnimplementedFilesystemException(); } + virtual std::map getMetadata(const Path& path) + { + throw UnimplementedFilesystemException(); + } - virtual void putMetadata(const Path& path, const std::map& metadata) - { throw UnimplementedFilesystemException(); } + virtual void putMetadata( + const Path& path, const std::map& metadata) + { + throw UnimplementedFilesystemException(); + } protected: - Filesystem(std::shared_ptr sectors); + Filesystem(std::shared_ptr 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 location_t; - std::vector _locations; - std::shared_ptr _sectors; + typedef std::tuple location_t; + std::vector _locations; + std::shared_ptr _sectors; public: static std::unique_ptr createBrother120Filesystem( @@ -96,8 +148,8 @@ public: static std::unique_ptr createFatFsFilesystem( const FilesystemProto& config, std::shared_ptr image); - static std::unique_ptr createFilesystem( - const FilesystemProto& config, std::shared_ptr image); + static std::unique_ptr createFilesystem( + const FilesystemProto& config, std::shared_ptr image); }; #endif diff --git a/src/fe-getfile.cc b/src/fe-getfile.cc index 334a3acc..55f1d015 100644 --- a/src/fe-getfile.cc +++ b/src/fe-getfile.cc @@ -16,7 +16,7 @@ #include #include -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; } diff --git a/src/fe-getfileinfo.cc b/src/fe-getfileinfo.cc index 550e9923..4d3c2bfc 100644 --- a/src/fe-getfileinfo.cc +++ b/src/fe-getfileinfo.cc @@ -16,7 +16,7 @@ #include #include -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; } diff --git a/src/fe-ls.cc b/src/fe-ls.cc index e293cc63..8fcfc412 100644 --- a/src/fe-ls.cc +++ b/src/fe-ls.cc @@ -15,7 +15,7 @@ #include #include -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; } diff --git a/src/formats/atarist360.textpb b/src/formats/atarist360.textpb index 28642a28..56007a10 100644 --- a/src/formats/atarist360.textpb +++ b/src/formats/atarist360.textpb @@ -69,3 +69,7 @@ heads { end: 0 } +filesystem { + fatfs {} +} + diff --git a/src/formats/atarist370.textpb b/src/formats/atarist370.textpb index f2b713ec..97018257 100644 --- a/src/formats/atarist370.textpb +++ b/src/formats/atarist370.textpb @@ -68,3 +68,8 @@ heads { end: 0 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/atarist400.textpb b/src/formats/atarist400.textpb index 7a331c04..85afcc6f 100644 --- a/src/formats/atarist400.textpb +++ b/src/formats/atarist400.textpb @@ -69,3 +69,8 @@ heads { end: 0 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/atarist410.textpb b/src/formats/atarist410.textpb index 123f0c30..d7df096a 100644 --- a/src/formats/atarist410.textpb +++ b/src/formats/atarist410.textpb @@ -69,3 +69,8 @@ heads { end: 0 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/atarist720.textpb b/src/formats/atarist720.textpb index b69a4717..a03b74e1 100644 --- a/src/formats/atarist720.textpb +++ b/src/formats/atarist720.textpb @@ -68,3 +68,8 @@ heads { end: 1 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/atarist740.textpb b/src/formats/atarist740.textpb index 82060bdb..d70bc540 100644 --- a/src/formats/atarist740.textpb +++ b/src/formats/atarist740.textpb @@ -68,3 +68,8 @@ heads { end: 1 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/atarist800.textpb b/src/formats/atarist800.textpb index 81f4c3ba..7707d126 100644 --- a/src/formats/atarist800.textpb +++ b/src/formats/atarist800.textpb @@ -69,3 +69,8 @@ heads { end: 1 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/atarist820.textpb b/src/formats/atarist820.textpb index d2ee0d47..3239048a 100644 --- a/src/formats/atarist820.textpb +++ b/src/formats/atarist820.textpb @@ -69,3 +69,8 @@ heads { end: 1 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/ibm1200.textpb b/src/formats/ibm1200.textpb index 573bd7b3..83d6b333 100644 --- a/src/formats/ibm1200.textpb +++ b/src/formats/ibm1200.textpb @@ -73,3 +73,8 @@ heads { end: 1 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/ibm1232.textpb b/src/formats/ibm1232.textpb index 345e6e1d..6426a99e 100644 --- a/src/formats/ibm1232.textpb +++ b/src/formats/ibm1232.textpb @@ -67,3 +67,8 @@ heads { end: 1 } +filesystem { + fatfs {} +} + + diff --git a/src/formats/ibm1440.textpb b/src/formats/ibm1440.textpb index 182bbef9..1379f2e2 100644 --- a/src/formats/ibm1440.textpb +++ b/src/formats/ibm1440.textpb @@ -72,3 +72,7 @@ heads { end: 1 } +filesystem { + fatfs {} +} + diff --git a/src/formats/ibm180.textpb b/src/formats/ibm180.textpb index dcce0849..0b923dcd 100644 --- a/src/formats/ibm180.textpb +++ b/src/formats/ibm180.textpb @@ -68,3 +68,9 @@ heads { } tpi: 48 + +filesystem { + fatfs {} +} + + diff --git a/src/formats/ibm360.textpb b/src/formats/ibm360.textpb index 0cb3e49c..2958e9ad 100644 --- a/src/formats/ibm360.textpb +++ b/src/formats/ibm360.textpb @@ -65,3 +65,8 @@ heads { tpi: 48 +filesystem { + fatfs {} +} + + diff --git a/src/formats/ibm720.textpb b/src/formats/ibm720.textpb index f9530923..c52b19f5 100644 --- a/src/formats/ibm720.textpb +++ b/src/formats/ibm720.textpb @@ -69,3 +69,7 @@ heads { end: 1 } +filesystem { + fatfs {} +} +