diff options
author | Marvin Borner | 2022-01-02 18:37:51 +0100 |
---|---|---|
committer | Marvin Borner | 2022-01-02 18:37:51 +0100 |
commit | e4b01ac99b3d13c5d1057eacc04757cea957cbd7 (patch) | |
tree | 5bc2a7af3a788047aab8482959eddc306831adfe /src/mkfs.c | |
parent | 6a1f4e4d394b3c5ff7a7d1c58a8775a645275e4a (diff) |
Read/write maybe more
Diffstat (limited to 'src/mkfs.c')
-rw-r--r-- | src/mkfs.c | 28 |
1 files changed, 24 insertions, 4 deletions
@@ -20,16 +20,35 @@ int main(int argc, char *argv[]) return 1; } + printf("Formatting %s...\n", argv[1]); + FILE *image = fopen(argv[1], "r+"); if (!image) { fprintf(stderr, "%s: error: no valid image specified\n", argv[0]); return 1; } + fseek(image, 0, SEEK_END); + size_t size = ftell(image); + + if (size < 2 * MARFS_ENTRY_SIZE) { + fprintf(stderr, "%s: error: too small image size: %luB but at least %dB needed\n", + argv[0], size, 2 * MARFS_ENTRY_SIZE); + return 1; + } + + struct marfs_entry_header entry_header = { 0 }; + for (u32 i = 0; i < size / MARFS_ENTRY_SIZE; i++) { + fseek(image, i * MARFS_ENTRY_SIZE, SEEK_SET); + assert(fwrite(&entry_header, 1, sizeof(entry_header), image) == + sizeof(entry_header)); + } + struct marfs_header header = { .magic = MARFS_MAGIC, .version = MARFS_SPEC_VERSION, .entry_size = MARFS_ENTRY_SIZE, + .entry_count = size / MARFS_ENTRY_SIZE, .main = MARFS_POINT(1, 1), .padding = { 0 }, }; @@ -40,15 +59,16 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: error: header write failed: %lu bytes written\n", argv[0], write); - struct marfs_dir_entry main = { + struct marfs_dir_entry_info info = { .header.type = MARFS_DIR_ENTRY, + .header.id = 1, .header.prev = MARFS_END, .header.next = MARFS_END, + .count = 0, }; - main.entries[0].name[0] = 0; - write = fwrite(&main, 1, sizeof(main), image); - if (write != sizeof(main)) + write = fwrite(&info, 1, sizeof(info), image); + if (write != sizeof(info)) fprintf(stderr, "%s: error: main directory write failed: %lu bytes written\n", argv[0], write); |