wip: add mkdir, cd, simplify find_filename_in_directory usage, fix old issues
This commit is contained in:
parent
a90f09ff6e
commit
ff684fad45
1
config.h
1
config.h
|
@ -26,6 +26,7 @@
|
||||||
#define FS_MAX_PATH_LEN 512
|
#define FS_MAX_PATH_LEN 512
|
||||||
#define FS_MAX_OPEN_FD 32
|
#define FS_MAX_OPEN_FD 32
|
||||||
#define FS_MAX_FNAME_LEN 11
|
#define FS_MAX_FNAME_LEN 11
|
||||||
|
#define FS_MAX_DIRECTORY_DEPTH 512
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
2
inc/fs.h
2
inc/fs.h
|
@ -61,6 +61,8 @@ int fs_seek(void *d);
|
||||||
int fs_read(void *d);
|
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_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);
|
||||||
int fs_prohibit_write(void *d);
|
int fs_prohibit_write(void *d);
|
||||||
|
|
76
inc/print.h
76
inc/print.h
|
@ -9,68 +9,58 @@
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
|
|
||||||
#if DEBUG == 1
|
#if DEBUG == 1
|
||||||
#define pr(...) { printf("[%s:%d] ", __FILE__, __LINE__); printf(__VA_ARGS__); }
|
#define pr(...) do { printf("[%s:%d] ", __FILE__, __LINE__); printf(__VA_ARGS__); } while (0)
|
||||||
#else
|
#else
|
||||||
#define pr(...) {}
|
#define pr(...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if ENABLE_STDOUT == 1
|
#if ENABLE_STDOUT == 1
|
||||||
#define pr_stdout(...) { printf(__VA_ARGS__); }
|
#define pr_stdout(...) do { printf(__VA_ARGS__); } while (0)
|
||||||
#define write_stdout(data, size) { write(1, (data), (size)); }
|
#define write_stdout(data, size) do { write(1, (data), (size)); } while (0)
|
||||||
#else
|
#else
|
||||||
#define pr_stdout(...) {}
|
#define pr_stdout(...)
|
||||||
#define write_stdout(data, size) {}
|
#define write_stdout(data, size)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if LOG_LEVEL >= 0
|
||||||
|
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1
|
||||||
|
#define pr_generic(color, prefix, ...) do { \
|
||||||
|
printf(color "[%s:%d] " prefix, __FILE__, __LINE__); \
|
||||||
|
printf(__VA_ARGS__); \
|
||||||
|
printf(COLOR_RESET); \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define pr_generic(color, prefix, ...) do { \
|
||||||
|
printf(color prefix); \
|
||||||
|
printf(__VA_ARGS__); \
|
||||||
|
printf(COLOR_RESET); \
|
||||||
|
} while (0)
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define pr_generic(color, prefix, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if LOG_LEVEL >= 2
|
#if LOG_LEVEL >= 2
|
||||||
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1
|
#define pr_err(...) pr_generic(COLOR_RED, "Error: ", __VA_ARGS__)
|
||||||
#define pr_err(...) { \
|
|
||||||
printf(COLOR_RED "[%s:%d] Error: ", __FILE__, __LINE__); \
|
|
||||||
printf(__VA_ARGS__); \
|
|
||||||
printf(COLOR_RESET); \
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
#define pr_err(...) { \
|
#define pr_err(...)
|
||||||
printf(COLOR_RED "Error: "); \
|
|
||||||
printf(__VA_ARGS__); \
|
|
||||||
printf(COLOR_RESET);\
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define pr_err(...) {}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if LOG_LEVEL >= 3
|
#if LOG_LEVEL >= 3
|
||||||
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1
|
#define pr_warn(...) pr_generic(COLOR_YELLOW, "Warning: ", __VA_ARGS__)
|
||||||
#define pr_warn(...) { \
|
|
||||||
printf(COLOR_YELLOW "[%s:%d] Warning: ", __FILE__, __LINE__); \
|
|
||||||
printf(__VA_ARGS__); \
|
|
||||||
printf(COLOR_RESET); \
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
#define pr_warn(...) { \
|
#define pr_warn(...)
|
||||||
printf(COLOR_YELLOW "Warning: "); \
|
|
||||||
printf(__VA_ARGS__); \
|
|
||||||
printf(COLOR_RESET); \
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define pr_warn(...) {}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if LOG_LEVEL >= 4
|
#if LOG_LEVEL >= 4
|
||||||
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1
|
#define pr_info(...) pr_generic(COLOR_RESET, "Info: ", __VA_ARGS__)
|
||||||
#define pr_info(...) { \
|
|
||||||
printf("[%s:%d] Info: ", __FILE__, __LINE__); \
|
|
||||||
printf(__VA_ARGS__); \
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
#define pr_info(...) { printf("Info: "); printf(__VA_ARGS__); }
|
#define pr_info(...)
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define pr_info(...) {}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
11
src/cli.c
11
src/cli.c
|
@ -26,6 +26,9 @@ static const struct CliCommandEntry cmd[] = {
|
||||||
{"write", 2, (enum CliArgType[]) {INT, STR}, fs_write},
|
{"write", 2, (enum CliArgType[]) {INT, STR}, fs_write},
|
||||||
{"close", 1, (enum CliArgType[]) {INT}, fs_close},
|
{"close", 1, (enum CliArgType[]) {INT}, fs_close},
|
||||||
|
|
||||||
|
{"mkdir", 1, (enum CliArgType[]) {STR}, fs_mkdir},
|
||||||
|
{"cd", 1, (enum CliArgType[]) {STR}, fs_cd},
|
||||||
|
|
||||||
// custom commands
|
// custom commands
|
||||||
{"use", 1, (enum CliArgType[]) {STR}, fs_use},
|
{"use", 1, (enum CliArgType[]) {STR}, fs_use},
|
||||||
{"la", 0, NULL, fs_la},
|
{"la", 0, NULL, fs_la},
|
||||||
|
@ -68,8 +71,8 @@ static char **tokenize_line(char *line, ssize_t result)
|
||||||
unsigned int i, t, l;
|
unsigned int i, t, l;
|
||||||
for (
|
for (
|
||||||
i = 0, t = 0, l = 0;
|
i = 0, t = 0, l = 0;
|
||||||
i < CLI_MAX_LINE_LENGTH,
|
i < CLI_MAX_LINE_LENGTH &&
|
||||||
t <= CLI_MAX_ACCEPTED_ARGS,
|
t <= CLI_MAX_ACCEPTED_ARGS &&
|
||||||
l < CLI_MAX_TOKEN_LENGTH;
|
l < CLI_MAX_TOKEN_LENGTH;
|
||||||
i++
|
i++
|
||||||
) {
|
) {
|
||||||
|
@ -204,9 +207,9 @@ unsigned int cli_poll_process_next(void)
|
||||||
fputs(fs_get_cwd(), stdout);
|
fputs(fs_get_cwd(), stdout);
|
||||||
|
|
||||||
if (fs_get_cwd()[0]) {
|
if (fs_get_cwd()[0]) {
|
||||||
fputs(" $ ", stdout);
|
printf(" $ ");
|
||||||
} else {
|
} else {
|
||||||
fputs("$ ", stdout);
|
printf("$ ");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t buf_size = 0;
|
size_t buf_size = 0;
|
||||||
|
|
396
src/fs.c
396
src/fs.c
|
@ -451,7 +451,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
||||||
read_block(dir.blocks[i], (void *) &r);
|
read_block(dir.blocks[i], (void *) &r);
|
||||||
|
|
||||||
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
||||||
if (r[j].inode_no)
|
if (r[j].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (found_block_no >= fsh.max_inode_count)
|
if (found_block_no >= fsh.max_inode_count)
|
||||||
|
@ -484,7 +484,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
||||||
read_block(ext.blocks[i], (void *) &r);
|
read_block(ext.blocks[i], (void *) &r);
|
||||||
|
|
||||||
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
||||||
if (r[j].inode_no)
|
if (r[j].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (found_block_no >= fsh.max_inode_count)
|
if (found_block_no >= fsh.max_inode_count)
|
||||||
|
@ -503,7 +503,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)
|
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;
|
struct fs_inode dir;
|
||||||
read_block(dir_inode, (void *) &dir);
|
read_block(dir_inode, (void *) &dir);
|
||||||
|
@ -516,7 +516,7 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
||||||
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].inode_no)
|
if (!recs[k].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strcmp(fname, recs[k].fname))
|
if (strcmp(fname, recs[k].fname))
|
||||||
|
@ -547,7 +547,7 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
||||||
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].inode_no)
|
if (!recs[k].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strcmp(fname, recs[k].fname))
|
if (strcmp(fname, recs[k].fname))
|
||||||
|
@ -567,6 +567,20 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
||||||
return NULL;
|
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];
|
||||||
|
else
|
||||||
|
res = -1;
|
||||||
|
|
||||||
|
free(r);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static int inode_is_referenced(unsigned int inode)
|
static int inode_is_referenced(unsigned int inode)
|
||||||
{
|
{
|
||||||
struct fs_inode f;
|
struct fs_inode f;
|
||||||
|
@ -620,7 +634,7 @@ static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int ta
|
||||||
if (r) {
|
if (r) {
|
||||||
free(r);
|
free(r);
|
||||||
pr_err("filename '%s' already exists\n", fname);
|
pr_err("filename '%s' already exists\n", fname);
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -778,7 +792,7 @@ static void fs_remove_fname_from_directory_removal(struct fs_directory_record *r
|
||||||
write_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
|
write_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
|
||||||
|
|
||||||
// clear directory record inode_ptr
|
// clear directory record inode_ptr
|
||||||
recs[k].inode_no = 0;
|
recs[k].fname[0] = 0;
|
||||||
write_block(dir_block_addr, (void *) 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
|
||||||
|
@ -823,7 +837,7 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
|
||||||
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].inode_no)
|
if (!recs[k].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strcmp(fname, recs[k].fname))
|
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);
|
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].inode_no)
|
if (!recs[k].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strcmp(fname, recs[k].fname))
|
if (strcmp(fname, recs[k].fname))
|
||||||
|
@ -884,6 +898,76 @@ fs_remove_fname_from_directory_finish:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int resolve_directory(char *path)
|
||||||
|
{
|
||||||
|
int path_len = strlen(path);
|
||||||
|
|
||||||
|
if (!path_len) {
|
||||||
|
pr_err("no path specified\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int current_dir_inode_ptr;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (path[0] == '/') {
|
||||||
|
current_dir_inode_ptr = 0;
|
||||||
|
i = 1;
|
||||||
|
} else {
|
||||||
|
current_dir_inode_ptr = fs_cwd_inode_ptr;
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char next_dir_name[FS_MAX_FNAME_LEN+1] = {};
|
||||||
|
int next_dir_name_ptr = 0;
|
||||||
|
|
||||||
|
for ( ; i < path_len; i++) {
|
||||||
|
if (path[i] != '/') {
|
||||||
|
next_dir_name[next_dir_name_ptr] = path[i];
|
||||||
|
next_dir_name_ptr++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// path[i] == '/', trying to change current directory
|
||||||
|
|
||||||
|
if (!next_dir_name_ptr)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (i+1 == path_len)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
{
|
||||||
|
int *res = find_filename_in_directory(current_dir_inode_ptr, next_dir_name);
|
||||||
|
if (!res) {
|
||||||
|
pr_err("directory '%s' does not exist\n", next_dir_name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
struct fs_inode d;
|
||||||
|
read_block(read_inode_ptr(res[2]), &d);
|
||||||
|
|
||||||
|
if (d.ftype == REGULAR) {
|
||||||
|
free(res);
|
||||||
|
pr_err("'%s' is a regular file\n", next_dir_name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pr("Found directory '%s'\n", next_dir_name);
|
||||||
|
current_dir_inode_ptr = res[2];
|
||||||
|
|
||||||
|
free(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
next_dir_name_ptr = 0;
|
||||||
|
memset(next_dir_name, 0, FS_MAX_FNAME_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
return current_dir_inode_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int fs_open(void *d)
|
int fs_open(void *d)
|
||||||
{
|
{
|
||||||
char *fname = *((char **) d);
|
char *fname = *((char **) d);
|
||||||
|
@ -1522,6 +1606,286 @@ original_inode_ptr_found:
|
||||||
pr("Updated inode ref_count (%d -> %d)\n", f.ref_count-1, f.ref_count);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int target_directory_inode_ptr = resolve_directory(dirpath);
|
||||||
|
if (target_directory_inode_ptr < 0) {
|
||||||
|
pr_err("failed to resolve path '%s'\n", dirpath);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int target_inode_ptr = find_free_inode_ptr();
|
||||||
|
if (!target_inode_ptr) {
|
||||||
|
pr_err("no free inode ptr\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int inode_block_ptr = find_free_block();
|
||||||
|
if (!inode_block_ptr) {
|
||||||
|
pr_err("no space left on device\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int parent_directory_inode_ptr;
|
||||||
|
{
|
||||||
|
char full_directory_path[FS_MAX_PATH_LEN+1];
|
||||||
|
strcpy(full_directory_path, fs_cwd);
|
||||||
|
strcat(full_directory_path, "/");
|
||||||
|
strcat(full_directory_path, dirpath);
|
||||||
|
|
||||||
|
char parent_directory_path[FS_MAX_PATH_LEN+1];
|
||||||
|
extract_parent_directory_path(full_directory_path, parent_directory_path);
|
||||||
|
|
||||||
|
parent_directory_inode_ptr = resolve_directory(parent_directory_path);
|
||||||
|
if (parent_directory_inode_ptr < 0) {
|
||||||
|
pr_err("parent directory ('%s') does not exist\n", parent_directory_path);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct fs_inode dir;
|
||||||
|
memset(&dir, 0, sizeof(dir));
|
||||||
|
|
||||||
|
dir.ftype = DIRECTORY;
|
||||||
|
dir.ref_count = 1;
|
||||||
|
|
||||||
|
write_block(inode_block_ptr, &dir);
|
||||||
|
mark_used(inode_block_ptr);
|
||||||
|
|
||||||
|
write_inode_ptr(target_inode_ptr, inode_block_ptr);
|
||||||
|
|
||||||
|
if (fs_add_fname_to_directory(target_directory_inode_ptr, target_inode_ptr, dirname) < 0) {
|
||||||
|
pr_err("failed to add record '%s' -> %d to directory inode_ptr=%d\n",
|
||||||
|
dirname, target_inode_ptr, target_directory_inode_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add . and ..
|
||||||
|
fs_add_fname_to_directory(target_inode_ptr, target_inode_ptr, ".");
|
||||||
|
fs_add_fname_to_directory(target_inode_ptr, parent_directory_inode_ptr, "..");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 path
|
||||||
|
//int *dir_inode_ptrs = NULL;
|
||||||
|
int dir_inode_ptrs[FS_MAX_DIRECTORY_DEPTH];
|
||||||
|
int dir_inode_ptrs_len = 1;
|
||||||
|
|
||||||
|
dir_inode_ptrs[0] = dir_inode_ptr;
|
||||||
|
int current_dir_inode_ptr = 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(current_dir_inode_ptr, "..");
|
||||||
|
|
||||||
|
if (!r) {
|
||||||
|
pr_err("failed to find parent directory of inode_ptr=%d\n", current_dir_inode_ptr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//realloc(dir_inode_ptrs, sizeof(int) * dir_inode_ptrs_len);
|
||||||
|
dir_inode_ptrs[dir_inode_ptrs_len] = r[2];
|
||||||
|
free(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// build path based on inode_ptr list
|
||||||
|
memset(path, 0, sizeof(FS_MAX_PATH_LEN));
|
||||||
|
path[0] = '/';
|
||||||
|
|
||||||
|
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");
|
||||||
|
//free(dir_inode_ptrs);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcat(path, fname);
|
||||||
|
}
|
||||||
|
|
||||||
|
//free(dir_inode_ptrs);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int new_dir_parent_inode_ptr = resolve_directory(new_dir);
|
||||||
|
if (new_dir_parent_inode_ptr < 0) {
|
||||||
|
pr_err("failed to resolve path '%s' from '%s'\n", new_dir, fs_cwd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char top_dir_name[FS_MAX_FNAME_LEN+1];
|
||||||
|
if (extract_basename(new_dir, top_dir_name) < 0) {
|
||||||
|
pr_err("failed to extract base name from '%s'\n", new_dir);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int new_dir_inode_ptr = find_fname_in_directory(new_dir_parent_inode_ptr, top_dir_name);
|
||||||
|
if (new_dir_inode_ptr < 0) {
|
||||||
|
pr_err("no such directory: '%s'\n", top_dir_name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char canonical_path[FS_MAX_PATH_LEN+1];
|
||||||
|
if (build_canonical_dir_path(canonical_path, new_dir_inode_ptr) < 0) {
|
||||||
|
pr_err("failed to build canonical path to '%s'\n", new_dir);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *cpp = (char *) canonical_path;
|
||||||
|
fs_chdir(&cpp);
|
||||||
|
fs_cwd_inode_ptr = new_dir_inode_ptr;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int fs_rm(void *d)
|
int fs_rm(void *d)
|
||||||
{
|
{
|
||||||
if (!write_permitted) {
|
if (!write_permitted) {
|
||||||
|
@ -1561,7 +1925,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].inode_no)
|
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);
|
||||||
|
@ -1584,7 +1948,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].inode_no)
|
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);
|
||||||
|
@ -1619,7 +1983,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].inode_no)
|
if (!recs[k].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
struct fs_inode f_inode;
|
struct fs_inode f_inode;
|
||||||
|
@ -1654,7 +2018,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].inode_no)
|
if (!recs[k].fname)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
struct fs_inode f_inode;
|
struct fs_inode f_inode;
|
||||||
|
@ -1744,6 +2108,7 @@ int fs_use(void *d)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (write_permitted) {
|
if (write_permitted) {
|
||||||
|
memset(fs_cwd, 0, sizeof(fs_cwd));
|
||||||
char *root_dir_path = "/";
|
char *root_dir_path = "/";
|
||||||
fs_chdir((void *) &root_dir_path);
|
fs_chdir((void *) &root_dir_path);
|
||||||
|
|
||||||
|
@ -1874,10 +2239,15 @@ finish_current_block:
|
||||||
// inode0 -> root_dir_block
|
// inode0 -> root_dir_block
|
||||||
write_inode_ptr(0, free_block_index);
|
write_inode_ptr(0, free_block_index);
|
||||||
|
|
||||||
|
memset(fs_cwd, 0, sizeof(fs_cwd));
|
||||||
char *root_dir_path = "/";
|
char *root_dir_path = "/";
|
||||||
fs_chdir((void *) &root_dir_path);
|
fs_chdir((void *) &root_dir_path);
|
||||||
|
|
||||||
fs_cwd_inode_ptr = 0;
|
fs_cwd_inode_ptr = 0;
|
||||||
|
|
||||||
|
// add . and ..
|
||||||
|
fs_add_fname_to_directory(0, 0, ".");
|
||||||
|
fs_add_fname_to_directory(0, 0, "..");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue