From: Baptiste Daroussin Date: Thu, 4 Nov 2021 14:07:15 +0000 (+0100) Subject: prepstdreply: reduce memory allocation by using file descriptors X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe546a46ef32595e204fa86f523b831d6680bbcd;p=thirdparty%2Fmlmmj.git prepstdreply: reduce memory allocation by using file descriptors --- diff --git a/include/controls.h b/include/controls.h index af5e037f..2fa25106 100644 --- a/include/controls.h +++ b/include/controls.h @@ -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 */ diff --git a/src/controls.c b/src/controls.c index b893feba..9b9b9f9f 100644 --- a/src/controls.c +++ b/src/controls.c @@ -42,14 +42,22 @@ 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) { diff --git a/src/prepstdreply.c b/src/prepstdreply.c index 0e5fef6a..ca6d3442 100644 --- a/src/prepstdreply.c +++ b/src/prepstdreply.c @@ -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;