forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.c
395 lines (346 loc) · 10.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*****************************************************************************
* filesystem.c: OS/2 file system helpers
*****************************************************************************
* Copyright (C) 2005-2006 VLC authors and VideoLAN
* Copyright © 2005-2008 Rémi Denis-Courmont
* Copyright (C) 2012 KO Myung-Hun
*
* Authors: Rémi Denis-Courmont <rem # videolan.org>
* KO Myung-Hun <[email protected]>
*
* 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.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/socket.h>
#include <vlc_common.h>
#include <vlc_charset.h>
#include <vlc_fs.h>
#include "libvlc.h" /* vlc_mkdir */
/**
* Opens a system file handle.
*
* @param filename file path to open (with UTF-8 encoding)
* @param flags open() flags, see the C library open() documentation
* @return a file handle on success, -1 on error (see errno).
* @note Contrary to standard open(), this function returns file handles
* with the close-on-exec flag enabled.
*/
int vlc_open (const char *filename, int flags, ...)
{
unsigned int mode = 0;
va_list ap;
va_start (ap, flags);
if (flags & O_CREAT)
mode = va_arg (ap, unsigned int);
va_end (ap);
const char *local_name = ToLocale (filename);
if (local_name == NULL)
{
errno = ENOENT;
return -1;
}
int fd = open (local_name, flags, mode);
if (fd != -1)
fcntl (fd, F_SETFD, FD_CLOEXEC);
LocaleFree (local_name);
return fd;
}
/**
* Opens a system file handle relative to an existing directory handle.
*
* @param dir directory file descriptor
* @param filename file path to open (with UTF-8 encoding)
* @param flags open() flags, see the C library open() documentation
* @return a file handle on success, -1 on error (see errno).
* @note Contrary to standard open(), this function returns file handles
* with the close-on-exec flag enabled.
*/
int vlc_openat (int dir, const char *filename, int flags, ...)
{
errno = ENOSYS;
return -1;
}
/**
* Creates a directory using UTF-8 paths.
*
* @param dirname a UTF-8 string with the name of the directory that you
* want to create.
* @param mode directory permissions
* @return 0 on success, -1 on error (see errno).
*/
int vlc_mkdir (const char *dirname, mode_t mode)
{
char *locname = ToLocale (dirname);
if (unlikely(locname == NULL))
{
errno = ENOENT;
return -1;
}
int res = mkdir (locname, mode);
LocaleFree (locname);
return res;
}
/**
* Opens a DIR pointer.
*
* @param dirname UTF-8 representation of the directory name
* @return a pointer to the DIR struct, or NULL in case of error.
* Release with standard closedir().
*/
DIR *vlc_opendir (const char *dirname)
{
const char *locname = ToLocale (dirname);
if (unlikely(locname == NULL))
{
errno = ENOENT;
return NULL;
}
DIR *dir = opendir (locname);
LocaleFree (locname);
return dir;
}
/**
* Reads the next file name from an open directory.
*
* @param dir The directory that is being read
*
* @return a UTF-8 string of the directory entry. Use free() to release it.
* If there are no more entries in the directory, NULL is returned.
* If an error occurs, errno is set and NULL is returned.
*/
char *vlc_readdir( DIR *dir )
{
/* Beware that readdir_r() assumes <buf> is large enough to hold the result
* dirent including the file name. A buffer overflow could occur otherwise.
* In particular, pathconf() and _POSIX_NAME_MAX cannot be used here. */
struct dirent *ent;
char *path = NULL;
/* In the implementation of Innotek LIBC, aka kLIBC on OS/2,
* fpathconf (_PC_NAME_MAX) is broken, and errno is set to EBADF.
* Moreover, d_name is not the last member of struct dirent.
* So just allocate as many as the size of struct dirent. */
#if 1
long len = sizeof (struct dirent);
#else
long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
len += offsetof (struct dirent, d_name) + 1;
#endif
struct dirent *buf = malloc (len);
if (unlikely(buf == NULL))
return NULL;
int val = readdir_r (dir, buf, &ent);
if (val != 0)
errno = val;
else if (ent != NULL)
path = FromLocaleDup (ent->d_name);
free (buf);
return path;
}
static int vlc_statEx (const char *filename, struct stat *buf, bool deref)
{
const char *local_name = ToLocale (filename);
if (unlikely(local_name == NULL))
{
errno = ENOENT;
return -1;
}
int res = deref ? stat (local_name, buf)
: lstat (local_name, buf);
LocaleFree (local_name);
return res;
}
/**
* Finds file/inode information, as stat().
* Consider using fstat() instead, if possible.
*
* @param filename UTF-8 file path
*/
int vlc_stat (const char *filename, struct stat *buf)
{
return vlc_statEx (filename, buf, true);
}
/**
* Finds file/inode information, as lstat().
* Consider using fstat() instead, if possible.
*
* @param filename UTF-8 file path
*/
int vlc_lstat (const char *filename, struct stat *buf)
{
return vlc_statEx (filename, buf, false);
}
/**
* Removes a file.
*
* @param filename a UTF-8 string with the name of the file you want to delete.
* @return A 0 return value indicates success. A -1 return value indicates an
* error, and an error code is stored in errno
*/
int vlc_unlink (const char *filename)
{
const char *local_name = ToLocale (filename);
if (unlikely(local_name == NULL))
{
errno = ENOENT;
return -1;
}
int ret = unlink (local_name);
LocaleFree (local_name);
return ret;
}
/**
* Moves a file atomically. This only works within a single file system.
*
* @param oldpath path to the file before the move
* @param newpath intended path to the file after the move
* @return A 0 return value indicates success. A -1 return value indicates an
* error, and an error code is stored in errno
*/
int vlc_rename (const char *oldpath, const char *newpath)
{
const char *lo = ToLocale (oldpath);
if (lo == NULL)
goto error;
const char *ln = ToLocale (newpath);
if (ln == NULL)
{
LocaleFree (lo);
error:
errno = ENOENT;
return -1;
}
int ret = rename (lo, ln);
LocaleFree (lo);
LocaleFree (ln);
return ret;
}
/**
* Determines the current working directory.
*
* @return the current working directory (must be free()'d)
* or NULL on error
*/
char *vlc_getcwd (void)
{
/* Try $PWD */
const char *pwd = getenv ("PWD");
if (pwd != NULL)
{
struct stat s1, s2;
/* Make sure $PWD is correct */
if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
&& s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
return ToLocaleDup (pwd);
}
/* Otherwise iterate getcwd() until the buffer is big enough */
long path_max = pathconf (".", _PC_PATH_MAX);
size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
for (;; size *= 2)
{
char *buf = malloc (size);
if (unlikely(buf == NULL))
break;
if (getcwd (buf, size) != NULL)
{
char *ret = ToLocaleDup (buf);
free (buf);
return ret; /* success */
}
free (buf);
if (errno != ERANGE)
break;
}
return NULL;
}
/**
* Duplicates a file descriptor. The new file descriptor has the close-on-exec
* descriptor flag set.
* @return a new file descriptor or -1
*/
int vlc_dup (int oldfd)
{
int newfd;
newfd = dup (oldfd);
if (likely(newfd != -1))
fcntl (newfd, F_SETFD, FD_CLOEXEC);
return newfd;
}
/**
* Creates a pipe (see "man pipe" for further reference).
*/
int vlc_pipe (int fds[2])
{
if (pipe (fds))
return -1;
fcntl (fds[0], F_SETFD, FD_CLOEXEC);
fcntl (fds[1], F_SETFD, FD_CLOEXEC);
return 0;
}
#include <vlc_network.h>
/**
* Creates a socket file descriptor. The new file descriptor has the
* close-on-exec flag set.
* @param pf protocol family
* @param type socket type
* @param proto network protocol
* @param nonblock true to create a non-blocking socket
* @return a new file descriptor or -1
*/
int vlc_socket (int pf, int type, int proto, bool nonblock)
{
int fd;
fd = socket (pf, type, proto);
if (fd == -1)
return -1;
fcntl (fd, F_SETFD, FD_CLOEXEC);
if (nonblock)
fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
return fd;
}
/**
* Accepts an inbound connection request on a listening socket.
* The new file descriptor has the close-on-exec flag set.
* @param lfd listening socket file descriptor
* @param addr pointer to the peer address or NULL [OUT]
* @param alen pointer to the length of the peer address or NULL [OUT]
* @param nonblock whether to put the new socket in non-blocking mode
* @return a new file descriptor, or -1 on error.
*/
int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
{
do
{
int fd = accept (lfd, addr, alen);
if (fd != -1)
{
fcntl (fd, F_SETFD, FD_CLOEXEC);
if (nonblock)
fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
return fd;
}
}
while (errno == EINTR);
return -1;
}