]> git.sesse.net Git - vlc/blob - src/text/filesystem.c
WinCE: replace _wmkdir by _mkdir
[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)
185     (void) mode;
186     /* mkdir converts internally to wchar */
187     return _mkdir(dirname);
188 #elif defined (WIN32)
189     (void) mode;
190     CONVERT_PATH (dirname, wpath, -1);
191     return _wmkdir (wpath);
192
193 #else
194     char *locname = ToLocale( dirname );
195     int res;
196
197     if( locname == NULL )
198     {
199         errno = ENOENT;
200         return -1;
201     }
202     res = mkdir( locname, mode );
203
204     LocaleFree( locname );
205     return res;
206 #endif
207 }
208
209 /**
210  * Opens a DIR pointer.
211  *
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().
215  */
216 DIR *utf8_opendir( const char *dirname )
217 {
218 #ifdef WIN32
219     CONVERT_PATH (dirname, wpath, NULL);
220     return (DIR *)vlc_wopendir (wpath);
221
222 #else
223     const char *local_name = ToLocale( dirname );
224
225     if( local_name != NULL )
226     {
227         DIR *dir = opendir( local_name );
228         LocaleFree( local_name );
229         return dir;
230     }
231 #endif
232
233     errno = ENOENT;
234     return NULL;
235 }
236
237 /**
238  * Reads the next file name from an open directory.
239  *
240  * @param dir The directory that is being read
241  *
242  * @return a UTF-8 string of the directory entry.
243  * Use free() to free this memory.
244  */
245 char *utf8_readdir( DIR *dir )
246 {
247 #ifdef WIN32
248     struct _wdirent *ent = vlc_wreaddir (dir);
249     if (ent == NULL)
250         return NULL;
251
252     return FromWide (ent->d_name);
253 #else
254     struct dirent *ent;
255
256     ent = readdir( (DIR *)dir );
257     if( ent == NULL )
258         return NULL;
259
260     return vlc_fix_readdir( ent->d_name );
261 #endif
262 }
263
264 static int dummy_select( const char *str )
265 {
266     (void)str;
267     return 1;
268 }
269
270 /**
271  * Does the same as utf8_scandir(), but takes an open directory pointer
272  * instead of a directory path.
273  */
274 int utf8_loaddir( DIR *dir, char ***namelist,
275                   int (*select)( const char * ),
276                   int (*compar)( const char **, const char ** ) )
277 {
278     if( select == NULL )
279         select = dummy_select;
280
281     if( dir == NULL )
282         return -1;
283     else
284     {
285         char **tab = NULL;
286         char *entry;
287         unsigned num = 0;
288
289         rewinddir( dir );
290
291         while( ( entry = utf8_readdir( dir ) ) != NULL )
292         {
293             char **newtab;
294
295             if( !select( entry ) )
296             {
297                 free( entry );
298                 continue;
299             }
300
301             newtab = realloc( tab, sizeof( char * ) * (num + 1) );
302             if( newtab == NULL )
303             {
304                 free( entry );
305                 goto error;
306             }
307             tab = newtab;
308             tab[num++] = entry;
309         }
310
311         if( compar != NULL )
312             qsort( tab, num, sizeof( tab[0] ),
313                    (int (*)( const void *, const void *))compar );
314
315         *namelist = tab;
316         return num;
317
318     error:{
319         unsigned i;
320
321         for( i = 0; i < num; i++ )
322             free( tab[i] );
323         if( tab != NULL )
324             free( tab );
325         }
326     }
327     return -1;
328 }
329
330 /**
331  * Selects file entries from a directory, as GNU C scandir().
332  *
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.
337  *
338  * @return How many file names were selected (possibly 0),
339  * or -1 in case of error.
340  */
341 int utf8_scandir( const char *dirname, char ***namelist,
342                   int (*select)( const char * ),
343                   int (*compar)( const char **, const char ** ) )
344 {
345     DIR *dir = utf8_opendir (dirname);
346     int val = -1;
347
348     if (dir != NULL)
349     {
350         val = utf8_loaddir (dir, namelist, select, compar);
351         closedir (dir);
352     }
353     return val;
354 }
355
356 static int utf8_statEx( const char *filename, struct stat *buf,
357                         bool deref )
358 {
359 #ifdef UNDER_CE
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);
365
366 #endif
367 #ifdef HAVE_SYS_STAT_H
368     const char *local_name = ToLocale( filename );
369
370     if( local_name != NULL )
371     {
372         int res = deref ? stat( local_name, buf )
373                        : lstat( local_name, buf );
374         LocaleFree( local_name );
375         return res;
376     }
377     errno = ENOENT;
378 #endif
379     return -1;
380 }
381
382 /**
383  * Finds file/inode informations, as stat().
384  * Consider using fstat() instead, if possible.
385  *
386  * @param filename UTF-8 file path
387  */
388 int utf8_stat( const char *filename, struct stat *buf)
389 {
390     return utf8_statEx( filename, buf, true );
391 }
392
393 /**
394  * Finds file/inode informations, as lstat().
395  * Consider using fstat() instead, if possible.
396  *
397  * @param filename UTF-8 file path
398  */
399 int utf8_lstat( const char *filename, struct stat *buf)
400 {
401     return utf8_statEx( filename, buf, false );
402 }
403
404 /**
405  * Removes a file.
406  *
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
410  */
411 int utf8_unlink( const char *filename )
412 {
413 #ifdef UNDER_CE
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);
419
420 #endif
421     const char *local_name = ToLocale( filename );
422
423     if( local_name == NULL )
424     {
425         errno = ENOENT;
426         return -1;
427     }
428
429     int ret = unlink( local_name );
430     LocaleFree( local_name );
431     return ret;
432 }