From 4fa1b32871f661a7e11898b274ec162d52c23ee9 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 1 Jun 2012 20:04:39 -0500 Subject: Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra. --- toys/chgrp.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'toys/chgrp.c') diff --git a/toys/chgrp.c b/toys/chgrp.c index 5cf43896..935e1781 100644 --- a/toys/chgrp.c +++ b/toys/chgrp.c @@ -40,19 +40,12 @@ DEFINE_GLOBALS( static int do_chgrp(struct dirtree *node) { - int fd, ret = 1, flags = toys.optflags; + int ret, flags = toys.optflags; - if (!dirtree_notdotdot(node)) return 0; + ret = dirtree_comeagain(node, flags & FLAG_R); + if (!ret || ret == DIRTREE_COMEAGAIN) return ret; - // Handle recursion, and make it depth first - if (S_ISDIR(node->st.st_mode)) { - if (!node->extra) node->extra = dup(node->data); - if ((flags & FLAG_R) && node->data != -1) return DIRTREE_COMEAGAIN; - fd = node->extra; - } else fd = openat(node->parent ? node->parent->data : AT_FDCWD, - node->name, 0); - - if (fd != -1) ret = fchown(fd, -1, TT.group); + if (node->extra != -1) ret = fchown(node->extra, -1, TT.group); if (ret || (flags & FLAG_v)) { char *path = dirtree_path(node, 0); @@ -62,7 +55,7 @@ static int do_chgrp(struct dirtree *node) perror_msg("changing group of '%s' to '%s'", path, TT.group_name); free(path); } - close(fd); + close(node->extra); toys.exitval |= ret; return 0; -- cgit v1.2.3