Add error handling to oxfstool insert

This commit is contained in:
2016-07-03 02:07:30 -05:00
parent c68a4cbbc0
commit 3f941517ea
+33 -5
View File
@@ -18,6 +18,7 @@ const char *usage = "usage:\n"
char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
FILE *file = fopen(path, "rw");
if (file) {
fseek(file, 0, SEEK_END);
const auto size = ftell(file);
auto buff = (char*) malloc(size);
@@ -27,6 +28,9 @@ char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
*sizeOut = size;
}
return buff;
} else {
return nullptr;
}
}
int format(int argc, char **args) {
@@ -82,6 +86,7 @@ int write(int argc, char **args) {
::size_t srcSize;
FILE *fsFile = fopen(fsPath, "rw");
if (fsFile) {
fseek(fsFile, 0, SEEK_END);
const auto fsSize = ftell(fsFile);
@@ -89,22 +94,45 @@ int write(int argc, char **args) {
fread(fs, fsSize, 1, fsFile);
auto srcBuff = loadFileBuff(srcPath, &srcSize);
if (srcBuff) {
auto type = *((ox::std::uint32_t*) fs);
switch (type) {
case ox::fs::OxFS16:
((FileStore16*) fs)->write(inode, srcBuff, srcSize);
err |= ((FileStore16*) fs)->write(inode, srcBuff, srcSize);
break;
case ox::fs::OxFS32:
((FileStore32*) fs)->write(inode, srcBuff, srcSize);
err |= ((FileStore32*) fs)->write(inode, srcBuff, srcSize);
break;
case ox::fs::OxFS64:
((FileStore64*) fs)->write(inode, srcBuff, srcSize);
err |= ((FileStore64*) fs)->write(inode, srcBuff, srcSize);
break;
}
} else {
err = 1;
fprintf(stderr, "Could not load source file.\n");
}
if (err) {
fprintf(stderr, "Could not write to file system.\n");
err = 0;
} else {
err = fwrite(fs, fsSize, 1, fsFile);
if (err) {
fprintf(stderr, "Could not write to file system.\n");
}
}
fwrite(fs, fsSize, 1, fsFile);
err = fclose(fsFile);
if (err) {
fprintf(stderr, "Could not write to file system file.\n");
}
free(fs);
free(srcBuff);
} else {
fprintf(stderr, "Could not open file system\n");
}
}
return err;
}