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, ...)
94 unsigned int mode = 0;
99 mode = va_arg (ap, unsigned int);
107 /*_open translates to wchar internally on WinCE*/
108 return _open (filename, flags, mode);
109 #elif defined (WIN32)
111 * open() cannot open files with non-“ANSI” characters on Windows.
112 * We use _wopen() instead. Same thing for mkdir() and stat().
114 CONVERT_PATH(filename, wpath, -1);
115 return _wopen (wpath, flags, mode);
118 const char *local_name = ToLocale (filename);
120 if (local_name == NULL)
126 int fd = open (local_name, flags, mode);
129 fcntl (fd, F_SETFD, FD_CLOEXEC);
132 LocaleFree (local_name);
137 * Opens a FILE pointer.
138 * @param filename file path, using UTF-8 encoding
139 * @param mode fopen file open mode
140 * @return NULL on error, an open FILE pointer on success.
142 FILE *utf8_fopen (const char *filename, const char *mode)
144 int rwflags = 0, oflags = 0;
147 for (const char *ptr = mode; *ptr; ptr++)
163 oflags |= O_CREAT | O_TRUNC;
178 int fd = utf8_open (filename, rwflags | oflags, 0666);
182 if (append && (lseek (fd, 0, SEEK_END) == -1))
188 FILE *stream = fdopen (fd, mode);
196 * Creates a directory using UTF-8 paths.
198 * @param dirname a UTF-8 string with the name of the directory that you
200 * @param mode directory permissions
201 * @return 0 on success, -1 on error (see errno).
203 int utf8_mkdir( const char *dirname, mode_t mode )
205 #if defined (UNDER_CE)
207 /* mkdir converts internally to wchar */
208 return _mkdir(dirname);
209 #elif defined (WIN32)
211 CONVERT_PATH (dirname, wpath, -1);
212 return _wmkdir (wpath);
215 char *locname = ToLocale( dirname );
218 if( locname == NULL )
223 res = mkdir( locname, mode );
225 LocaleFree( locname );
231 * Opens a DIR pointer.
233 * @param dirname UTF-8 representation of the directory name
234 * @return a pointer to the DIR struct, or NULL in case of error.
235 * Release with standard closedir().
237 DIR *utf8_opendir( const char *dirname )
240 CONVERT_PATH (dirname, wpath, NULL);
241 return (DIR *)vlc_wopendir (wpath);
244 const char *local_name = ToLocale( dirname );
246 if( local_name != NULL )
248 DIR *dir = opendir( local_name );
249 LocaleFree( local_name );
259 * Reads the next file name from an open directory.
261 * @param dir The directory that is being read
263 * @return a UTF-8 string of the directory entry.
264 * Use free() to free this memory.
266 char *utf8_readdir( DIR *dir )
269 struct _wdirent *ent = vlc_wreaddir (dir);
273 return FromWide (ent->d_name);
277 ent = readdir( (DIR *)dir );
281 return vlc_fix_readdir( ent->d_name );
285 static int dummy_select( const char *str )
292 * Does the same as utf8_scandir(), but takes an open directory pointer
293 * instead of a directory path.
295 int utf8_loaddir( DIR *dir, char ***namelist,
296 int (*select)( const char * ),
297 int (*compar)( const char **, const char ** ) )
300 select = dummy_select;
312 while( ( entry = utf8_readdir( dir ) ) != NULL )
316 if( !select( entry ) )
322 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
333 qsort( tab, num, sizeof( tab[0] ),
334 (int (*)( const void *, const void *))compar );
342 for( i = 0; i < num; i++ )
351 * Selects file entries from a directory, as GNU C scandir().
353 * @param dirname UTF-8 diretory path
354 * @param pointer [OUT] pointer set, on succesful completion, to the address
355 * of a table of UTF-8 filenames. All filenames must be freed with free().
356 * The table itself must be freed with free() as well.
358 * @return How many file names were selected (possibly 0),
359 * or -1 in case of error.
361 int utf8_scandir( const char *dirname, char ***namelist,
362 int (*select)( const char * ),
363 int (*compar)( const char **, const char ** ) )
365 DIR *dir = utf8_opendir (dirname);
370 val = utf8_loaddir (dir, namelist, select, compar);
376 static int utf8_statEx( const char *filename, struct stat *buf,
380 /*_stat translates to wchar internally on WinCE*/
381 return _stat( filename, buf );
382 #elif defined (WIN32)
383 CONVERT_PATH (filename, wpath, -1);
384 return _wstati64 (wpath, buf);
387 #ifdef HAVE_SYS_STAT_H
388 const char *local_name = ToLocale( filename );
390 if( local_name != NULL )
392 int res = deref ? stat( local_name, buf )
393 : lstat( local_name, buf );
394 LocaleFree( local_name );
403 * Finds file/inode informations, as stat().
404 * Consider using fstat() instead, if possible.
406 * @param filename UTF-8 file path
408 int utf8_stat( const char *filename, struct stat *buf)
410 return utf8_statEx( filename, buf, true );
414 * Finds file/inode informations, as lstat().
415 * Consider using fstat() instead, if possible.
417 * @param filename UTF-8 file path
419 int utf8_lstat( const char *filename, struct stat *buf)
421 return utf8_statEx( filename, buf, false );
427 * @param filename a UTF-8 string with the name of the file you want to delete.
428 * @return A 0 return value indicates success. A -1 return value indicates an
429 * error, and an error code is stored in errno
431 int utf8_unlink( const char *filename )
434 /*_open translates to wchar internally on WinCE*/
435 return _unlink( filename );
436 #elif defined (WIN32)
437 CONVERT_PATH (filename, wpath, -1);
438 return _wunlink (wpath);
441 const char *local_name = ToLocale( filename );
443 if( local_name == NULL )
449 int ret = unlink( local_name );
450 LocaleFree( local_name );
455 * Moves a file atomically. This only works within a single file system.
457 * @param oldpath path to the file before the move
458 * @param newpath intended path to the file after the move
459 * @return A 0 return value indicates success. A -1 return value indicates an
460 * error, and an error code is stored in errno
462 int utf8_rename (const char *oldpath, const char *newpath)
465 CONVERT_PATH (oldpath, wold, -1);
466 CONVERT_PATH (newpath, wnew, -1);
468 /* FIXME: errno support */
469 if (MoveFileW (wold, wnew))
474 return _wrename (wold, wnew);
478 const char *lo = ToLocale (oldpath);
482 const char *ln = ToLocale (newpath);
491 int ret = rename (lo, ln);
497 int utf8_mkstemp( char *template )
499 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
500 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
505 /* Check template validity */
506 const size_t i_length = strlen( template );
507 char *psz_rand = &template[i_length-6];
509 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
516 for( int i = 0; i < 256; i++ )
518 /* Create a pseudo random file name */
521 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
522 for( int j = 0; j < 6; j++ )
523 psz_rand[j] = digits[pi_rand[j] % i_digits];
526 int fd = utf8_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
529 if( errno != EEXIST )