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>
60 # define lstat( a, b ) stat(a, b)
64 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
66 if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
71 wpath[MAX_PATH] = L'\0';
74 # define CONVERT_PATH(path, wpath, err) \
75 wchar_t wpath[MAX_PATH+1]; \
76 if (convert_path (path, wpath)) \
81 * Opens a system file handle.
83 * @param filename file path to open (with UTF-8 encoding)
84 * @param flags open() flags, see the C library open() documentation
85 * @param mode file permissions if creating a new file
86 * @return a file handle on success, -1 on error (see errno).
88 int utf8_open (const char *filename, int flags, mode_t mode)
91 /*_open translates to wchar internally on WinCE*/
92 return _open (filename, flags, mode);
95 * open() cannot open files with non-“ANSI” characters on Windows.
96 * We use _wopen() instead. Same thing for mkdir() and stat().
98 CONVERT_PATH(filename, wpath, -1);
99 return _wopen (wpath, flags, mode);
102 const char *local_name = ToLocale (filename);
104 if (local_name == NULL)
110 int fd = open (local_name, flags, mode);
111 LocaleFree (local_name);
116 * Opens a FILE pointer.
117 * @param filename file path, using UTF-8 encoding
118 * @param mode fopen file open mode
119 * @return NULL on error, an open FILE pointer on success.
121 FILE *utf8_fopen (const char *filename, const char *mode)
123 int rwflags = 0, oflags = 0;
126 for (const char *ptr = mode; *ptr; ptr++)
142 oflags |= O_CREAT | O_TRUNC;
157 int fd = utf8_open (filename, rwflags | oflags, 0666);
161 if (append && (lseek (fd, 0, SEEK_END) == -1))
167 FILE *stream = fdopen (fd, mode);
175 * Creates a directory using UTF-8 paths.
177 * @param dirname a UTF-8 string with the name of the directory that you
179 * @param mode directory permissions
180 * @return 0 on success, -1 on error (see errno).
182 int utf8_mkdir( const char *dirname, mode_t mode )
184 #if defined (UNDER_CE)
186 /* mkdir converts internally to wchar */
187 return _mkdir(dirname);
188 #elif defined (WIN32)
190 CONVERT_PATH (dirname, wpath, -1);
191 return _wmkdir (wpath);
194 char *locname = ToLocale( dirname );
197 if( locname == NULL )
202 res = mkdir( locname, mode );
204 LocaleFree( locname );
210 * Opens a DIR pointer.
212 * @param dirname UTF-8 representation of the directory name
213 * @return a pointer to the DIR struct, or NULL in case of error.
214 * Release with standard closedir().
216 DIR *utf8_opendir( const char *dirname )
219 CONVERT_PATH (dirname, wpath, NULL);
220 return (DIR *)vlc_wopendir (wpath);
223 const char *local_name = ToLocale( dirname );
225 if( local_name != NULL )
227 DIR *dir = opendir( local_name );
228 LocaleFree( local_name );
238 * Reads the next file name from an open directory.
240 * @param dir The directory that is being read
242 * @return a UTF-8 string of the directory entry.
243 * Use free() to free this memory.
245 char *utf8_readdir( DIR *dir )
248 struct _wdirent *ent = vlc_wreaddir (dir);
252 return FromWide (ent->d_name);
256 ent = readdir( (DIR *)dir );
260 return vlc_fix_readdir( ent->d_name );
264 static int dummy_select( const char *str )
271 * Does the same as utf8_scandir(), but takes an open directory pointer
272 * instead of a directory path.
274 int utf8_loaddir( DIR *dir, char ***namelist,
275 int (*select)( const char * ),
276 int (*compar)( const char **, const char ** ) )
279 select = dummy_select;
291 while( ( entry = utf8_readdir( dir ) ) != NULL )
295 if( !select( entry ) )
301 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
312 qsort( tab, num, sizeof( tab[0] ),
313 (int (*)( const void *, const void *))compar );
321 for( i = 0; i < num; i++ )
331 * Selects file entries from a directory, as GNU C scandir().
333 * @param dirname UTF-8 diretory path
334 * @param pointer [OUT] pointer set, on succesful completion, to the address
335 * of a table of UTF-8 filenames. All filenames must be freed with free().
336 * The table itself must be freed with free() as well.
338 * @return How many file names were selected (possibly 0),
339 * or -1 in case of error.
341 int utf8_scandir( const char *dirname, char ***namelist,
342 int (*select)( const char * ),
343 int (*compar)( const char **, const char ** ) )
345 DIR *dir = utf8_opendir (dirname);
350 val = utf8_loaddir (dir, namelist, select, compar);
356 static int utf8_statEx( const char *filename, struct stat *buf,
360 /*_stat translates to wchar internally on WinCE*/
361 return _stat( filename, buf );
362 #elif defined (WIN32)
363 CONVERT_PATH (filename, wpath, -1);
364 return _wstati64 (wpath, buf);
367 #ifdef HAVE_SYS_STAT_H
368 const char *local_name = ToLocale( filename );
370 if( local_name != NULL )
372 int res = deref ? stat( local_name, buf )
373 : lstat( local_name, buf );
374 LocaleFree( local_name );
383 * Finds file/inode informations, as stat().
384 * Consider using fstat() instead, if possible.
386 * @param filename UTF-8 file path
388 int utf8_stat( const char *filename, struct stat *buf)
390 return utf8_statEx( filename, buf, true );
394 * Finds file/inode informations, as lstat().
395 * Consider using fstat() instead, if possible.
397 * @param filename UTF-8 file path
399 int utf8_lstat( const char *filename, struct stat *buf)
401 return utf8_statEx( filename, buf, false );
407 * @param filename a UTF-8 string with the name of the file you want to delete.
408 * @return A 0 return value indicates success. A -1 return value indicates an
409 * error, and an error code is stored in errno
411 int utf8_unlink( const char *filename )
414 /*_open translates to wchar internally on WinCE*/
415 return _unlink( filename );
416 #elif defined (WIN32)
417 CONVERT_PATH (filename, wpath, -1);
418 return _wunlink (wpath);
421 const char *local_name = ToLocale( filename );
423 if( local_name == NULL )
429 int ret = unlink( local_name );
430 LocaleFree( local_name );