From: Vsevolod Stakhov Date: Mon, 15 Feb 2016 16:29:03 +0000 (+0000) Subject: Add common routine to map a file X-Git-Tag: 1.2.0~232 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2575a3dc952f1875037029fbeaadf3f724902611;p=thirdparty%2Frspamd.git Add common routine to map a file --- diff --git a/src/libutil/util.c b/src/libutil/util.c index e715c97cf8..30665dfb89 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2117,20 +2117,61 @@ rspamd_file_xopen (const char *fname, int oflags, guint mode) struct stat sb; int fd; + if (lstat (fname, &sb) == -1) { + + if (errno != ENOENT) { + return (-1); + } + } + else if (!S_ISREG (sb.st_mode)) { + return -1; + } + #ifdef HAVE_ONOFOLLOW fd = open (fname, oflags | O_NOFOLLOW, mode); #else fd = open (fname, oflags, mode); #endif + return (fd); +} + +gpointer +rspamd_file_xmap (const char *fname, guint mode, + gsize *size) +{ + gint fd; + struct stat sb; + gpointer map; + + g_assert (fname != NULL); + g_assert (size != NULL); + + if (mode & PROT_WRITE) { + fd = rspamd_file_xopen (fname, O_RDWR, 0); + } + else { + fd = rspamd_file_xopen (fname, O_RDONLY, 0); + } + if (fd == -1) { - return (-1); + return NULL; } if (fstat (fd, &sb) == -1 || !S_ISREG (sb.st_mode)) { close (fd); - return (-1); + + return NULL; } - return (fd); + map = mmap (NULL, sb.st_size, mode, MAP_SHARED, fd, 0); + close (fd); + + if (map == MAP_FAILED) { + return NULL; + } + + *size = sb.st_size; + + return map; } diff --git a/src/libutil/util.h b/src/libutil/util.h index 06b6f7d425..43b0eb01bf 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -441,4 +441,14 @@ struct event_base * event_get_base (struct event *ev); */ int rspamd_file_xopen (const char *fname, int oflags, guint mode); +/** + * Map file without following symlinks or special stuff + * @param fname filename + * @param mode mode to open + * @param size target size (must NOT be NULL) + * @return pointer to memory (should be freed using munmap) or NULL in case of error + */ +gpointer rspamd_file_xmap (const char *fname, guint mode, + gsize *size); + #endif