generalize resolve_directory into resolve_path, fix mkdir, cd, update rm, create, add rmdir
This commit is contained in:
parent
117e8706a8
commit
d68b874b24
3
inc/fs.h
3
inc/fs.h
|
@ -1,5 +1,7 @@
|
|||
#include "config.h"
|
||||
|
||||
#define FOLLOW_LAST_SYMLINK 0x1
|
||||
|
||||
enum fs_filetype {
|
||||
REGULAR,
|
||||
DIRECTORY
|
||||
|
@ -69,6 +71,7 @@ int fs_read(void *d);
|
|||
int fs_write(void *d);
|
||||
int fs_close(void *d);
|
||||
int fs_mkdir(void *d);
|
||||
int fs_rmdir(void *d);
|
||||
int fs_cd(void *d);
|
||||
int fs_truncate(void *d);
|
||||
int fs_allow_write(void *d);
|
||||
|
|
|
@ -27,6 +27,7 @@ static const struct CliCommandEntry cmd[] = {
|
|||
{"close", 1, (enum CliArgType[]) {INT}, fs_close},
|
||||
|
||||
{"mkdir", 1, (enum CliArgType[]) {STR}, fs_mkdir},
|
||||
{"rmdir", 1, (enum CliArgType[]) {STR}, fs_rmdir},
|
||||
{"cd", 1, (enum CliArgType[]) {STR}, fs_cd},
|
||||
|
||||
// custom commands
|
||||
|
|
247
src/fs.c
247
src/fs.c
|
@ -790,13 +790,13 @@ static void fs_remove_fname_from_directory_removal(struct fs_directory_record *r
|
|||
|
||||
// decrement ref_count
|
||||
struct fs_inode f;
|
||||
read_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
|
||||
read_block(read_inode_ptr(recs[k].inode_no), &f);
|
||||
f.ref_count--;
|
||||
write_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
|
||||
write_block(read_inode_ptr(recs[k].inode_no), &f);
|
||||
|
||||
// clear directory record inode_ptr
|
||||
recs[k].fname[0] = 0;
|
||||
write_block(dir_block_addr, (void *) recs);
|
||||
write_block(dir_block_addr, recs);
|
||||
|
||||
// if it drops to zero, nullify inode_ptr pointing to this inode
|
||||
if (f.ref_count)
|
||||
|
@ -824,11 +824,8 @@ 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)
|
||||
{
|
||||
// find directory record with this fname
|
||||
int dir_inode = read_inode_ptr(fs_cwd_inode_ptr);
|
||||
|
||||
struct fs_inode dir;
|
||||
read_block(dir_inode, (void *) &dir);
|
||||
read_block(read_inode_ptr(dir_inode_ptr), &dir);
|
||||
|
||||
// list entries from base inode
|
||||
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
||||
|
@ -837,7 +834,7 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
|
|||
if (!dir.blocks[i])
|
||||
continue;
|
||||
|
||||
read_block(dir.blocks[i], (void *) &recs);
|
||||
read_block(dir.blocks[i], &recs);
|
||||
|
||||
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
||||
if (!recs[k].fname)
|
||||
|
@ -901,7 +898,7 @@ fs_remove_fname_from_directory_finish:
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void resolve_directory(struct resolved_path *rp, char *path)
|
||||
static void resolve_path(struct resolved_path *rp, char *path, unsigned int params)
|
||||
{
|
||||
rp->parent_inode_ptr = -1;
|
||||
rp->target_inode_ptr = -1;
|
||||
|
@ -913,44 +910,52 @@ static void resolve_directory(struct resolved_path *rp, char *path)
|
|||
return;
|
||||
}
|
||||
|
||||
char current_dir_name[FS_MAX_FNAME_LEN+1] = {};
|
||||
int current_dir_inode_ptr;
|
||||
if (!strcmp(path, "/")) {
|
||||
rp->parent_inode_ptr = 0;
|
||||
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;
|
||||
|
||||
if (path[0] == '/') {
|
||||
current_dir_inode_ptr = 0;
|
||||
current_inode_ptr = 0;
|
||||
rp->parent_inode_ptr = 0;
|
||||
strcpy(current_dir_name, "/");
|
||||
strcpy(current_fname, "/");
|
||||
i = 1;
|
||||
} else {
|
||||
current_dir_inode_ptr = fs_cwd_inode_ptr;
|
||||
current_inode_ptr = fs_cwd_inode_ptr;
|
||||
rp->parent_inode_ptr = fs_cwd_inode_ptr;
|
||||
extract_basename(fs_cwd, current_dir_name);
|
||||
extract_basename(fs_cwd, current_fname);
|
||||
i = 0;
|
||||
}
|
||||
|
||||
char next_dir_name[FS_MAX_FNAME_LEN+1] = {};
|
||||
int next_dir_name_ptr = 0;
|
||||
char next_fname[FS_MAX_FNAME_LEN+1] = {};
|
||||
int next_fname_ptr = 0;
|
||||
|
||||
for ( ; i < path_len; i++) {
|
||||
if (path[i] != '/') {
|
||||
next_dir_name[next_dir_name_ptr] = path[i];
|
||||
next_dir_name_ptr++;
|
||||
next_fname[next_fname_ptr] = path[i];
|
||||
next_fname_ptr++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// path[i] == '/', trying to change current directory
|
||||
|
||||
if (!next_dir_name_ptr)
|
||||
if (!next_fname_ptr)
|
||||
continue;
|
||||
|
||||
if (i+1 == path_len)
|
||||
continue;
|
||||
|
||||
{
|
||||
int res = find_fname_in_directory(current_dir_inode_ptr, next_dir_name);
|
||||
int res = find_fname_in_directory(current_inode_ptr, next_fname);
|
||||
if (res < 0) {
|
||||
pr_warn("directory '%s' does not exist\n", next_dir_name);
|
||||
pr_warn("directory '%s' does not exist\n", next_fname);
|
||||
rp->parent_inode_ptr = -1;
|
||||
break;
|
||||
}
|
||||
|
@ -960,20 +965,20 @@ static void resolve_directory(struct resolved_path *rp, char *path)
|
|||
read_block(read_inode_ptr(res), &d);
|
||||
|
||||
if (d.ftype == REGULAR) {
|
||||
pr_warn("'%s' is a regular file\n", next_dir_name);
|
||||
pr_warn("'%s' is a regular file\n", next_fname);
|
||||
rp->parent_inode_ptr = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pr("Found directory '%s'\n", next_dir_name);
|
||||
current_dir_inode_ptr = res;
|
||||
pr("Found directory '%s'\n", next_fname);
|
||||
current_inode_ptr = res;
|
||||
rp->parent_inode_ptr = res;
|
||||
}
|
||||
|
||||
next_dir_name_ptr = 0;
|
||||
strcpy(current_dir_name, next_dir_name);
|
||||
memset(next_dir_name, 0, FS_MAX_FNAME_LEN);
|
||||
next_fname_ptr = 0;
|
||||
strcpy(current_fname, next_fname);
|
||||
memset(next_fname, 0, FS_MAX_FNAME_LEN);
|
||||
}
|
||||
|
||||
if (rp->parent_inode_ptr < 0) {
|
||||
|
@ -981,16 +986,16 @@ static void resolve_directory(struct resolved_path *rp, char *path)
|
|||
return;
|
||||
}
|
||||
|
||||
strcpy(rp->parent_fname, current_dir_name);
|
||||
strcpy(rp->parent_fname, current_fname);
|
||||
|
||||
{
|
||||
int res = find_fname_in_directory(current_dir_inode_ptr, next_dir_name);
|
||||
int res = find_fname_in_directory(current_inode_ptr, next_fname);
|
||||
if (res < 0) {
|
||||
pr_warn("target fname '%s' does not exist\n", next_dir_name);
|
||||
pr_warn("target fname '%s' does not exist\n", next_fname);
|
||||
rp->target_inode_ptr = -1;
|
||||
} else {
|
||||
rp->target_inode_ptr = res;
|
||||
strcpy(rp->target_fname, next_dir_name);
|
||||
strcpy(rp->target_fname, next_fname);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1467,24 +1472,32 @@ int fs_create(void *d)
|
|||
return 0;
|
||||
}
|
||||
|
||||
char *fname = *((char **) d);
|
||||
int fname_len = strlen(fname);
|
||||
|
||||
char *path = *((char **) d);
|
||||
{
|
||||
// check if duplicate filename exists in current directory
|
||||
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname);
|
||||
if (r) {
|
||||
free(r);
|
||||
pr_err("filename '%s' already exists\n", fname);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (fname_len > FS_MAX_FNAME_LEN) {
|
||||
pr_err("filename too long (%d > %d)\n", fname_len, FS_MAX_FNAME_LEN);
|
||||
struct resolved_path rp;
|
||||
resolve_path(&rp, path, 0);
|
||||
|
||||
if (rp.parent_inode_ptr < 0) {
|
||||
pr_err("unable to create file: no such directory: '%s'\n", path);
|
||||
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);
|
||||
|
||||
int inode_block_no = find_free_block();
|
||||
|
@ -1525,7 +1538,7 @@ int fs_create(void *d)
|
|||
write_inode_ptr(inode_ptr_index, inode_block_no);
|
||||
|
||||
// add filename to current directory inode
|
||||
fs_add_fname_to_directory(fs_cwd_inode_ptr, inode_ptr_index, fname);
|
||||
fs_add_fname_to_directory(rp.parent_inode_ptr, inode_ptr_index, fname);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1678,6 +1691,8 @@ static void extract_parent_directory_path(char *path, char *parent_path)
|
|||
} else {
|
||||
strcpy(parent_path, "/");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
int fs_mkdir(void *d)
|
||||
|
@ -1709,7 +1724,7 @@ int fs_mkdir(void *d)
|
|||
}
|
||||
|
||||
struct resolved_path rp;
|
||||
resolve_directory(&rp, dirpath);
|
||||
resolve_path(&rp, dirpath, 0);
|
||||
if (rp.parent_inode_ptr < 0) {
|
||||
pr_err("failed to find parent of '%s'\n", dirpath);
|
||||
return 0;
|
||||
|
@ -1754,6 +1769,114 @@ 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)
|
||||
{
|
||||
struct fs_inode dir;
|
||||
|
@ -1869,7 +1992,7 @@ int fs_cd(void *d)
|
|||
}
|
||||
|
||||
struct resolved_path rp;
|
||||
resolve_directory(&rp, new_dir);
|
||||
resolve_path(&rp, new_dir, FOLLOW_LAST_SYMLINK);
|
||||
|
||||
if (rp.parent_inode_ptr < 0) {
|
||||
pr_err("failed to find parent of '%s' from '%s'\n", new_dir, fs_cwd);
|
||||
|
@ -1877,7 +2000,7 @@ int fs_cd(void *d)
|
|||
}
|
||||
|
||||
if (rp.target_inode_ptr < 0) {
|
||||
pr_err("no fname '%s' in directory '%s'\n", rp.target_fname, rp.parent_fname);
|
||||
pr_err("no such directory: '%s'\n", new_dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1906,12 +2029,36 @@ int fs_rm(void *d)
|
|||
return 0;
|
||||
}
|
||||
|
||||
char *fname = *((char **) d);
|
||||
char *path = *((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;
|
||||
}
|
||||
}
|
||||
|
||||
if (fs_remove_fname_from_directory(fs_cwd_inode_ptr, fname) < 0) {
|
||||
pr_err("failed to unlink '%s'\n", fname);
|
||||
struct resolved_path rp;
|
||||
resolve_path(&rp, path, 0);
|
||||
|
||||
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 {
|
||||
pr("Unlinked '%s'\n", fname);
|
||||
pr("Unlinked '%s'\n", rp.target_fname);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue