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>
59 # define lstat( a, b ) stat(a, b)
63 /* Define this if the OS always use UTF-8 internally */
64 # define ASSUME_UTF8 1
67 #if defined (ASSUME_UTF8)
69 #elif defined (WIN32) || defined (UNDER_CE)
71 #elif defined (HAVE_ICONV)
74 # error No UTF8 charset conversion implemented on this platform!
78 * Opens a system file handle.
80 * @param filename file path to open (with UTF-8 encoding)
81 * @param flags open() flags, see the C library open() documentation
82 * @param mode file permissions if creating a new file
83 * @return a file handle on success, -1 on error (see errno).
85 int utf8_open (const char *filename, int flags, mode_t mode)
88 /*_open translates to wchar internally on WinCE*/
89 return _open (filename, flags, mode);
91 /* for Windows NT and above */
92 wchar_t wpath[MAX_PATH + 1];
94 if (!MultiByteToWideChar (CP_UTF8, 0, filename, -1, wpath, MAX_PATH))
99 wpath[MAX_PATH] = L'\0';
102 * open() cannot open files with non-“ANSI” characters on Windows.
103 * We use _wopen() instead. Same thing for mkdir() and stat().
105 return _wopen (wpath, flags, mode);
108 const char *local_name = ToLocale (filename);
110 if (local_name == NULL)
116 int fd = open (local_name, flags, mode);
117 LocaleFree (local_name);
122 * Opens a FILE pointer.
123 * @param filename file path, using UTF-8 encoding
124 * @param mode fopen file open mode
125 * @return NULL on error, an open FILE pointer on success.
127 FILE *utf8_fopen (const char *filename, const char *mode)
129 int rwflags = 0, oflags = 0;
132 for (const char *ptr = mode; *ptr; ptr++)
148 oflags |= O_CREAT | O_TRUNC;
163 int fd = utf8_open (filename, rwflags | oflags, 0666);
167 if (append && (lseek (fd, 0, SEEK_END) == -1))
173 FILE *stream = fdopen (fd, mode);
181 * Creates a directory using UTF-8 paths.
183 * @param dirname a UTF-8 string with the name of the directory that you
185 * @param mode directory permissions
186 * @return 0 on success, -1 on error (see errno).
188 int utf8_mkdir( const char *dirname, mode_t mode )
190 #if defined (UNDER_CE) || defined (WIN32)
193 wchar_t wname[MAX_PATH + 1];
194 char mod[MAX_PATH + 1];
197 /* Convert '/' into '\' */
198 for( i = 0; *dirname; i++ )
201 return -1; /* overflow */
203 if( *dirname == '/' )
212 if( MultiByteToWideChar( CP_UTF8, 0, mod, -1, wname, MAX_PATH ) == 0 )
217 wname[MAX_PATH] = L'\0';
219 if( CreateDirectoryW( wname, NULL ) == 0 )
221 if( GetLastError( ) == ERROR_ALREADY_EXISTS )
229 char *locname = ToLocale( dirname );
232 if( locname == NULL )
237 res = mkdir( locname, mode );
239 LocaleFree( locname );
245 * Opens a DIR pointer.
247 * @param dirname UTF-8 representation of the directory name
248 * @return a pointer to the DIR struct, or NULL in case of error.
249 * Release with standard closedir().
251 DIR *utf8_opendir( const char *dirname )
254 wchar_t wname[MAX_PATH + 1];
256 if (MultiByteToWideChar (CP_UTF8, 0, dirname, -1, wname, MAX_PATH))
258 wname[MAX_PATH] = L'\0';
259 return (DIR *)vlc_wopendir (wname);
262 const char *local_name = ToLocale( dirname );
264 if( local_name != NULL )
266 DIR *dir = opendir( local_name );
267 LocaleFree( local_name );
277 * Reads the next file name from an open directory.
279 * @param dir The directory that is being read
281 * @return a UTF-8 string of the directory entry.
282 * Use free() to free this memory.
284 char *utf8_readdir( DIR *dir )
287 struct _wdirent *ent = vlc_wreaddir (dir);
291 return FromWide (ent->d_name);
295 ent = readdir( (DIR *)dir );
299 return vlc_fix_readdir( ent->d_name );
303 static int dummy_select( const char *str )
310 * Does the same as utf8_scandir(), but takes an open directory pointer
311 * instead of a directory path.
313 int utf8_loaddir( DIR *dir, char ***namelist,
314 int (*select)( const char * ),
315 int (*compar)( const char **, const char ** ) )
318 select = dummy_select;
330 while( ( entry = utf8_readdir( dir ) ) != NULL )
334 if( !select( entry ) )
340 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
351 qsort( tab, num, sizeof( tab[0] ),
352 (int (*)( const void *, const void *))compar );
360 for( i = 0; i < num; i++ )
370 * Selects file entries from a directory, as GNU C scandir().
372 * @param dirname UTF-8 diretory path
373 * @param pointer [OUT] pointer set, on succesful completion, to the address
374 * of a table of UTF-8 filenames. All filenames must be freed with free().
375 * The table itself must be freed with free() as well.
377 * @return How many file names were selected (possibly 0),
378 * or -1 in case of error.
380 int utf8_scandir( const char *dirname, char ***namelist,
381 int (*select)( const char * ),
382 int (*compar)( const char **, const char ** ) )
384 DIR *dir = utf8_opendir (dirname);
389 val = utf8_loaddir (dir, namelist, select, compar);
395 static int utf8_statEx( const char *filename, struct stat *buf,
399 /*_stat translates to wchar internally on WinCE*/
400 return _stat( filename, buf );
401 #elif defined (WIN32)
402 /* for Windows NT and above */
403 wchar_t wpath[MAX_PATH + 1];
405 if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
410 wpath[MAX_PATH] = L'\0';
412 return _wstati64( wpath, buf );
415 #ifdef HAVE_SYS_STAT_H
416 const char *local_name = ToLocale( filename );
418 if( local_name != NULL )
420 int res = deref ? stat( local_name, buf )
421 : lstat( local_name, buf );
422 LocaleFree( local_name );
431 * Finds file/inode informations, as stat().
432 * Consider using fstat() instead, if possible.
434 * @param filename UTF-8 file path
436 int utf8_stat( const char *filename, struct stat *buf)
438 return utf8_statEx( filename, buf, true );
442 * Finds file/inode informations, as lstat().
443 * Consider using fstat() instead, if possible.
445 * @param filename UTF-8 file path
447 int utf8_lstat( const char *filename, struct stat *buf)
449 return utf8_statEx( filename, buf, false );
455 * @param filename a UTF-8 string with the name of the file you want to delete.
456 * @return A 0 return value indicates success. A -1 return value indicates an
457 * error, and an error code is stored in errno
459 int utf8_unlink( const char *filename )
462 /*_open translates to wchar internally on WinCE*/
463 return _unlink( filename );
464 #elif defined (WIN32)
465 /* for Windows NT and above */
466 wchar_t wpath[MAX_PATH + 1];
468 if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) )
473 wpath[MAX_PATH] = L'\0';
476 * unlink() cannot open files with non-“ANSI” characters on Windows.
477 * We use _wunlink() instead.
479 return _wunlink( wpath );
481 const char *local_name = ToLocale( filename );
483 if( local_name == NULL )
489 int ret = unlink( local_name );
490 LocaleFree( local_name );