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;
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);
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/mman.h>
#include <sys/resource.h>
#include <json.h>
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;
}
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);