From: Ramiro Polla Date: Thu, 15 Jul 2010 17:38:13 +0000 (-0300) Subject: Introduce and use x_fmmap() and x_munmap() X-Git-Tag: v3.1~191 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1fc590556fdca7297611aa2352b72e3ec3b0042e;p=thirdparty%2Fccache.git Introduce and use x_fmmap() and x_munmap() x_fmmap() opens, stats, and mmap()s a file. x_munmap() is currently just a wrapper around munmap(). --- diff --git a/ccache.c b/ccache.c index a2660ad5b..9073ce19b 100644 --- a/ccache.c +++ b/ccache.c @@ -32,7 +32,6 @@ #include #include -#include #include #include #include @@ -330,7 +329,6 @@ static void remember_include_file(char *path, size_t path_len) struct file_hash *h; struct mdfour fhash; struct stat st; - int fd = -1; char *data = (char *)-1; char *source; int result; @@ -364,27 +362,20 @@ static void remember_include_file(char *path, size_t path_len) } /* Let's hash the include file. */ - fd = open(path, O_RDONLY|O_BINARY); - if (fd == -1) { - cc_log("Failed to open include file %s", path); - goto failure; - } if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME) && st.st_mtime >= time_of_compilation) { cc_log("Include file %s too new", path); goto failure; } if (st.st_size > 0) { - data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + data = x_fmmap(path, &st.st_size, "include file"); if (data == (char *)-1) { - cc_log("Failed to mmap %s", path); goto failure; } source = data; } else { source = ""; } - close(fd); hash_start(&fhash); result = hash_source_code_string(&fhash, source, st.st_size, path); @@ -397,7 +388,7 @@ static void remember_include_file(char *path, size_t path_len) hash_result_as_bytes(&fhash, h->hash); h->size = fhash.totalN; hashtable_insert(included_files, path, h); - munmap(data, st.st_size); + x_munmap(data, st.st_size); return; failure: @@ -407,10 +398,7 @@ failure: ignore: free(path); if (data != (char *)-1) { - munmap(data, st.st_size); - } - if (fd != -1) { - close(fd); + x_munmap(data, st.st_size); } } @@ -442,26 +430,12 @@ static char *make_relative_path(char *path) */ static int process_preprocessed_file(struct mdfour *hash, const char *path) { - int fd; char *data; char *p, *q, *end; off_t size; - struct stat st; - fd = open(path, O_RDONLY); - if (fd == -1) { - cc_log("Failed to open %s", path); - return 0; - } - if (fstat(fd, &st) != 0) { - cc_log("Failed to fstat %s", path); - return 0; - } - size = st.st_size; - data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); - close(fd); + data = x_fmmap(path, &size, "preprocessed file"); if (data == (void *)-1) { - cc_log("Failed to mmap %s", path); return 0; } @@ -507,7 +481,7 @@ static int process_preprocessed_file(struct mdfour *hash, const char *path) q++; if (q >= end) { cc_log("Failed to parse included file path"); - munmap(data, size); + x_munmap(data, size); return 0; } /* q points to the beginning of an include file path */ @@ -532,7 +506,7 @@ static int process_preprocessed_file(struct mdfour *hash, const char *path) } hash_buffer(hash, p, (end - p)); - munmap(data, size); + x_munmap(data, size); return 1; } diff --git a/ccache.h b/ccache.h index 5c155f06a..5a99ebfb4 100644 --- a/ccache.h +++ b/ccache.h @@ -108,6 +108,8 @@ char *get_cwd(); size_t common_dir_prefix_length(const char *s1, const char *s2); char *get_relative_path(const char *from, const char *to); void update_mtime(const char *path); +void *x_fmmap(const char *fname, off_t *size, const char *errstr); +int x_munmap(void *addr, size_t length); void stats_update(enum stats stat); void stats_flush(void); diff --git a/configure.ac b/configure.ac index 08d0dad14..8753be9f9 100644 --- a/configure.ac +++ b/configure.ac @@ -171,7 +171,7 @@ AC_HEADER_DIRENT AC_HEADER_TIME AC_HEADER_SYS_WAIT -AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h) +AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h sys/mman.h) AC_CHECK_FUNCS(asprintf) AC_CHECK_FUNCS(gethostname) diff --git a/hashutil.c b/hashutil.c index 2b745b362..d8f00bd72 100644 --- a/hashutil.c +++ b/hashutil.c @@ -21,7 +21,6 @@ #include "murmurhashneutral2.h" #include -#include #include #include @@ -199,7 +198,6 @@ end: int hash_source_code_file(struct mdfour *hash, const char *path) { - int fd; struct stat st; char *data; int result; @@ -211,19 +209,13 @@ hash_source_code_file(struct mdfour *hash, const char *path) if (st.st_size == 0) { return HASH_SOURCE_CODE_OK; } - fd = open(path, O_RDONLY|O_BINARY); - if (fd == -1) { - cc_log("Failed to open %s", path); - return HASH_SOURCE_CODE_ERROR; - } - data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - close(fd); + + data = x_fmmap(path, &st.st_size, "source code file"); if (data == (void *)-1) { - cc_log("Failed to mmap %s", path); return HASH_SOURCE_CODE_ERROR; } result = hash_source_code_string(hash, data, st.st_size, path); - munmap(data, st.st_size); + x_munmap(data, st.st_size); return result; } diff --git a/unify.c b/unify.c index 18c47ec44..66d31847e 100644 --- a/unify.c +++ b/unify.c @@ -33,8 +33,6 @@ #include "ccache.h" #include -#include -#include #include #include #include @@ -246,31 +244,19 @@ static void unify(struct mdfour *hash, unsigned char *p, size_t size) */ int unify_hash(struct mdfour *hash, const char *fname) { - int fd; - struct stat st; + off_t size; char *map; - fd = open(fname, O_RDONLY|O_BINARY); - if (fd == -1 || fstat(fd, &st) != 0) { - cc_log("Failed to open preprocessor output %s", fname); + map = x_fmmap(fname, &size, "preprocessor output"); + if (map == (void *) -1) { stats_update(STATS_PREPROCESSOR); return -1; } - /* we use mmap() to make it easy to handle arbitrarily long - lines in preprocessor output. I have seen lines of over - 100k in length, so this is well worth it */ - map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - close(fd); - if (map == (char *)-1) { - cc_log("Failed to mmap %s", fname); - return -1; - } - /* pass it through the unifier */ - unify(hash, (unsigned char *)map, st.st_size); + unify(hash, (unsigned char *)map, size); - munmap(map, st.st_size); + x_munmap(map, size); return 0; } diff --git a/util.c b/util.c index d02f16bbf..cb58c0a6d 100644 --- a/util.c +++ b/util.c @@ -21,6 +21,9 @@ #include #include +#ifdef HAVE_SYS_MMAN_H +#include +#endif #include #include #include @@ -946,3 +949,40 @@ update_mtime(const char *path) utime(path, NULL); #endif } + +/* + * Map file into memory. Return a pointer to the mapped area if successful or + * -1 if any error occurred. The file size is also returned, + */ +void *x_fmmap(const char *fname, off_t *size, const char *errstr) +{ + struct stat st; + void *data = (void *) -1; + int fd = -1; + fd = open(fname, O_RDONLY | O_BINARY); + if (fd == -1) { + cc_log("Failed to open %s %s", errstr, fname); + goto error; + } + if (fstat(fd, &st) == -1) { + cc_log("Failed to fstat %s %s", errstr, fname); + close(fd); + goto error; + } + data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + close(fd); + if (data == (void *) -1) { + cc_log("Failed to mmap %s %s", errstr, fname); + } + *size = st.st_size; +error: + return data; +} + +/* + * Unmap file from memory. + */ +int x_munmap(void *addr, size_t length) +{ + return munmap(addr, length); +}