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 */
39 #include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
62 # define lstat( a, b ) stat(a, b)
66 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
68 if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
73 wpath[MAX_PATH] = L'\0';
76 # define CONVERT_PATH(path, wpath, err) \
77 wchar_t wpath[MAX_PATH+1]; \
78 if (convert_path (path, wpath)) \
83 * Opens a system file handle.
85 * @param filename file path to open (with UTF-8 encoding)
86 * @param flags open() flags, see the C library open() documentation
87 * @param mode file permissions if creating a new file
88 * @return a file handle on success, -1 on error (see errno).
90 int utf8_open (const char *filename, int flags, mode_t mode)
93 /*_open translates to wchar internally on WinCE*/
94 return _open (filename, flags, mode);
97 * open() cannot open files with non-“ANSI” characters on Windows.
98 * We use _wopen() instead. Same thing for mkdir() and stat().
100 CONVERT_PATH(filename, wpath, -1);
101 return _wopen (wpath, flags, mode);
104 const char *local_name = ToLocale (filename);
106 if (local_name == NULL)
112 int fd = open (local_name, flags, mode);
113 LocaleFree (local_name);
118 * Opens a FILE pointer.
119 * @param filename file path, using UTF-8 encoding
120 * @param mode fopen file open mode
121 * @return NULL on error, an open FILE pointer on success.
123 FILE *utf8_fopen (const char *filename, const char *mode)
125 int rwflags = 0, oflags = 0;
128 for (const char *ptr = mode; *ptr; ptr++)
144 oflags |= O_CREAT | O_TRUNC;
159 int fd = utf8_open (filename, rwflags | oflags, 0666);
163 if (append && (lseek (fd, 0, SEEK_END) == -1))
169 FILE *stream = fdopen (fd, mode);
177 * Creates a directory using UTF-8 paths.
179 * @param dirname a UTF-8 string with the name of the directory that you
181 * @param mode directory permissions
182 * @return 0 on success, -1 on error (see errno).
184 int utf8_mkdir( const char *dirname, mode_t mode )
186 #if defined (UNDER_CE)
188 /* mkdir converts internally to wchar */
189 return _mkdir(dirname);
190 #elif defined (WIN32)
192 CONVERT_PATH (dirname, wpath, -1);
193 return _wmkdir (wpath);
196 char *locname = ToLocale( dirname );
199 if( locname == NULL )
204 res = mkdir( locname, mode );
206 LocaleFree( locname );
212 * Opens a DIR pointer.
214 * @param dirname UTF-8 representation of the directory name
215 * @return a pointer to the DIR struct, or NULL in case of error.
216 * Release with standard closedir().
218 DIR *utf8_opendir( const char *dirname )
221 CONVERT_PATH (dirname, wpath, NULL);
222 return (DIR *)vlc_wopendir (wpath);
225 const char *local_name = ToLocale( dirname );
227 if( local_name != NULL )
229 DIR *dir = opendir( local_name );
230 LocaleFree( local_name );
240 * Reads the next file name from an open directory.
242 * @param dir The directory that is being read
244 * @return a UTF-8 string of the directory entry.
245 * Use free() to free this memory.
247 char *utf8_readdir( DIR *dir )
250 struct _wdirent *ent = vlc_wreaddir (dir);
254 return FromWide (ent->d_name);
258 ent = readdir( (DIR *)dir );
262 return vlc_fix_readdir( ent->d_name );
266 static int dummy_select( const char *str )
273 * Does the same as utf8_scandir(), but takes an open directory pointer
274 * instead of a directory path.
276 int utf8_loaddir( DIR *dir, char ***namelist,
277 int (*select)( const char * ),
278 int (*compar)( const char **, const char ** ) )
281 select = dummy_select;
293 while( ( entry = utf8_readdir( dir ) ) != NULL )
297 if( !select( entry ) )
303 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
314 qsort( tab, num, sizeof( tab[0] ),
315 (int (*)( const void *, const void *))compar );
323 for( i = 0; i < num; i++ )
333 * Selects file entries from a directory, as GNU C scandir().
335 * @param dirname UTF-8 diretory path
336 * @param pointer [OUT] pointer set, on succesful completion, to the address
337 * of a table of UTF-8 filenames. All filenames must be freed with free().
338 * The table itself must be freed with free() as well.
340 * @return How many file names were selected (possibly 0),
341 * or -1 in case of error.
343 int utf8_scandir( const char *dirname, char ***namelist,
344 int (*select)( const char * ),
345 int (*compar)( const char **, const char ** ) )
347 DIR *dir = utf8_opendir (dirname);
352 val = utf8_loaddir (dir, namelist, select, compar);
358 static int utf8_statEx( const char *filename, struct stat *buf,
362 /*_stat translates to wchar internally on WinCE*/
363 return _stat( filename, buf );
364 #elif defined (WIN32)
365 CONVERT_PATH (filename, wpath, -1);
366 return _wstati64 (wpath, buf);
369 #ifdef HAVE_SYS_STAT_H
370 const char *local_name = ToLocale( filename );
372 if( local_name != NULL )
374 int res = deref ? stat( local_name, buf )
375 : lstat( local_name, buf );
376 LocaleFree( local_name );
385 * Finds file/inode informations, as stat().
386 * Consider using fstat() instead, if possible.
388 * @param filename UTF-8 file path
390 int utf8_stat( const char *filename, struct stat *buf)
392 return utf8_statEx( filename, buf, true );
396 * Finds file/inode informations, as lstat().
397 * Consider using fstat() instead, if possible.
399 * @param filename UTF-8 file path
401 int utf8_lstat( const char *filename, struct stat *buf)
403 return utf8_statEx( filename, buf, false );
409 * @param filename a UTF-8 string with the name of the file you want to delete.
410 * @return A 0 return value indicates success. A -1 return value indicates an
411 * error, and an error code is stored in errno
413 int utf8_unlink( const char *filename )
416 /*_open translates to wchar internally on WinCE*/
417 return _unlink( filename );
418 #elif defined (WIN32)
419 CONVERT_PATH (filename, wpath, -1);
420 return _wunlink (wpath);
423 const char *local_name = ToLocale( filename );
425 if( local_name == NULL )
431 int ret = unlink( local_name );
432 LocaleFree( local_name );