diff toys/chgrp.c @ 588:9c2277b92b86

Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra.
author Rob Landley <rob@landley.net>
date Fri, 01 Jun 2012 20:04:39 -0500
parents b88bc7dcdb48
children 7013fd450ff4
line wrap: on
line diff
--- a/toys/chgrp.c	Fri Jun 01 17:59:11 2012 -0500
+++ b/toys/chgrp.c	Fri Jun 01 20:04:39 2012 -0500
@@ -40,19 +40,12 @@
 
 static int do_chgrp(struct dirtree *node)
 {
-	int fd, ret = 1, flags = toys.optflags;
-
-	if (!dirtree_notdotdot(node)) return 0;
+	int ret, flags = toys.optflags;
 
-	// 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);
+	ret = dirtree_comeagain(node, flags & FLAG_R);
+	if (!ret || ret == DIRTREE_COMEAGAIN) return ret;
 
-	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 @@
 			perror_msg("changing group of '%s' to '%s'", path, TT.group_name);
 		free(path);
 	}
-	close(fd);
+	close(node->extra);
 	toys.exitval |= ret;
 
 	return 0;