]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-maintd: simplify the code after the conversion to at functions
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 2 Nov 2021 16:50:59 +0000 (17:50 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 3 Nov 2021 13:10:28 +0000 (14:10 +0100)
src/mlmmj-maintd.c

index 64c6ca58dbb857048e62af349eda5c4bb94f1fc7..c84ca0c83c9a1a79b735fda614f205b21623962c 100644 (file)
@@ -933,7 +933,7 @@ int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
 void do_maintenance(const char *listdir, const char *mlmmjsend,
                    const char *mlmmjbounce, const char *mlmmjunsub)
 {
-       char *random, *logname, *logstr;
+       char *random, *logname;
        char timenow[64];
        struct stat st;
        int maintdlogfd;
@@ -946,12 +946,17 @@ void do_maintenance(const char *listdir, const char *mlmmjsend,
 
        mlmmj_list_init(&list);
 
-       if (stat(listdir, &st) < 0) {
+       if (!mlmmj_list_open(&list)) {
+               log_error(LOG_ARGS, "Could not open '%s'", list.dir);
+               return;
+       }
+
+       if (fstat(list.fd, &st) < 0) {
                log_error(LOG_ARGS, "Could not stat(%s) "
                                    "No maintenance run performed.", listdir);
                return;
        }
-       
+
        if(uid == 0) { /* We're root. Do something about it.*/
                if(setuid(st.st_uid) < 0) {
                        log_error(LOG_ARGS, "Could not setuid listdir owner.");
@@ -970,13 +975,12 @@ void do_maintenance(const char *listdir, const char *mlmmjsend,
                return;
        }
 
-       mlmmj_list_open(&list);
 
        random = random_str();
-       logname = concatstr(3, listdir, "/maintdlog-", random);
+       myasprintf(&logname, "maintdlog-%s", random);
        myfree(random);
 
-       maintdlogfd = open(logname, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR);
+       maintdlogfd = openat(list.fd, logname, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR);
        if(maintdlogfd < 0) {
                log_error(LOG_ARGS, "Could not open %s", logname);
                myfree(logname);
@@ -1020,14 +1024,11 @@ void do_maintenance(const char *listdir, const char *mlmmjsend,
 
        close(maintdlogfd);
 
-       logstr = concatstr(3, listdir, "/", MAINTD_LOGFILE);
-
-       if(rename(logname, logstr) < 0)
-               log_error(LOG_ARGS, "Could not rename(%s,%s)",
-                               logname, logstr);
+       if(renameat(list.fd, logname, list.fd, MAINTD_LOGFILE) < 0)
+               log_error(LOG_ARGS, "Could not rename(%s/%s,%s/"MAINTD_LOGFILE")",
+                   list.dir, logname, list.dir);
 
        myfree(logname);
-       myfree(logstr);
        mlmmj_list_close(&list);
 }
 
@@ -1035,8 +1036,7 @@ int main(int argc, char **argv)
 {
        int opt, daemonize = 1, ret = 0;
        char *bindir, *listdir = NULL, *mlmmjsend, *mlmmjbounce, *mlmmjunsub;
-       char *dirlists = NULL, *s, *listiter;
-       struct stat st;
+       char *dirlists = NULL, *listiter;
        struct dirent *dp;
        DIR *dirp;
 
@@ -1124,33 +1124,9 @@ int main(int argc, char **argv)
                        if((strcmp(dp->d_name, "..") == 0) ||
                                        (strcmp(dp->d_name, ".") == 0))
                                continue;
-                       
-                       listiter = concatstr(3, dirlists, "/", dp->d_name);
-
-                       if(stat(listiter, &st) < 0) {
-                               log_error(LOG_ARGS, "Could not stat(%s)",
-                                               listiter);
-                               myfree(listiter);
-                               continue;
-                       }
-
-                       if(!S_ISDIR(st.st_mode)) {
-                               myfree(listiter);
-                               continue;
-                       }
-
-                       s = concatstr(2, listiter, "/control/listaddress");
-                       ret = stat(s, &st);
-                       myfree(s);
-
-                       if(ret < 0) { /* If ret < 0 it's not a listiter */
-                               myfree(listiter);
-                               continue;
-                       }
-
+                       myasprintf(&listiter, "%s/%s", dirlists, dp->d_name);
                        do_maintenance(listiter, mlmmjsend, mlmmjbounce,
                                        mlmmjunsub);
-
                        myfree(listiter);
                }