pang-o-lin/tests/fragment-file.c
Sean Cross 383403eba4 mass WIP commit
Signed-off-by: Sean Cross <sean@xobs.io>
2019-07-29 22:29:50 +08:00

104 lines
2.6 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;
}
const char *foo_filename[] = { "foo.bar", NULL };
const char *baz_filename[] = { "baz.qux", NULL };
memset_str(tmp_bfr, "baz.qux\x0a", 16384);
fat12_write_file(part, baz_filename, 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_filename, 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_filename);
// 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_filename, 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_defragment_file(part, baz_filename);//const char *path[])
fat12_close(part);
fat12_free(&part);
return 0;
}