r8282: make the deletion of the smbd.tmp directory recursive. This cleans up the...
authorAndrew Tridgell <tridge@samba.org>
Sun, 10 Jul 2005 08:06:28 +0000 (08:06 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:19:31 +0000 (13:19 -0500)
directory
(This used to be commit 783679e0df6c059ebd26f78115445e81e304bc84)

source4/smbd/server.c

index 717de0c5233d24132ce023139c2369ca9230e036..f5f37a55cfbd1e10963c2ba84880c627858d7608 100644 (file)
 
 
 /*
-  cleanup temporary files. This is the new alternative to
-  TDB_CLEAR_IF_FIRST. Unfortunately TDB_CLEAR_IF_FIRST is not
-  efficient on unix systems due to the lack of scaling of the byte
-  range locking system. So instead of putting the burden on tdb to
-  cleanup tmp files, this function deletes them. 
+  recursively delete a directory tree
 */
-static void cleanup_tmp_files(void)
+static void recursive_delete(const char *path)
 {
-       char *path;
        DIR *dir;
        struct dirent *de;
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
-
-       path = smbd_tmp_path(mem_ctx, NULL);
 
        dir = opendir(path);
        if (!dir) {
-               talloc_free(mem_ctx);
                return;
        }
 
        for (de=readdir(dir);de;de=readdir(dir)) {
-               /*
-                * Don't try to delete . and ..
-                */
-               if (strcmp(de->d_name, ".") != 0 &&
-                   strcmp(de->d_name, "..") != 0) {
-                   char *fname = talloc_asprintf(mem_ctx, "%s/%s", path, de->d_name);
-                   int ret = unlink(fname);
-                   if (ret == -1 &&
-                       errno != ENOENT &&
-                       errno != EPERM &&
-                       errno != EISDIR) {
-                           DEBUG(0,("Unabled to delete '%s' - %s\n", 
-                                     fname, strerror(errno)));
-                           smb_panic("unable to cleanup tmp files");
-                   }
-                   if (ret == -1 &&
-                       errno == EPERM) {
-                       /*
-                        * If it is a dir, don't complain
-                        * NOTE! The test will only happen if we have
-                        * sys/stat.h, otherwise we will always error out
-                        */
-#ifdef HAVE_SYS_STAT_H
-                       struct stat sb;
-                       if (stat(fname, &sb) != -1 &&
-                           !S_ISDIR(sb.st_mode))
-#endif
-                       {
-                            DEBUG(0,("Unable to delete '%s' - %s\n",
-                                     fname, strerror(errno)));
-                            smb_panic("unable to cleanup tmp files");
-                       }
-                   }
-                   talloc_free(fname);
+               char *fname;
+               struct stat st;
+
+               if (strcmp(de->d_name, ".") == 0 ||
+                   strcmp(de->d_name, "..") == 0) {
+                       continue;
+               }
+
+               fname = talloc_asprintf(path, "%s/%s", path, de->d_name);
+               if (stat(fname, &st) != 0) {
+                       continue;
+               }
+               if (S_ISDIR(st.st_mode)) {
+                       recursive_delete(fname);
+                       talloc_free(fname);
+                       continue;
+               }
+               if (unlink(fname) != 0) {
+                       DEBUG(0,("Unabled to delete '%s' - %s\n", 
+                                fname, strerror(errno)));
+                       smb_panic("unable to cleanup tmp files");
                }
+               talloc_free(fname);
        }
        closedir(dir);
+}
+
+/*
+  cleanup temporary files. This is the new alternative to
+  TDB_CLEAR_IF_FIRST. Unfortunately TDB_CLEAR_IF_FIRST is not
+  efficient on unix systems due to the lack of scaling of the byte
+  range locking system. So instead of putting the burden on tdb to
+  cleanup tmp files, this function deletes them. 
+*/
+static void cleanup_tmp_files(void)
+{
+       char *path;
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
+
+       path = smbd_tmp_path(mem_ctx, NULL);
 
+       recursive_delete(path);
        talloc_free(mem_ctx);
 }