From b6bc6da235112dd041aa2e7f59b8b0c80222eebb Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 18 May 2025 13:21:19 +0300 Subject: [PATCH] handle symlinks in ls, la and stat, improve resolve_path logging --- src/fs.c | 73 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/src/fs.c b/src/fs.c index 751ed25..598f0bb 100644 --- a/src/fs.c +++ b/src/fs.c @@ -998,6 +998,8 @@ static void resolve_path(struct resolved_path * const rp, const char * const ori break; } + pr("Found symlink '%s', following (-> '%s')\n", next_fname, symlink_path); + if (symlink_path[0] == '/') { memmove(&(path[symlink_path_len]), &(path[i+1]), path_len-i-1); strcpy(path, &(symlink_path[1])); @@ -1021,6 +1023,7 @@ static void resolve_path(struct resolved_path * const rp, const char * const ori } } else { if (!(params & FOLLOW_LAST_SYMLINK)) { + pr("Stopping at symlink '%s', not following\n", next_fname); rp->target_inode_ptr = res; strcpy(rp->parent_fname, current_fname); strcpy(rp->target_fname, next_fname); @@ -1045,6 +1048,8 @@ static void resolve_path(struct resolved_path * const rp, const char * const ori break; } + pr("Found symlink '%s' at last position, following (-> '%s')\n", next_fname, symlink_path); + if (!strcmp(symlink_path, "/")) { rp->parent_inode_ptr = 0; rp->target_inode_ptr = 0; @@ -1078,6 +1083,7 @@ static void resolve_path(struct resolved_path * const rp, const char * const ori rp->target_inode_ptr = -1; break; } else { + pr("Stopping at regular file '%s'\n", next_fname); rp->target_inode_ptr = res; strcpy(rp->parent_fname, current_fname); strcpy(rp->target_fname, next_fname); @@ -1092,6 +1098,7 @@ static void resolve_path(struct resolved_path * const rp, const char * const ori strcpy(current_fname, next_fname); memset(next_fname, 0, FS_MAX_FNAME_LEN); } else { + pr("Stopping at directory '%s'\n", next_fname); rp->target_inode_ptr = res; strcpy(rp->parent_fname, current_fname); strcpy(rp->target_fname, next_fname); @@ -2419,15 +2426,29 @@ int fs_la(void *d) struct fs_inode f_inode; read_block(read_inode_ptr(recs[k].inode_no), (void *) &f_inode); - if (f_inode.ftype == DIRECTORY) { + if (f_inode.ftype == REGULAR) { + pr_stdout("%s (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=reg)\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size); + } else if (f_inode.ftype == DIRECTORY) { pr_stdout(COLOR_BLUE "%s" COLOR_RESET " (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=dir)\n", recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), f_inode.ref_count, f_inode.size); - } else { - pr_stdout("%s (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=reg)\n", - recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), - f_inode.ref_count, f_inode.size); + } else if (f_inode.ftype == SYMLINK) { + char l_path[FS_MAX_PATH_LEN+1]; + int l_path_len = read_symlink(recs[k].inode_no, l_path); + + if (l_path_len > 0) + pr_stdout(COLOR_CYAN "%s" COLOR_RESET + " (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=lnk, -> '%s')\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size, l_path); + else + pr_stdout(COLOR_RED "%s" COLOR_RESET + " (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=lnk)\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size); } } } @@ -2454,15 +2475,29 @@ int fs_la(void *d) struct fs_inode f_inode; read_block(read_inode_ptr(recs[k].inode_no), (void *) &f_inode); - if (f_inode.ftype == DIRECTORY) { + if (f_inode.ftype == REGULAR) { + pr_stdout("%s (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=reg)\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size); + } else if (f_inode.ftype == DIRECTORY) { pr_stdout(COLOR_BLUE "%s" COLOR_RESET " (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=dir)\n", recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), f_inode.ref_count, f_inode.size); - } else { - pr_stdout("%s (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=reg)\n", - recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), - f_inode.ref_count, f_inode.size); + } else if (f_inode.ftype == SYMLINK) { + char l_path[FS_MAX_PATH_LEN+1]; + int l_path_len = read_symlink(recs[k].inode_no, l_path); + + if (l_path_len > 0) + pr_stdout(COLOR_CYAN "%s" COLOR_RESET + " (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=lnk, -> '%s')\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size, l_path); + else + pr_stdout(COLOR_RED "%s" COLOR_RESET + " (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=lnk)\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size); } } } @@ -2486,12 +2521,22 @@ int fs_stat(void *d) struct fs_inode i; read_block(read_inode_ptr(rp.target_inode_ptr), &i); - if (i.ftype == DIRECTORY) { - pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=dir\n", + if (i.ftype == REGULAR) { + pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=regular\n", rp.target_inode_ptr, read_inode_ptr(rp.target_inode_ptr), i.ref_count, i.size); - } else { - pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=reg\n", + } else if (i.ftype == DIRECTORY) { + pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=directory\n", rp.target_inode_ptr, read_inode_ptr(rp.target_inode_ptr), i.ref_count, i.size); + } else if (i.ftype == SYMLINK) { + char l_path[FS_MAX_PATH_LEN+1]; + int l_path_len = read_symlink(rp.target_inode_ptr, l_path); + if (l_path_len > 0) { + pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=symlink\npoints to: %s\n", + rp.target_inode_ptr, read_inode_ptr(rp.target_inode_ptr), i.ref_count, i.size, l_path); + } else { + pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=symlink\n\n", + rp.target_inode_ptr, read_inode_ptr(rp.target_inode_ptr), i.ref_count, i.size); + } } return 0;