pang-o-lin/tests/fragment-file.c
Sean Cross 48f1f33c49 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>
2019-06-13 10:58:19 +08:00

88 lines
2.4 KiB
C

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "fat12.h"
#include "io.h"
static int make_image(const char *name, uint32_t size) {
struct pang_io *io = pang_open(name);
if (!io) {
fprintf(stderr, "couldn't open %s: %s\n", name, strerror(errno));
return 1;
}
if (-1 == fat12_mkfs(io, size)) {
fprintf(stderr, "couldn't make fat12 on %s: %s\n", name, strerror(errno));
pang_close(&io);
return 1;
}
pang_close(&io);
return 0;
}
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) {
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, 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);
fat12_free(&part);
return 0;
}