summaryrefslogtreecommitdiffstats
path: root/src/libutil/util.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-02-15 16:29:03 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-02-15 16:29:03 +0000
commit2575a3dc952f1875037029fbeaadf3f724902611 (patch)
tree1b9457746b50822112f2a78da8228eb1a8bfc54f /src/libutil/util.c
parent4f3a539f03893f0ddff60ff2b529a8fb1d22fab6 (diff)
downloadrspamd-2575a3dc952f1875037029fbeaadf3f724902611.tar.gz
rspamd-2575a3dc952f1875037029fbeaadf3f724902611.zip
Add common routine to map a file
Diffstat (limited to 'src/libutil/util.c')
-rw-r--r--src/libutil/util.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c
index e715c97cf..30665dfb8 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;
}