]> git.sesse.net Git - vlc/blob - src/posix/filesystem.c
LGPL
[vlc] / src / posix / filesystem.c
1 /*****************************************************************************
2  * filesystem.c: POSIX file system helpers
3  *****************************************************************************
4  * Copyright (C) 2005-2006 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_charset.h>
30 #include <vlc_fs.h>
31 #include "libvlc.h" /* vlc_mkdir */
32
33 #include <assert.h>
34
35 #include <stdio.h>
36 #include <limits.h> /* NAME_MAX */
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42 #include <dirent.h>
43 #include <sys/socket.h>
44
45 #ifndef HAVE_LSTAT
46 # define lstat( a, b ) stat(a, b)
47 #endif
48
49 /**
50  * Opens a system file handle.
51  *
52  * @param filename file path to open (with UTF-8 encoding)
53  * @param flags open() flags, see the C library open() documentation
54  * @return a file handle on success, -1 on error (see errno).
55  * @note Contrary to standard open(), this function returns file handles
56  * with the close-on-exec flag enabled.
57  */
58 int vlc_open (const char *filename, int flags, ...)
59 {
60     unsigned int mode = 0;
61     va_list ap;
62
63     va_start (ap, flags);
64     if (flags & O_CREAT)
65         mode = va_arg (ap, unsigned int);
66     va_end (ap);
67
68 #ifdef O_CLOEXEC
69     flags |= O_CLOEXEC;
70 #endif
71
72     const char *local_name = ToLocale (filename);
73
74     if (local_name == NULL)
75     {
76         errno = ENOENT;
77         return -1;
78     }
79
80     int fd = open (local_name, flags, mode);
81     if (fd != -1)
82         fcntl (fd, F_SETFD, FD_CLOEXEC);
83
84     LocaleFree (local_name);
85     return fd;
86 }
87
88 /**
89  * Opens a system file handle relative to an existing directory handle.
90  *
91  * @param dir directory file descriptor
92  * @param filename file path to open (with UTF-8 encoding)
93  * @param flags open() flags, see the C library open() documentation
94  * @return a file handle on success, -1 on error (see errno).
95  * @note Contrary to standard open(), this function returns file handles
96  * with the close-on-exec flag enabled.
97  */
98 int vlc_openat (int dir, const char *filename, int flags, ...)
99 {
100     unsigned int mode = 0;
101     va_list ap;
102
103     va_start (ap, flags);
104     if (flags & O_CREAT)
105         mode = va_arg (ap, unsigned int);
106     va_end (ap);
107
108 #ifdef O_CLOEXEC
109     flags |= O_CLOEXEC;
110 #endif
111
112     const char *local_name = ToLocale (filename);
113     if (local_name == NULL)
114     {
115         errno = ENOENT;
116         return -1;
117     }
118
119 #ifdef HAVE_OPENAT
120     int fd = openat (dir, local_name, flags, mode);
121     if (fd != -1)
122         fcntl (fd, F_SETFD, FD_CLOEXEC);
123 #else
124     int fd = -1;
125     errno = ENOSYS;
126 #endif
127
128     LocaleFree (local_name);
129     return fd;
130 }
131
132
133 /**
134  * Creates a directory using UTF-8 paths.
135  *
136  * @param dirname a UTF-8 string with the name of the directory that you
137  *        want to create.
138  * @param mode directory permissions
139  * @return 0 on success, -1 on error (see errno).
140  */
141 int vlc_mkdir (const char *dirname, mode_t mode)
142 {
143     char *locname = ToLocale (dirname);
144     if (unlikely(locname == NULL))
145     {
146         errno = ENOENT;
147         return -1;
148     }
149
150     int res = mkdir (locname, mode);
151     LocaleFree (locname);
152     return res;
153 }
154
155 /**
156  * Opens a DIR pointer.
157  *
158  * @param dirname UTF-8 representation of the directory name
159  * @return a pointer to the DIR struct, or NULL in case of error.
160  * Release with standard closedir().
161  */
162 DIR *vlc_opendir (const char *dirname)
163 {
164     const char *local_name = ToLocale (dirname);
165     if (unlikely(local_name == NULL))
166     {
167         errno = ENOENT;
168         return NULL;
169     }
170
171     DIR *dir = opendir (local_name);
172     LocaleFree (local_name);
173     return dir;
174 }
175
176 /**
177  * Reads the next file name from an open directory.
178  *
179  * @param dir The directory that is being read
180  *
181  * @return a UTF-8 string of the directory entry. Use free() to release it.
182  * If there are no more entries in the directory, NULL is returned.
183  * If an error occurs, errno is set and NULL is returned.
184  */
185 char *vlc_readdir( DIR *dir )
186 {
187     /* Beware that readdir_r() assumes <buf> is large enough to hold the result
188      * dirent including the file name. A buffer overflow could occur otherwise.
189      * In particular, pathconf() and _POSIX_NAME_MAX cannot be used here. */
190     struct dirent *ent;
191     char *path = NULL;
192
193     long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
194 #if !defined(__OS2__) || !defined(__INNOTEK_LIBC__)
195 #ifdef NAME_MAX
196     /* POSIX says there shall we room for NAME_MAX bytes at all times */
197     if (/*len == -1 ||*/ len < NAME_MAX)
198         len = NAME_MAX;
199 #else
200     /* OS is broken. Lets assume there is no files left. */
201     if (len == -1)
202         return NULL;
203 #endif
204     len += offsetof (struct dirent, d_name) + 1;
205 #else /* __OS2__ && __INNOTEK_LIBC__ */
206     /* In the implementation of Innotek LIBC, aka kLIBC on OS/2,
207      * fpathconf (_PC_NAME_MAX) is broken, and d_name is not the last member
208      * of struct dirent.
209      * So just allocate as many as the size of struct dirent. */
210     len = sizeof (struct dirent);
211 #endif
212
213     struct dirent *buf = malloc (len);
214     if (unlikely(buf == NULL))
215         return NULL;
216
217     int val = readdir_r (dir, buf, &ent);
218     if (val != 0)
219         errno = val;
220     else if (ent != NULL)
221 #ifndef __APPLE__
222         path = FromLocaleDup (ent->d_name);
223 #else
224         path = FromCharset ("UTF-8-MAC", ent->d_name, strlen (ent->d_name));
225 #endif
226     free (buf);
227     return path;
228 }
229
230 static int vlc_statEx (const char *filename, struct stat *buf, bool deref)
231 {
232     const char *local_name = ToLocale (filename);
233     if (unlikely(local_name == NULL))
234     {
235         errno = ENOENT;
236         return -1;
237     }
238
239     int res = deref ? stat (local_name, buf)
240                     : lstat (local_name, buf);
241     LocaleFree (local_name);
242     return res;
243 }
244
245 /**
246  * Finds file/inode information, as stat().
247  * Consider using fstat() instead, if possible.
248  *
249  * @param filename UTF-8 file path
250  */
251 int vlc_stat (const char *filename, struct stat *buf)
252 {
253     return vlc_statEx (filename, buf, true);
254 }
255
256 /**
257  * Finds file/inode information, as lstat().
258  * Consider using fstat() instead, if possible.
259  *
260  * @param filename UTF-8 file path
261  */
262 int vlc_lstat (const char *filename, struct stat *buf)
263 {
264     return vlc_statEx (filename, buf, false);
265 }
266
267 /**
268  * Removes a file.
269  *
270  * @param filename a UTF-8 string with the name of the file you want to delete.
271  * @return A 0 return value indicates success. A -1 return value indicates an
272  *        error, and an error code is stored in errno
273  */
274 int vlc_unlink (const char *filename)
275 {
276     const char *local_name = ToLocale (filename);
277     if (unlikely(local_name == NULL))
278     {
279         errno = ENOENT;
280         return -1;
281     }
282
283     int ret = unlink (local_name);
284     LocaleFree (local_name);
285     return ret;
286 }
287
288 /**
289  * Moves a file atomically. This only works within a single file system.
290  *
291  * @param oldpath path to the file before the move
292  * @param newpath intended path to the file after the move
293  * @return A 0 return value indicates success. A -1 return value indicates an
294  *        error, and an error code is stored in errno
295  */
296 int vlc_rename (const char *oldpath, const char *newpath)
297 {
298     const char *lo = ToLocale (oldpath);
299     if (lo == NULL)
300         goto error;
301
302     const char *ln = ToLocale (newpath);
303     if (ln == NULL)
304     {
305         LocaleFree (lo);
306 error:
307         errno = ENOENT;
308         return -1;
309     }
310
311     int ret = rename (lo, ln);
312     LocaleFree (lo);
313     LocaleFree (ln);
314     return ret;
315 }
316
317 /**
318  * Determines the current working directory.
319  *
320  * @return the current working directory (must be free()'d)
321  *         or NULL on error
322  */
323 char *vlc_getcwd (void)
324 {
325     /* Try $PWD */
326     const char *pwd = getenv ("PWD");
327     if (pwd != NULL)
328     {
329         struct stat s1, s2;
330         /* Make sure $PWD is correct */
331         if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
332          && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
333             return ToLocaleDup (pwd);
334     }
335
336     /* Otherwise iterate getcwd() until the buffer is big enough */
337     long path_max = pathconf (".", _PC_PATH_MAX);
338     size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
339
340     for (;; size *= 2)
341     {
342         char *buf = malloc (size);
343         if (unlikely(buf == NULL))
344             break;
345
346         if (getcwd (buf, size) != NULL)
347 #ifdef ASSUME_UTF8
348             return buf;
349 #else
350         {
351             char *ret = ToLocaleDup (buf);
352             free (buf);
353             return ret; /* success */
354         }
355 #endif
356         free (buf);
357
358         if (errno != ERANGE)
359             break;
360     }
361     return NULL;
362 }
363
364 /**
365  * Duplicates a file descriptor. The new file descriptor has the close-on-exec
366  * descriptor flag set.
367  * @return a new file descriptor or -1
368  */
369 int vlc_dup (int oldfd)
370 {
371     int newfd;
372
373 #ifdef F_DUPFD_CLOEXEC
374     newfd = fcntl (oldfd, F_DUPFD_CLOEXEC);
375     if (unlikely(newfd == -1 && errno == EINVAL))
376 #endif
377     {
378         newfd = dup (oldfd);
379         if (likely(newfd != -1))
380             fcntl (newfd, F_SETFD, FD_CLOEXEC);
381     }
382     return newfd;
383 }
384
385 #ifdef __ANDROID__ /* && we support android < 2.3 */
386 /* pipe2() is declared and available since android-9 NDK,
387  * although it is available in libc.a since android-3
388  * We redefine the function here in order to be able to run
389  * on versions of Android older than 2.3
390  */
391 #include <sys/syscall.h>
392 //#include <sys/linux-syscalls.h> // fucking brokeness
393 int pipe2(int fds[2], int flags)
394 {
395     return syscall(/*__NR_pipe2 */ 359, fds, flags);
396 }
397 #endif /* __ANDROID__ */
398
399 /**
400  * Creates a pipe (see "man pipe" for further reference).
401  */
402 int vlc_pipe (int fds[2])
403 {
404 #ifdef HAVE_PIPE2
405     if (pipe2 (fds, O_CLOEXEC) == 0)
406         return 0;
407     if (errno != ENOSYS)
408         return -1;
409 #endif
410
411     if (pipe (fds))
412         return -1;
413
414     fcntl (fds[0], F_SETFD, FD_CLOEXEC);
415     fcntl (fds[1], F_SETFD, FD_CLOEXEC);
416     return 0;
417 }
418
419 #include <vlc_network.h>
420
421 /**
422  * Creates a socket file descriptor. The new file descriptor has the
423  * close-on-exec flag set.
424  * @param pf protocol family
425  * @param type socket type
426  * @param proto network protocol
427  * @param nonblock true to create a non-blocking socket
428  * @return a new file descriptor or -1
429  */
430 int vlc_socket (int pf, int type, int proto, bool nonblock)
431 {
432     int fd;
433
434 #ifdef SOCK_CLOEXEC
435     type |= SOCK_CLOEXEC;
436     if (nonblock)
437         type |= SOCK_NONBLOCK;
438     fd = socket (pf, type, proto);
439     if (fd != -1 || errno != EINVAL)
440         return fd;
441
442     type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
443 #endif
444
445     fd = socket (pf, type, proto);
446     if (fd == -1)
447         return -1;
448
449     fcntl (fd, F_SETFD, FD_CLOEXEC);
450     if (nonblock)
451         fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
452     return fd;
453 }
454
455 /**
456  * Accepts an inbound connection request on a listening socket.
457  * The new file descriptor has the close-on-exec flag set.
458  * @param lfd listening socket file descriptor
459  * @param addr pointer to the peer address or NULL [OUT]
460  * @param alen pointer to the length of the peer address or NULL [OUT]
461  * @param nonblock whether to put the new socket in non-blocking mode
462  * @return a new file descriptor, or -1 on error.
463  */
464 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
465 {
466 #ifdef HAVE_ACCEPT4
467     int flags = SOCK_CLOEXEC;
468     if (nonblock)
469         flags |= SOCK_NONBLOCK;
470
471     do
472     {
473         int fd = accept4 (lfd, addr, alen, flags);
474         if (fd != -1)
475             return fd;
476     }
477     while (errno == EINTR);
478
479     if (errno != ENOSYS)
480         return -1;
481 #endif
482
483     do
484     {
485         int fd = accept (lfd, addr, alen);
486         if (fd != -1)
487         {
488             fcntl (fd, F_SETFD, FD_CLOEXEC);
489             if (nonblock)
490                 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
491             return fd;
492         }
493     }
494     while (errno == EINTR);
495
496     return -1;
497 }