From 9281b58398de76815c23361826615478a611bc60 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 10 Jul 2005 08:06:28 +0000 Subject: r8282: make the deletion of the smbd.tmp directory recursive. This cleans up the messaging directory (This used to be commit 783679e0df6c059ebd26f78115445e81e304bc84) --- source4/smbd/server.c | 85 ++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/source4/smbd/server.c b/source4/smbd/server.c index 717de0c523..f5f37a55cf 100644 --- a/source4/smbd/server.c +++ b/source4/smbd/server.c @@ -33,66 +33,61 @@ /* - 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); } -- cgit