switch_root: code shrink - use iterate_on_dir()

function                                             old     new   delta
rmrf                                                   -      46     +46
delete_contents                                      181      99     -82
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 46/-82)            Total: -36 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c
index f2674b5..901c0b8 100644
--- a/util-linux/switch_root.c
+++ b/util-linux/switch_root.c
@@ -68,11 +68,22 @@
 # define MS_MOVE     8192
 #endif
 
+static void delete_contents(const char *directory, dev_t rootdev);
+
+static int FAST_FUNC rmrf(const char *directory, struct dirent *d, void *rootdevp)
+{
+	char *newdir = concat_subpath_file(directory, d->d_name);
+	if (newdir) { // not . or ..
+		// Recurse to delete contents
+		delete_contents(newdir, *(dev_t*)rootdevp);
+		free(newdir);
+	}
+	return 0;
+}
+
 // Recursively delete contents of rootfs
 static void delete_contents(const char *directory, dev_t rootdev)
 {
-	DIR *dir;
-	struct dirent *d;
 	struct stat st;
 
 	// Don't descend into other filesystems
@@ -81,25 +92,7 @@
 
 	// Recursively delete the contents of directories
 	if (S_ISDIR(st.st_mode)) {
-		dir = opendir(directory);
-		if (dir) {
-			while ((d = readdir(dir))) {
-				char *newdir = d->d_name;
-
-				// Skip . and ..
-				if (DOT_OR_DOTDOT(newdir))
-					continue;
-
-				// Recurse to delete contents
-				newdir = concat_path_file(directory, newdir);
-				delete_contents(newdir, rootdev);
-				free(newdir);
-			}
-			closedir(dir);
-
-			// Directory should now be empty, zap it
-			rmdir(directory);
-		}
+		iterate_on_dir(directory, rmrf, &rootdev);
 	} else {
 		// It wasn't a directory, zap it
 		unlink(directory);