]> git.sesse.net Git - vlc/blobdiff - src/text/filesystem.c
Add some fallback for language we have a translation
[vlc] / src / text / filesystem.c
index 304d7af6d637737f4a2ee629b1510b5297df0d17..f6bab4a9088975974872b7e2e6a5e17cfeaa4934 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>
 
@@ -84,11 +85,20 @@ static int convert_path (const char *restrict path, wchar_t *restrict wpath)
  *
  * @param filename file path to open (with UTF-8 encoding)
  * @param flags open() flags, see the C library open() documentation
- * @param mode file permissions if creating a new file
  * @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 utf8_open (const char *filename, int flags, mode_t mode)
+int utf8_open (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 UNDER_CE
     /*_open translates to wchar internally on WinCE*/
     return _open (filename, flags, mode);
@@ -109,7 +119,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;
 }
@@ -322,8 +348,7 @@ int utf8_loaddir( DIR *dir, char ***namelist,
 
         for( i = 0; i < num; i++ )
             free( tab[i] );
-        if( tab != NULL )
-            free( tab );
+        free( tab );
         }
     }
     return -1;
@@ -433,6 +458,49 @@ int utf8_unlink( const char *filename )
     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";
@@ -451,17 +519,15 @@ int utf8_mkstemp( char *template )
         return -1;
     }
 
-    uint64_t i_rand = mdate();
-
     /* */
     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++ )
-        {
-            i_rand = i_rand * UINT64_C(1103515245) + 12345;
-            psz_rand[j] = digits[((i_rand >> 16) & 0xffff) % i_digits];
-        }
+            psz_rand[j] = digits[pi_rand[j] % i_digits];
 
         /* */
         int fd = utf8_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );