Add error handling to oxfstool insert
This commit is contained in:
+55
-27
@@ -18,15 +18,19 @@ const char *usage = "usage:\n"
|
|||||||
|
|
||||||
char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
|
char *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
|
||||||
FILE *file = fopen(path, "rw");
|
FILE *file = fopen(path, "rw");
|
||||||
fseek(file, 0, SEEK_END);
|
if (file) {
|
||||||
const auto size = ftell(file);
|
fseek(file, 0, SEEK_END);
|
||||||
auto buff = (char*) malloc(size);
|
const auto size = ftell(file);
|
||||||
fread(buff, size, 1, file);
|
auto buff = (char*) malloc(size);
|
||||||
fclose(file);
|
fread(buff, size, 1, file);
|
||||||
if (sizeOut) {
|
fclose(file);
|
||||||
*sizeOut = size;
|
if (sizeOut) {
|
||||||
|
*sizeOut = size;
|
||||||
|
}
|
||||||
|
return buff;
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
return buff;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int format(int argc, char **args) {
|
int format(int argc, char **args) {
|
||||||
@@ -82,29 +86,53 @@ int write(int argc, char **args) {
|
|||||||
::size_t srcSize;
|
::size_t srcSize;
|
||||||
|
|
||||||
FILE *fsFile = fopen(fsPath, "rw");
|
FILE *fsFile = fopen(fsPath, "rw");
|
||||||
fseek(fsFile, 0, SEEK_END);
|
if (fsFile) {
|
||||||
|
fseek(fsFile, 0, SEEK_END);
|
||||||
|
|
||||||
const auto fsSize = ftell(fsFile);
|
const auto fsSize = ftell(fsFile);
|
||||||
auto fs = (char*) malloc(fsSize);
|
auto fs = (char*) malloc(fsSize);
|
||||||
fread(fs, fsSize, 1, fsFile);
|
fread(fs, fsSize, 1, fsFile);
|
||||||
|
|
||||||
auto srcBuff = loadFileBuff(srcPath, &srcSize);
|
auto srcBuff = loadFileBuff(srcPath, &srcSize);
|
||||||
|
if (srcBuff) {
|
||||||
|
auto type = *((ox::std::uint32_t*) fs);
|
||||||
|
switch (type) {
|
||||||
|
case ox::fs::OxFS16:
|
||||||
|
err |= ((FileStore16*) fs)->write(inode, srcBuff, srcSize);
|
||||||
|
break;
|
||||||
|
case ox::fs::OxFS32:
|
||||||
|
err |= ((FileStore32*) fs)->write(inode, srcBuff, srcSize);
|
||||||
|
break;
|
||||||
|
case ox::fs::OxFS64:
|
||||||
|
err |= ((FileStore64*) fs)->write(inode, srcBuff, srcSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = 1;
|
||||||
|
fprintf(stderr, "Could not load source file.\n");
|
||||||
|
}
|
||||||
|
|
||||||
auto type = *((ox::std::uint32_t*) fs);
|
if (err) {
|
||||||
switch (type) {
|
fprintf(stderr, "Could not write to file system.\n");
|
||||||
case ox::fs::OxFS16:
|
err = 0;
|
||||||
((FileStore16*) fs)->write(inode, srcBuff, srcSize);
|
} else {
|
||||||
break;
|
err = fwrite(fs, fsSize, 1, fsFile);
|
||||||
case ox::fs::OxFS32:
|
if (err) {
|
||||||
((FileStore32*) fs)->write(inode, srcBuff, srcSize);
|
fprintf(stderr, "Could not write to file system.\n");
|
||||||
break;
|
}
|
||||||
case ox::fs::OxFS64:
|
}
|
||||||
((FileStore64*) fs)->write(inode, srcBuff, srcSize);
|
|
||||||
break;
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite(fs, fsSize, 1, fsFile);
|
|
||||||
err = fclose(fsFile);
|
|
||||||
}
|
}
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user