]> git.ipfire.org Git - pakfire.git/commitdiff
linter: Always mmap() the file
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 17:41:31 +0000 (17:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 26 Oct 2024 17:41:31 +0000 (17:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/linter-file.c

index f0f51a856ad1c673945af3a16c5355010e32420f..6dd8dfef5afb2a0bc1506da8b07daac1b53df477 100644 (file)
@@ -50,7 +50,7 @@ struct pakfire_linter_file {
        size_t length;
 
        // Mapped Data
-       void* data;
+       char* data;
 
        // Path
        const char* path;
@@ -63,6 +63,23 @@ struct pakfire_linter_file {
 #define pakfire_linter_file_error(lfile, format, ...) \
        pakfire_linter_result(lfile->linter, lfile->file, PAKFIRE_LINTER_ERROR, format, ## __VA_ARGS__)
 
+/*
+       Maps the file into memory
+*/
+static int pakfire_linter_file_map(struct pakfire_linter_file* lfile) {
+       // Store the length
+       lfile->length = lseek(lfile->fd, 0, SEEK_END);
+       if (lfile->length <= 0)
+               return -errno;
+
+       // Map the data
+       lfile->data = mmap(NULL, lfile->length, PROT_READ, MAP_PRIVATE, lfile->fd, 0);
+       if (lfile->data == MAP_FAILED)
+               return -errno;
+
+       return 0;
+}
+
 int pakfire_linter_file_create(struct pakfire_linter_file** lfile,
                struct pakfire_ctx* ctx, struct pakfire_linter* linter, struct pakfire_file* file, int fd) {
        struct pakfire_linter_file* l = NULL;
@@ -104,11 +121,10 @@ int pakfire_linter_file_create(struct pakfire_linter_file** lfile,
                goto ERROR;
        }
 
-       // Store the length
-       l->length = lseek(l->fd, 0, SEEK_END);
-       if (l->length <= 0) {
-               ERROR(l->ctx, "Could not determine the length of the file: %m\n");
-               r = -errno;
+       // Map the file
+       r = pakfire_linter_file_map(l);
+       if (r < 0) {
+               ERROR(l->ctx, "Could not map the file: %s\n", strerror(-r));
                goto ERROR;
        }
 
@@ -155,23 +171,6 @@ struct pakfire_linter_file* pakfire_linter_file_unref(struct pakfire_linter_file
        return NULL;
 }
 
-/*
-       Maps the file into memory
-*/
-static const char* pakfire_linter_file_map(struct pakfire_linter_file* lfile) {
-       // Map the data if not already done so
-       if (!lfile->data) {
-               lfile->data = mmap(NULL, lfile->length, PROT_READ, MAP_PRIVATE, lfile->fd, 0);
-               if (lfile->data == MAP_FAILED) {
-                       ERROR(lfile->ctx, "Could not mmap() %s: %m\n", lfile->path);
-
-                       lfile->data = NULL;
-               }
-       }
-
-       return (const char*)lfile->data;
-}
-
 static int pakfire_linter_file_check_caps(struct pakfire_linter_file* lfile) {
        // Files cannot have capabilities but not be executable
        if (!pakfire_file_is_executable(lfile->file) && pakfire_file_has_caps(lfile->file))
@@ -185,7 +184,6 @@ static int pakfire_linter_file_check_caps(struct pakfire_linter_file* lfile) {
 
 static int __pakfire_linter_file_get_script_interpreter(struct pakfire_linter_file* lfile,
                char* interpreter, size_t length) {
-       const char* data = NULL;
        char shebang[PATH_MAX];
        char* eol = NULL;
        char* p = NULL;
@@ -203,24 +201,19 @@ static int __pakfire_linter_file_get_script_interpreter(struct pakfire_linter_fi
        if (lfile->length <= 4)
                return 0;
 
-       // Map the file into memory
-       data = pakfire_linter_file_map(lfile);
-       if (!data)
-               return -errno;
-
        // The file must start with #!
-       if (data[0] == '#' && data[1] == '!') {
+       if (lfile->data[0] == '#' && lfile->data[1] == '!') {
                // Scan for the end of the first line
-               eol = memchr(data, '\n', lfile->length);
+               eol = memchr(lfile->data, '\n', lfile->length);
                if (!eol) {
                        ERROR(lfile->ctx, "Could not find end-of-line in %s: %m\n", lfile->path);
                        return -errno;
                }
 
-               int l = eol - data;
+               int l = eol - lfile->data;
 
                // Copy the line into a buffer
-               r = pakfire_string_format(shebang, "%.*s", l, data);
+               r = pakfire_string_format(shebang, "%.*s", l, lfile->data);
                if (r < 0)
                        return r;