]> git.sesse.net Git - vlc/blob - src/text/filesystem.c
Code factorization
[vlc] / src / text / filesystem.c
1 /*****************************************************************************
2  * filesystem.c: File system helpers
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * Copyright © 2005-2008 Rémi Denis-Courmont
6  *
7  * Authors: Rémi Denis-Courmont <rem # videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "libvlc.h" /* utf8_mkdir */
34
35 #include <assert.h>
36
37 #include <stdio.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #ifdef HAVE_DIRENT_H
41 #  include <dirent.h>
42 #endif
43 #ifdef UNDER_CE
44 #  include <tchar.h>
45 #endif
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
48 #endif
49 #ifdef HAVE_FCNTL_H
50 # include <fcntl.h>
51 #endif
52 #ifdef WIN32
53 # include <io.h>
54 # include <direct.h>
55 #else
56 # include <unistd.h>
57 #endif
58
59 #ifndef HAVE_LSTAT
60 # define lstat( a, b ) stat(a, b)
61 #endif
62
63 #ifdef WIN32
64 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
65 {
66     if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
67     {
68         errno = ENOENT;
69         return -1;
70     }
71     wpath[MAX_PATH] = L'\0';
72     return 0;
73 }
74 # define CONVERT_PATH(path, wpath, err) \
75   wchar_t wpath[MAX_PATH+1]; \
76   if (convert_path (path, wpath)) \
77       return (err)
78 #endif
79
80 /**
81  * Opens a system file handle.
82  *
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).
87  */
88 int utf8_open (const char *filename, int flags, mode_t mode)
89 {
90 #ifdef UNDER_CE
91     /*_open translates to wchar internally on WinCE*/
92     return _open (filename, flags, mode);
93 #elif defined (WIN32)
94     /*
95      * open() cannot open files with non-“ANSI” characters on Windows.
96      * We use _wopen() instead. Same thing for mkdir() and stat().
97      */
98     CONVERT_PATH(filename, wpath, -1);
99     return _wopen (wpath, flags, mode);
100
101 #endif
102     const char *local_name = ToLocale (filename);
103
104     if (local_name == NULL)
105     {
106         errno = ENOENT;
107         return -1;
108     }
109
110     int fd = open (local_name, flags, mode);
111     LocaleFree (local_name);
112     return fd;
113 }
114
115 /**
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.
120  */
121 FILE *utf8_fopen (const char *filename, const char *mode)
122 {
123     int rwflags = 0, oflags = 0;
124     bool append = false;
125
126     for (const char *ptr = mode; *ptr; ptr++)
127     {
128         switch (*ptr)
129         {
130             case 'r':
131                 rwflags = O_RDONLY;
132                 break;
133
134             case 'a':
135                 rwflags = O_WRONLY;
136                 oflags |= O_CREAT;
137                 append = true;
138                 break;
139
140             case 'w':
141                 rwflags = O_WRONLY;
142                 oflags |= O_CREAT | O_TRUNC;
143                 break;
144
145             case '+':
146                 rwflags = O_RDWR;
147                 break;
148
149 #ifdef O_TEXT
150             case 't':
151                 oflags |= O_TEXT;
152                 break;
153 #endif
154         }
155     }
156
157     int fd = utf8_open (filename, rwflags | oflags, 0666);
158     if (fd == -1)
159         return NULL;
160
161     if (append && (lseek (fd, 0, SEEK_END) == -1))
162     {
163         close (fd);
164         return NULL;
165     }
166
167     FILE *stream = fdopen (fd, mode);
168     if (stream == NULL)
169         close (fd);
170
171     return stream;
172 }
173
174 /**
175  * Creates a directory using UTF-8 paths.
176  *
177  * @param dirname a UTF-8 string with the name of the directory that you
178  *        want to create.
179  * @param mode directory permissions
180  * @return 0 on success, -1 on error (see errno).
181  */
182 int utf8_mkdir( const char *dirname, mode_t mode )
183 {
184 #if defined (UNDER_CE) || defined (WIN32)
185     (void) mode;
186     CONVERT_PATH (dirname, wpath, -1);
187     return _wmkdir (wpath);
188
189 #else
190     char *locname = ToLocale( dirname );
191     int res;
192
193     if( locname == NULL )
194     {
195         errno = ENOENT;
196         return -1;
197     }
198     res = mkdir( locname, mode );
199
200     LocaleFree( locname );
201     return res;
202 #endif
203 }
204
205 /**
206  * Opens a DIR pointer.
207  *
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().
211  */
212 DIR *utf8_opendir( const char *dirname )
213 {
214 #ifdef WIN32
215     CONVERT_PATH (dirname, wpath, NULL);
216     return (DIR *)vlc_wopendir (wpath);
217
218 #else
219     const char *local_name = ToLocale( dirname );
220
221     if( local_name != NULL )
222     {
223         DIR *dir = opendir( local_name );
224         LocaleFree( local_name );
225         return dir;
226     }
227 #endif
228
229     errno = ENOENT;
230     return NULL;
231 }
232
233 /**
234  * Reads the next file name from an open directory.
235  *
236  * @param dir The directory that is being read
237  *
238  * @return a UTF-8 string of the directory entry.
239  * Use free() to free this memory.
240  */
241 char *utf8_readdir( DIR *dir )
242 {
243 #ifdef WIN32
244     struct _wdirent *ent = vlc_wreaddir (dir);
245     if (ent == NULL)
246         return NULL;
247
248     return FromWide (ent->d_name);
249 #else
250     struct dirent *ent;
251
252     ent = readdir( (DIR *)dir );
253     if( ent == NULL )
254         return NULL;
255
256     return vlc_fix_readdir( ent->d_name );
257 #endif
258 }
259
260 static int dummy_select( const char *str )
261 {
262     (void)str;
263     return 1;
264 }
265
266 /**
267  * Does the same as utf8_scandir(), but takes an open directory pointer
268  * instead of a directory path.
269  */
270 int utf8_loaddir( DIR *dir, char ***namelist,
271                   int (*select)( const char * ),
272                   int (*compar)( const char **, const char ** ) )
273 {
274     if( select == NULL )
275         select = dummy_select;
276
277     if( dir == NULL )
278         return -1;
279     else
280     {
281         char **tab = NULL;
282         char *entry;
283         unsigned num = 0;
284
285         rewinddir( dir );
286
287         while( ( entry = utf8_readdir( dir ) ) != NULL )
288         {
289             char **newtab;
290
291             if( !select( entry ) )
292             {
293                 free( entry );
294                 continue;
295             }
296
297             newtab = realloc( tab, sizeof( char * ) * (num + 1) );
298             if( newtab == NULL )
299             {
300                 free( entry );
301                 goto error;
302             }
303             tab = newtab;
304             tab[num++] = entry;
305         }
306
307         if( compar != NULL )
308             qsort( tab, num, sizeof( tab[0] ),
309                    (int (*)( const void *, const void *))compar );
310
311         *namelist = tab;
312         return num;
313
314     error:{
315         unsigned i;
316
317         for( i = 0; i < num; i++ )
318             free( tab[i] );
319         if( tab != NULL )
320             free( tab );
321         }
322     }
323     return -1;
324 }
325
326 /**
327  * Selects file entries from a directory, as GNU C scandir().
328  *
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.
333  *
334  * @return How many file names were selected (possibly 0),
335  * or -1 in case of error.
336  */
337 int utf8_scandir( const char *dirname, char ***namelist,
338                   int (*select)( const char * ),
339                   int (*compar)( const char **, const char ** ) )
340 {
341     DIR *dir = utf8_opendir (dirname);
342     int val = -1;
343
344     if (dir != NULL)
345     {
346         val = utf8_loaddir (dir, namelist, select, compar);
347         closedir (dir);
348     }
349     return val;
350 }
351
352 static int utf8_statEx( const char *filename, struct stat *buf,
353                         bool deref )
354 {
355 #ifdef UNDER_CE
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);
361
362 #endif
363 #ifdef HAVE_SYS_STAT_H
364     const char *local_name = ToLocale( filename );
365
366     if( local_name != NULL )
367     {
368         int res = deref ? stat( local_name, buf )
369                        : lstat( local_name, buf );
370         LocaleFree( local_name );
371         return res;
372     }
373     errno = ENOENT;
374 #endif
375     return -1;
376 }
377
378 /**
379  * Finds file/inode informations, as stat().
380  * Consider using fstat() instead, if possible.
381  *
382  * @param filename UTF-8 file path
383  */
384 int utf8_stat( const char *filename, struct stat *buf)
385 {
386     return utf8_statEx( filename, buf, true );
387 }
388
389 /**
390  * Finds file/inode informations, as lstat().
391  * Consider using fstat() instead, if possible.
392  *
393  * @param filename UTF-8 file path
394  */
395 int utf8_lstat( const char *filename, struct stat *buf)
396 {
397     return utf8_statEx( filename, buf, false );
398 }
399
400 /**
401  * Removes a file.
402  *
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
406  */
407 int utf8_unlink( const char *filename )
408 {
409 #ifdef UNDER_CE
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);
415
416 #endif
417     const char *local_name = ToLocale( filename );
418
419     if( local_name == NULL )
420     {
421         errno = ENOENT;
422         return -1;
423     }
424
425     int ret = unlink( local_name );
426     LocaleFree( local_name );
427     return ret;
428 }