]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
util: Create a convenience function to map files into memory
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 13:20:22 +0000 (13:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 13:20:22 +0000 (13:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/file.c
src/pakfire/util.c
src/pakfire/util.h

index 6e1be494e3202c330135e85557bbab76a66a3214..13b34f53cb1d210a9de2685dcf58b82b6326e3f0 100644 (file)
@@ -1131,8 +1131,6 @@ FILE* pakfire_file_fopen(struct pakfire_file* file, const char* mode) {
        Opens the file and returns a mapping of the payload in memory
 */
 static int pakfire_file_mmap(struct pakfire_file* file, char** data, size_t* length) {
-       struct stat st = {};
-       char* buffer = NULL;
        int fd = -EBADF;
        int r;
 
@@ -1141,26 +1139,13 @@ static int pakfire_file_mmap(struct pakfire_file* file, char** data, size_t* len
        if (fd < 0)
                goto ERROR;
 
-       // Stat the file
-       r = fstat(fd, &st);
+       // Map the file
+       r = pakfire_mmap(fd, data, length);
        if (r < 0) {
-               ERROR(file->ctx, "Could not stat %s: %m\n", pakfire_file_get_path(file));
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Map the data
-       buffer = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       if (buffer == MAP_FAILED) {
-               ERROR(file->ctx, "Could not map %s: %m\n", pakfire_file_get_path(file));
-               r = -errno;
+               ERROR(file->ctx, "Could not map %s: %s\n", pakfire_file_get_path(file), strerror(-r));
                goto ERROR;
        }
 
-       // Return the values
-       *data = buffer;
-       *length = st.st_size;
-
 ERROR:
        if (fd >= 0)
                close(fd);
index 943b8082647dc134a5de2a31c5ab81c4025b346c..634c45f0c7293256e2cb2af52628487e6fcaef90 100644 (file)
@@ -23,6 +23,7 @@
 #include <ftw.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/mman.h>
 #include <sys/resource.h>
 
 #include <json.h>
@@ -132,6 +133,37 @@ int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len) {
        return 0;
 }
 
+/*
+       Maps a file descriptor into memory
+*/
+int pakfire_mmap(int fd, char** data, size_t* length) {
+       char* buffer = NULL;
+       struct stat st = {};
+       int r;
+
+       // Check inputs
+       if (fd < 0)
+               return -EBADF;
+       else if (!data || !length)
+               return -EINVAL;
+
+       // Stat the file
+       r = fstat(fd, &st);
+       if (r < 0)
+               return -errno;
+
+       // Map the data
+       buffer = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       if (buffer == MAP_FAILED)
+               return -errno;
+
+       // Return the values
+       *data   = buffer;
+       *length = st.st_size;
+
+       return 0;
+}
+
 char* pakfire_generate_uuid(void) {
        uuid_t uuid;
 
index b2cdf80f1bfd39d44eddd7444fd7fadb25ce0a07..6310face78a9ed911950e6d10956f64044cbd225 100644 (file)
@@ -43,6 +43,7 @@ static inline void* pakfire_realloc(void* p, size_t size) {
 }
 
 int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len);
+int pakfire_mmap(int fd, char** data, size_t* length);
 
 const char* pakfire_path_relpath(const char* root, const char* path);