diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-02-02 23:42:42 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-02-02 23:42:42 +0000 |
commit | 63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f (patch) | |
tree | c921bf01006b5a0b4ee8122bcde547d973ce4d28 /src/libutil | |
parent | 29f54d263c352640cfcf386a062f5ff37b401f7e (diff) | |
download | rspamd-63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f.tar.gz rspamd-63f8b66c21b2731408f34d7ab4a2d09d2e41cc2f.zip |
Add plain open file API method
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/util.c | 31 | ||||
-rw-r--r-- | src/libutil/util.h | 9 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c index c4f9ef242..6893172c9 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2096,3 +2096,34 @@ event_get_base (struct event *ev) return ev->ev_base; } #endif + +int +rspamd_file_xopen (const char *fname, int oflags, guint mode) +{ + struct stat sb; + int fd; + char *rp, rp_buf[PATH_MAX]; + + rp = realpath (fname, rp_buf); + + if (rp == NULL) { + return -1; + } + +#ifdef HAVE_ONOFOLLOW + fd = open (fname, oflags | O_NOFOLLOW, mode); +#else + fd = open (fname, oflags, mode); +#endif + + if (fd == -1) { + return (-1); + } + + if (fstat (fd, &sb) == -1 || !S_ISREG (sb.st_mode)) { + close (fd); + return (-1); + } + + return (fd); +} diff --git a/src/libutil/util.h b/src/libutil/util.h index b4de2331a..06b6f7d42 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -432,4 +432,13 @@ struct event_base * event_get_base (struct event *ev); event_set((ev), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg)) #endif +/** + * Open file without following symlinks or special stuff + * @param fname filename + * @param oflags open flags + * @param mode mode to open + * @return fd or -1 in case of error + */ +int rspamd_file_xopen (const char *fname, int oflags, guint mode); + #endif |