ops: add the ability to write and delete files

Right now it only works on the root directory, and has much more
functionality that needs adding.  But it passes the tests.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
2019-06-13 10:58:19 +08:00
parent cf239b9b7a
commit 48f1f33c49
4 changed files with 244 additions and 9 deletions

View File

@ -30,23 +30,55 @@ static int list_directory(void *data, const struct fat12_dirent *dirent) {
printf("%d %d %x %d %d %s\n", dirent->ctime, dirent->mtime, dirent->file_attributes, dirent->size, dirent->first_cluster, dirent->filename);
}
static int run_command(const char *cmd) {
int ret = system(cmd);
if (ret) {
exit(1);
}
return 0;
}
static void memset_str(uint8_t *bfr, uint8_t *val, size_t count) {
int i;
for (i = 0; i < count; i++) {
bfr[i] = val[i & 0x7];
}
}
int main(int argc, char **argv) {
// const char *img_name = "build/fat12-1800k.img";
const char *img_name = "disk-image";
uint8_t tmp_bfr[32768];
const char *img_name = "build/fat12-fragmented.img";
int ret = 0;
int fd;
struct fat12_partition *part = fat12_alloc();
// ret = make_image(img_name, 1800 * 1024);
// if (ret == -1) {
// return 1;
// }
ret = make_image(img_name, 9 * 1024 * 1024);
if (ret == -1) {
return 1;
}
if (-1 == fat12_open(part, img_name)) {
fprintf(stderr, "couldn't open fat12 on %s: %s\n", img_name, strerror(errno));
return 1;
}
memset_str(tmp_bfr, "baz.qux\x0a", 16384);
fat12_write_file(part, "baz.qux", tmp_bfr, 16384);
// run_command("yes baz.qux | dd bs=16384 count=1 | fattool build/fat12-fragmented.img writefile baz.qux");
memset_str(tmp_bfr, "foo.bar\x0a", 16384);
fat12_write_file(part, "foo.bar", tmp_bfr, 16384);
// run_command("yes foo.bar | dd bs=16384 count=1 | fattool build/fat12-fragmented.img writefile foo.bar");
fat12_delete_file(part, "baz.qux");
// fat12_flush(part);
// run_command("fattool build/fat12-fragmented.img deletefile baz.qux");
// fat12_sync(part);
memset_str(tmp_bfr, "baz.qux\x0a", 32768);
fat12_write_file(part, "baz.qux", tmp_bfr, 32768);
// run_command("yes baz.qux | dd bs=16384 count=2 | fattool build/fat12-fragmented.img writefile baz.qux");
fat12_ls_foreach(part, 0, NULL, list_directory);
fat12_close(part);