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 * Opens a system file handle relative to an existing directory handle.
199 * @param dir directory file descriptor
200 * @param filename file path to open (with UTF-8 encoding)
201 * @param flags open() flags, see the C library open() documentation
202 * @return a file handle on success, -1 on error (see errno).
203 * @note Contrary to standard open(), this function returns file handles
204 * with the close-on-exec flag enabled.
206 int vlc_openat (int dir, const char *filename, int flags, ...)
208 unsigned int mode = 0;
211 va_start (ap, flags);
213 mode = va_arg (ap, unsigned int);
220 const char *local_name = ToLocale (filename);
221 if (local_name == NULL)
227 #ifdef HAVE_FDOPENDIR
228 int fd = openat (dir, local_name, flags, mode);
231 fcntl (fd, F_SETFD, FD_CLOEXEC);
238 LocaleFree (local_name);
244 * Creates a directory using UTF-8 paths.
246 * @param dirname a UTF-8 string with the name of the directory that you
248 * @param mode directory permissions
249 * @return 0 on success, -1 on error (see errno).
251 int vlc_mkdir( const char *dirname, mode_t mode )
253 #if defined (UNDER_CE)
255 /* mkdir converts internally to wchar */
256 return _mkdir(dirname);
257 #elif defined (WIN32)
259 CONVERT_PATH (dirname, wpath, -1);
260 return _wmkdir (wpath);
263 char *locname = ToLocale( dirname );
266 if( locname == NULL )
271 res = mkdir( locname, mode );
273 LocaleFree( locname );
279 * Opens a DIR pointer.
281 * @param dirname UTF-8 representation of the directory name
282 * @return a pointer to the DIR struct, or NULL in case of error.
283 * Release with standard closedir().
285 DIR *vlc_opendir( const char *dirname )
288 CONVERT_PATH (dirname, wpath, NULL);
289 return (DIR *)vlc_wopendir (wpath);
292 const char *local_name = ToLocale( dirname );
294 if( local_name != NULL )
296 DIR *dir = opendir( local_name );
297 LocaleFree( local_name );
307 * Reads the next file name from an open directory.
309 * @param dir The directory that is being read
311 * @return a UTF-8 string of the directory entry.
312 * Use free() to free this memory.
314 char *vlc_readdir( DIR *dir )
317 struct _wdirent *ent = vlc_wreaddir (dir);
321 return FromWide (ent->d_name);
325 ent = readdir( (DIR *)dir );
329 return vlc_fix_readdir( ent->d_name );
333 static int dummy_select( const char *str )
340 * Does the same as vlc_scandir(), but takes an open directory pointer
341 * instead of a directory path.
343 int vlc_loaddir( DIR *dir, char ***namelist,
344 int (*select)( const char * ),
345 int (*compar)( const char **, const char ** ) )
348 select = dummy_select;
360 while( ( entry = vlc_readdir( dir ) ) != NULL )
364 if( !select( entry ) )
370 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
381 qsort( tab, num, sizeof( tab[0] ),
382 (int (*)( const void *, const void *))compar );
390 for( i = 0; i < num; i++ )
399 * Selects file entries from a directory, as GNU C scandir().
401 * @param dirname UTF-8 diretory path
402 * @param pointer [OUT] pointer set, on succesful completion, to the address
403 * of a table of UTF-8 filenames. All filenames must be freed with free().
404 * The table itself must be freed with free() as well.
406 * @return How many file names were selected (possibly 0),
407 * or -1 in case of error.
409 int vlc_scandir( const char *dirname, char ***namelist,
410 int (*select)( const char * ),
411 int (*compar)( const char **, const char ** ) )
413 DIR *dir = vlc_opendir (dirname);
418 val = vlc_loaddir (dir, namelist, select, compar);
424 static int vlc_statEx( const char *filename, struct stat *buf,
428 /*_stat translates to wchar internally on WinCE*/
429 return _stat( filename, buf );
430 #elif defined (WIN32)
431 CONVERT_PATH (filename, wpath, -1);
432 return _wstati64 (wpath, buf);
435 #ifdef HAVE_SYS_STAT_H
436 const char *local_name = ToLocale( filename );
438 if( local_name != NULL )
440 int res = deref ? stat( local_name, buf )
441 : lstat( local_name, buf );
442 LocaleFree( local_name );
451 * Finds file/inode informations, as stat().
452 * Consider using fstat() instead, if possible.
454 * @param filename UTF-8 file path
456 int vlc_stat( const char *filename, struct stat *buf)
458 return vlc_statEx( filename, buf, true );
462 * Finds file/inode informations, as lstat().
463 * Consider using fstat() instead, if possible.
465 * @param filename UTF-8 file path
467 int utf8_lstat( const char *filename, struct stat *buf)
469 return vlc_statEx( filename, buf, false );
475 * @param filename a UTF-8 string with the name of the file you want to delete.
476 * @return A 0 return value indicates success. A -1 return value indicates an
477 * error, and an error code is stored in errno
479 int vlc_unlink( const char *filename )
482 /*_open translates to wchar internally on WinCE*/
483 return _unlink( filename );
484 #elif defined (WIN32)
485 CONVERT_PATH (filename, wpath, -1);
486 return _wunlink (wpath);
489 const char *local_name = ToLocale( filename );
491 if( local_name == NULL )
497 int ret = unlink( local_name );
498 LocaleFree( local_name );
503 * Moves a file atomically. This only works within a single file system.
505 * @param oldpath path to the file before the move
506 * @param newpath intended path to the file after the move
507 * @return A 0 return value indicates success. A -1 return value indicates an
508 * error, and an error code is stored in errno
510 int vlc_rename (const char *oldpath, const char *newpath)
513 CONVERT_PATH (oldpath, wold, -1);
514 CONVERT_PATH (newpath, wnew, -1);
516 /* FIXME: errno support */
517 if (MoveFileW (wold, wnew))
522 return _wrename (wold, wnew);
526 const char *lo = ToLocale (oldpath);
530 const char *ln = ToLocale (newpath);
539 int ret = rename (lo, ln);
545 int vlc_mkstemp( char *template )
547 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
548 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
553 /* Check template validity */
554 const size_t i_length = strlen( template );
555 char *psz_rand = &template[i_length-6];
557 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
564 for( int i = 0; i < 256; i++ )
566 /* Create a pseudo random file name */
569 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
570 for( int j = 0; j < 6; j++ )
571 psz_rand[j] = digits[pi_rand[j] % i_digits];
574 int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
577 if( errno != EEXIST )
586 # define dup(fd) (fd, -1)
590 * Duplicates a file descriptor. The new file descriptor has the close-on-exec
591 * descriptor flag set.
592 * @return a new file descriptor or -1
594 int vlc_dup (int oldfd)
599 /* Unfortunately, dup3() works like dup2(), not like plain dup(). So we
600 * need such contortion to find the new file descriptor while preserving
601 * thread safety of the file descriptor table. */
602 newfd = vlc_open ("/dev/null", O_RDONLY);
603 if (likely(newfd != -1))
605 if (likely(dup3 (oldfd, newfd, O_CLOEXEC) == newfd))
613 if (likely(newfd != -1))
614 fcntl (newfd, F_SETFD, FD_CLOEXEC);