size_t length;
// Mapped Data
- void* data;
+ char* data;
// Path
const char* path;
#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;
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;
}
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))
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;
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;