forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.c
236 lines (197 loc) · 5.92 KB
/
filesystem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*****************************************************************************
* filesystem.c: Common file system helpers
*****************************************************************************
* Copyright (C) 2005-2006 VLC authors and VideoLAN
* Copyright © 2005-2008 Rémi Denis-Courmont
*
* Authors: Rémi Denis-Courmont <rem # videolan.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_fs.h>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
/**
* Opens a FILE pointer.
* @param filename file path, using UTF-8 encoding
* @param mode fopen file open mode
* @return NULL on error, an open FILE pointer on success.
*/
FILE *vlc_fopen (const char *filename, const char *mode)
{
int rwflags = 0, oflags = 0;
for (const char *ptr = mode; *ptr; ptr++)
{
switch (*ptr)
{
case 'r':
rwflags = O_RDONLY;
break;
case 'a':
rwflags = O_WRONLY;
oflags |= O_CREAT | O_APPEND;
break;
case 'w':
rwflags = O_WRONLY;
oflags |= O_CREAT | O_TRUNC;
break;
case 'x':
oflags |= O_EXCL;
break;
case '+':
rwflags = O_RDWR;
break;
#ifdef O_BINARY
case 'b':
oflags = (oflags & ~O_TEXT) | O_BINARY;
break;
case 't':
oflags = (oflags & ~O_BINARY) | O_TEXT;
break;
#endif
}
}
int fd = vlc_open (filename, rwflags | oflags, 0666);
if (fd == -1)
return NULL;
FILE *stream = fdopen (fd, mode);
if (stream == NULL)
vlc_close (fd);
return stream;
}
static int dummy_select( const char *str )
{
(void)str;
return 1;
}
/**
* Does the same as vlc_scandir(), but takes an open directory pointer
* instead of a directory path.
*/
int vlc_loaddir( DIR *dir, char ***namelist,
int (*select)( const char * ),
int (*compar)( const char **, const char ** ) )
{
assert (dir);
if (select == NULL)
select = dummy_select;
char **tab = NULL;
unsigned num = 0;
rewinddir (dir);
for (unsigned size = 0;;)
{
errno = 0;
const char *entry = vlc_readdir (dir);
if (entry == NULL)
{
if (errno)
goto error;
break;
}
if (!select (entry))
continue;
if (num >= size)
{
size = size ? (2 * size) : 16;
char **newtab = realloc (tab, sizeof (*tab) * (size));
if (unlikely(newtab == NULL))
goto error;
tab = newtab;
}
tab[num] = strdup(entry);
if (likely(tab[num] != NULL))
num++;
}
if (compar != NULL && num > 0)
qsort (tab, num, sizeof (*tab),
(int (*)( const void *, const void *))compar);
*namelist = tab;
return num;
error:
for (unsigned i = 0; i < num; i++)
free (tab[i]);
free (tab);
return -1;
}
/**
* Selects file entries from a directory, as GNU C scandir().
*
* @param dirname UTF-8 diretory path
* @param pointer [OUT] pointer set, on successful completion, to the address
* of a table of UTF-8 filenames. All filenames must be freed with free().
* The table itself must be freed with free() as well.
*
* @return How many file names were selected (possibly 0),
* or -1 in case of error.
*/
int vlc_scandir( const char *dirname, char ***namelist,
int (*select)( const char * ),
int (*compar)( const char **, const char ** ) )
{
DIR *dir = vlc_opendir (dirname);
int val = -1;
if (dir != NULL)
{
val = vlc_loaddir (dir, namelist, select, compar);
closedir (dir);
}
return val;
}
#if defined (_WIN32) || defined (__OS2__)
# include <vlc_rand.h>
int vlc_mkstemp( char *template )
{
static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
/* */
assert( template );
/* Check template validity */
const size_t i_length = strlen( template );
char *psz_rand = &template[i_length-6];
if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
{
errno = EINVAL;
return -1;
}
/* */
for( int i = 0; i < 256; i++ )
{
/* Create a pseudo random file name */
uint8_t pi_rand[6];
vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
for( int j = 0; j < 6; j++ )
psz_rand[j] = digits[pi_rand[j] % i_digits];
/* */
int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
if( fd >= 0 )
return fd;
if( errno != EEXIST )
return -1;
}
errno = EEXIST;
return -1;
}
#endif