]> git.sesse.net Git - vlc/blobdiff - src/text/filesystem.c
Made picture_Hold return the provided picture.
[vlc] / src / text / filesystem.c
index 7cd4455206c858677febd15f0bf83c0e7a779c4a..bc602ae5f366c3511e74af4cf750a67a4d37efca 100644 (file)
@@ -31,6 +31,7 @@
 #include <vlc_common.h>
 #include <vlc_charset.h>
 #include "libvlc.h" /* utf8_mkdir */
+#include <vlc_rand.h>
 
 #include <assert.h>
 
@@ -51,7 +52,9 @@
 #endif
 #ifdef WIN32
 # include <io.h>
-# include <direct.h>
+# ifndef UNDER_CE
+#  include <direct.h>
+# endif
 #else
 # include <unistd.h>
 #endif
@@ -107,7 +110,23 @@ int utf8_open (const char *filename, int flags, mode_t mode)
         return -1;
     }
 
-    int fd = open (local_name, flags, mode);
+    int fd;
+
+#ifdef O_CLOEXEC
+    fd = open (local_name, flags | O_CLOEXEC, mode);
+    if (fd == -1 && errno == EINVAL)
+#endif
+    {
+        fd = open (local_name, flags, mode);
+#ifdef HAVE_FCNTL
+        if (fd != -1)
+        {
+            int flags = fcntl (fd, F_GETFD);
+            fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
+        }
+#endif
+    }
+
     LocaleFree (local_name);
     return fd;
 }
@@ -181,7 +200,11 @@ FILE *utf8_fopen (const char *filename, const char *mode)
  */
 int utf8_mkdir( const char *dirname, mode_t mode )
 {
-#if defined (UNDER_CE) || defined (WIN32)
+#if defined (UNDER_CE)
+    (void) mode;
+    /* mkdir converts internally to wchar */
+    return _mkdir(dirname);
+#elif defined (WIN32)
     (void) mode;
     CONVERT_PATH (dirname, wpath, -1);
     return _wmkdir (wpath);
@@ -426,3 +449,87 @@ int utf8_unlink( const char *filename )
     LocaleFree( local_name );
     return ret;
 }
+
+/**
+ * Moves a file atomically. This only works within a single file system.
+ *
+ * @param oldpath path to the file before the move
+ * @param newpath intended path to the file after the move
+ * @return A 0 return value indicates success. A -1 return value indicates an
+ *        error, and an error code is stored in errno
+ */
+int utf8_rename (const char *oldpath, const char *newpath)
+{
+#if defined (WIN32)
+    CONVERT_PATH (oldpath, wold, -1);
+    CONVERT_PATH (newpath, wnew, -1);
+# ifdef UNDER_CE
+    /* FIXME: errno support */
+    if (MoveFileW (wold, wnew))
+        return 0;
+    else
+        return -1;
+#else
+    return _wrename (wold, wnew);
+#endif
+
+#endif
+    const char *lo = ToLocale (oldpath);
+    if (lo == NULL)
+        goto error;
+
+    const char *ln = ToLocale (newpath);
+    if (ln == NULL)
+    {
+        LocaleFree (lo);
+error:
+        errno = ENOENT;
+        return -1;
+    }
+
+    int ret = rename (lo, ln);
+    LocaleFree (lo);
+    LocaleFree (ln);
+    return ret;
+}
+
+int utf8_mkstemp( char *template )
+{
+    static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
+
+    /* */
+    assert( template );
+
+    /* Check template validity */
+    const size_t i_length = strlen( template );
+    char *psz_rand = &template[i_length-6];
+
+    if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    /* */
+    for( int i = 0; i < 256; i++ )
+    {
+        /* Create a pseudo random file name */
+        uint8_t pi_rand[6];
+
+        vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
+        for( int j = 0; j < 6; j++ )
+            psz_rand[j] = digits[pi_rand[j] % i_digits];
+
+        /* */
+        int fd = utf8_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
+        if( fd >= 0 )
+            return fd;
+        if( errno != EEXIST )
+            return -1;
+    }
+
+    errno = EEXIST;
+    return -1;
+}
+