Add walk command.

This commit is contained in:
2017-09-09 19:32:45 -05:00
parent 15e05df202
commit 84533e557e
3 changed files with 53 additions and 2 deletions
+13
View File
@@ -268,6 +268,8 @@ class FileStore {
*/
typename Header::FsSize_t available();
void walk(int(*cb)(const char*, uint64_t start, uint64_t end));
uint16_t fsType();
uint16_t version();
@@ -887,6 +889,17 @@ uint16_t FileStore<Header>::version() {
return m_header.getVersion();
};
template<typename Header>
void FileStore<Header>::walk(int(*cb)(const char*, uint64_t start, uint64_t end)) {
auto err = cb("Header", 0, sizeof(Header));
auto inode = ptr<Inode*>(firstInode());
while (!err && inode != (Inode*) begin()) {
inode = ptr<Inode*>(inode->getNext());
auto start = ptr(inode);
err = cb("Inode", start, start + inode->size());
}
}
template<typename Header>
uint8_t *FileStore<Header>::format(uint8_t *buffer, typename Header::FsSize_t size, uint16_t fsType) {
ox_memset(buffer, 0, size);
+10
View File
@@ -244,6 +244,8 @@ class FileSystem {
virtual uint8_t *buff() = 0;
virtual void walk(int(*cb)(const char*, uint64_t, uint64_t)) = 0;
protected:
virtual int readDirectory(const char *path, Directory<uint64_t, uint64_t> *dirOut) = 0;
};
@@ -326,6 +328,7 @@ class FileSystemTemplate: public FileSystem {
uint64_t size() override;
uint8_t *buff() override;
int move(const char *src, const char *dest) override;
/**
@@ -333,6 +336,8 @@ class FileSystemTemplate: public FileSystem {
*/
int rmDirectoryEntry(const char *path);
void walk(int(*cb)(const char*, uint64_t, uint64_t)) override;
static uint8_t *format(uint8_t *buffer, typename FileStore::FsSize_t size, bool useDirectories);
protected:
@@ -849,6 +854,11 @@ void FileSystemTemplate<FileStore, FS_TYPE>::expand(uint64_t newSize) {
}
}
template<typename FileStore, FsType FS_TYPE>
void FileSystemTemplate<FileStore, FS_TYPE>::walk(int(*cb)(const char*, uint64_t, uint64_t)) {
m_store->walk(cb);
}
typedef FileSystemTemplate<FileStore16, OxFS_16> FileSystem16;
typedef FileSystemTemplate<FileStore32, OxFS_32> FileSystem32;
typedef FileSystemTemplate<FileStore64, OxFS_64> FileSystem64;
+30 -2
View File
@@ -20,7 +20,7 @@
using namespace ox;
using namespace std;
const static auto oxfstoolVersion = "1.3.0";
const static auto oxfstoolVersion = "1.4.0";
const static auto usage = "usage:\n"
"\toxfs format [16,32,64] <size> <path>\n"
"\toxfs read <FS file> <inode>\n"
@@ -28,6 +28,7 @@ const static auto usage = "usage:\n"
"\toxfs write-expand <FS file> <inode> <insertion file>\n"
"\toxfs rm <FS file> <inode>\n"
"\toxfs compact <FS file>\n"
"\toxfs walk <FS file>\n"
"\toxfs version\n";
uint8_t *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) {
@@ -148,7 +149,7 @@ int read(int argc, char **args) {
size_t fileSize;
auto fsBuff = loadFileBuff(fsPath, &fsSize);
if (fsBuff) {
auto fs = createFileSystem(fsBuff, fsSize);
@@ -333,6 +334,31 @@ int remove(int argc, char **args) {
return err;
}
int walk(int argc, char **args) {
int err = 0;
size_t fsSize;
auto fsPath = args[2];
auto fsBuff = loadFileBuff(fsPath, &fsSize);
if (fsBuff) {
auto fs = createFileSystem(fsBuff, fsSize);
if (fs) {
fs->walk([](const char *type, uint64_t start, uint64_t end) {
cout << "narf\n";
cout << type << ", start: " << start << ", end: " << end << endl;
return 0;
});
delete fs;
} else {
cerr << "Invalid file system.\n";
err = 1;
}
delete []fsBuff;
} else {
err = 2;
}
return err;
}
int main(int argc, char **args) {
auto err = 0;
if (argc > 1) {
@@ -349,6 +375,8 @@ int main(int argc, char **args) {
err = compact(argc, args);
} else if (ox_strcmp(cmd, "rm") == 0) {
err = remove(argc, args);
} else if (ox_strcmp(cmd, "walk") == 0) {
err = walk(argc, args);
} else if (ox_strcmp(cmd, "help") == 0) {
printf("%s\n", usage);
} else if (ox_strcmp(cmd, "version") == 0) {