]> git.sesse.net Git - vlc/blob - src/text/filesystem.c
vlc_fopen: implement binary mode (useful on Windows)
[vlc] / src / text / filesystem.c
1 /*****************************************************************************
2  * filesystem.c: Common 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_fs.h>
33 #include <vlc_rand.h>
34
35 #include <assert.h>
36
37 #include <stdio.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44
45 /**
46  * Opens a FILE pointer.
47  * @param filename file path, using UTF-8 encoding
48  * @param mode fopen file open mode
49  * @return NULL on error, an open FILE pointer on success.
50  */
51 FILE *vlc_fopen (const char *filename, const char *mode)
52 {
53     int rwflags = 0, oflags = 0;
54     bool append = false;
55
56     for (const char *ptr = mode; *ptr; ptr++)
57     {
58         switch (*ptr)
59         {
60             case 'r':
61                 rwflags = O_RDONLY;
62                 break;
63
64             case 'a':
65                 rwflags = O_WRONLY;
66                 oflags |= O_CREAT;
67                 append = true;
68                 break;
69
70             case 'w':
71                 rwflags = O_WRONLY;
72                 oflags |= O_CREAT | O_TRUNC;
73                 break;
74
75             case '+':
76                 rwflags = O_RDWR;
77                 break;
78
79 #ifdef O_TEXT
80             case 'b':
81                 oflags = (oflags & ~O_TEXT) | O_BINARY;
82                 break;
83
84             case 't':
85                 oflags = (oflags & ~O_BINARY) | O_TEXT;
86                 break;
87 #endif
88         }
89     }
90
91     int fd = vlc_open (filename, rwflags | oflags, 0666);
92     if (fd == -1)
93         return NULL;
94
95     if (append && (lseek (fd, 0, SEEK_END) == -1))
96     {
97         close (fd);
98         return NULL;
99     }
100
101     FILE *stream = fdopen (fd, mode);
102     if (stream == NULL)
103         close (fd);
104
105     return stream;
106 }
107
108
109 static int dummy_select( const char *str )
110 {
111     (void)str;
112     return 1;
113 }
114
115 /**
116  * Does the same as vlc_scandir(), but takes an open directory pointer
117  * instead of a directory path.
118  */
119 int vlc_loaddir( DIR *dir, char ***namelist,
120                   int (*select)( const char * ),
121                   int (*compar)( const char **, const char ** ) )
122 {
123     assert (dir);
124
125     if (select == NULL)
126         select = dummy_select;
127
128     char **tab = NULL;
129     unsigned num = 0;
130
131     rewinddir (dir);
132
133     for (unsigned size = 0;;)
134     {
135         errno = 0;
136         char *entry = vlc_readdir (dir);
137         if (entry == NULL)
138         {
139             if (errno)
140                 goto error;
141             break;
142         }
143
144         if (!select (entry))
145         {
146             free (entry);
147             continue;
148         }
149
150         if (num >= size)
151         {
152             size = size ? (2 * size) : 16;
153             char **newtab = realloc (tab, sizeof (*tab) * (size));
154
155             if (unlikely(newtab == NULL))
156             {
157                 free (entry);
158                 goto error;
159             }
160             tab = newtab;
161         }
162
163         tab[num++] = entry;
164     }
165
166     if (compar != NULL)
167         qsort (tab, num, sizeof (*tab),
168                (int (*)( const void *, const void *))compar);
169     *namelist = tab;
170     return num;
171
172 error:
173     for (unsigned i = 0; i < num; i++)
174         free (tab[i]);
175     free (tab);
176     return -1;
177 }
178
179 /**
180  * Selects file entries from a directory, as GNU C scandir().
181  *
182  * @param dirname UTF-8 diretory path
183  * @param pointer [OUT] pointer set, on successful completion, to the address
184  * of a table of UTF-8 filenames. All filenames must be freed with free().
185  * The table itself must be freed with free() as well.
186  *
187  * @return How many file names were selected (possibly 0),
188  * or -1 in case of error.
189  */
190 int vlc_scandir( const char *dirname, char ***namelist,
191                   int (*select)( const char * ),
192                   int (*compar)( const char **, const char ** ) )
193 {
194     DIR *dir = vlc_opendir (dirname);
195     int val = -1;
196
197     if (dir != NULL)
198     {
199         val = vlc_loaddir (dir, namelist, select, compar);
200         closedir (dir);
201     }
202     return val;
203 }
204
205 int vlc_mkstemp( char *template )
206 {
207     static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
208     static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
209
210     /* */
211     assert( template );
212
213     /* Check template validity */
214     const size_t i_length = strlen( template );
215     char *psz_rand = &template[i_length-6];
216
217     if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
218     {
219         errno = EINVAL;
220         return -1;
221     }
222
223     /* */
224     for( int i = 0; i < 256; i++ )
225     {
226         /* Create a pseudo random file name */
227         uint8_t pi_rand[6];
228
229         vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
230         for( int j = 0; j < 6; j++ )
231             psz_rand[j] = digits[pi_rand[j] % i_digits];
232
233         /* */
234         int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
235         if( fd >= 0 )
236             return fd;
237         if( errno != EEXIST )
238             return -1;
239     }
240
241     errno = EEXIST;
242     return -1;
243 }