Proposed patch to fix potential double rmdir() when --parents is
specified.

Example:  Execution of

   rmdir -p a/b/c/

Will try to remove dir a/b/c twice -- once in main() and again in
remove_parents() -- because of confusion over trailing slashes.

All trailing slashes must be removed before the call to
remove_parents(), or rather before the while() loop in remove_parents()
is entered.

Ciao.
kw.

diff -Nru fileutils-4.1.8.ORG/src/rmdir.c fileutils-4.1.8/src/rmdir.c
--- fileutils-4.1.8.ORG/src/rmdir.c     Sun Dec  2 17:15:54 2001
+++ fileutils-4.1.8/src/rmdir.c Thu Apr  4 12:33:04 2002
@@ -98,6 +98,27 @@
   char *slash;
   int fail = 0;
 
+  /* All trailing slashes must be removed before the while() is entered.
+     Otherwise we'll try to rmdir(path) -- which we've already done
+     in main() and hence wind up with an ENOENT here.  Example:
+
+     kw@vader[6]:/opt/build$ mkdir -p a/b      
+     kw@vader[6]:/opt/build$ rmdir -p --verbose a/b/
+     rmdir: removing directory, a/b/
+     rmdir: removing directory, a/b
+     rmdir: `a/b': No such file or directory
+                                                                        */
+
+  slash = strrchr (path, '/');
+  if (slash == NULL)
+    return fail;
+  if (slash[1] == '\0')
+    {
+      while (slash > path && *slash == '/')
+        --slash;
+      slash[1] = 0;
+    }
+  
   while (1)
     {
       slash = strrchr (path, '/');

Reply via email to