#include <vlc_common.h>
#include <vlc_charset.h>
#include "libvlc.h" /* utf8_mkdir */
+#include <vlc_rand.h>
#include <assert.h>
#endif
#ifdef WIN32
# include <io.h>
+# ifndef UNDER_CE
+# include <direct.h>
+# endif
#else
# include <unistd.h>
#endif
# define lstat( a, b ) stat(a, b)
#endif
-#ifdef __APPLE__
-/* Define this if the OS always use UTF-8 internally */
-# define ASSUME_UTF8 1
-#endif
-
-#if defined (ASSUME_UTF8)
-/* Cool */
-#elif defined (WIN32) || defined (UNDER_CE)
-# define USE_MB2MB 1
-#elif defined (HAVE_ICONV)
-# define USE_ICONV 1
-#else
-# error No UTF8 charset conversion implemented on this platform!
+#ifdef WIN32
+static int convert_path (const char *restrict path, wchar_t *restrict wpath)
+{
+ if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
+ {
+ errno = ENOENT;
+ return -1;
+ }
+ wpath[MAX_PATH] = L'\0';
+ return 0;
+}
+# define CONVERT_PATH(path, wpath, err) \
+ wchar_t wpath[MAX_PATH+1]; \
+ if (convert_path (path, wpath)) \
+ return (err)
#endif
/**
/*_open translates to wchar internally on WinCE*/
return _open (filename, flags, mode);
#elif defined (WIN32)
- /* for Windows NT and above */
- wchar_t wpath[MAX_PATH + 1];
-
- if (!MultiByteToWideChar (CP_UTF8, 0, filename, -1, wpath, MAX_PATH))
- {
- errno = ENOENT;
- return -1;
- }
- wpath[MAX_PATH] = L'\0';
-
/*
- * open() cannot open files with non-“ANSI” characters on Windows.
- * We use _wopen() instead. Same thing for mkdir() and stat().
- */
+ * open() cannot open files with non-“ANSI” characters on Windows.
+ * We use _wopen() instead. Same thing for mkdir() and stat().
+ */
+ CONVERT_PATH(filename, wpath, -1);
return _wopen (wpath, flags, mode);
#endif
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;
}
*/
int utf8_mkdir( const char *dirname, mode_t mode )
{
-#if defined (UNDER_CE) || defined (WIN32)
- VLC_UNUSED( mode );
-
- wchar_t wname[MAX_PATH + 1];
- char mod[MAX_PATH + 1];
- int i;
-
- /* Convert '/' into '\' */
- for( i = 0; *dirname; i++ )
- {
- if( i == MAX_PATH )
- return -1; /* overflow */
-
- if( *dirname == '/' )
- mod[i] = '\\';
- else
- mod[i] = *dirname;
- dirname++;
-
- }
- mod[i] = 0;
-
- if( MultiByteToWideChar( CP_UTF8, 0, mod, -1, wname, MAX_PATH ) == 0 )
- {
- errno = ENOENT;
- return -1;
- }
- wname[MAX_PATH] = L'\0';
+#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);
- if( CreateDirectoryW( wname, NULL ) == 0 )
- {
- if( GetLastError( ) == ERROR_ALREADY_EXISTS )
- errno = EEXIST;
- else
- errno = ENOENT;
- return -1;
- }
- return 0;
#else
char *locname = ToLocale( dirname );
int res;
DIR *utf8_opendir( const char *dirname )
{
#ifdef WIN32
- wchar_t wname[MAX_PATH + 1];
+ CONVERT_PATH (dirname, wpath, NULL);
+ return (DIR *)vlc_wopendir (wpath);
- if (MultiByteToWideChar (CP_UTF8, 0, dirname, -1, wname, MAX_PATH))
- {
- wname[MAX_PATH] = L'\0';
- return (DIR *)vlc_wopendir (wname);
- }
#else
const char *local_name = ToLocale( dirname );
/*_stat translates to wchar internally on WinCE*/
return _stat( filename, buf );
#elif defined (WIN32)
- /* for Windows NT and above */
- wchar_t wpath[MAX_PATH + 1];
-
- if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
- {
- errno = ENOENT;
- return -1;
- }
- wpath[MAX_PATH] = L'\0';
-
- return _wstati64( wpath, buf );
+ CONVERT_PATH (filename, wpath, -1);
+ return _wstati64 (wpath, buf);
#endif
#ifdef HAVE_SYS_STAT_H
/*_open translates to wchar internally on WinCE*/
return _unlink( filename );
#elif defined (WIN32)
- /* for Windows NT and above */
- wchar_t wpath[MAX_PATH + 1];
+ CONVERT_PATH (filename, wpath, -1);
+ return _wunlink (wpath);
- if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
+#endif
+ const char *local_name = ToLocale( filename );
+
+ if( local_name == NULL )
{
errno = ENOENT;
return -1;
}
- wpath[MAX_PATH] = L'\0';
- /*
- * unlink() cannot open files with non-“ANSI” characters on Windows.
- * We use _wunlink() instead.
- */
- return _wunlink( wpath );
+ int ret = unlink( local_name );
+ 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);
+ return _wrename (wold, wnew);
+
#endif
- const char *local_name = ToLocale( filename );
+ const char *lo = ToLocale (oldpath);
+ if (lo == NULL)
+ goto error;
- if( local_name == NULL )
+ const char *ln = ToLocale (newpath);
+ if (ln == NULL)
{
+ LocaleFree (lo);
+error:
errno = ENOENT;
return -1;
}
- int ret = unlink( local_name );
- LocaleFree( local_name );
+ 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;
+}
+