mirror of
https://github.com/palera1n/palera1n.git
synced 2026-10-28 12:09:48 +01:00
code cleanup
This commit is contained in:
1 parent
cd80b66f28
commit
c9537d7a46
8 files changed
+485
-393
No files matched your search
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <getopt.h>
|
||||
#include <sys/mman.h>
|
||||
#include <errno.h>
|
||||
#include "common.h"
|
||||
|
||||
int override_file(override_file_t *finfo, niarelap_file_t** orig, unsigned int *orig_len, char *filename) {
|
||||
int ret = 0;
|
||||
int fd = open(filename, O_RDONLY);
|
||||
LOG(LOG_VERBOSE5, "override_file() called!");
|
||||
if (fd == -1) {
|
||||
LOG(LOG_ERROR, "Cannot open file %s: %d (%s)", filename, errno, strerror(errno));
|
||||
return errno;
|
||||
}
|
||||
LOG(LOG_VERBOSE5, "override_file: opened %s!", filename);
|
||||
struct stat st;
|
||||
ret = fstat(fd, &st);
|
||||
if (ret) {
|
||||
LOG(LOG_ERROR, "Cannot fstat fd from file %s: %d (%s)", filename, errno, strerror(errno));
|
||||
return errno;
|
||||
}
|
||||
LOG(LOG_VERBOSE5, "override_file: fstat fd %d succeeded!", fd);
|
||||
void *addr = mmap(NULL, st.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
|
||||
if (addr == MAP_FAILED) {
|
||||
LOG(LOG_ERROR, "Failed to map file %s: %d (%s)", filename, errno, strerror(errno));
|
||||
return errno;
|
||||
}
|
||||
LOG(LOG_VERBOSE5, "override_file: Override file mapped successfully");
|
||||
finfo->magic = OVERRIDE_MAGIC;
|
||||
finfo->fd = fd;
|
||||
finfo->len = (unsigned int)st.st_size;;
|
||||
finfo->ptr = *(niarelap_file_t*)addr;
|
||||
finfo->orig_len = *orig_len;
|
||||
finfo->orig_ptr = **orig;
|
||||
*orig = (niarelap_file_t*)addr;
|
||||
*orig_len = (unsigned int)st.st_size;
|
||||
LOG(LOG_VERBOSE5, "override_file() finished!");
|
||||
return 0;
|
||||
};
|
||||
Reference in new issue
Block a user