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

@@ -167,7 +167,8 @@ private:
throw BadFilesystemException();
default:
throw FilesystemException();
throw FilesystemException(
fmt::format("unknown fatfs error {}", res));
}
}

View File

@@ -33,12 +33,49 @@ 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>
{
@@ -54,25 +91,40 @@ class Filesystem
{
public:
virtual void create()
{ throw UnimplementedFilesystemException(); }
{
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(); }
{
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);

View File

@@ -27,6 +27,8 @@ int mainGetFile(int argc, const char* argv[])
showProfiles("getfile", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
try
{
Path inputFilename(directory);
if (inputFilename.size() == 0)
Error() << "you must supply a filename to read";
@@ -39,6 +41,11 @@ int mainGetFile(int argc, const char* argv[])
auto filesystem = createFilesystemFromConfig();
auto data = filesystem->getFile(inputFilename);
data.writeToFile(outputFilename);
}
catch (const FilesystemException& e)
{
Error() << e.message;
}
return 0;
}

View File

@@ -26,11 +26,18 @@ int mainGetFileInfo(int argc, const char* argv[])
showProfiles("getfileinfo", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
try
{
auto filesystem = createFilesystemFromConfig();
auto attributes = filesystem->getMetadata(Path(directory));
for (const auto& e : attributes)
fmt::print("{} = {}\n", e.first, e.second);
}
catch (const FilesystemException& e)
{
Error() << e.message;
}
return 0;
}

View File

@@ -40,6 +40,8 @@ int mainLs(int argc, const char* argv[])
showProfiles("ls", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);
try
{
auto filesystem = createFilesystemFromConfig();
auto files = filesystem->list(Path(directory));
@@ -58,6 +60,11 @@ int mainLs(int argc, const char* argv[])
total += dirent->length;
}
fmt::print("({} files, {} bytes)\n", files.size(), total);
}
catch (const FilesystemException& e)
{
Error() << e.message;
}
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 {}
}