From 25d3d0da11d20694b4b5ee088e59482f1e5bf1ef Mon Sep 17 00:00:00 2001 From: Dave Dykstra <2129743+DrDaveD@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:13:46 -0500 Subject: [PATCH] add "." and ".." in directory listings --- dir.c | 21 +++++++++++++++++++++ traverse.c | 13 +++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/dir.c b/dir.c index 2dbe945e..7c14cea3 100644 --- a/dir.c +++ b/dir.c @@ -66,6 +66,8 @@ sqfs_err sqfs_dir_open(sqfs *fs, sqfs_inode *inode, sqfs_dir *dir, dir->offset = 0; dir->total = inode->xtra.dir.dir_size <= 3 ? 0 : inode->xtra.dir.dir_size - 3; + /* The first two offsets indicate unstored "." and ".." files */ + dir->total += 2; if (offset) { /* Fast forward to the given offset */ @@ -129,6 +131,25 @@ bool sqfs_dir_next(sqfs *fs, sqfs_dir *dir, sqfs_dir_entry *entry, *err = SQFS_OK; entry->offset = dir->offset; + + if (entry->offset < 2) { + /* offsets 0 and 1 are '.' and '..' which are not stored */ + entry->type = SQUASHFS_DIR_TYPE; + if (entry->name != NULL) { + entry->name[0] = '.'; + entry->name_size = 1; + if (entry->offset == 1) { + entry->name[1] = '.'; + entry->name_size += 1; + } + entry->name[entry->name_size++] = '\0'; + } + entry->inode = 0; + entry->inode_number = 0; + entry->next_offset = dir->offset; + dir->offset += 1; + return true; + } while (dir->header.count == 0) { if (dir->offset >= dir->total) diff --git a/traverse.c b/traverse.c index e5407c9f..2a3e36a4 100644 --- a/traverse.c +++ b/traverse.c @@ -145,10 +145,19 @@ bool sqfs_traverse_next(sqfs_traverse *trv, sqfs_err *err) { found = sqfs_dir_next(trv->fs, &level->dir, &trv->entry, err); if (*err) goto error; - if (found) + if (found) { + char *name = trv->entry.name; + if ((name[0] == '.') && + ((name[1] == '\0') || + ((name[1] == '.') && + (name[2] == '\0')))) { + /* ignore '.' and '..' */ + break; + } trv->state = TRAVERSE_NAME_ADD; - else + } else { trv->state = TRAVERSE_ASCEND; + } break; case TRAVERSE_NAME_ADD: