]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
prepstdreply: reduce memory allocation by using file descriptors
authorBaptiste Daroussin <bapt@FreeBSD.org>
Thu, 4 Nov 2021 14:07:15 +0000 (15:07 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Thu, 4 Nov 2021 14:07:15 +0000 (15:07 +0100)
include/controls.h
src/controls.c
src/prepstdreply.c

index af5e037fc72cd5601c555afbd0609054e53bf5e3..2fa2510687e42577fa36e89a4ecf1fa22eee1d7a 100644 (file)
@@ -35,5 +35,6 @@ time_t ctrltimet(struct mlmmj_list *list, const char *ctrlstr);
 long ctrllong(struct mlmmj_list *list, const char *ctrlstr);
 int ctrlint(struct mlmmj_list *list, const char *ctrlstr);
 size_t ctrlsizet(struct mlmmj_list *list, const char *ctrlstr);
+int open_control_file(struct mlmmj_list *list, const char *ctrlstr);
 
 #endif /* STATCTRL_H */
index b893febacdcd3539c630203792503eb646d5d3ee..9b9b9f9f1246b7b50308527e18fb3d48f2b4175c 100644 (file)
 static void
 open_control(struct mlmmj_list *list)
 {
-       if (list->controlfd == -1)
-               list->controlfd = openat(list->fd, "control", O_DIRECTORY|O_CLOEXEC);
+       if (list->controlfd != -1)
+               return;
+       list->controlfd = openat(list->fd, "control", O_DIRECTORY|O_CLOEXEC);
        if (list->controlfd == -1) {
                log_error(LOG_ARGS, "Unable to open %s/control", list->dir);
                errx(EXIT_FAILURE, "Unable to open %s/control", list->dir);
        }
 }
 
+int
+open_control_file(struct mlmmj_list *list, const char *ctrlstr)
+{
+       open_control(list);
+       return (openat(list->controlfd, ctrlstr, O_RDONLY));
+}
+
 static char *
 readfile(int dirfd, const char *filename, bool oneline)
 {
index 0e5fef6abc4f763a1f1e0f31bb0f24be1c385537..ca6d344294aabc8f7da852c0c46d51d1e603027a 100644 (file)
@@ -452,7 +452,6 @@ char *substitute(const char *line, struct mlmmj_list *list, text *txt)
        return new;
 }
 
-
 static bool
 open_textdirs(struct mlmmj_list *list)
 {
@@ -467,6 +466,14 @@ open_textdirs(struct mlmmj_list *list)
        return (true);
 }
 
+static int
+open_textfile(struct mlmmj_list *list, const char *filename)
+{
+       if (!open_textdirs(list))
+               return -1;
+       return (openat(list->textfd, filename, O_RDONLY|O_CLOEXEC));
+}
+
 text *open_text_file(struct mlmmj_list *list, const char *filename)
 {
        text *txt;
@@ -602,12 +609,11 @@ void register_formatted(text *txt, const char *token,
 
 
 static void begin_new_source_file(text *txt, char **line_p, char **pos_p,
-               int *width_p, const char *filename, int transparent) {
+               int *width_p, int fd, int transparent) {
        char *line = *line_p;
        char *pos = *pos_p;
        char *tmp, *esc;
        source *src;
-       int fd;
        int i;
 
        /* Save any later lines for use after finishing the source */
@@ -620,7 +626,6 @@ static void begin_new_source_file(text *txt, char **line_p, char **pos_p,
                txt->src->processedwidth = 0;
        }
 
-       fd = open(filename, O_RDONLY);
        if (fd < 0) {
                /* Act as if the source were an empty line */
                **pos_p = '\0';
@@ -827,7 +832,6 @@ static int handle_directive(text *txt, char **line_p, char **pos_p,
        char *pos = *pos_p;
        char *token = pos + 1;
        char *endpos;
-       char *filename;
        int limit;
        formatted *fmt;
        conditional *cond;
@@ -1022,19 +1026,15 @@ static int handle_directive(text *txt, char **line_p, char **pos_p,
        } else if(strncmp(token, "control ", 8) == 0) {
                token = filename_token(token + 8);
                if (token != NULL) {
-                       filename = concatstr(3, list, "/control/", token);
-                       begin_new_source_file(txt,
-                                       line_p, pos_p, width_p, filename, 0);
-                       myfree(filename);
+                       int fd = open_control_file(list, token);
+                       begin_new_source_file(txt, line_p, pos_p, width_p, fd, 0);
                        return 0;
                }
        } else if(strncmp(token, "text ", 5) == 0) {
                token = filename_token(token + 5);
                if (token != NULL) {
-                       filename = concatstr(3, list, "/text/", token);
-                       begin_new_source_file(txt,
-                                       line_p, pos_p, width_p, filename, 0);
-                       myfree(filename);
+                       int fd = open_textfile(list, token);
+                       begin_new_source_file(txt, line_p, pos_p, width_p, fd, 0);
                        return 0;
                }
        } else if(strncmp(token, "originalmail", 12) == 0 &&
@@ -1051,8 +1051,9 @@ static int handle_directive(text *txt, char **line_p, char **pos_p,
                        if (token != NULL) limit = atol(token);
                }
                if (limit != 0) {
+                       int fd = open(txt->mailname, O_RDONLY);
                        begin_new_source_file(txt, line_p, pos_p, width_p,
-                                       txt->mailname, 1);
+                                       fd, 1);
                        if (limit == -1) txt->src->limit = -1;
                        else txt->src->limit = limit - 1;
                        return 0;