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>
34 #include "libvlc.h" /* vlc_mkdir */
41 #include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
64 # define lstat( a, b ) stat(a, b)
68 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
70 if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
75 wpath[MAX_PATH] = L'\0';
78 # define CONVERT_PATH(path, wpath, err) \
79 wchar_t wpath[MAX_PATH+1]; \
80 if (convert_path (path, wpath)) \
85 * Opens a system file handle.
87 * @param filename file path to open (with UTF-8 encoding)
88 * @param flags open() flags, see the C library open() documentation
89 * @return a file handle on success, -1 on error (see errno).
90 * @note Contrary to standard open(), this function returns file handles
91 * with the close-on-exec flag enabled.
93 int vlc_open (const char *filename, int flags, ...)
95 unsigned int mode = 0;
100 mode = va_arg (ap, unsigned int);
108 /*_open translates to wchar internally on WinCE*/
109 return _open (filename, flags, mode);
110 #elif defined (WIN32)
112 * open() cannot open files with non-“ANSI” characters on Windows.
113 * We use _wopen() instead. Same thing for mkdir() and stat().
115 CONVERT_PATH(filename, wpath, -1);
116 return _wopen (wpath, flags, mode);
119 const char *local_name = ToLocale (filename);
121 if (local_name == NULL)
127 int fd = open (local_name, flags, mode);
130 fcntl (fd, F_SETFD, FD_CLOEXEC);
133 LocaleFree (local_name);
138 * Opens a FILE pointer.
139 * @param filename file path, using UTF-8 encoding
140 * @param mode fopen file open mode
141 * @return NULL on error, an open FILE pointer on success.
143 FILE *vlc_fopen (const char *filename, const char *mode)
145 int rwflags = 0, oflags = 0;
148 for (const char *ptr = mode; *ptr; ptr++)
164 oflags |= O_CREAT | O_TRUNC;
179 int fd = vlc_open (filename, rwflags | oflags, 0666);
183 if (append && (lseek (fd, 0, SEEK_END) == -1))
189 FILE *stream = fdopen (fd, mode);
197 * Creates a directory using UTF-8 paths.
199 * @param dirname a UTF-8 string with the name of the directory that you
201 * @param mode directory permissions
202 * @return 0 on success, -1 on error (see errno).
204 int vlc_mkdir( const char *dirname, mode_t mode )
206 #if defined (UNDER_CE)
208 /* mkdir converts internally to wchar */
209 return _mkdir(dirname);
210 #elif defined (WIN32)
212 CONVERT_PATH (dirname, wpath, -1);
213 return _wmkdir (wpath);
216 char *locname = ToLocale( dirname );
219 if( locname == NULL )
224 res = mkdir( locname, mode );
226 LocaleFree( locname );
232 * Opens a DIR pointer.
234 * @param dirname UTF-8 representation of the directory name
235 * @return a pointer to the DIR struct, or NULL in case of error.
236 * Release with standard closedir().
238 DIR *vlc_opendir( const char *dirname )
241 CONVERT_PATH (dirname, wpath, NULL);
242 return (DIR *)vlc_wopendir (wpath);
245 const char *local_name = ToLocale( dirname );
247 if( local_name != NULL )
249 DIR *dir = opendir( local_name );
250 LocaleFree( local_name );
260 * Reads the next file name from an open directory.
262 * @param dir The directory that is being read
264 * @return a UTF-8 string of the directory entry.
265 * Use free() to free this memory.
267 char *vlc_readdir( DIR *dir )
270 struct _wdirent *ent = vlc_wreaddir (dir);
274 return FromWide (ent->d_name);
278 ent = readdir( (DIR *)dir );
282 return vlc_fix_readdir( ent->d_name );
286 static int dummy_select( const char *str )
293 * Does the same as vlc_scandir(), but takes an open directory pointer
294 * instead of a directory path.
296 int vlc_loaddir( DIR *dir, char ***namelist,
297 int (*select)( const char * ),
298 int (*compar)( const char **, const char ** ) )
301 select = dummy_select;
313 while( ( entry = vlc_readdir( dir ) ) != NULL )
317 if( !select( entry ) )
323 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
334 qsort( tab, num, sizeof( tab[0] ),
335 (int (*)( const void *, const void *))compar );
343 for( i = 0; i < num; i++ )
352 * Selects file entries from a directory, as GNU C scandir().
354 * @param dirname UTF-8 diretory path
355 * @param pointer [OUT] pointer set, on succesful completion, to the address
356 * of a table of UTF-8 filenames. All filenames must be freed with free().
357 * The table itself must be freed with free() as well.
359 * @return How many file names were selected (possibly 0),
360 * or -1 in case of error.
362 int vlc_scandir( const char *dirname, char ***namelist,
363 int (*select)( const char * ),
364 int (*compar)( const char **, const char ** ) )
366 DIR *dir = vlc_opendir (dirname);
371 val = vlc_loaddir (dir, namelist, select, compar);
377 static int vlc_statEx( const char *filename, struct stat *buf,
381 /*_stat translates to wchar internally on WinCE*/
382 return _stat( filename, buf );
383 #elif defined (WIN32)
384 CONVERT_PATH (filename, wpath, -1);
385 return _wstati64 (wpath, buf);
388 #ifdef HAVE_SYS_STAT_H
389 const char *local_name = ToLocale( filename );
391 if( local_name != NULL )
393 int res = deref ? stat( local_name, buf )
394 : lstat( local_name, buf );
395 LocaleFree( local_name );
404 * Finds file/inode informations, as stat().
405 * Consider using fstat() instead, if possible.
407 * @param filename UTF-8 file path
409 int vlc_stat( const char *filename, struct stat *buf)
411 return vlc_statEx( filename, buf, true );
415 * Finds file/inode informations, as lstat().
416 * Consider using fstat() instead, if possible.
418 * @param filename UTF-8 file path
420 int utf8_lstat( const char *filename, struct stat *buf)
422 return vlc_statEx( filename, buf, false );
428 * @param filename a UTF-8 string with the name of the file you want to delete.
429 * @return A 0 return value indicates success. A -1 return value indicates an
430 * error, and an error code is stored in errno
432 int vlc_unlink( const char *filename )
435 /*_open translates to wchar internally on WinCE*/
436 return _unlink( filename );
437 #elif defined (WIN32)
438 CONVERT_PATH (filename, wpath, -1);
439 return _wunlink (wpath);
442 const char *local_name = ToLocale( filename );
444 if( local_name == NULL )
450 int ret = unlink( local_name );
451 LocaleFree( local_name );
456 * Moves a file atomically. This only works within a single file system.
458 * @param oldpath path to the file before the move
459 * @param newpath intended path to the file after the move
460 * @return A 0 return value indicates success. A -1 return value indicates an
461 * error, and an error code is stored in errno
463 int vlc_rename (const char *oldpath, const char *newpath)
466 CONVERT_PATH (oldpath, wold, -1);
467 CONVERT_PATH (newpath, wnew, -1);
469 /* FIXME: errno support */
470 if (MoveFileW (wold, wnew))
475 return _wrename (wold, wnew);
479 const char *lo = ToLocale (oldpath);
483 const char *ln = ToLocale (newpath);
492 int ret = rename (lo, ln);
498 int vlc_mkstemp( char *template )
500 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
501 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
506 /* Check template validity */
507 const size_t i_length = strlen( template );
508 char *psz_rand = &template[i_length-6];
510 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
517 for( int i = 0; i < 256; i++ )
519 /* Create a pseudo random file name */
522 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
523 for( int j = 0; j < 6; j++ )
524 psz_rand[j] = digits[pi_rand[j] % i_digits];
527 int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
530 if( errno != EEXIST )