705 lines
19 KiB
C
705 lines
19 KiB
C
#include "fs.h"
|
|
#include <assert.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void disk_read(Disk *d, Block block, uint16_t id)
|
|
{
|
|
assert(id < d->blocks_count);
|
|
memcpy(block, &d->blocks[id], size_of_block);
|
|
}
|
|
|
|
void disk_write(Disk *d, const Block block, uint16_t id)
|
|
{
|
|
assert(id < d->blocks_count);
|
|
memcpy(&d->blocks[id], block, size_of_block);
|
|
}
|
|
|
|
constexpr size_t blocks_cap = block_size * 16;
|
|
constexpr size_t direentry_max_size = 64;
|
|
|
|
constexpr size_t inode_ndir_blocks_count = inode_blocks_cap - 1;
|
|
constexpr size_t inode_ind_block_idx = inode_ndir_blocks_count;
|
|
|
|
typedef enum : uint16_t {
|
|
INode_File,
|
|
INode_Dir,
|
|
} INodeTy;
|
|
|
|
typedef struct {
|
|
uint16_t inode;
|
|
uint16_t next;
|
|
uint16_t name_len;
|
|
uint16_t name[inode_name_cap];
|
|
} DirEntry;
|
|
|
|
static_assert(sizeof(DirEntry) / sizeof(uint16_t) == direentry_max_size);
|
|
|
|
static size_t direntry_size(const DirEntry *entry)
|
|
{
|
|
return (sizeof(entry->inode) + sizeof(entry->next) + sizeof(entry->name_len)
|
|
+ sizeof(entry->name[0]) * entry->name_len)
|
|
/ sizeof(uint16_t);
|
|
}
|
|
|
|
static void bitmap_set(uint16_t bitmap[], uint16_t id, bool v)
|
|
{
|
|
uint16_t *row = &bitmap[id / 16];
|
|
uint16_t bit = 1u << id % 16;
|
|
*row = v ? *row | bit : *row & ~bit;
|
|
}
|
|
|
|
static bool bitmap_get(uint16_t bitmap[], uint16_t id)
|
|
{
|
|
return bitmap[id / 16] >> id % 16 & 1;
|
|
}
|
|
|
|
static uint16_t allocate_block(Fs *fs)
|
|
{
|
|
uint16_t block_id = 0;
|
|
for (size_t i = 2; i < blocks_cap; ++i) {
|
|
if (!bitmap_get(*fs->block_bitmap, i)) {
|
|
block_id = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
assert(block_id != 0);
|
|
|
|
bitmap_set(*fs->block_bitmap, block_id, true);
|
|
|
|
fs->out_of_sync = true;
|
|
return block_id;
|
|
}
|
|
|
|
static void deallocate_block(Fs *fs, uint16_t block_id)
|
|
{
|
|
bitmap_set(*fs->block_bitmap, block_id, false);
|
|
fs->out_of_sync = true;
|
|
}
|
|
|
|
static uint16_t allocate_inode(Fs *fs)
|
|
{
|
|
Superblock *sb = fs->sb;
|
|
|
|
uint16_t inode_id = UINT16_MAX;
|
|
for (size_t i = 0; i < sb->inodeblocks_count * inodes_per_block; ++i) {
|
|
if (!bitmap_get(sb->inodebitmap, i)) {
|
|
inode_id = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (inode_id == UINT16_MAX) {
|
|
assert(sb->inodeblocks_count < inodeblocks_cap);
|
|
uint16_t new_block_id = allocate_block(fs);
|
|
|
|
inode_id = sb->inodeblocks_count * inodes_per_block;
|
|
|
|
sb->inodeblocks[sb->inodeblocks_count] = new_block_id;
|
|
sb->inodeblocks_count += 1;
|
|
}
|
|
|
|
bitmap_set(sb->inodebitmap, inode_id, true);
|
|
fs->out_of_sync = true;
|
|
|
|
return inode_id;
|
|
}
|
|
|
|
static void deallocate_inode(Fs *fs, uint16_t inode_id)
|
|
{
|
|
bitmap_set(fs->sb->inodebitmap, inode_id, false);
|
|
fs->out_of_sync = true;
|
|
}
|
|
|
|
static void write_block(Fs *fs, const Block block, uint16_t id)
|
|
{
|
|
disk_write(fs->disk, block, id + fs->block_offset);
|
|
}
|
|
|
|
static void read_block(Fs *fs, Block block, uint16_t id)
|
|
{
|
|
disk_read(fs->disk, block, id + fs->block_offset);
|
|
}
|
|
|
|
static void write_inode(Fs *fs, const INode *inode)
|
|
{
|
|
uint16_t inode_block_idx = inode->id / inodes_per_block;
|
|
uint16_t block_offset = inode->id % inodes_per_block * inode_size;
|
|
assert(inode_block_idx < fs->blocks_count && block_offset < block_size);
|
|
|
|
uint16_t block_id = fs->sb->inodeblocks[inode_block_idx];
|
|
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
memcpy(&block[block_offset], inode, sizeof(INode));
|
|
write_block(fs, block, block_id);
|
|
}
|
|
|
|
static void read_inode(Fs *fs, INode *out_inode, uint16_t inode_id)
|
|
{
|
|
uint16_t inode_block_idx = inode_id / inodes_per_block;
|
|
uint16_t block_offset = inode_id % inodes_per_block * inode_size;
|
|
assert(inode_block_idx < fs->blocks_count && block_offset < block_size);
|
|
|
|
uint16_t block_id = fs->sb->inodeblocks[inode_block_idx];
|
|
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
memcpy(out_inode, &block[block_offset], sizeof(INode));
|
|
}
|
|
|
|
static uint16_t inode_allocate_block(Fs *fs, INode *inode)
|
|
{
|
|
assert((size_t)inode->blocks_count + 1 < inode_blocks_cap);
|
|
|
|
uint16_t block_id = allocate_block(fs);
|
|
|
|
if (inode->blocks_count < inode_ndir_blocks_count) {
|
|
inode->blocks[inode->blocks_count] = block_id;
|
|
inode->blocks_count += 1;
|
|
return block_id;
|
|
}
|
|
|
|
uint16_t ind_block_id = inode->blocks[inode_ind_block_idx];
|
|
Block ind_block;
|
|
read_block(fs, ind_block, ind_block_id);
|
|
ind_block[inode->blocks_count - inode_ndir_blocks_count] = block_id;
|
|
write_block(fs, ind_block, ind_block_id);
|
|
return block_id;
|
|
}
|
|
|
|
static bool inode_has_ind_block(Fs *fs, const INode *inode)
|
|
{
|
|
(void)fs;
|
|
return inode->blocks_count > inode_ndir_blocks_count;
|
|
}
|
|
|
|
static uint16_t *inode_block_at(INode *inode, uint16_t i, Block ind_block)
|
|
{
|
|
assert(i < inode->blocks_count);
|
|
|
|
if (i < inode_ndir_blocks_count)
|
|
return &inode->blocks[i];
|
|
|
|
assert(i < inode_ndir_blocks_count + block_size);
|
|
return &ind_block[i - inode_ndir_blocks_count];
|
|
}
|
|
|
|
static void inode_deallocate_block(Fs *fs, INode *inode, uint16_t block_id)
|
|
{
|
|
assert(inode->blocks_count > 0);
|
|
|
|
bool has_ind_block_before = inode_has_ind_block(fs, inode);
|
|
uint16_t ind_block_id = inode->blocks[inode_ind_block_idx];
|
|
|
|
Block ind_block;
|
|
if (has_ind_block_before)
|
|
read_block(fs, ind_block, ind_block_id);
|
|
|
|
uint16_t i = 0;
|
|
for (; i < inode->blocks_count; ++i)
|
|
if (*inode_block_at(inode, i, ind_block) == block_id)
|
|
break;
|
|
|
|
for (; i < inode->blocks_count - 1; ++i)
|
|
*inode_block_at(inode, i, ind_block)
|
|
= *inode_block_at(inode, i + 1, ind_block);
|
|
|
|
if (inode_has_ind_block(fs, inode))
|
|
write_block(fs, ind_block, ind_block_id);
|
|
else if (has_ind_block_before)
|
|
deallocate_block(fs, ind_block_id);
|
|
}
|
|
|
|
static uint16_t inode_last_block(Fs *fs, INode *inode)
|
|
{
|
|
Block ind_block;
|
|
if (inode_has_ind_block(fs, inode))
|
|
read_block(fs, ind_block, inode->blocks[inode_ind_block_idx]);
|
|
return *inode_block_at(inode, inode->blocks_count - 1, ind_block);
|
|
}
|
|
|
|
static void make_dir_with_inode_ids(
|
|
Fs *fs, INode *out_inode, uint16_t inode_id, uint16_t parent_inode_id)
|
|
{
|
|
*out_inode = (INode) {
|
|
.id = inode_id,
|
|
.mode = INode_Dir,
|
|
.blocks_count = 0,
|
|
.last_block_size = 0,
|
|
.blocks = { },
|
|
};
|
|
|
|
DirEntry parent_entry = {
|
|
.inode = parent_inode_id,
|
|
.next = 0,
|
|
.name_len = 2,
|
|
.name = { '.', '.' },
|
|
};
|
|
|
|
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
|
Block block = { };
|
|
memcpy(
|
|
block, &parent_entry, direntry_size(&parent_entry) * sizeof(uint16_t));
|
|
write_block(fs, block, entry_block_id);
|
|
out_inode->last_block_size = direntry_size(&parent_entry);
|
|
|
|
write_inode(fs, out_inode);
|
|
}
|
|
|
|
static void write_dir_entry(Fs *fs, INode *dir_inode, const DirEntry *direntry)
|
|
{
|
|
uint16_t block_id = inode_last_block(fs, dir_inode);
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
DirEntry entry = { };
|
|
uint16_t offset = 0;
|
|
memcpy(&entry, &block[offset], direentry_max_size * sizeof(uint16_t));
|
|
|
|
while (entry.next) {
|
|
offset = entry.next;
|
|
memcpy(&entry, &block[offset], direentry_max_size * sizeof(uint16_t));
|
|
}
|
|
|
|
if (offset + direntry_size(direntry) > block_size) {
|
|
uint16_t block_id = inode_allocate_block(fs, dir_inode);
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
memcpy(block, direntry, direntry_size(direntry) * sizeof(uint16_t));
|
|
write_block(fs, block, block_id);
|
|
return;
|
|
}
|
|
|
|
uint16_t new_offset = offset + direntry_size(&entry);
|
|
entry.next = new_offset;
|
|
|
|
memcpy(&block[offset], &entry, direntry_size(&entry) * sizeof(uint16_t));
|
|
|
|
memcpy(&block[new_offset],
|
|
direntry,
|
|
direntry_size(direntry) * sizeof(uint16_t));
|
|
write_block(fs, block, block_id);
|
|
}
|
|
|
|
static void add_inode_to_dir(
|
|
Fs *fs, INode *dir_inode, const INode *inode, const char *name)
|
|
{
|
|
size_t name_len = strlen(name);
|
|
assert(dir_inode->mode == INode_Dir && name_len <= inode_name_cap);
|
|
|
|
DirEntry entry = {
|
|
.inode = inode->id,
|
|
.next = 0,
|
|
.name_len = name_len,
|
|
.name = { },
|
|
};
|
|
for (size_t i = 0; i < name_len; ++i)
|
|
entry.name[i] = name[i];
|
|
|
|
write_dir_entry(fs, dir_inode, &entry);
|
|
write_inode(fs, dir_inode);
|
|
}
|
|
|
|
static void inode_foreach_block(Fs *fs,
|
|
const INode *inode,
|
|
void (*func)(uint16_t block_id, void *arg),
|
|
void *arg)
|
|
{
|
|
size_t i = 0;
|
|
for (; i < inode->blocks_count && i < inode_ndir_blocks_count; ++i)
|
|
func(inode->blocks[i], arg);
|
|
|
|
if (inode->blocks_count <= inode_ndir_blocks_count)
|
|
return;
|
|
assert(inode->blocks_count <= inode_ndir_blocks_count + block_size);
|
|
|
|
Block ind_block;
|
|
read_block(fs, ind_block, inode->blocks[inode_ind_block_idx]);
|
|
|
|
for (; i < inode->blocks_count; ++i)
|
|
func(ind_block[i], arg);
|
|
}
|
|
|
|
void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count)
|
|
{
|
|
assert(blocks_count <= sizeof(Block) * 8);
|
|
*fs = (Fs) {
|
|
.disk = disk,
|
|
.block_offset = block_offset,
|
|
.blocks_count = blocks_count,
|
|
.superblock = calloc(1, sizeof(Block)),
|
|
.sb = nullptr,
|
|
.block_bitmap = calloc(1, sizeof(Block)),
|
|
.out_of_sync = true,
|
|
};
|
|
|
|
fs->sb = (Superblock *)fs->superblock;
|
|
|
|
bitmap_set(*fs->block_bitmap, 0, true);
|
|
bitmap_set(*fs->block_bitmap, 1, true);
|
|
|
|
uint16_t root_inode_id = allocate_inode(fs);
|
|
assert(root_inode_id == 0);
|
|
|
|
INode root_inode;
|
|
make_dir_with_inode_ids(fs, &root_inode, root_inode_id, root_inode_id);
|
|
}
|
|
|
|
void fs_destroy(Fs *fs)
|
|
{
|
|
free(fs->superblock);
|
|
free(fs->block_bitmap);
|
|
}
|
|
|
|
void fs_sync(Fs *fs)
|
|
{
|
|
if (!fs->out_of_sync)
|
|
return;
|
|
write_block(fs, *fs->superblock, 0);
|
|
write_block(fs, *fs->block_bitmap, 1);
|
|
fs->out_of_sync = false;
|
|
}
|
|
|
|
void fs_root(Fs *fs, INode *out_inode)
|
|
{
|
|
read_inode(fs, out_inode, 0);
|
|
}
|
|
|
|
void fs_inode(Fs *fs, INode *out_inode, uint16_t inode_id)
|
|
{
|
|
read_inode(fs, out_inode, inode_id);
|
|
}
|
|
|
|
void fs_make_dir(Fs *fs, INode *out_inode, INode *parent, const char *name)
|
|
{
|
|
uint16_t inode_id = allocate_inode(fs);
|
|
make_dir_with_inode_ids(fs, out_inode, inode_id, parent->id);
|
|
write_inode(fs, out_inode);
|
|
add_inode_to_dir(fs, parent, out_inode, name);
|
|
}
|
|
|
|
void fs_make_file(Fs *fs, INode *out_inode, INode *parent, const char *name)
|
|
{
|
|
uint16_t inode_id = allocate_inode(fs);
|
|
|
|
*out_inode = (INode) {
|
|
.id = inode_id,
|
|
.mode = INode_File,
|
|
.blocks_count = 0,
|
|
.last_block_size = 0,
|
|
.blocks = { },
|
|
};
|
|
|
|
write_inode(fs, out_inode);
|
|
add_inode_to_dir(fs, parent, out_inode, name);
|
|
}
|
|
|
|
static void remove_block_fn(uint16_t block_id, void *arg)
|
|
{
|
|
Fs *fs = arg;
|
|
deallocate_block(fs, block_id);
|
|
}
|
|
|
|
struct remove_direntry_fn_args {
|
|
Fs *fs;
|
|
uint16_t inode_id;
|
|
uint16_t out_empty_block_id;
|
|
bool matched;
|
|
};
|
|
|
|
static void inode_remove_blocks(Fs *fs, INode *inode)
|
|
{
|
|
inode_foreach_block(fs, inode, remove_block_fn, fs);
|
|
if (inode->blocks_count > inode_ndir_blocks_count)
|
|
deallocate_block(fs, inode->blocks[inode_ind_block_idx]);
|
|
inode->blocks_count = 0;
|
|
inode->last_block_size = 0;
|
|
}
|
|
|
|
static void remove_direntry_fn(uint16_t block_id, void *arg)
|
|
{
|
|
struct remove_direntry_fn_args *args = arg;
|
|
Fs *fs = args->fs;
|
|
|
|
if (args->matched)
|
|
return;
|
|
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
DirEntry entry = { };
|
|
uint16_t curr = 0;
|
|
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
|
|
|
if (!curr && !entry.next && entry.inode == args->inode_id) {
|
|
args->out_empty_block_id = block_id;
|
|
args->matched = true;
|
|
return;
|
|
}
|
|
|
|
int16_t prev = 0;
|
|
|
|
while (entry.next && entry.inode != args->inode_id) {
|
|
prev = curr;
|
|
curr = entry.next;
|
|
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
|
}
|
|
|
|
if (!entry.next && entry.inode != args->inode_id)
|
|
return;
|
|
|
|
if (!entry.next) {
|
|
memcpy(&entry, &block[prev], direentry_max_size * sizeof(uint16_t));
|
|
entry.next = 0;
|
|
memcpy(&block[prev], &entry, direntry_size(&entry) * sizeof(uint16_t));
|
|
}
|
|
|
|
while (entry.next) {
|
|
prev = curr;
|
|
curr = entry.next;
|
|
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
|
memcpy(&block[prev], &entry, direntry_size(&entry) * sizeof(uint16_t));
|
|
}
|
|
|
|
write_block(fs, block, block_id);
|
|
args->matched = true;
|
|
}
|
|
|
|
void fs_remove(Fs *fs, INode *inode, INode *parent)
|
|
{
|
|
inode_remove_blocks(fs, inode);
|
|
struct remove_direntry_fn_args args = { fs, inode->id, 0, false };
|
|
inode_foreach_block(fs, parent, remove_direntry_fn, &args);
|
|
assert(args.matched);
|
|
if (args.out_empty_block_id)
|
|
inode_deallocate_block(fs, inode, args.out_empty_block_id);
|
|
deallocate_inode(fs, inode->id);
|
|
}
|
|
|
|
struct list_block_entries_fn_args {
|
|
Fs *fs;
|
|
ListEntry *entries;
|
|
size_t entries_cap;
|
|
size_t *entries_size;
|
|
};
|
|
|
|
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
|
|
|
static void list_block_entries_fn(uint16_t block_id, void *arg)
|
|
{
|
|
struct list_block_entries_fn_args *args = arg;
|
|
Fs *fs = args->fs;
|
|
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
DirEntry entry = { };
|
|
do {
|
|
if (*args->entries_size + 1 >= args->entries_cap)
|
|
return;
|
|
|
|
memcpy(&entry,
|
|
&block[entry.next],
|
|
MIN(direentry_max_size, block_size - entry.next));
|
|
|
|
ListEntry list_entry = {
|
|
.inode = entry.inode,
|
|
.name = { },
|
|
};
|
|
for (size_t i = 0; i < entry.name_len; ++i)
|
|
list_entry.name[i] = entry.name[i];
|
|
args->entries[*args->entries_size] = list_entry;
|
|
*args->entries_size += 1;
|
|
} while (entry.next != 0);
|
|
}
|
|
|
|
size_t fs_list(
|
|
Fs *fs, ListEntry *entries, size_t entries_cap, const INode *inode)
|
|
{
|
|
size_t entries_size = 0;
|
|
struct list_block_entries_fn_args args
|
|
= { fs, entries, entries_cap, &entries_size };
|
|
inode_foreach_block(fs, inode, list_block_entries_fn, &args);
|
|
return entries_size;
|
|
}
|
|
|
|
uint16_t fs_write(
|
|
Fs *fs, INode *inode, const uint16_t *buffer, uint16_t buffer_size)
|
|
{
|
|
inode_remove_blocks(fs, inode);
|
|
uint16_t i = 0;
|
|
while (i < buffer_size) {
|
|
uint16_t block_id = inode_allocate_block(fs, inode);
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
uint16_t chunk_size = MIN((size_t)buffer_size - i, block_size);
|
|
memcpy(block, &buffer[i], chunk_size * sizeof(uint16_t));
|
|
write_block(fs, block, block_id);
|
|
inode->last_block_size = chunk_size;
|
|
i += chunk_size;
|
|
}
|
|
write_inode(fs, inode);
|
|
return i;
|
|
}
|
|
|
|
uint16_t fs_read(Fs *fs,
|
|
INode *inode,
|
|
uint16_t *buffer,
|
|
uint16_t buffer_size,
|
|
uint16_t offset)
|
|
{
|
|
Block ind_block;
|
|
if (inode_has_ind_block(fs, inode))
|
|
read_block(fs, ind_block, inode->blocks[inode_ind_block_idx]);
|
|
|
|
uint16_t block_idx = offset / block_size;
|
|
uint16_t block_offset = offset / block_size;
|
|
uint16_t i = 0;
|
|
|
|
for (; block_idx < inode->blocks_count && i < buffer_size; ++block_idx) {
|
|
Block block;
|
|
read_block(fs, block, *inode_block_at(inode, block_idx, ind_block));
|
|
uint16_t chunk_size
|
|
= MIN((size_t)buffer_size - i, block_size - block_offset);
|
|
memcpy(&buffer[i], &block[block_offset], chunk_size);
|
|
i += chunk_size;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
void fs_dump_info(Fs *fs)
|
|
{
|
|
printf("block_offset = %u\n", fs->block_offset);
|
|
printf("blocks_count = %u\n", fs->blocks_count);
|
|
printf("out_of_sync = %s\n", fs->out_of_sync ? "true" : "false");
|
|
}
|
|
|
|
void fs_dump_superblock(Fs *fs)
|
|
{
|
|
Superblock *sb = fs->sb;
|
|
printf("blocks_count = %u\n", sb->blocks_count);
|
|
printf("inode_blocks_count = %u\n", sb->inodeblocks_count);
|
|
printf("inode_blocks = [");
|
|
for (size_t i = 0; i < sb->inodeblocks_count; ++i)
|
|
printf("%s%u", i != 0 ? ", " : "", sb->inodeblocks[i]);
|
|
printf("]\n");
|
|
printf("inode_bitmap = [");
|
|
for (size_t i = 0; i < inodebitmap_size; ++i)
|
|
if (bitmap_get(sb->inodebitmap, i))
|
|
printf("%s%zu", i > 0 ? ", " : "", i);
|
|
printf("]\n");
|
|
}
|
|
|
|
void fs_dump_block_bitmap(Fs *fs)
|
|
{
|
|
printf("[");
|
|
for (size_t i = 0; i < inodebitmap_size; ++i)
|
|
if (bitmap_get(*fs->block_bitmap, i))
|
|
printf("%s%zu", i > 0 ? ", " : "", i);
|
|
printf("]\n");
|
|
}
|
|
|
|
void fs_dump_inode(Fs *fs, uint16_t inode_id)
|
|
{
|
|
INode inode;
|
|
read_inode(fs, &inode, inode_id);
|
|
printf("id = %u\n", inode.id);
|
|
printf("mode = %u (%s)\n",
|
|
inode.mode,
|
|
inode.mode == INode_File ? "File" : "Dir");
|
|
printf("blocks_count = %u\n", inode.blocks_count);
|
|
printf("last_block_size = %u\n", inode.last_block_size);
|
|
printf("ndir_blocks = [");
|
|
for (size_t i = 0; i < inode.blocks_count && i < inode_ndir_blocks_count;
|
|
++i)
|
|
printf("%s%u", i > 0 ? ", " : "", inode.blocks[i]);
|
|
printf("]\n");
|
|
printf("ind_block = %u\n", inode.blocks[inode_ind_block_idx]);
|
|
}
|
|
|
|
void fs_dump_block_hex(Fs *fs, uint16_t block_id)
|
|
{
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
constexpr size_t values_per_row = 16;
|
|
for (size_t i = 0; i < block_size; i += values_per_row) {
|
|
for (size_t j = 0; j < values_per_row; ++j)
|
|
printf("%s%4x", j > 0 ? " " : "", block[i + j]);
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void fs_dump_block_dec(Fs *fs, uint16_t block_id)
|
|
{
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
constexpr size_t values_per_row = 16;
|
|
for (size_t i = 0; i < block_size; i += values_per_row) {
|
|
for (size_t j = 0; j < values_per_row; ++j)
|
|
printf("%s%5u", j > 0 ? " " : "", block[i + j]);
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void fs_dump_block_char(Fs *fs, uint16_t block_id)
|
|
{
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
char conversions[256] = {
|
|
['\0'] = '0',
|
|
['\t'] = 't',
|
|
['\n'] = 'n',
|
|
['\f'] = 'f',
|
|
['\b'] = 'b',
|
|
};
|
|
|
|
constexpr size_t values_per_row = 16;
|
|
for (size_t i = 0; i < block_size; i += values_per_row) {
|
|
for (size_t j = 0; j < values_per_row; ++j) {
|
|
char ch = block[i + j];
|
|
printf("%s", j ? " " : "");
|
|
if (ch == ' ')
|
|
printf("' '");
|
|
else if (ch > ' ')
|
|
printf("%2c ", ch);
|
|
else if (conversions[(size_t)ch])
|
|
printf("\\%-2c", conversions[(size_t)ch]);
|
|
else
|
|
printf("\\x%-2x", ch);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void fs_dump_direntry_block(Fs *fs, uint16_t block_id)
|
|
{
|
|
Block block;
|
|
read_block(fs, block, block_id);
|
|
|
|
int i = 0;
|
|
DirEntry entry = { };
|
|
do {
|
|
memcpy(&entry, &block[entry.next], direentry_max_size);
|
|
|
|
printf("dir_entries[%d]\n", i);
|
|
printf(" inode = %u\n", entry.inode);
|
|
printf(" next = %u\n", entry.next);
|
|
printf(" name_len = %u\n", entry.name_len);
|
|
char name[inode_name_cap];
|
|
for (size_t i = 0; i < entry.name_len; ++i)
|
|
name[i] = entry.name[i];
|
|
printf(" name = \"%.*s\"\n", (int)entry.name_len, name);
|
|
|
|
i += 1;
|
|
} while (entry.next != 0);
|
|
}
|