]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
cleanup: eliminate some concatstr for file descriptor manipulation
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 2 Nov 2021 16:36:40 +0000 (17:36 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 3 Nov 2021 13:10:28 +0000 (14:10 +0100)
src/mlmmj-maintd.c
src/send_digest.c

index c61ad1bdf05aafb41a77df6bd41dfdc584be6cb7..64c6ca58dbb857048e62af349eda5c4bb94f1fc7 100644 (file)
@@ -822,12 +822,11 @@ int unsub_bouncers(struct mlmmj_list *list, const char *mlmmjunsub)
 int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
 {
        char *lasttimestr, *lastindexstr, *lastissuestr;
-       char *digestname, *indexname;
        char *digestintervalstr, *digestmaxmailsstr;
        char *s1, *s2;
        time_t digestinterval, t, lasttime;
        long digestmaxmails, lastindex, index, lastissue;
-       int fd, indexfd, lock;
+       int fd, indexfd;
 
        if (statctrl(list, "noarchive")) {
                return 0;
@@ -849,22 +848,11 @@ int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
                digestmaxmails = DIGESTMAXMAILS;
        }
 
-       digestname = concatstr(2, list->dir, "/lastdigest");
-       fd = open(digestname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+       fd = openat(list->fd, "latestdigest", O_RDWR | O_CREAT | O_EXLOCK, S_IRUSR | S_IWUSR);
        if (fd < 0) {
-               log_error(LOG_ARGS, "Could not open '%s'", digestname);
-               myfree(digestname);
+               log_error(LOG_ARGS, "Could not open '%s/latestdigest'", list->dir);
                return 1;
        }
-       
-       lock = myexcllock(fd);
-       if(lock) {
-               log_error(LOG_ARGS, "Error locking lastdigest");
-               myfree(digestname);
-               close(fd);
-               return 1;
-       }
-
        s1 = mygetline(fd);
 
        /* Syntax is lastindex:lasttime or lastindex:lasttime:lastissue */
@@ -881,9 +869,8 @@ int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
                lastindex = atol(lastindexstr);
        } else {
                if (s1 && (strlen(s1) > 0)) {
-                       log_error(LOG_ARGS, "'%s' contains malformed data",
-                                       digestname);
-                       myfree(digestname);
+                       log_error(LOG_ARGS, "'%s/latestdigest' contains malformed data",
+                                       list->dir);
                        myfree(s1);
                        close(fd);
                        return 1;
@@ -893,13 +880,10 @@ int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
                lastindex = 0;
                lastissue = 0;
        }
-       
-       indexname = concatstr(2, list->dir, "/index");
-       indexfd = open(indexname, O_RDONLY);
+
+       indexfd = openat(list->fd, "index", O_RDONLY);
        if (indexfd < 0) {
-               log_error(LOG_ARGS, "Could not open '%s'", indexname);
-               myfree(digestname);
-               myfree(indexname);
+               log_error(LOG_ARGS, "Could not open '%s/index'", list->dir);
                myfree(s1);
                close(fd);
                return 1;
@@ -909,8 +893,6 @@ int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
        if (!s2) {
                /* If we don't have an index, no mails have been sent to the
                 * list, and therefore we don't need to send a digest */
-               myfree(digestname);
-               myfree(indexname);
                myfree(s1);
                close(fd);
                return 1;
@@ -931,18 +913,16 @@ int run_digests(struct mlmmj_list *list, const char *mlmmjsend)
                }
 
                if (lseek(fd, 0, SEEK_SET) < 0) {
-                       log_error(LOG_ARGS, "Could not seek '%s'", digestname);
+                       log_error(LOG_ARGS, "Could not seek '%s/latestdigest'", list->dir);
                } else {
                        /* index + ':' + time + ':' + issue + '\n' + '\0' */
                        if (dprintf(fd, "%ld:%ld:%ld\n", index, (long)t, lastissue) == -1) {
-                               log_error(LOG_ARGS, "Could not write new '%s'",
-                                               digestname);
+                               log_error(LOG_ARGS, "Could not write new '%s/latestdigest'",
+                                               list->dir);
                        }
                }
        }
 
-       myfree(digestname);
-       myfree(indexname);
        myfree(s1);
        myfree(s2);
        close(fd);
index 6535d32dc3061487c89acc6431a8667b651635c5..96e959ffcd5f277df488dcd7ddd1ff1ff50bbee1 100644 (file)
@@ -271,10 +271,7 @@ int send_digest(struct mlmmj_list *list, int firstindex, int lastindex,
                return -1;
        }
 
-       tmp = concatstr(2, list->dir, "/control/customheaders");
-       hdrfd = open(tmp, O_RDONLY);
-       myfree(tmp);
-
+       hdrfd = openat(list->fd, "control/customheaders", O_RDONLY);
        boundary = random_str();
 
        txt = open_text_file(list, "digest");