delete old fs
This commit is contained in:
parent
a33dd11f03
commit
7d093a9ae0
739
src/fs.c
739
src/fs.c
@ -1,739 +0,0 @@
|
|||||||
#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 struct {
|
|
||||||
uint16_t inode_id;
|
|
||||||
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_id) + 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 self_entry = {
|
|
||||||
.inode_id = inode_id,
|
|
||||||
.next = 0,
|
|
||||||
.name_len = 1,
|
|
||||||
.name = { '.' },
|
|
||||||
};
|
|
||||||
size_t self_entry_size = direntry_size(&self_entry);
|
|
||||||
self_entry.next = self_entry_size;
|
|
||||||
|
|
||||||
DirEntry parent_entry = {
|
|
||||||
.inode_id = parent_inode_id,
|
|
||||||
.next = 0,
|
|
||||||
.name_len = 2,
|
|
||||||
.name = { '.', '.' },
|
|
||||||
};
|
|
||||||
|
|
||||||
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
|
||||||
Block block = { };
|
|
||||||
memcpy(block, &self_entry, self_entry_size * sizeof(uint16_t));
|
|
||||||
memcpy(&block[self_entry_size],
|
|
||||||
&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_id = 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;
|
|
||||||
fs->sb->magic = superblock_magic;
|
|
||||||
|
|
||||||
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_load(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 = false,
|
|
||||||
};
|
|
||||||
|
|
||||||
fs->sb = (Superblock *)fs->superblock;
|
|
||||||
|
|
||||||
read_block(fs, *fs->superblock, 0);
|
|
||||||
read_block(fs, *fs->block_bitmap, 1);
|
|
||||||
|
|
||||||
assert(fs->sb->magic == superblock_magic);
|
|
||||||
}
|
|
||||||
|
|
||||||
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_id == args->inode_id) {
|
|
||||||
args->out_empty_block_id = block_id;
|
|
||||||
args->matched = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t prev = 0;
|
|
||||||
|
|
||||||
while (entry.next && entry.inode_id != args->inode_id) {
|
|
||||||
prev = curr;
|
|
||||||
curr = entry.next;
|
|
||||||
memcpy(&entry, &block[curr], direentry_max_size * sizeof(uint16_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!entry.next && entry.inode_id != 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;
|
|
||||||
FsListEntry *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));
|
|
||||||
|
|
||||||
FsListEntry list_entry = {
|
|
||||||
.inode = entry.inode_id,
|
|
||||||
.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, FsListEntry *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 = buffer_size - i;
|
|
||||||
chunk_size = MIN((size_t)chunk_size, block_size - block_offset);
|
|
||||||
if (block_idx == inode->blocks_count - 1)
|
|
||||||
chunk_size = MIN(chunk_size, inode->last_block_size);
|
|
||||||
|
|
||||||
memcpy(&buffer[i], &block[block_offset], chunk_size * sizeof(uint16_t));
|
|
||||||
block_offset = 0;
|
|
||||||
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("magic = 0x%4x\n", sb->magic);
|
|
||||||
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 = (char)block[i + j];
|
|
||||||
printf("%s", j ? " " : "");
|
|
||||||
if (ch == ' ')
|
|
||||||
printf("' '");
|
|
||||||
else if (ch > ' ')
|
|
||||||
printf("%2c ", ch);
|
|
||||||
else if (conversions[(unsigned char)ch])
|
|
||||||
printf("\\%-2c", conversions[(unsigned char)ch]);
|
|
||||||
else
|
|
||||||
printf("\\x%-2x", (unsigned char)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_id);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
97
src/fs.h
97
src/fs.h
@ -1,97 +0,0 @@
|
|||||||
#ifndef FS_H
|
|
||||||
#define FS_H
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
constexpr size_t block_size = 512;
|
|
||||||
typedef uint16_t Block[block_size];
|
|
||||||
constexpr size_t size_of_block = sizeof(Block);
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
Block *blocks;
|
|
||||||
size_t blocks_count;
|
|
||||||
} Disk;
|
|
||||||
|
|
||||||
constexpr size_t inode_size = 16;
|
|
||||||
constexpr size_t inode_blocks_cap = 12;
|
|
||||||
|
|
||||||
constexpr size_t inodes_per_block = block_size / inode_size;
|
|
||||||
constexpr size_t inodeblocks_cap = 32;
|
|
||||||
constexpr size_t inodes_cap = inodeblocks_cap * inodes_per_block;
|
|
||||||
constexpr size_t inodebitmap_size = inodes_cap / 16;
|
|
||||||
constexpr size_t inode_name_cap = 61;
|
|
||||||
|
|
||||||
typedef enum : uint16_t {
|
|
||||||
INode_File,
|
|
||||||
INode_Dir,
|
|
||||||
} INodeTy;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint16_t id;
|
|
||||||
uint16_t mode;
|
|
||||||
uint16_t blocks_count;
|
|
||||||
uint16_t last_block_size;
|
|
||||||
uint16_t blocks[inode_blocks_cap];
|
|
||||||
} INode;
|
|
||||||
|
|
||||||
static_assert(sizeof(INode) / sizeof(uint16_t) == inode_size);
|
|
||||||
|
|
||||||
constexpr uint16_t superblock_magic = 0x5a5a;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint16_t magic;
|
|
||||||
uint16_t blocks_count;
|
|
||||||
uint16_t inodeblocks_count;
|
|
||||||
uint16_t inodeblocks[inodeblocks_cap];
|
|
||||||
uint16_t inodebitmap[inodebitmap_size];
|
|
||||||
} Superblock;
|
|
||||||
|
|
||||||
static_assert(sizeof(Superblock) <= sizeof(Block));
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
Disk *disk;
|
|
||||||
uint16_t block_offset;
|
|
||||||
uint16_t blocks_count;
|
|
||||||
Block *superblock;
|
|
||||||
Superblock *sb;
|
|
||||||
Block *block_bitmap;
|
|
||||||
bool out_of_sync;
|
|
||||||
} Fs;
|
|
||||||
|
|
||||||
void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count);
|
|
||||||
void fs_load(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count);
|
|
||||||
void fs_destroy(Fs *fs);
|
|
||||||
void fs_sync(Fs *fs);
|
|
||||||
void fs_root(Fs *fs, INode *out_inode);
|
|
||||||
void fs_inode(Fs *fs, INode *out_inode, uint16_t inode_id);
|
|
||||||
void fs_make_dir(Fs *fs, INode *out_inode, INode *parent, const char *name);
|
|
||||||
void fs_make_file(Fs *fs, INode *out_inode, INode *parent, const char *name);
|
|
||||||
void fs_remove(Fs *fs, INode *inode, INode *parent);
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint16_t inode;
|
|
||||||
char name[inode_name_cap + 1];
|
|
||||||
} FsListEntry;
|
|
||||||
|
|
||||||
size_t fs_list(
|
|
||||||
Fs *fs, FsListEntry *entries, size_t entries_cap, const INode *inode);
|
|
||||||
|
|
||||||
uint16_t fs_write(
|
|
||||||
Fs *fs, INode *inode, const uint16_t *buffer, uint16_t buffer_size);
|
|
||||||
uint16_t fs_read(Fs *fs,
|
|
||||||
INode *inode,
|
|
||||||
uint16_t *buffer,
|
|
||||||
uint16_t buffer_size,
|
|
||||||
uint16_t offset);
|
|
||||||
|
|
||||||
void fs_dump_info(Fs *fs);
|
|
||||||
void fs_dump_superblock(Fs *fs);
|
|
||||||
void fs_dump_block_bitmap(Fs *fs);
|
|
||||||
void fs_dump_inode(Fs *fs, uint16_t inode_id);
|
|
||||||
void fs_dump_block_hex(Fs *fs, uint16_t block_id);
|
|
||||||
void fs_dump_block_dec(Fs *fs, uint16_t block_id);
|
|
||||||
void fs_dump_block_char(Fs *fs, uint16_t block_id);
|
|
||||||
void fs_dump_direntry_block(Fs *fs, uint16_t block_id);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
193
src/fscli.c
193
src/fscli.c
@ -1,193 +0,0 @@
|
|||||||
#include "fs.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
(void)argc, (void)argv;
|
|
||||||
const char *filename = nullptr;
|
|
||||||
if (argc >= 2)
|
|
||||||
filename = argv[1];
|
|
||||||
|
|
||||||
constexpr size_t amount_of_blocks = 128;
|
|
||||||
|
|
||||||
Disk disk = {
|
|
||||||
.blocks = calloc(amount_of_blocks, sizeof(Block)),
|
|
||||||
.blocks_count = amount_of_blocks,
|
|
||||||
};
|
|
||||||
|
|
||||||
Fs fs_val;
|
|
||||||
Fs *fs = &fs_val;
|
|
||||||
|
|
||||||
if (filename) {
|
|
||||||
FILE *file = fopen(filename, "r");
|
|
||||||
assert(file);
|
|
||||||
|
|
||||||
fread(
|
|
||||||
disk.blocks, sizeof(uint16_t), block_size * amount_of_blocks, file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
fs_load(fs, &disk, 0, amount_of_blocks);
|
|
||||||
} else {
|
|
||||||
fs_init(fs, &disk, 0, amount_of_blocks);
|
|
||||||
fs_sync(fs);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
char command[32];
|
|
||||||
printf("> ");
|
|
||||||
if (scanf(" %31s", command) != 1)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (strcmp(command, "q") == 0) {
|
|
||||||
break;
|
|
||||||
} else if (strcmp(command, "h") == 0) {
|
|
||||||
printf("h help\n"
|
|
||||||
"fs print fs info\n"
|
|
||||||
"sb print superblock\n"
|
|
||||||
"bm print block bitmap\n"
|
|
||||||
"in i print inode[i]\n"
|
|
||||||
"bh i print block[i] as hex\n"
|
|
||||||
"bd i print block[i] as decimals\n"
|
|
||||||
"bc i print block[i] as chars\n"
|
|
||||||
"deb i print block[i] as dir entry\n"
|
|
||||||
"root print root inode id\n"
|
|
||||||
"mkdir i n make dir with name n in inode[i]\n"
|
|
||||||
"touch i n make file with name n in inode[i]\n"
|
|
||||||
"rm p i remove inode[i] from dir inode[p]\n"
|
|
||||||
"ls i list entries in inode[i]\n"
|
|
||||||
"w i write to inode[i]\n"
|
|
||||||
"r i read from inode[i]\n"
|
|
||||||
"save save to disk file\n"
|
|
||||||
"");
|
|
||||||
} else if (strcmp(command, "fs") == 0) {
|
|
||||||
fs_dump_info(fs);
|
|
||||||
} else if (strcmp(command, "sb") == 0) {
|
|
||||||
fs_dump_superblock(fs);
|
|
||||||
} else if (strcmp(command, "bm") == 0) {
|
|
||||||
fs_dump_block_bitmap(fs);
|
|
||||||
} else if (strcmp(command, "in") == 0) {
|
|
||||||
uint16_t inode_id;
|
|
||||||
if (scanf(" %hu", &inode_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
fs_dump_inode(fs, inode_id);
|
|
||||||
} else if (strcmp(command, "bh") == 0) {
|
|
||||||
uint16_t block_id;
|
|
||||||
if (scanf(" %hu", &block_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
fs_dump_block_hex(fs, block_id);
|
|
||||||
} else if (strcmp(command, "bd") == 0) {
|
|
||||||
uint16_t block_id;
|
|
||||||
if (scanf(" %hu", &block_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
fs_dump_block_dec(fs, block_id);
|
|
||||||
} else if (strcmp(command, "bc") == 0) {
|
|
||||||
uint16_t block_id;
|
|
||||||
if (scanf(" %hu", &block_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
fs_dump_block_char(fs, block_id);
|
|
||||||
} else if (strcmp(command, "deb") == 0) {
|
|
||||||
uint16_t block_id;
|
|
||||||
if (scanf(" %hu", &block_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
fs_dump_direntry_block(fs, block_id);
|
|
||||||
} else if (strcmp(command, "root") == 0) {
|
|
||||||
INode root;
|
|
||||||
fs_root(fs, &root);
|
|
||||||
printf("id = %u\n", root.id);
|
|
||||||
} else if (strcmp(command, "mkdir") == 0) {
|
|
||||||
uint16_t parent_id;
|
|
||||||
char name[62];
|
|
||||||
if (scanf(" %hu %61s", &parent_id, name) != 2)
|
|
||||||
goto malformed_command;
|
|
||||||
INode parent;
|
|
||||||
fs_inode(fs, &parent, parent_id);
|
|
||||||
INode inode;
|
|
||||||
fs_make_dir(fs, &inode, &parent, name);
|
|
||||||
fs_sync(fs);
|
|
||||||
printf("id = %u\n", inode.id);
|
|
||||||
} else if (strcmp(command, "touch") == 0) {
|
|
||||||
uint16_t parent_id;
|
|
||||||
char name[62];
|
|
||||||
if (scanf(" %hu %61s", &parent_id, name) != 2)
|
|
||||||
goto malformed_command;
|
|
||||||
INode parent;
|
|
||||||
fs_inode(fs, &parent, parent_id);
|
|
||||||
INode inode;
|
|
||||||
fs_make_file(fs, &inode, &parent, name);
|
|
||||||
fs_sync(fs);
|
|
||||||
printf("id = %u\n", inode.id);
|
|
||||||
} else if (strcmp(command, "rm") == 0) {
|
|
||||||
uint16_t parent_id;
|
|
||||||
uint16_t inode_id;
|
|
||||||
if (scanf(" %hu %hu", &parent_id, &inode_id) != 2)
|
|
||||||
goto malformed_command;
|
|
||||||
INode parent;
|
|
||||||
fs_inode(fs, &parent, parent_id);
|
|
||||||
INode inode;
|
|
||||||
fs_inode(fs, &inode, inode_id);
|
|
||||||
fs_remove(fs, &inode, &parent);
|
|
||||||
fs_sync(fs);
|
|
||||||
} else if (strcmp(command, "ls") == 0) {
|
|
||||||
uint16_t inode_id;
|
|
||||||
if (scanf(" %hu", &inode_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
INode inode;
|
|
||||||
fs_inode(fs, &inode, inode_id);
|
|
||||||
FsListEntry entries[32];
|
|
||||||
size_t entries_size = fs_list(
|
|
||||||
fs, entries, sizeof(entries) / sizeof(entries[0]), &inode);
|
|
||||||
fs_sync(fs);
|
|
||||||
for (size_t i = 0; i < entries_size; ++i)
|
|
||||||
printf("%2u %s\n", entries[i].inode, entries[i].name);
|
|
||||||
} else if (strcmp(command, "w") == 0) {
|
|
||||||
uint16_t inode_id;
|
|
||||||
if (scanf(" %hu", &inode_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
INode inode;
|
|
||||||
fs_inode(fs, &inode, inode_id);
|
|
||||||
uint16_t buffer[64] = { 0 };
|
|
||||||
size_t i = 0;
|
|
||||||
fgetc(stdin);
|
|
||||||
int ch;
|
|
||||||
while ((ch = fgetc(stdin)) != EOF)
|
|
||||||
buffer[i++] = ch;
|
|
||||||
clearerr(stdin);
|
|
||||||
fs_write(fs, &inode, buffer, i);
|
|
||||||
fs_sync(fs);
|
|
||||||
} else if (strcmp(command, "r") == 0) {
|
|
||||||
uint16_t inode_id;
|
|
||||||
if (scanf(" %hu", &inode_id) != 1)
|
|
||||||
goto malformed_command;
|
|
||||||
INode inode;
|
|
||||||
fs_inode(fs, &inode, inode_id);
|
|
||||||
uint16_t *buffer = calloc(65535, sizeof(uint16_t));
|
|
||||||
size_t buffer_size = fs_read(fs, &inode, buffer, 65535, 0);
|
|
||||||
printf("buffer_size = %zu\n", buffer_size);
|
|
||||||
for (size_t i = 0; i < buffer_size; ++i)
|
|
||||||
fputc(buffer[i], stdout);
|
|
||||||
free(buffer);
|
|
||||||
} else if (strcmp(command, "save") == 0) {
|
|
||||||
fs_sync(fs);
|
|
||||||
FILE *file = fopen(filename, "w");
|
|
||||||
assert(file);
|
|
||||||
size_t words_written = fwrite(disk.blocks,
|
|
||||||
sizeof(uint16_t),
|
|
||||||
block_size * amount_of_blocks,
|
|
||||||
file);
|
|
||||||
fclose(file);
|
|
||||||
printf("wrote %zu words to %s\n", words_written, filename);
|
|
||||||
} else {
|
|
||||||
printf("error: unknown command '%s'\n", command);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
malformed_command:
|
|
||||||
printf("error: malformed '%s' command\n", command);
|
|
||||||
}
|
|
||||||
|
|
||||||
fs_destroy(fs);
|
|
||||||
free(disk.blocks);
|
|
||||||
}
|
|
||||||
287
src/fstool.c
287
src/fstool.c
@ -1,287 +0,0 @@
|
|||||||
#include "fs.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
constexpr size_t amount_of_blocks = 128;
|
|
||||||
|
|
||||||
static void load(Fs *fs, Disk *disk, const char *filename)
|
|
||||||
{
|
|
||||||
*disk = (Disk) {
|
|
||||||
.blocks = calloc(amount_of_blocks, sizeof(Block)),
|
|
||||||
.blocks_count = amount_of_blocks,
|
|
||||||
};
|
|
||||||
|
|
||||||
FILE *file = fopen(filename, "r");
|
|
||||||
if (file) {
|
|
||||||
fread(disk->blocks,
|
|
||||||
sizeof(uint16_t),
|
|
||||||
block_size * amount_of_blocks,
|
|
||||||
file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
fs_load(fs, disk, 0, amount_of_blocks);
|
|
||||||
} else {
|
|
||||||
printf(
|
|
||||||
"warning: could not find '%s'. one will be created.\n", filename);
|
|
||||||
fs_init(fs, disk, 0, amount_of_blocks);
|
|
||||||
fs_sync(fs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void save(Fs *fs, Disk *disk, const char *filename)
|
|
||||||
{
|
|
||||||
fs_sync(fs);
|
|
||||||
|
|
||||||
FILE *file = fopen(filename, "w");
|
|
||||||
assert(file);
|
|
||||||
size_t words_written = fwrite(
|
|
||||||
disk->blocks, sizeof(uint16_t), block_size * amount_of_blocks, file);
|
|
||||||
fclose(file);
|
|
||||||
printf("%zu words written\n", words_written);
|
|
||||||
|
|
||||||
fs_destroy(fs);
|
|
||||||
free(disk->blocks);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void close_without_saving(Fs *fs, Disk *disk)
|
|
||||||
{
|
|
||||||
fs_destroy(fs);
|
|
||||||
free(disk->blocks);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint16_t search_entries(
|
|
||||||
FsListEntry entries[], size_t entries_size, const char *name)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < entries_size; ++i)
|
|
||||||
if (strcmp(entries[i].name, name) == 0)
|
|
||||||
return entries[i].inode;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void resolve_path(Fs *fs, INode *inode, const char *path)
|
|
||||||
{
|
|
||||||
assert(path[0] == '/');
|
|
||||||
char *path_copy = strdup(path);
|
|
||||||
fs_root(fs, inode);
|
|
||||||
|
|
||||||
char *tok = strtok(path_copy, "/");
|
|
||||||
while (tok) {
|
|
||||||
constexpr size_t entries_cap = 16;
|
|
||||||
FsListEntry entries[entries_cap];
|
|
||||||
|
|
||||||
size_t entries_size = fs_list(fs, entries, entries_cap, inode);
|
|
||||||
assert(entries_size < entries_cap);
|
|
||||||
|
|
||||||
uint16_t inode_id = search_entries(entries, entries_size, tok);
|
|
||||||
assert(inode_id && "not found");
|
|
||||||
|
|
||||||
fs_inode(fs, inode, inode_id);
|
|
||||||
|
|
||||||
tok = strtok(nullptr, "/");
|
|
||||||
}
|
|
||||||
free(path_copy);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *help = ""
|
|
||||||
"h help\n"
|
|
||||||
"mkdir <path>/<name> create dir with name at path\n"
|
|
||||||
"mkfile <path>/<name> create file with name at path\n"
|
|
||||||
"ls <path> print contents of dir\n"
|
|
||||||
"import <path> <path> copy host file to fs\n"
|
|
||||||
"importb <path> <path> copy host file to fs, u8 -> u16\n"
|
|
||||||
"";
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
assert(argc >= 3);
|
|
||||||
const char *disk_filename = argv[1];
|
|
||||||
const char *cmd = argv[2];
|
|
||||||
|
|
||||||
Disk disk;
|
|
||||||
Fs fs_val, *fs = &fs_val;
|
|
||||||
|
|
||||||
if (strcmp(cmd, "h") == 0) {
|
|
||||||
printf("%s", help);
|
|
||||||
} else if (strcmp(cmd, "mkdir") == 0) {
|
|
||||||
if (argc < 4)
|
|
||||||
goto malformed_command;
|
|
||||||
const char *path = argv[3];
|
|
||||||
|
|
||||||
load(fs, &disk, disk_filename);
|
|
||||||
|
|
||||||
char *path_copy = strdup(path);
|
|
||||||
char *name = strrchr(path_copy, '/');
|
|
||||||
*name = '\0';
|
|
||||||
name += 1;
|
|
||||||
|
|
||||||
INode parent;
|
|
||||||
if (!path_copy[0]) {
|
|
||||||
fs_root(fs, &parent);
|
|
||||||
} else {
|
|
||||||
resolve_path(fs, &parent, path_copy);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr size_t entries_cap = 16;
|
|
||||||
FsListEntry entries[entries_cap];
|
|
||||||
|
|
||||||
size_t entries_size = fs_list(fs, entries, entries_cap, &parent);
|
|
||||||
assert(entries_size < entries_cap);
|
|
||||||
|
|
||||||
uint16_t inode_id = search_entries(entries, entries_size, name);
|
|
||||||
if (inode_id) {
|
|
||||||
printf("folder %s already exists in %s\n",
|
|
||||||
name,
|
|
||||||
*path_copy ? path_copy : "/");
|
|
||||||
free(path_copy);
|
|
||||||
close_without_saving(fs, &disk);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("creating directory '%s' in %d\n", name, parent.id);
|
|
||||||
INode inode;
|
|
||||||
fs_make_dir(fs, &inode, &parent, name);
|
|
||||||
|
|
||||||
free(path_copy);
|
|
||||||
save(fs, &disk, disk_filename);
|
|
||||||
} else if (strcmp(cmd, "mkfile") == 0) {
|
|
||||||
if (argc < 4)
|
|
||||||
goto malformed_command;
|
|
||||||
const char *path = argv[3];
|
|
||||||
|
|
||||||
load(fs, &disk, disk_filename);
|
|
||||||
|
|
||||||
char *path_copy = strdup(path);
|
|
||||||
char *name = strrchr(path_copy, '/');
|
|
||||||
*name = '\0';
|
|
||||||
name += 1;
|
|
||||||
|
|
||||||
INode parent;
|
|
||||||
if (!path_copy[0]) {
|
|
||||||
fs_root(fs, &parent);
|
|
||||||
} else {
|
|
||||||
resolve_path(fs, &parent, path_copy);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr size_t entries_cap = 16;
|
|
||||||
FsListEntry entries[entries_cap];
|
|
||||||
|
|
||||||
size_t entries_size = fs_list(fs, entries, entries_cap, &parent);
|
|
||||||
assert(entries_size < entries_cap);
|
|
||||||
|
|
||||||
uint16_t inode_id = search_entries(entries, entries_size, name);
|
|
||||||
if (inode_id) {
|
|
||||||
printf("file %s already exists in %s\n",
|
|
||||||
name,
|
|
||||||
*path_copy ? path_copy : "/");
|
|
||||||
free(path_copy);
|
|
||||||
close_without_saving(fs, &disk);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("creating file %s in %s\n", name, *path_copy ? path_copy : "/");
|
|
||||||
INode inode;
|
|
||||||
fs_make_file(fs, &inode, &parent, name);
|
|
||||||
|
|
||||||
free(path_copy);
|
|
||||||
save(fs, &disk, disk_filename);
|
|
||||||
} else if (strcmp(cmd, "ls") == 0) {
|
|
||||||
if (argc < 4)
|
|
||||||
goto malformed_command;
|
|
||||||
const char *path = argv[3];
|
|
||||||
|
|
||||||
load(fs, &disk, disk_filename);
|
|
||||||
|
|
||||||
INode parent;
|
|
||||||
resolve_path(fs, &parent, path);
|
|
||||||
|
|
||||||
constexpr size_t entries_cap = 16;
|
|
||||||
FsListEntry entries[entries_cap];
|
|
||||||
|
|
||||||
size_t entries_size = fs_list(fs, entries, entries_cap, &parent);
|
|
||||||
for (size_t i = 0; i < entries_size; ++i) {
|
|
||||||
INode inode;
|
|
||||||
fs_inode(fs, &inode, entries[i].inode);
|
|
||||||
bool is_dir = i >= 2 && inode.mode == INode_Dir;
|
|
||||||
printf("%s%s\n", entries[i].name, is_dir ? "/" : "");
|
|
||||||
}
|
|
||||||
|
|
||||||
close_without_saving(fs, &disk);
|
|
||||||
} else if (strcmp(cmd, "import") == 0) {
|
|
||||||
if (argc < 5)
|
|
||||||
goto malformed_command;
|
|
||||||
const char *rpath = argv[3];
|
|
||||||
const char *vpath = argv[4];
|
|
||||||
|
|
||||||
load(fs, &disk, disk_filename);
|
|
||||||
|
|
||||||
INode inode;
|
|
||||||
resolve_path(fs, &inode, vpath);
|
|
||||||
|
|
||||||
FILE *file = fopen(rpath, "r");
|
|
||||||
assert(file);
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
long file_size = ftell(file);
|
|
||||||
assert(file_size > 0);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
size_t buffer_size = (file_size + 1) / 2;
|
|
||||||
uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t));
|
|
||||||
size_t words_read = fread(buffer, sizeof(uint16_t), buffer_size, file);
|
|
||||||
assert(words_read == buffer_size);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
fs_write(fs, &inode, buffer, buffer_size);
|
|
||||||
|
|
||||||
printf("imported %zu words to file %s\n", words_read, vpath);
|
|
||||||
free(buffer);
|
|
||||||
save(fs, &disk, disk_filename);
|
|
||||||
} else if (strcmp(cmd, "importb") == 0) {
|
|
||||||
if (argc < 5)
|
|
||||||
goto malformed_command;
|
|
||||||
const char *rpath = argv[3];
|
|
||||||
const char *vpath = argv[4];
|
|
||||||
|
|
||||||
load(fs, &disk, disk_filename);
|
|
||||||
|
|
||||||
INode inode;
|
|
||||||
resolve_path(fs, &inode, vpath);
|
|
||||||
|
|
||||||
FILE *file = fopen(rpath, "r");
|
|
||||||
assert(file);
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
long file_size = ftell(file);
|
|
||||||
assert(file_size > 0);
|
|
||||||
fseek(file, 0, SEEK_SET);
|
|
||||||
|
|
||||||
size_t buffer_size = file_size;
|
|
||||||
uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t));
|
|
||||||
size_t words_read = 0;
|
|
||||||
|
|
||||||
int ch;
|
|
||||||
while ((ch = fgetc(file)) != EOF) {
|
|
||||||
buffer[words_read] = ch;
|
|
||||||
words_read += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(words_read == buffer_size);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
fs_write(fs, &inode, buffer, buffer_size);
|
|
||||||
|
|
||||||
printf("imported %zu words to file %s\n", words_read, vpath);
|
|
||||||
free(buffer);
|
|
||||||
save(fs, &disk, disk_filename);
|
|
||||||
} else {
|
|
||||||
printf("error: uknown command '%s'\n", cmd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
malformed_command:
|
|
||||||
printf("error: malformed %s command\n", cmd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user