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