]> git.sesse.net Git - vlc/commitdiff
add vlc_openat wrapper around openat
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 14 Feb 2010 11:33:23 +0000 (13:33 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 14 Feb 2010 11:33:23 +0000 (13:33 +0200)
include/vlc_fs.h
src/libvlccore.sym
src/text/filesystem.c

index 51bfd60c8e44564e832473ebb8df1831cfe1f5de..42d5b9b9c50a560cab87f175442b30ecc7b8ce33 100644 (file)
@@ -33,6 +33,7 @@
 
 VLC_EXPORT( int, vlc_open, ( const char *filename, int flags, ... ) LIBVLC_USED );
 VLC_EXPORT( FILE *, vlc_fopen, ( const char *filename, const char *mode ) LIBVLC_USED );
+VLC_EXPORT( int, vlc_openat, ( int fd, const char *filename, int flags, ... ) LIBVLC_USED );
 
 VLC_EXPORT( DIR *, vlc_opendir, ( const char *dirname ) LIBVLC_USED );
 VLC_EXPORT( char *, vlc_readdir, ( DIR *dir ) LIBVLC_USED );
index d293878c176696a24b7cb72fd44fe00f08b9c941..d620078fc0daf75bf624670a3e45cde7042983fe 100644 (file)
@@ -434,6 +434,7 @@ utf8_lstat
 vlc_mkdir
 vlc_mkstemp
 vlc_open
+vlc_openat
 vlc_opendir
 vlc_readdir
 vlc_scandir
index 0e3723a41e02bad7a7daab931e807208b6906fcf..b514da7a24dfcdee2ce2f4e6611b61a5c9af9aa6 100644 (file)
@@ -193,6 +193,53 @@ FILE *vlc_fopen (const char *filename, const char *mode)
     return stream;
 }
 
+/**
+ * Opens a system file handle relative to an existing directory handle.
+ *
+ * @param dir directory file descriptor
+ * @param filename file path to open (with UTF-8 encoding)
+ * @param flags open() flags, see the C library open() documentation
+ * @return a file handle on success, -1 on error (see errno).
+ * @note Contrary to standard open(), this function returns file handles
+ * with the close-on-exec flag enabled.
+ */
+int vlc_openat (int dir, const char *filename, int flags, ...)
+{
+    unsigned int mode = 0;
+    va_list ap;
+
+    va_start (ap, flags);
+    if (flags & O_CREAT)
+        mode = va_arg (ap, unsigned int);
+    va_end (ap);
+
+#ifdef O_CLOEXEC
+    flags |= O_CLOEXEC;
+#endif
+
+    const char *local_name = ToLocale (filename);
+    if (local_name == NULL)
+    {
+        errno = ENOENT;
+        return -1;
+    }
+
+#ifdef HAVE_FDOPENDIR
+    int fd = openat (dir, local_name, flags, mode);
+# ifdef HAVE_FCNTL
+    if (fd != -1)
+        fcntl (fd, F_SETFD, FD_CLOEXEC);
+# endif
+#else
+    int fd = -1;
+    errno = ENOSYS;
+#endif
+
+    LocaleFree (local_name);
+    return fd;
+}
+
+
 /**
  * Creates a directory using UTF-8 paths.
  *