Compare commits

..

No commits in common. "d68b874b2417c21253205cde910e664223a86d0d" and "941c51200c9bbcdc0fd60041a1656643f72d1d18" have entirely different histories.

3 changed files with 64 additions and 209 deletions

View File

@ -1,7 +1,5 @@
#include "config.h" #include "config.h"
#define FOLLOW_LAST_SYMLINK 0x1
enum fs_filetype { enum fs_filetype {
REGULAR, REGULAR,
DIRECTORY DIRECTORY
@ -71,7 +69,6 @@ int fs_read(void *d);
int fs_write(void *d); int fs_write(void *d);
int fs_close(void *d); int fs_close(void *d);
int fs_mkdir(void *d); int fs_mkdir(void *d);
int fs_rmdir(void *d);
int fs_cd(void *d); int fs_cd(void *d);
int fs_truncate(void *d); int fs_truncate(void *d);
int fs_allow_write(void *d); int fs_allow_write(void *d);

View File

@ -27,7 +27,6 @@ static const struct CliCommandEntry cmd[] = {
{"close", 1, (enum CliArgType[]) {INT}, fs_close}, {"close", 1, (enum CliArgType[]) {INT}, fs_close},
{"mkdir", 1, (enum CliArgType[]) {STR}, fs_mkdir}, {"mkdir", 1, (enum CliArgType[]) {STR}, fs_mkdir},
{"rmdir", 1, (enum CliArgType[]) {STR}, fs_rmdir},
{"cd", 1, (enum CliArgType[]) {STR}, fs_cd}, {"cd", 1, (enum CliArgType[]) {STR}, fs_cd},
// custom commands // custom commands

265
src/fs.c
View File

@ -790,13 +790,13 @@ static void fs_remove_fname_from_directory_removal(struct fs_directory_record *r
// decrement ref_count // decrement ref_count
struct fs_inode f; struct fs_inode f;
read_block(read_inode_ptr(recs[k].inode_no), &f); read_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
f.ref_count--; f.ref_count--;
write_block(read_inode_ptr(recs[k].inode_no), &f); write_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
// clear directory record inode_ptr // clear directory record inode_ptr
recs[k].fname[0] = 0; recs[k].fname[0] = 0;
write_block(dir_block_addr, recs); write_block(dir_block_addr, (void *) recs);
// if it drops to zero, nullify inode_ptr pointing to this inode // if it drops to zero, nullify inode_ptr pointing to this inode
if (f.ref_count) if (f.ref_count)
@ -824,8 +824,11 @@ static void fs_remove_fname_from_directory_removal(struct fs_directory_record *r
static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fname) static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fname)
{ {
// find directory record with this fname
int dir_inode = read_inode_ptr(fs_cwd_inode_ptr);
struct fs_inode dir; struct fs_inode dir;
read_block(read_inode_ptr(dir_inode_ptr), &dir); read_block(dir_inode, (void *) &dir);
// list entries from base inode // list entries from base inode
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) { for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
@ -834,7 +837,7 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
if (!dir.blocks[i]) if (!dir.blocks[i])
continue; continue;
read_block(dir.blocks[i], &recs); read_block(dir.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].fname) if (!recs[k].fname)
@ -898,7 +901,7 @@ fs_remove_fname_from_directory_finish:
return 0; return 0;
} }
static void resolve_path(struct resolved_path *rp, char *path, unsigned int params) static void resolve_directory(struct resolved_path *rp, char *path)
{ {
rp->parent_inode_ptr = -1; rp->parent_inode_ptr = -1;
rp->target_inode_ptr = -1; rp->target_inode_ptr = -1;
@ -910,52 +913,44 @@ static void resolve_path(struct resolved_path *rp, char *path, unsigned int para
return; return;
} }
if (!strcmp(path, "/")) { char current_dir_name[FS_MAX_FNAME_LEN+1] = {};
rp->parent_inode_ptr = 0; int current_dir_inode_ptr;
rp->target_inode_ptr = 0;
strcpy(rp->parent_fname, "/");
strcpy(rp->target_fname, "/");
return;
}
char current_fname[FS_MAX_FNAME_LEN+1] = {};
int current_inode_ptr;
int i; int i;
if (path[0] == '/') { if (path[0] == '/') {
current_inode_ptr = 0; current_dir_inode_ptr = 0;
rp->parent_inode_ptr = 0; rp->parent_inode_ptr = 0;
strcpy(current_fname, "/"); strcpy(current_dir_name, "/");
i = 1; i = 1;
} else { } else {
current_inode_ptr = fs_cwd_inode_ptr; current_dir_inode_ptr = fs_cwd_inode_ptr;
rp->parent_inode_ptr = fs_cwd_inode_ptr; rp->parent_inode_ptr = fs_cwd_inode_ptr;
extract_basename(fs_cwd, current_fname); extract_basename(fs_cwd, current_dir_name);
i = 0; i = 0;
} }
char next_fname[FS_MAX_FNAME_LEN+1] = {}; char next_dir_name[FS_MAX_FNAME_LEN+1] = {};
int next_fname_ptr = 0; int next_dir_name_ptr = 0;
for ( ; i < path_len; i++) { for ( ; i < path_len; i++) {
if (path[i] != '/') { if (path[i] != '/') {
next_fname[next_fname_ptr] = path[i]; next_dir_name[next_dir_name_ptr] = path[i];
next_fname_ptr++; next_dir_name_ptr++;
continue; continue;
} }
// path[i] == '/', trying to change current directory // path[i] == '/', trying to change current directory
if (!next_fname_ptr) if (!next_dir_name_ptr)
continue; continue;
if (i+1 == path_len) if (i+1 == path_len)
continue; continue;
{ {
int res = find_fname_in_directory(current_inode_ptr, next_fname); int res = find_fname_in_directory(current_dir_inode_ptr, next_dir_name);
if (res < 0) { if (res < 0) {
pr_warn("directory '%s' does not exist\n", next_fname); pr_warn("directory '%s' does not exist\n", next_dir_name);
rp->parent_inode_ptr = -1; rp->parent_inode_ptr = -1;
break; break;
} }
@ -965,20 +960,20 @@ static void resolve_path(struct resolved_path *rp, char *path, unsigned int para
read_block(read_inode_ptr(res), &d); read_block(read_inode_ptr(res), &d);
if (d.ftype == REGULAR) { if (d.ftype == REGULAR) {
pr_warn("'%s' is a regular file\n", next_fname); pr_warn("'%s' is a regular file\n", next_dir_name);
rp->parent_inode_ptr = -1; rp->parent_inode_ptr = -1;
break; break;
} }
} }
pr("Found directory '%s'\n", next_fname); pr("Found directory '%s'\n", next_dir_name);
current_inode_ptr = res; current_dir_inode_ptr = res;
rp->parent_inode_ptr = res; rp->parent_inode_ptr = res;
} }
next_fname_ptr = 0; next_dir_name_ptr = 0;
strcpy(current_fname, next_fname); strcpy(current_dir_name, next_dir_name);
memset(next_fname, 0, FS_MAX_FNAME_LEN); memset(next_dir_name, 0, FS_MAX_FNAME_LEN);
} }
if (rp->parent_inode_ptr < 0) { if (rp->parent_inode_ptr < 0) {
@ -986,16 +981,16 @@ static void resolve_path(struct resolved_path *rp, char *path, unsigned int para
return; return;
} }
strcpy(rp->parent_fname, current_fname); strcpy(rp->parent_fname, current_dir_name);
{ {
int res = find_fname_in_directory(current_inode_ptr, next_fname); int res = find_fname_in_directory(current_dir_inode_ptr, next_dir_name);
if (res < 0) { if (res < 0) {
pr_warn("target fname '%s' does not exist\n", next_fname); pr_warn("target fname '%s' does not exist\n", next_dir_name);
rp->target_inode_ptr = -1; rp->target_inode_ptr = -1;
} else { } else {
rp->target_inode_ptr = res; rp->target_inode_ptr = res;
strcpy(rp->target_fname, next_fname); strcpy(rp->target_fname, next_dir_name);
} }
} }
@ -1472,32 +1467,24 @@ int fs_create(void *d)
return 0; return 0;
} }
char *path = *((char **) d); char *fname = *((char **) d);
int fname_len = strlen(fname);
{ {
int path_len = strlen(path); // check if duplicate filename exists in current directory
if (path_len > FS_MAX_PATH_LEN) { int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname);
pr_err("path too long (%d > %d)\n", if (r) {
path_len, FS_MAX_PATH_LEN); free(r);
pr_err("filename '%s' already exists\n", fname);
return 0; return 0;
} }
} }
struct resolved_path rp; if (fname_len > FS_MAX_FNAME_LEN) {
resolve_path(&rp, path, 0); pr_err("filename too long (%d > %d)\n", fname_len, FS_MAX_FNAME_LEN);
if (rp.parent_inode_ptr < 0) {
pr_err("unable to create file: no such directory: '%s'\n", path);
return 0; return 0;
} }
if (rp.target_inode_ptr >= 0) {
pr_err("file already exists: '%s'\n", rp.target_fname);
return 0;
}
char fname[FS_MAX_FNAME_LEN+1];
extract_basename(path, fname);
pr("Creating regular file '%s'\n", fname); pr("Creating regular file '%s'\n", fname);
int inode_block_no = find_free_block(); int inode_block_no = find_free_block();
@ -1538,7 +1525,7 @@ int fs_create(void *d)
write_inode_ptr(inode_ptr_index, inode_block_no); write_inode_ptr(inode_ptr_index, inode_block_no);
// add filename to current directory inode // add filename to current directory inode
fs_add_fname_to_directory(rp.parent_inode_ptr, inode_ptr_index, fname); fs_add_fname_to_directory(fs_cwd_inode_ptr, inode_ptr_index, fname);
return 0; return 0;
} }
@ -1691,8 +1678,6 @@ static void extract_parent_directory_path(char *path, char *parent_path)
} else { } else {
strcpy(parent_path, "/"); strcpy(parent_path, "/");
} }
} }
int fs_mkdir(void *d) int fs_mkdir(void *d)
@ -1724,7 +1709,7 @@ int fs_mkdir(void *d)
} }
struct resolved_path rp; struct resolved_path rp;
resolve_path(&rp, dirpath, 0); resolve_directory(&rp, dirpath);
if (rp.parent_inode_ptr < 0) { if (rp.parent_inode_ptr < 0) {
pr_err("failed to find parent of '%s'\n", dirpath); pr_err("failed to find parent of '%s'\n", dirpath);
return 0; return 0;
@ -1769,114 +1754,6 @@ int fs_mkdir(void *d)
} }
static int count_active_directory_records(struct fs_inode *inode)
{
int total_active_records = 0;
// base inode
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
if (!inode->blocks[i])
continue;
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
read_block(inode->blocks[i], &recs);
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
if (!recs[j].fname[0])
continue;
if (!strcmp(recs[j].fname, "."))
continue;
if (!strcmp(recs[j].fname, ".."))
continue;
total_active_records++;
}
}
// inode extensions
unsigned int next_ext = inode->next_extension;
unsigned int curr_ext = 0;
struct fs_inode_extension ext;
while (next_ext) {
read_block(next_ext, &ext);
curr_ext = next_ext;
next_ext = ext.next_extension;
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
if (!ext.blocks[i])
continue;
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
read_block(ext.blocks[i], &recs);
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
if (!recs[j].fname[0])
continue;
if (!strcmp(recs[j].fname, "."))
continue;
if (!strcmp(recs[j].fname, ".."))
continue;
total_active_records++;
}
}
}
return total_active_records;
}
int fs_rmdir(void *d)
{
char *dirpath = *((char **)d);
int dirpath_len = strlen(dirpath);
if (dirpath_len > FS_MAX_PATH_LEN) {
pr_err("path too long ('%s', %d > %d)\n",
dirpath, dirpath_len, FS_MAX_FNAME_LEN);
return 0;
}
struct resolved_path rp;
resolve_path(&rp, dirpath, 0);
if (rp.parent_inode_ptr < 0) {
pr_err("failed to find parent directory of '%s'\n", dirpath);
return 0;
} else if (rp.target_inode_ptr < 0) {
pr_err("no such file: '%s'\n", dirpath);
return 0;
} else if (!strcmp(rp.target_fname, ".")) {
pr_err("can not remove '.' from a directory\n");
return 0;
}
{
struct fs_inode i;
read_block(read_inode_ptr(rp.target_inode_ptr), &i);
if (i.ftype != DIRECTORY) {
pr_err("'%s' is not a directory\n", rp.target_fname);
return 0;
}
if (count_active_directory_records(&i)) {
pr_err("directory '%s' is not empty\n", rp.target_fname);
return 0;
}
}
fs_remove_fname_from_directory(rp.parent_inode_ptr, rp.target_fname);
return 0;
}
static int get_fname_by_inode_ptr(int dir_inode_ptr, int file_inode_ptr, char *name) static int get_fname_by_inode_ptr(int dir_inode_ptr, int file_inode_ptr, char *name)
{ {
struct fs_inode dir; struct fs_inode dir;
@ -1992,7 +1869,7 @@ int fs_cd(void *d)
} }
struct resolved_path rp; struct resolved_path rp;
resolve_path(&rp, new_dir, FOLLOW_LAST_SYMLINK); resolve_directory(&rp, new_dir);
if (rp.parent_inode_ptr < 0) { if (rp.parent_inode_ptr < 0) {
pr_err("failed to find parent of '%s' from '%s'\n", new_dir, fs_cwd); pr_err("failed to find parent of '%s' from '%s'\n", new_dir, fs_cwd);
@ -2000,7 +1877,7 @@ int fs_cd(void *d)
} }
if (rp.target_inode_ptr < 0) { if (rp.target_inode_ptr < 0) {
pr_err("no such directory: '%s'\n", new_dir); pr_err("no fname '%s' in directory '%s'\n", rp.target_fname, rp.parent_fname);
return 0; return 0;
} }
@ -2029,36 +1906,12 @@ int fs_rm(void *d)
return 0; return 0;
} }
char *path = *((char **) d); char *fname = *((char **) d);
{
int path_len = strlen(path);
if (path_len > FS_MAX_PATH_LEN) {
pr_err("path too long (%d > %d)\n",
path_len, FS_MAX_PATH_LEN);
return 0;
}
}
struct resolved_path rp; if (fs_remove_fname_from_directory(fs_cwd_inode_ptr, fname) < 0) {
resolve_path(&rp, path, 0); pr_err("failed to unlink '%s'\n", fname);
if (rp.target_inode_ptr < 0) {
pr_err("file does not exist: '%s'\n", path);
return 0;
}
struct fs_inode i;
read_block(read_inode_ptr(rp.target_inode_ptr), &i);
if (i.ftype == DIRECTORY) {
pr_err("file '%s' is a directory\n", rp.target_fname);
return 0;
}
if (fs_remove_fname_from_directory(rp.parent_inode_ptr, rp.target_fname) < 0) {
pr_err("failed to unlink '%s'\n", rp.target_fname);
} else { } else {
pr("Unlinked '%s'\n", rp.target_fname); pr("Unlinked '%s'\n", fname);
} }
return 0; return 0;
@ -2085,7 +1938,7 @@ int fs_ls(void *d)
read_block(dir.blocks[i], (void *) &recs); read_block(dir.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].fname[0]) if (!recs[k].fname)
continue; continue;
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no); pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
@ -2108,7 +1961,7 @@ int fs_ls(void *d)
read_block(ext.blocks[i], (void *) &recs); read_block(ext.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].fname[0]) if (!recs[k].fname)
continue; continue;
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no); pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
@ -2143,7 +1996,7 @@ int fs_la(void *d)
read_block(dir.blocks[i], (void *) &recs); read_block(dir.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].fname[0]) if (!recs[k].fname)
continue; continue;
struct fs_inode f_inode; struct fs_inode f_inode;
@ -2178,7 +2031,7 @@ int fs_la(void *d)
read_block(ext.blocks[i], (void *) &recs); read_block(ext.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].fname[0]) if (!recs[k].fname)
continue; continue;
struct fs_inode f_inode; struct fs_inode f_inode;
@ -2205,12 +2058,18 @@ int fs_stat(void *d)
{ {
char *fname = *((char **) d); char *fname = *((char **) d);
int file_inode_ptr = find_fname_in_directory(fs_cwd_inode_ptr, fname); int file_inode_ptr;
if (file_inode_ptr < 0) { {
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname);
if (r == NULL) {
pr_err("no such file: '%s'\n", fname); pr_err("no such file: '%s'\n", fname);
return 0; return 0;
} }
file_inode_ptr = r[2];
free(r);
}
struct fs_inode f; struct fs_inode f;
read_block(read_inode_ptr(file_inode_ptr), (void *) &f); read_block(read_inode_ptr(file_inode_ptr), (void *) &f);