Mercurial > dovecot > original-hg > dovecot-1.2
changeset 6957:d421f14ba797 HEAD
Removed unlink_lockfiles() and replaced it with a simpler unlink_old_files()
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sat, 08 Dec 2007 17:05:17 +0200 |
parents | f0dbbc1f0586 |
children | 7ae8505870c1 |
files | src/lib/Makefile.am src/lib/unlink-lockfiles.c src/lib/unlink-lockfiles.h src/lib/unlink-old-files.c src/lib/unlink-old-files.h |
diffstat | 5 files changed, 93 insertions(+), 98 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/Makefile.am Sat Dec 08 16:41:35 2007 +0200 +++ b/src/lib/Makefile.am Sat Dec 08 17:05:17 2007 +0200 @@ -95,7 +95,7 @@ strfuncs.c \ unix-socket-create.c \ unlink-directory.c \ - unlink-lockfiles.c \ + unlink-old-files.c \ unichar.c \ utc-offset.c \ utc-mktime.c \ @@ -177,7 +177,7 @@ strfuncs.h \ unix-socket-create.h \ unlink-directory.h \ - unlink-lockfiles.h \ + unlink-old-files.h \ unichar.h \ utc-offset.h \ utc-mktime.h \
--- a/src/lib/unlink-lockfiles.c Sat Dec 08 16:41:35 2007 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */ - -#include "lib.h" -#include "str.h" -#include "unlink-lockfiles.h" - -#include <stdlib.h> -#include <signal.h> -#include <unistd.h> -#include <dirent.h> -#include <sys/stat.h> - -static int -unlink_lockfiles_dir(DIR *dirp, const char *dir, const char *pidprefix, - const char *otherprefix, time_t other_min_time) -{ - struct dirent *d; - struct stat st; - string_t *path; - unsigned int pidlen, otherlen; - int ret = 1; - - path = t_str_new(512); - pidlen = pidprefix == NULL ? 0 : strlen(pidprefix); - otherlen = otherprefix == NULL ? 0 : strlen(otherprefix); - - while ((d = readdir(dirp)) != NULL) { - const char *fname = d->d_name; - - if (pidprefix != NULL && - strncmp(fname, pidprefix, pidlen) == 0 && - is_numeric(fname+pidlen, '\0')) { - /* found a lock file from our host - see if the PID - is valid (meaning it exists, and the it's with - the same UID as us) */ - if (kill(atol(fname+pidlen), 0) == 0 || errno != ESRCH) - continue; /* valid */ - - str_truncate(path, 0); - str_printfa(path, "%s/%s", dir, fname); - if (unlink(str_c(path)) < 0 && errno != ENOENT) { - i_error("unlink(%s) failed: %m", str_c(path)); - ret = 0; - } - } else if (otherprefix != NULL && - strncmp(fname, otherprefix, otherlen) == 0) { - str_truncate(path, 0); - str_printfa(path, "%s/%s", dir, fname); - if (stat(str_c(path), &st) == 0 && - st.st_mtime < other_min_time && - st.st_ctime < other_min_time) - if (unlink(str_c(path)) < 0 && - errno != ENOENT) { - i_error("unlink(%s) failed: %m", - str_c(path)); - ret = 0; - } - } - } - return ret; -} - -int unlink_lockfiles(const char *dir, const char *pidprefix, - const char *otherprefix, time_t other_min_time) -{ - DIR *dirp; - int ret; - - /* check for any invalid access files */ - dirp = opendir(dir); - if (dirp == NULL) - return -1; - - T_FRAME( - ret = unlink_lockfiles_dir(dirp, dir, pidprefix, - otherprefix, other_min_time); - ); - - if (closedir(dirp) < 0) - i_error("closedir(%s) failed: %m", dir); - return ret; -}
--- a/src/lib/unlink-lockfiles.h Sat Dec 08 16:41:35 2007 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -#ifndef UNLINK_LOCKFILES_H -#define UNLINK_LOCKFILES_H - -/* Delete stale lock files. Filenames beginning with pidprefix<PID> are - deleted immediately if PID doesn't exist. Filenames beginning with - otherprefix are deleted if their mtime and ctime is older than - other_min_time. - - Returns 1 if everything was successful, 0 if some of the files - couldn't be deleted, -1 if directory couldn't be opened at all. */ -int unlink_lockfiles(const char *dir, const char *pidprefix, - const char *otherprefix, time_t other_min_time); - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/unlink-old-files.c Sat Dec 08 17:05:17 2007 +0200 @@ -0,0 +1,83 @@ +/* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "ioloop.h" +#include "str.h" +#include "unlink-old-files.h" + +#include <stdlib.h> +#include <signal.h> +#include <unistd.h> +#include <dirent.h> +#include <sys/stat.h> +#include <utime.h> + +static int +unlink_old_files_real(const char *dir, const char *prefix, time_t min_time) +{ + DIR *dirp; + struct dirent *d; + struct stat st; + string_t *path; + unsigned int prefix_len, dir_len; + + dirp = opendir(dir); + if (dirp == NULL) { + if (errno != ENOENT) + i_error("opendir(%s) failed: %m", dir); + return -1; + } + + path = t_str_new(256); + str_printfa(path, "%s/", dir); + dir_len = str_len(path); + + prefix_len = strlen(prefix); + while ((d = readdir(dirp)) != NULL) { + if (d->d_name[0] == '.' && + (d->d_name[1] == '\0' || + (d->d_name[1] == '.' && d->d_name[2] == '\0'))) { + /* skip . and .. */ + continue; + } + if (strncmp(d->d_name, prefix, prefix_len) != 0) + continue; + + str_truncate(path, dir_len); + str_append(path, d->d_name); + if (stat(str_c(path), &st) < 0) { + if (errno != ENOENT) + i_error("stat(%s) failed: %m", str_c(path)); + } else if (!S_ISDIR(st.st_mode) && st.st_ctime < min_time) { + if (unlink(str_c(path)) < 0 && errno != ENOENT) + i_error("unlink(%s) failed: %m", str_c(path)); + } + } + +#ifdef HAVE_DIRFD + if (fstat(dirfd(dirp), &st) < 0) + i_error("fstat(%s) failed: %m", dir); +#else + if (stat(dir, &st) < 0) + i_error("stat(%s) failed: %m", dir); +#endif + else if (st.st_atime < ioloop_time) { + /* mounted with noatime. update it ourself. */ + if (utime(dir, NULL) < 0 && errno != ENOENT) + i_error("utime(%s) failed: %m", dir); + } + + if (closedir(dirp) < 0) + i_error("closedir(%s) failed: %m", dir); + return 0; +} + +int unlink_old_files(const char *dir, const char *prefix, time_t min_time) +{ + int ret; + + T_FRAME( + ret = unlink_old_files_real(dir, prefix, min_time); + ); + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/unlink-old-files.h Sat Dec 08 17:05:17 2007 +0200 @@ -0,0 +1,8 @@ +#ifndef UNLINK_OLD_FILES_H +#define UNLINK_OLD_FILES_H + +/* Unlink all files from directory beginning with given prefix and having + ctime older than min_time. Returns -1 if there were some errors. */ +int unlink_old_files(const char *dir, const char *prefix, time_t min_time); + +#endif