]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgparse: load_cfg_in_mem: take in account file size
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Wed, 7 Aug 2024 14:31:25 +0000 (16:31 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 7 Aug 2024 16:41:41 +0000 (18:41 +0200)
Let's take in account the given file size, when its reported via stat.

It's very convenient for large configuration files, as this allows to
perform only the one memory allocation call for precisely needeed file size.
This also allows to perform only the one call to fread().

We need to provide to fread() file_stat.st_size + 1 to be able to grab EOF.
Like this it sets feof(f)=1 flag and this allows to exit from the loop
immediately, just after fread call.

If /dev/stdin or /dev/null is provided as a file, we continue to read the
configuration chunk by chunk, stat doesn't report the size.

src/cfgparse.c

index bbaab763669995da1f3a67184241112a254018e1..a5dcf7aaf605ea326c884036bc1ee1da857b1858 100644 (file)
@@ -1759,10 +1759,23 @@ ssize_t load_cfg_in_mem(char *filename, char **cfg_content)
        size_t bytes_to_read = LINESIZE;
        size_t chunk_size = 0;
        size_t read_bytes = 0;
+       struct stat file_stat;
        char *new_area;
        size_t ret = 0;
        FILE *f;
 
+       /* let's try to obtain the size, if regular file */
+       if (stat(filename, &file_stat) != 0) {
+               ha_alert("stat() failed for configuration file %s : %s\n",
+                        filename, strerror(errno));
+               return -1;
+       }
+
+       if (file_stat.st_size) {
+               /* as we need to read EOF to have feof(f)=1 */
+               bytes_to_read = file_stat.st_size + 1;
+       }
+
        if ((f = fopen(filename,"r")) == NULL) {
                ha_alert("Could not open configuration file %s : %s\n",
                         filename, strerror(errno));