2019-06-09 12:32:53 +00:00
|
|
|
#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"
|
2019-06-12 10:22:12 +00:00
|
|
|
#include "io.h"
|
2019-06-09 12:32:53 +00:00
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
static int make_image(const char *name, uint32_t size)
|
|
|
|
{
|
2019-06-09 12:32:53 +00:00
|
|
|
|
2019-06-12 10:22:12 +00:00
|
|
|
struct pang_io *io = pang_open(name);
|
2019-06-13 08:12:19 +00:00
|
|
|
if (!io)
|
|
|
|
{
|
2019-06-09 12:32:53 +00:00
|
|
|
fprintf(stderr, "couldn't open %s: %s\n", name, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
if (-1 == fat12_mkfs(io, size))
|
|
|
|
{
|
2019-06-09 12:32:53 +00:00
|
|
|
fprintf(stderr, "couldn't make fat12 on %s: %s\n", name, strerror(errno));
|
2019-06-12 10:22:12 +00:00
|
|
|
pang_close(&io);
|
2019-06-09 12:32:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-06-12 10:22:12 +00:00
|
|
|
pang_close(&io);
|
2019-06-09 12:32:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
static int list_directory(void *data, const struct fat12_dirent *dirent)
|
|
|
|
{
|
2019-06-10 14:29:21 +00:00
|
|
|
printf("%d %d %x %d %d %s\n", dirent->ctime, dirent->mtime, dirent->file_attributes, dirent->size, dirent->first_cluster, dirent->filename);
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
static int run_command(const char *cmd)
|
|
|
|
{
|
2019-06-13 02:58:19 +00:00
|
|
|
int ret = system(cmd);
|
2019-06-13 08:12:19 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
2019-06-13 02:58:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
static void memset_str(uint8_t *bfr, uint8_t *val, size_t count)
|
|
|
|
{
|
2019-06-13 02:58:19 +00:00
|
|
|
int i;
|
2019-06-13 08:12:19 +00:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2019-06-13 02:58:19 +00:00
|
|
|
bfr[i] = val[i & 0x7];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-06-13 02:58:19 +00:00
|
|
|
uint8_t tmp_bfr[32768];
|
|
|
|
const char *img_name = "build/fat12-fragmented.img";
|
2019-06-09 12:32:53 +00:00
|
|
|
int ret = 0;
|
|
|
|
int fd;
|
|
|
|
struct fat12_partition *part = fat12_alloc();
|
2019-06-13 08:12:19 +00:00
|
|
|
|
2019-06-13 02:58:19 +00:00
|
|
|
ret = make_image(img_name, 9 * 1024 * 1024);
|
2019-06-13 08:12:19 +00:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
2019-06-13 02:58:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-06-09 12:32:53 +00:00
|
|
|
|
2019-06-13 08:12:19 +00:00
|
|
|
if (-1 == fat12_open(part, img_name))
|
|
|
|
{
|
2019-06-09 12:32:53 +00:00
|
|
|
fprintf(stderr, "couldn't open fat12 on %s: %s\n", img_name, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-13 02:58:19 +00:00
|
|
|
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");
|
|
|
|
|
2019-06-10 14:29:21 +00:00
|
|
|
fat12_ls_foreach(part, 0, NULL, list_directory);
|
|
|
|
|
2019-06-09 12:32:53 +00:00
|
|
|
fat12_close(part);
|
|
|
|
fat12_free(&part);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|