From d336af4180542c9d526dece02d1a27232ef1ca6a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 6 Dec 2015 20:36:59 -0600 Subject: Rename dirtree->data to dirfd, and don't store symlink length in it. --- lib/dirtree.c | 21 +++++++++------------ lib/lib.h | 2 +- toys/posix/find.c | 2 +- toys/posix/ls.c | 3 ++- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/dirtree.c b/lib/dirtree.c index 1e898161..8b9f2993 100644 --- a/lib/dirtree.c +++ b/lib/dirtree.c @@ -32,7 +32,7 @@ struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags) if (name) { // open code this because haven't got node to call dirtree_parentfd() on yet - int fd = parent ? parent->data : AT_FDCWD; + int fd = parent ? parent->dirfd : AT_FDCWD; if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) goto error; @@ -48,10 +48,7 @@ struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags) memcpy(&(dt->st), &st, sizeof(struct stat)); strcpy(dt->name, name); - if (linklen) { - dt->symlink = memcpy(len+(char *)dt, libbuf, linklen); - dt->data = --linklen; - } + if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen); } return dt; @@ -96,7 +93,7 @@ char *dirtree_path(struct dirtree *node, int *plen) int dirtree_parentfd(struct dirtree *node) { - return node->parent ? node->parent->data : AT_FDCWD; + return node->parent ? node->parent->dirfd : AT_FDCWD; } // Handle callback for a node in the tree. Returns saved node(s) or NULL. @@ -118,7 +115,7 @@ struct dirtree *dirtree_handle_callback(struct dirtree *new, if (S_ISDIR(new->st.st_mode)) { if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { - new->data = openat(dirtree_parentfd(new), new->name, O_CLOEXEC); + new->dirfd = openat(dirtree_parentfd(new), new->name, O_CLOEXEC); flags = dirtree_recurse(new, callback, flags); } } @@ -132,8 +129,8 @@ struct dirtree *dirtree_handle_callback(struct dirtree *new, return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new; } -// Recursively read/process children of directory node (with dirfd in data), -// filtering through callback(). +// Recursively read/process children of directory node, filtering through +// callback(). Uses and closes supplied ->dirfd. int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node), int flags) @@ -142,13 +139,13 @@ int dirtree_recurse(struct dirtree *node, struct dirent *entry; DIR *dir; - if (node->data == -1 || !(dir = fdopendir(node->data))) { + if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) { if (!(flags & DIRTREE_SHUTUP)) { char *path = dirtree_path(node, 0); perror_msg("No %s", path); free(path); } - close(node->data); + close(node->dirfd); return flags; } @@ -174,7 +171,7 @@ int dirtree_recurse(struct dirtree *node, // This closes filehandle as well, so note it closedir(dir); - node->data = -1; + node->dirfd = -1; return flags; } diff --git a/lib/lib.h b/lib/lib.h index 3047d379..750e1240 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -67,7 +67,7 @@ struct dirtree { long extra; // place for user to store their stuff (can be pointer) struct stat st; char *symlink; - int data; // dirfd for directory, linklen for symlink + int dirfd; char again; char name[]; }; diff --git a/toys/posix/find.c b/toys/posix/find.c index 3019cb60..1e748feb 100644 --- a/toys/posix/find.c +++ b/toys/posix/find.c @@ -83,7 +83,7 @@ static int flush_exec(struct dirtree *new, struct exec_range *aa) // switch to directory for -execdir, or back to top if we have an -execdir // _and_ a normal -exec, or are at top of tree in -execdir - if (aa->dir && new->parent) rc = fchdir(new->parent->data); + if (aa->dir && new->parent) rc = fchdir(new->parent->dirfd); else if (TT.topdir != -1) rc = fchdir(TT.topdir); if (rc) { perror_msg("%s", new->name); diff --git a/toys/posix/ls.c b/toys/posix/ls.c index 7d15935c..04f64152 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -12,6 +12,7 @@ config LS default y help usage: ls [-ACFHLRSZacdfhiklmnpqrstux1] [directory...] + list files what to show: @@ -317,7 +318,7 @@ static void listfiles(int dirfd, struct dirtree *indir) } else { // Read directory contents. We dup() the fd because this will close it. // This reads/saves contents to display later, except for in "ls -1f" mode. - indir->data = dup(dirfd); + indir->dirfd = dup(dirfd); dirtree_recurse(indir, filter, DIRTREE_SYMFOLLOW*!!(flags&FLAG_L)); } -- cgit v1.2.3