|
|
|
@@ -23,6 +23,8 @@ static unsigned int fs_cwd_inode_ptr;
|
|
|
|
|
|
|
|
|
|
static struct fs_file_description fs_file_descriptions[FS_MAX_OPEN_FD];
|
|
|
|
|
|
|
|
|
|
static int extract_basename(char *path, char *name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int read_block(unsigned int block_no, void *data)
|
|
|
|
|
{
|
|
|
|
@@ -451,7 +453,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
|
|
|
|
read_block(dir.blocks[i], (void *) &r);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
|
|
|
|
if (r[j].inode_no)
|
|
|
|
|
if (r[j].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (found_block_no >= fsh.max_inode_count)
|
|
|
|
@@ -484,7 +486,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
|
|
|
|
read_block(ext.blocks[i], (void *) &r);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
|
|
|
|
if (r[j].inode_no)
|
|
|
|
|
if (r[j].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (found_block_no >= fsh.max_inode_count)
|
|
|
|
@@ -503,7 +505,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
|
|
|
|
|
|
|
|
|
static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
{
|
|
|
|
|
int dir_inode = read_inode_ptr(fs_cwd_inode_ptr);
|
|
|
|
|
int dir_inode = read_inode_ptr(dir_inode_ptr);
|
|
|
|
|
|
|
|
|
|
struct fs_inode dir;
|
|
|
|
|
read_block(dir_inode, (void *) &dir);
|
|
|
|
@@ -516,7 +518,7 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
read_block(dir.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
@@ -547,7 +549,7 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
@@ -567,6 +569,21 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int find_fname_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
{
|
|
|
|
|
int *r = find_filename_in_directory(dir_inode_ptr, fname);
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
|
res = r[2];
|
|
|
|
|
free(r);
|
|
|
|
|
} else {
|
|
|
|
|
res = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int inode_is_referenced(unsigned int inode)
|
|
|
|
|
{
|
|
|
|
|
struct fs_inode f;
|
|
|
|
@@ -620,7 +637,7 @@ static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int ta
|
|
|
|
|
if (r) {
|
|
|
|
|
free(r);
|
|
|
|
|
pr_err("filename '%s' already exists\n", fname);
|
|
|
|
|
return 0;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -773,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].inode_no = 0;
|
|
|
|
|
write_block(dir_block_addr, (void *) recs);
|
|
|
|
|
recs[k].fname[0] = 0;
|
|
|
|
|
write_block(dir_block_addr, recs);
|
|
|
|
|
|
|
|
|
|
// if it drops to zero, nullify inode_ptr pointing to this inode
|
|
|
|
|
if (f.ref_count)
|
|
|
|
@@ -807,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++) {
|
|
|
|
@@ -820,10 +834,10 @@ 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].inode_no)
|
|
|
|
|
if (!recs[k].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
@@ -860,7 +874,7 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
|
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
@@ -884,6 +898,111 @@ fs_remove_fname_from_directory_finish:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void resolve_path(struct resolved_path *rp, char *path, unsigned int params)
|
|
|
|
|
{
|
|
|
|
|
rp->parent_inode_ptr = -1;
|
|
|
|
|
rp->target_inode_ptr = -1;
|
|
|
|
|
|
|
|
|
|
int path_len = strlen(path);
|
|
|
|
|
|
|
|
|
|
if (!path_len) {
|
|
|
|
|
pr_err("no path specified\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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_inode_ptr = 0;
|
|
|
|
|
rp->parent_inode_ptr = 0;
|
|
|
|
|
strcpy(current_fname, "/");
|
|
|
|
|
i = 1;
|
|
|
|
|
} else {
|
|
|
|
|
current_inode_ptr = fs_cwd_inode_ptr;
|
|
|
|
|
rp->parent_inode_ptr = fs_cwd_inode_ptr;
|
|
|
|
|
extract_basename(fs_cwd, current_fname);
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char next_fname[FS_MAX_FNAME_LEN+1] = {};
|
|
|
|
|
int next_fname_ptr = 0;
|
|
|
|
|
|
|
|
|
|
for ( ; i < path_len; i++) {
|
|
|
|
|
if (path[i] != '/') {
|
|
|
|
|
next_fname[next_fname_ptr] = path[i];
|
|
|
|
|
next_fname_ptr++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// path[i] == '/', trying to change current directory
|
|
|
|
|
|
|
|
|
|
if (!next_fname_ptr)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (i+1 == path_len)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
int res = find_fname_in_directory(current_inode_ptr, next_fname);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
pr_warn("directory '%s' does not exist\n", next_fname);
|
|
|
|
|
rp->parent_inode_ptr = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
struct fs_inode d;
|
|
|
|
|
read_block(read_inode_ptr(res), &d);
|
|
|
|
|
|
|
|
|
|
if (d.ftype == REGULAR) {
|
|
|
|
|
pr_warn("'%s' is a regular file\n", next_fname);
|
|
|
|
|
rp->parent_inode_ptr = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pr("Found directory '%s'\n", next_fname);
|
|
|
|
|
current_inode_ptr = res;
|
|
|
|
|
rp->parent_inode_ptr = res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next_fname_ptr = 0;
|
|
|
|
|
strcpy(current_fname, next_fname);
|
|
|
|
|
memset(next_fname, 0, FS_MAX_FNAME_LEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rp->parent_inode_ptr < 0) {
|
|
|
|
|
rp->target_inode_ptr = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strcpy(rp->parent_fname, current_fname);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
int res = find_fname_in_directory(current_inode_ptr, next_fname);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
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_fname);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int fs_open(void *d)
|
|
|
|
|
{
|
|
|
|
|
char *fname = *((char **) d);
|
|
|
|
@@ -1353,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();
|
|
|
|
@@ -1411,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;
|
|
|
|
|
}
|
|
|
|
@@ -1522,6 +1649,379 @@ original_inode_ptr_found:
|
|
|
|
|
pr("Updated inode ref_count (%d -> %d)\n", f.ref_count-1, f.ref_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int last_significant_slash_position(char *path, int path_len)
|
|
|
|
|
{
|
|
|
|
|
int last_position = -1;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < path_len; i++)
|
|
|
|
|
if ((path[i] == '/') && (i+1 != path_len))
|
|
|
|
|
last_position = i;
|
|
|
|
|
|
|
|
|
|
return last_position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int extract_basename(char *path, char *name)
|
|
|
|
|
{
|
|
|
|
|
int path_len = strlen(path);
|
|
|
|
|
|
|
|
|
|
int lsp = last_significant_slash_position(path, path_len);
|
|
|
|
|
int fname_len = path_len - lsp - 1;
|
|
|
|
|
|
|
|
|
|
if (fname_len > FS_MAX_FNAME_LEN) {
|
|
|
|
|
pr_err("filename too long (%d > %d)",
|
|
|
|
|
fname_len, FS_MAX_FNAME_LEN);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strcpy(name, &(path[lsp+1]));
|
|
|
|
|
|
|
|
|
|
return fname_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void extract_parent_directory_path(char *path, char *parent_path)
|
|
|
|
|
{
|
|
|
|
|
int path_len = strlen(path);
|
|
|
|
|
int lsp = last_significant_slash_position(path, path_len);
|
|
|
|
|
int parent_len = last_significant_slash_position(path, lsp-1);
|
|
|
|
|
|
|
|
|
|
if (parent_len > 1) {
|
|
|
|
|
memcpy(parent_path, path, parent_len-1);
|
|
|
|
|
parent_path[parent_len] = '\0';
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(parent_path, "/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fs_mkdir(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char dirname[FS_MAX_FNAME_LEN+1];
|
|
|
|
|
{
|
|
|
|
|
int res = extract_basename(dirpath, dirname);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
pr_err("failed to extract dirname from path '%s'\n",
|
|
|
|
|
dirpath);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int dirname_len = strlen(dirname);
|
|
|
|
|
|
|
|
|
|
if (dirname_len > FS_MAX_FNAME_LEN) {
|
|
|
|
|
pr_err("directory name too long ('%s', %d > %d)\n",
|
|
|
|
|
dirname, dirname_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 of '%s'\n", dirpath);
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (rp.target_inode_ptr >= 0) {
|
|
|
|
|
pr_err("fname '%s' already exists in directory '%s'\n",
|
|
|
|
|
rp.target_fname, rp.parent_fname);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int target_inode_ptr = find_free_inode_ptr();
|
|
|
|
|
if (!target_inode_ptr) {
|
|
|
|
|
pr_err("no free inode ptr\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int target_inode_block = find_free_block();
|
|
|
|
|
if (!target_inode_block) {
|
|
|
|
|
pr_err("no space left on device\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fs_inode dir;
|
|
|
|
|
memset(&dir, 0, sizeof(dir));
|
|
|
|
|
|
|
|
|
|
dir.ftype = DIRECTORY;
|
|
|
|
|
dir.ref_count = 1;
|
|
|
|
|
|
|
|
|
|
write_block(target_inode_block, &dir);
|
|
|
|
|
mark_used(target_inode_block);
|
|
|
|
|
|
|
|
|
|
write_inode_ptr(target_inode_ptr, target_inode_block);
|
|
|
|
|
|
|
|
|
|
if (fs_add_fname_to_directory(rp.parent_inode_ptr, target_inode_ptr, dirname) < 0) {
|
|
|
|
|
pr_err("failed to add record '%s' -> inode_ptr=%d to directory '%s' (inode_ptr=%d)\n",
|
|
|
|
|
dirname, target_inode_ptr, rp.parent_fname, rp.parent_inode_ptr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add . and ..
|
|
|
|
|
fs_add_fname_to_directory(target_inode_ptr, target_inode_ptr, ".");
|
|
|
|
|
fs_add_fname_to_directory(target_inode_ptr, rp.parent_inode_ptr, "..");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
read_block(read_inode_ptr(dir_inode_ptr), &dir);
|
|
|
|
|
|
|
|
|
|
// search base inode
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
|
|
|
|
|
|
if (!dir.blocks[i])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
read_block(dir.blocks[i], &recs);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
|
|
|
|
|
if (!recs[j].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (recs[j].inode_no != file_inode_ptr)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
strcpy(name, recs[j].fname);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// search inode extensions
|
|
|
|
|
unsigned int next_ext = dir.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++) {
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
|
|
|
|
|
|
if (!ext.blocks[i])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
read_block(ext.blocks[i], &recs);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
|
|
|
|
|
if (!recs[j].fname)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (recs[j].inode_no != file_inode_ptr)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
strcpy(name, recs[j].fname);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pr_err("no such file (inode_ptr=%d)\n", file_inode_ptr);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int build_canonical_dir_path(char *path, int dir_inode_ptr)
|
|
|
|
|
{
|
|
|
|
|
// build directory inode_ptr-based reverse path
|
|
|
|
|
int dir_inode_ptrs[FS_MAX_DIRECTORY_DEPTH];
|
|
|
|
|
int dir_inode_ptrs_len = 1;
|
|
|
|
|
|
|
|
|
|
dir_inode_ptrs[0] = dir_inode_ptr;
|
|
|
|
|
|
|
|
|
|
for ( ; dir_inode_ptrs_len < FS_MAX_DIRECTORY_DEPTH; dir_inode_ptrs_len++) {
|
|
|
|
|
// check if reached the root dir
|
|
|
|
|
if (!dir_inode_ptrs[dir_inode_ptrs_len-1])
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
int *r = find_filename_in_directory(dir_inode_ptrs[dir_inode_ptrs_len-1], "..");
|
|
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
|
pr_err("failed to find parent directory of inode_ptr=%d\n", dir_inode_ptrs[dir_inode_ptrs_len-1]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir_inode_ptrs[dir_inode_ptrs_len] = r[2];
|
|
|
|
|
free(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build string path based on inode_ptr list
|
|
|
|
|
memset(path, 0, sizeof(FS_MAX_PATH_LEN));
|
|
|
|
|
|
|
|
|
|
for (int i = dir_inode_ptrs_len - 2; i >= 0; i--) {
|
|
|
|
|
char fname[FS_MAX_FNAME_LEN+1];
|
|
|
|
|
|
|
|
|
|
if (get_fname_by_inode_ptr(dir_inode_ptrs[i+1], dir_inode_ptrs[i], fname) < 0) {
|
|
|
|
|
pr_err("failed to build canonical path\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strcat(path, "/");
|
|
|
|
|
strcat(path, fname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fs_cd(void *d)
|
|
|
|
|
{
|
|
|
|
|
char *new_dir = *((char **)d);
|
|
|
|
|
int new_dir_len = strlen(new_dir);
|
|
|
|
|
|
|
|
|
|
if (new_dir_len > FS_MAX_PATH_LEN) {
|
|
|
|
|
pr_err("path too long (%d > %d)\n", new_dir_len, FS_MAX_PATH_LEN);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct resolved_path rp;
|
|
|
|
|
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);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rp.target_inode_ptr < 0) {
|
|
|
|
|
pr_err("no such directory: '%s'\n", new_dir);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char canonical_path[FS_MAX_PATH_LEN+1];
|
|
|
|
|
if (build_canonical_dir_path(canonical_path, rp.target_inode_ptr) < 0) {
|
|
|
|
|
pr_err("failed to build canonical path to '%s' (inode_ptr=%d)\n",
|
|
|
|
|
new_dir, rp.target_inode_ptr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!canonical_path[0])
|
|
|
|
|
canonical_path[0] = '/';
|
|
|
|
|
|
|
|
|
|
char *cpp = (char *) canonical_path;
|
|
|
|
|
fs_chdir(&cpp);
|
|
|
|
|
fs_cwd_inode_ptr = rp.target_inode_ptr;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int fs_rm(void *d)
|
|
|
|
|
{
|
|
|
|
|
if (!write_permitted) {
|
|
|
|
@@ -1529,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;
|
|
|
|
@@ -1561,7 +2085,7 @@ int fs_ls(void *d)
|
|
|
|
|
read_block(dir.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname[0])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
|
|
|
|
@@ -1584,7 +2108,7 @@ int fs_ls(void *d)
|
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname[0])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
|
|
|
|
@@ -1619,7 +2143,7 @@ int fs_la(void *d)
|
|
|
|
|
read_block(dir.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname[0])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
struct fs_inode f_inode;
|
|
|
|
@@ -1654,7 +2178,7 @@ int fs_la(void *d)
|
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
if (!recs[k].fname[0])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
struct fs_inode f_inode;
|
|
|
|
@@ -1681,16 +2205,10 @@ int fs_stat(void *d)
|
|
|
|
|
{
|
|
|
|
|
char *fname = *((char **) d);
|
|
|
|
|
|
|
|
|
|
int file_inode_ptr;
|
|
|
|
|
{
|
|
|
|
|
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname);
|
|
|
|
|
if (r == NULL) {
|
|
|
|
|
pr_err("no such file: '%s'\n", fname);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file_inode_ptr = r[2];
|
|
|
|
|
free(r);
|
|
|
|
|
int file_inode_ptr = find_fname_in_directory(fs_cwd_inode_ptr, fname);
|
|
|
|
|
if (file_inode_ptr < 0) {
|
|
|
|
|
pr_err("no such file: '%s'\n", fname);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fs_inode f;
|
|
|
|
@@ -1744,6 +2262,7 @@ int fs_use(void *d)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (write_permitted) {
|
|
|
|
|
memset(fs_cwd, 0, sizeof(fs_cwd));
|
|
|
|
|
char *root_dir_path = "/";
|
|
|
|
|
fs_chdir((void *) &root_dir_path);
|
|
|
|
|
|
|
|
|
@@ -1874,10 +2393,15 @@ finish_current_block:
|
|
|
|
|
// inode0 -> root_dir_block
|
|
|
|
|
write_inode_ptr(0, free_block_index);
|
|
|
|
|
|
|
|
|
|
memset(fs_cwd, 0, sizeof(fs_cwd));
|
|
|
|
|
char *root_dir_path = "/";
|
|
|
|
|
fs_chdir((void *) &root_dir_path);
|
|
|
|
|
|
|
|
|
|
fs_cwd_inode_ptr = 0;
|
|
|
|
|
|
|
|
|
|
// add . and ..
|
|
|
|
|
fs_add_fname_to_directory(0, 0, ".");
|
|
|
|
|
fs_add_fname_to_directory(0, 0, "..");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|