fix fs generate disk
This commit is contained in:
parent
07d3d93839
commit
2bddf34798
@ -29,6 +29,7 @@ block()
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${fs_file}
|
||||
COMMAND fs -d ${fs_file} --init -c quit
|
||||
COMMAND fs -d ${fs_file} -c importw2w ${kernel_file} /kernel.bin
|
||||
COMMAND fs -d ${fs_file} -c mkdir /docs
|
||||
COMMAND fs -d ${fs_file} -c importb2w ${CMAKE_CURRENT_SOURCE_DIR}/readme.txt /docs/readme.txt
|
||||
|
||||
98
src/fs.c
98
src/fs.c
@ -283,7 +283,7 @@ static void make_dir_inode(
|
||||
};
|
||||
|
||||
uint16_t entry_block_id = inode_allocate_block(fs, out_inode);
|
||||
load_direntry_block(fs, entry_block_id);
|
||||
fs->current_direntry_block_id = entry_block_id;
|
||||
memset(fs->direntry_block, 0, fs_block_size * sizeof(word));
|
||||
memcpy(fs->direntry_block, &self_entry, self_entry_size * sizeof(word));
|
||||
memcpy(&fs->direntry_block[self_entry_size],
|
||||
@ -713,27 +713,34 @@ iword fs_write(Fs *fs, FileCursor *cursor, const word *buffer, word count)
|
||||
load_ind_block(fs, inode->blocks[fs_inode_ind_block_idx]);
|
||||
|
||||
word words_written = 0;
|
||||
word block_id;
|
||||
|
||||
while (words_written < count) {
|
||||
|
||||
word block_id;
|
||||
if (cursor->current_block_idx >= inode->blocks_count)
|
||||
block_id = inode_allocate_block(fs, inode);
|
||||
else
|
||||
block_id = *inode_block_at(
|
||||
inode, cursor->current_block_idx, fs->ind_block);
|
||||
|
||||
load_data_block(fs, block_id);
|
||||
if (cursor->current_offset != 0)
|
||||
load_data_block(fs, block_id);
|
||||
else
|
||||
memset(fs->data_block, 0, fs_block_size * sizeof(word));
|
||||
|
||||
word block_write_size = word_min(
|
||||
fs_block_size - cursor->current_offset, count - words_written);
|
||||
|
||||
memcpy(fs->data_block, buffer, block_write_size * sizeof(word));
|
||||
memcpy(&fs->data_block[cursor->current_offset],
|
||||
buffer,
|
||||
block_write_size * sizeof(word));
|
||||
|
||||
words_written += block_write_size;
|
||||
cursor->current_offset += block_write_size;
|
||||
|
||||
if (cursor->current_offset >= fs_block_size) {
|
||||
write_block(fs, fs->data_block, block_id);
|
||||
|
||||
cursor->current_block_idx += 1;
|
||||
cursor->current_offset = 0;
|
||||
}
|
||||
@ -742,6 +749,9 @@ iword fs_write(Fs *fs, FileCursor *cursor, const word *buffer, word count)
|
||||
if (cursor->current_block_idx == inode->blocks_count - 1)
|
||||
inode->last_block_size = cursor->current_offset;
|
||||
|
||||
if (cursor->current_offset > 0)
|
||||
write_block(fs, fs->data_block, block_id);
|
||||
|
||||
write_inode(fs, inode);
|
||||
|
||||
return words_written;
|
||||
@ -773,6 +783,10 @@ void fs_mkfs(Fs *fs, Disk *disk, word block_offset)
|
||||
init_fs(fs, disk, block_offset);
|
||||
fs->sb->magic = fs_superblock_magic;
|
||||
|
||||
// Make sure the 4 first blocks exists.
|
||||
for (word i = 0; i < 4; ++i)
|
||||
write_block(fs, fs->data_block, i);
|
||||
|
||||
bitmap_set(fs->block_bitmap, 0);
|
||||
bitmap_set(fs->block_bitmap, 1);
|
||||
|
||||
@ -997,11 +1011,12 @@ static int do_mkdir(Shell *s)
|
||||
word result = fs_mkdir(s->fs, &inode, &parent, name);
|
||||
if (result == 2) {
|
||||
printf("already exists\n");
|
||||
return 0;
|
||||
} else if (result != 0) {
|
||||
printf("error occured (status %d)\n", result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fs_sync(s->fs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1015,12 +1030,15 @@ static int do_rmdir(Shell *s)
|
||||
INode inode = *s->cwd;
|
||||
if (resolve_path(s->fs, &inode, &s->argv[1], nullptr) != 0) {
|
||||
printf("not found\n");
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
word result = fs_rmdir(s->fs, &inode);
|
||||
if (result != 0)
|
||||
if (result != 0) {
|
||||
printf("error occured\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1087,7 +1105,6 @@ static int do_edit(Shell *s)
|
||||
fs_close(s->fs, &cursor);
|
||||
|
||||
printf("\n-- wrote %d words --\n", words_written);
|
||||
|
||||
return 0;
|
||||
leave_error:
|
||||
printf("error occured (status %d)\n", status);
|
||||
@ -1096,20 +1113,18 @@ leave_error:
|
||||
|
||||
static int do_cat(Shell *s)
|
||||
{
|
||||
INode inode = *s->cwd;
|
||||
bool is_dir;
|
||||
if (s->argc < 2) {
|
||||
printf("expected path\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
INode inode = *s->cwd;
|
||||
bool is_dir;
|
||||
if (resolve_path(s->fs, &inode, &s->argv[1], &is_dir) != 0 || is_dir) {
|
||||
} else if (resolve_path(s->fs, &inode, &s->argv[1], &is_dir) != 0
|
||||
|| is_dir) {
|
||||
printf("invalid path\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int status;
|
||||
|
||||
FileCursor cursor;
|
||||
if ((status = fs_open(s->fs, &cursor, &inode, File_Read)) != 0)
|
||||
goto leave_error;
|
||||
@ -1118,7 +1133,6 @@ static int do_cat(Shell *s)
|
||||
word buffer[buffer_cap] = { };
|
||||
|
||||
int words_read = 0;
|
||||
|
||||
int read;
|
||||
while ((read = fs_read(s->fs, &cursor, buffer, buffer_cap)) > 0) {
|
||||
for (int i = 0; i < read; ++i)
|
||||
@ -1139,6 +1153,50 @@ leave_error:
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int do_hexdump(Shell *s)
|
||||
{
|
||||
INode inode = *s->cwd;
|
||||
bool is_dir;
|
||||
if (s->argc < 2) {
|
||||
printf("expected path\n");
|
||||
return 1;
|
||||
} else if (resolve_path(s->fs, &inode, &s->argv[1], &is_dir) != 0
|
||||
|| is_dir) {
|
||||
printf("invalid path\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int status;
|
||||
FileCursor cursor;
|
||||
if ((status = fs_open(s->fs, &cursor, &inode, File_Read)) != 0)
|
||||
goto leave_error;
|
||||
|
||||
constexpr size_t buffer_cap = 8;
|
||||
word buffer[buffer_cap] = { };
|
||||
|
||||
int words_read = 0;
|
||||
int read;
|
||||
while ((read = fs_read(s->fs, &cursor, buffer, buffer_cap)) > 0) {
|
||||
printf("%07x", (words_read & ~7) << 1);
|
||||
for (int i = 0; i < read; ++i)
|
||||
printf(" %04x", buffer[i]);
|
||||
printf("\n");
|
||||
words_read += read;
|
||||
}
|
||||
if (read < 0) {
|
||||
status = read;
|
||||
goto leave_error;
|
||||
}
|
||||
|
||||
fs_close(s->fs, &cursor);
|
||||
|
||||
printf("\n-- read %d words --\n", words_read);
|
||||
return 0;
|
||||
leave_error:
|
||||
printf("error occured (status %d)\n", status);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int do_importw2w(Shell *s)
|
||||
{
|
||||
if (s->argc < 3) {
|
||||
@ -1258,7 +1316,7 @@ static int run_command(Shell *s)
|
||||
size_t cmd_len = strlen(cmd);
|
||||
|
||||
if (strcmp(cmd, "exit") == 0 || strncmp(cmd, "quit", cmd_len) == 0) {
|
||||
return 2;
|
||||
return 1000;
|
||||
} else if (strcmp(cmd, "ls") == 0) {
|
||||
return do_ls(s);
|
||||
} else if (strcmp(cmd, "mkdir") == 0) {
|
||||
@ -1269,6 +1327,8 @@ static int run_command(Shell *s)
|
||||
return do_edit(s);
|
||||
} else if (strcmp(cmd, "cat") == 0) {
|
||||
return do_cat(s);
|
||||
} else if (strcmp(cmd, "hexdump") == 0) {
|
||||
return do_hexdump(s);
|
||||
} else if (strcmp(cmd, "importw2w") == 0) {
|
||||
return do_importw2w(s);
|
||||
} else if (strcmp(cmd, "importb2w") == 0) {
|
||||
@ -1308,7 +1368,7 @@ static void run_shell(Fs *fs)
|
||||
argc += 1;
|
||||
|
||||
Shell shell = { fs, &cwd, argv, argc };
|
||||
if (run_command(&shell) == 2)
|
||||
if (run_command(&shell) == 1000)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -1416,7 +1476,7 @@ int main(int argc, char *argv[])
|
||||
size_t blocks_count = 128;
|
||||
disk_impl.mem_disk = (MemDisk) {
|
||||
.disk = { mem_disk_read_block, mem_disk_write_block },
|
||||
.blocks = calloc(blocks_count, sizeof(word) * fs_block_size),
|
||||
.blocks = calloc(blocks_count, fs_block_size * sizeof(word)),
|
||||
.blocks_count = blocks_count,
|
||||
};
|
||||
|
||||
@ -1431,6 +1491,8 @@ int main(int argc, char *argv[])
|
||||
fs_root(&fs, &cwd);
|
||||
Shell shell = { &fs, &cwd, &argv[cmd_off], argc - cmd_off };
|
||||
status = run_command(&shell);
|
||||
if (status == 1000)
|
||||
status = 0;
|
||||
} else {
|
||||
run_shell(&fs);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user