diff options
author | Tilman Sauerbeck <tilman@code-monkey.de> | 2016-01-30 09:57:39 +0100 |
---|---|---|
committer | Juergen Daubert <jue@jue.li> | 2016-01-30 12:52:06 +0100 |
commit | a44631cd68deab2b5cc3316ed94998a44018bcfb (patch) | |
tree | ee35a189c1fabfaac2a9efb5c799cf768f0c1e56 /revdep/utility.c | |
parent | ba955ea3c852885b0303f16ca2e3d9da0f3a800b (diff) | |
download | prt-utils-a44631cd68deab2b5cc3316ed94998a44018bcfb.tar.gz prt-utils-a44631cd68deab2b5cc3316ed94998a44018bcfb.tar.xz |
revdep: When mmap()-ing a file, don't keep the fd around.
We can close the file as soon as mmap() succeeded.
Diffstat (limited to 'revdep/utility.c')
-rw-r--r-- | revdep/utility.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/revdep/utility.c b/revdep/utility.c index fcf6b41..67247e1 100644 --- a/revdep/utility.c +++ b/revdep/utility.c @@ -143,46 +143,48 @@ extern unsigned int string_crc32(const char *s) return hash; } -extern int open_file_in_memory(const char *path, int *fd, char **text, unsigned int *length) +extern int open_file_in_memory(const char *path, char **text, unsigned int *length) { struct stat st; + int fd; - if(path == NULL || path[0] == '\0' || fd == NULL || text == NULL || length == NULL) + if(path == NULL || path[0] == '\0' || text == NULL || length == NULL) { errno = EINVAL; return -1; } - if((fd[0] = open(path, O_RDONLY)) == -1) + if((fd = open(path, O_RDONLY)) == -1) { return -1; } - if(fstat(fd[0], &st) == -1) + if(fstat(fd, &st) == -1) { - close(fd[0]); + close(fd); return -1; } - if((text[0] = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd[0], 0)) == NULL) + if((text[0] = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == NULL) { - close(fd[0]); + close(fd); return -1; } + // We don't need to keep the fd around. + close(fd); + length[0] = st.st_size; return 0; } -extern void close_file_in_memory(int fd, char *text, unsigned int length) +extern void close_file_in_memory(char *text, unsigned int length) { - if(fd == -1 || text == NULL || length == 0) + if(text == NULL || length == 0) return; munmap(text, length); - - close(fd); } extern int parse_file_in_memory(char *text, parse_cb_t cb) |