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) || defined (WIN32)
186 CONVERT_PATH (dirname, wpath, -1);
187 return _wmkdir (wpath);
190 char *locname = ToLocale( dirname );
193 if( locname == NULL )
198 res = mkdir( locname, mode );
200 LocaleFree( locname );
206 * Opens a DIR pointer.
208 * @param dirname UTF-8 representation of the directory name
209 * @return a pointer to the DIR struct, or NULL in case of error.
210 * Release with standard closedir().
212 DIR *utf8_opendir( const char *dirname )
215 CONVERT_PATH (dirname, wpath, NULL);
216 return (DIR *)vlc_wopendir (wpath);
219 const char *local_name = ToLocale( dirname );
221 if( local_name != NULL )
223 DIR *dir = opendir( local_name );
224 LocaleFree( local_name );
234 * Reads the next file name from an open directory.
236 * @param dir The directory that is being read
238 * @return a UTF-8 string of the directory entry.
239 * Use free() to free this memory.
241 char *utf8_readdir( DIR *dir )
244 struct _wdirent *ent = vlc_wreaddir (dir);
248 return FromWide (ent->d_name);
252 ent = readdir( (DIR *)dir );
256 return vlc_fix_readdir( ent->d_name );
260 static int dummy_select( const char *str )
267 * Does the same as utf8_scandir(), but takes an open directory pointer
268 * instead of a directory path.
270 int utf8_loaddir( DIR *dir, char ***namelist,
271 int (*select)( const char * ),
272 int (*compar)( const char **, const char ** ) )
275 select = dummy_select;
287 while( ( entry = utf8_readdir( dir ) ) != NULL )
291 if( !select( entry ) )
297 newtab = realloc( tab, sizeof( char * ) * (num + 1) );
308 qsort( tab, num, sizeof( tab[0] ),
309 (int (*)( const void *, const void *))compar );
317 for( i = 0; i < num; i++ )
327 * Selects file entries from a directory, as GNU C scandir().
329 * @param dirname UTF-8 diretory path
330 * @param pointer [OUT] pointer set, on succesful completion, to the address
331 * of a table of UTF-8 filenames. All filenames must be freed with free().
332 * The table itself must be freed with free() as well.
334 * @return How many file names were selected (possibly 0),
335 * or -1 in case of error.
337 int utf8_scandir( const char *dirname, char ***namelist,
338 int (*select)( const char * ),
339 int (*compar)( const char **, const char ** ) )
341 DIR *dir = utf8_opendir (dirname);
346 val = utf8_loaddir (dir, namelist, select, compar);
352 static int utf8_statEx( const char *filename, struct stat *buf,
356 /*_stat translates to wchar internally on WinCE*/
357 return _stat( filename, buf );
358 #elif defined (WIN32)
359 CONVERT_PATH (filename, wpath, -1);
360 return _wstati64 (wpath, buf);
363 #ifdef HAVE_SYS_STAT_H
364 const char *local_name = ToLocale( filename );
366 if( local_name != NULL )
368 int res = deref ? stat( local_name, buf )
369 : lstat( local_name, buf );
370 LocaleFree( local_name );
379 * Finds file/inode informations, as stat().
380 * Consider using fstat() instead, if possible.
382 * @param filename UTF-8 file path
384 int utf8_stat( const char *filename, struct stat *buf)
386 return utf8_statEx( filename, buf, true );
390 * Finds file/inode informations, as lstat().
391 * Consider using fstat() instead, if possible.
393 * @param filename UTF-8 file path
395 int utf8_lstat( const char *filename, struct stat *buf)
397 return utf8_statEx( filename, buf, false );
403 * @param filename a UTF-8 string with the name of the file you want to delete.
404 * @return A 0 return value indicates success. A -1 return value indicates an
405 * error, and an error code is stored in errno
407 int utf8_unlink( const char *filename )
410 /*_open translates to wchar internally on WinCE*/
411 return _unlink( filename );
412 #elif defined (WIN32)
413 CONVERT_PATH (filename, wpath, -1);
414 return _wunlink (wpath);
417 const char *local_name = ToLocale( filename );
419 if( local_name == NULL )
425 int ret = unlink( local_name );
426 LocaleFree( local_name );