From: Baptiste Daroussin Date: Tue, 2 Nov 2021 16:36:40 +0000 (+0100) Subject: cleanup: eliminate some concatstr for file descriptor manipulation X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59bb2d03ebf95de765ba5f94c34ee9eebaa94ef2;p=thirdparty%2Fmlmmj.git cleanup: eliminate some concatstr for file descriptor manipulation --- diff --git a/src/mlmmj-maintd.c b/src/mlmmj-maintd.c index c61ad1bd..64c6ca58 100644 --- a/src/mlmmj-maintd.c +++ b/src/mlmmj-maintd.c @@ -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); diff --git a/src/send_digest.c b/src/send_digest.c index 6535d32d..96e959ff 100644 --- a/src/send_digest.c +++ b/src/send_digest.c @@ -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");