1 /*****************************************************************************
2 * filesystem.c: File system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
5 * Copyright © 2005-2008 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "libvlc.h" /* utf8_mkdir */
40 #include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
63 # define lstat( a, b ) stat(a, b)
67 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
69 if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
74 wpath[MAX_PATH] = L'\0';
77 # define CONVERT_PATH(path, wpath, err) \
78 wchar_t wpath[MAX_PATH+1]; \
79 if (convert_path (path, wpath)) \
84 * Opens a system file handle.
86 * @param filename file path to open (with UTF-8 encoding)
87 * @param flags open() flags, see the C library open() documentation
88 * @return a file handle on success, -1 on error (see errno).
89 * @note Contrary to standard open(), this function returns file handles
90 * with the close-on-exec flag enabled.
92 int utf8_open (const char *filename, int flags, ...)
99 mode = va_arg (ap, mode_t);
103 /*_open translates to wchar internally on WinCE*/
104 return _open (filename, flags, mode);
105 #elif defined (WIN32)
107 * open() cannot open files with non-“ANSI” characters on Windows.
108 * We use _wopen() instead. Same thing for mkdir() and stat().
110 CONVERT_PATH(filename, wpath, -1);
111 return _wopen (wpath, flags, mode);
114 const char *local_name = ToLocale (filename);
116 if (local_name == NULL)
125 fd = open (local_name, flags | O_CLOEXEC, mode);
126 if (fd == -1 && errno == EINVAL)
129 fd = open (local_name, flags, mode);
133 int flags = fcntl (fd, F_GETFD);
134 fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
139 LocaleFree (local_name);
144 * Opens a FILE pointer.
145 * @param filename file path, using UTF-8 encoding
146 * @param mode fopen file open mode
147 * @return NULL on error, an open FILE pointer on success.
149 FILE *utf8_fopen (const char *filename, const char *mode)
151 int rwflags = 0, oflags = 0;
154 for (const char *ptr = mode; *ptr; ptr++)
170 oflags |= O_CREAT | O_TRUNC;
185 int fd = utf8_open (filename, rwflags | oflags, 0666);
189 if (append && (lseek (fd, 0, SEEK_END) == -1))
195 FILE *stream = fdopen (fd, mode);
203 * Creates a directory using UTF-8 paths.
205 * @param dirname a UTF-8 string with the name of the directory that you
207 * @param mode directory permissions
208 * @return 0 on success, -1 on error (see errno).
210 int utf8_mkdir( const char *dirname, mode_t mode )
212 #if defined (UNDER_CE)
214 /* mkdir converts internally to wchar */
215 return _mkdir(dirname);
216 #elif defined (WIN32)
218 CONVERT_PATH (dirname, wpath, -1);
219 return _wmkdir (wpath);
222 char *locname = ToLocale( dirname );
225 if( locname == NULL )
230 res = mkdir( locname, mode );
232 LocaleFree( locname );
238 * Opens a DIR pointer.
240 * @param dirname UTF-8 representation of the directory name
241 * @return a pointer to the DIR struct, or NULL in case of error.
242 * Release with standard closedir().
244 DIR *utf8_opendir( const char *dirname )
247 CONVERT_PATH (dirname, wpath, NULL);
248 return (DIR *)vlc_wopendir (wpath);
251 const char *local_name = ToLocale( dirname );
253 if( local_name != NULL )
255 DIR *dir = opendir( local_name );
256 LocaleFree( local_name );
266 * Reads the next file name from an open directory.
268 * @param dir The directory that is being read
270 * @return a UTF-8 string of the directory entry.
271 * Use free() to free this memory.
273 char *utf8_readdir( DIR *dir )
276 struct _wdirent *ent = vlc_wreaddir (dir);
280 return FromWide (ent->d_name);
284 ent = readdir( (DIR *)dir );
288 return vlc_fix_readdir( ent->d_name );
292 static int dummy_select( const char *str )
299 * Does the same as utf8_scandir(), but takes an open directory pointer
300 * instead of a directory path.
302 int utf8_loaddir( DIR *dir, char ***namelist,
303 int (*select)( const char * ),
304 int (*compar)( const char **, const char ** ) )
307 select = dummy_select;
319 while( ( entry = utf8_readdir( dir ) ) != NULL )
323 if( !select( entry ) )
329 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
340 qsort( tab, num, sizeof( tab[0] ),
341 (int (*)( const void *, const void *))compar );
349 for( i = 0; i < num; i++ )
358 * Selects file entries from a directory, as GNU C scandir().
360 * @param dirname UTF-8 diretory path
361 * @param pointer [OUT] pointer set, on succesful completion, to the address
362 * of a table of UTF-8 filenames. All filenames must be freed with free().
363 * The table itself must be freed with free() as well.
365 * @return How many file names were selected (possibly 0),
366 * or -1 in case of error.
368 int utf8_scandir( const char *dirname, char ***namelist,
369 int (*select)( const char * ),
370 int (*compar)( const char **, const char ** ) )
372 DIR *dir = utf8_opendir (dirname);
377 val = utf8_loaddir (dir, namelist, select, compar);
383 static int utf8_statEx( const char *filename, struct stat *buf,
387 /*_stat translates to wchar internally on WinCE*/
388 return _stat( filename, buf );
389 #elif defined (WIN32)
390 CONVERT_PATH (filename, wpath, -1);
391 return _wstati64 (wpath, buf);
394 #ifdef HAVE_SYS_STAT_H
395 const char *local_name = ToLocale( filename );
397 if( local_name != NULL )
399 int res = deref ? stat( local_name, buf )
400 : lstat( local_name, buf );
401 LocaleFree( local_name );
410 * Finds file/inode informations, as stat().
411 * Consider using fstat() instead, if possible.
413 * @param filename UTF-8 file path
415 int utf8_stat( const char *filename, struct stat *buf)
417 return utf8_statEx( filename, buf, true );
421 * Finds file/inode informations, as lstat().
422 * Consider using fstat() instead, if possible.
424 * @param filename UTF-8 file path
426 int utf8_lstat( const char *filename, struct stat *buf)
428 return utf8_statEx( filename, buf, false );
434 * @param filename a UTF-8 string with the name of the file you want to delete.
435 * @return A 0 return value indicates success. A -1 return value indicates an
436 * error, and an error code is stored in errno
438 int utf8_unlink( const char *filename )
441 /*_open translates to wchar internally on WinCE*/
442 return _unlink( filename );
443 #elif defined (WIN32)
444 CONVERT_PATH (filename, wpath, -1);
445 return _wunlink (wpath);
448 const char *local_name = ToLocale( filename );
450 if( local_name == NULL )
456 int ret = unlink( local_name );
457 LocaleFree( local_name );
462 * Moves a file atomically. This only works within a single file system.
464 * @param oldpath path to the file before the move
465 * @param newpath intended path to the file after the move
466 * @return A 0 return value indicates success. A -1 return value indicates an
467 * error, and an error code is stored in errno
469 int utf8_rename (const char *oldpath, const char *newpath)
472 CONVERT_PATH (oldpath, wold, -1);
473 CONVERT_PATH (newpath, wnew, -1);
475 /* FIXME: errno support */
476 if (MoveFileW (wold, wnew))
481 return _wrename (wold, wnew);
485 const char *lo = ToLocale (oldpath);
489 const char *ln = ToLocale (newpath);
498 int ret = rename (lo, ln);
504 int utf8_mkstemp( char *template )
506 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
507 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
512 /* Check template validity */
513 const size_t i_length = strlen( template );
514 char *psz_rand = &template[i_length-6];
516 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
523 for( int i = 0; i < 256; i++ )
525 /* Create a pseudo random file name */
528 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
529 for( int j = 0; j < 6; j++ )
530 psz_rand[j] = digits[pi_rand[j] % i_digits];
533 int fd = utf8_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
536 if( errno != EEXIST )