Qt: adapt the UI for correct spatializer values
[vlc.git] / src / posix / filesystem.c
blob8066bb135fba110c329064b6f5bb4cb480c589b5
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
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
30 #include <stdio.h>
31 #include <limits.h> /* NAME_MAX */
32 #include <errno.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #ifndef HAVE_LSTAT
39 # define lstat(a, b) stat(a, b)
40 #endif
41 #include <dirent.h>
42 #include <sys/socket.h>
44 #include <vlc_common.h>
45 #include <vlc_fs.h>
46 #include "libvlc.h" /* vlc_mkdir */
48 /**
49 * Opens a system file handle.
51 * @param filename file path to open (with UTF-8 encoding)
52 * @param flags open() flags, see the C library open() documentation
53 * @return a file handle on success, -1 on error (see errno).
54 * @note Contrary to standard open(), this function returns file handles
55 * with the close-on-exec flag enabled.
57 int vlc_open (const char *filename, int flags, ...)
59 unsigned int mode = 0;
60 va_list ap;
62 va_start (ap, flags);
63 if (flags & O_CREAT)
64 mode = va_arg (ap, unsigned int);
65 va_end (ap);
67 #ifdef O_CLOEXEC
68 flags |= O_CLOEXEC;
69 #endif
71 int fd = open (filename, flags, mode);
72 if (fd != -1)
73 fcntl (fd, F_SETFD, FD_CLOEXEC);
74 return fd;
77 /**
78 * Opens a system file handle relative to an existing directory handle.
80 * @param dir directory file descriptor
81 * @param filename file path to open (with UTF-8 encoding)
82 * @param flags open() flags, see the C library open() documentation
83 * @return a file handle on success, -1 on error (see errno).
84 * @note Contrary to standard open(), this function returns file handles
85 * with the close-on-exec flag enabled.
87 int vlc_openat (int dir, const char *filename, int flags, ...)
89 unsigned int mode = 0;
90 va_list ap;
92 va_start (ap, flags);
93 if (flags & O_CREAT)
94 mode = va_arg (ap, unsigned int);
95 va_end (ap);
97 #ifdef O_CLOEXEC
98 flags |= O_CLOEXEC;
99 #endif
101 #ifdef HAVE_OPENAT
102 int fd = openat (dir, filename, flags, mode);
103 if (fd != -1)
104 fcntl (fd, F_SETFD, FD_CLOEXEC);
105 #else
106 int fd = -1;
107 errno = ENOSYS;
108 (void) mode;
109 #endif
110 return fd;
115 * Creates a directory using UTF-8 paths.
117 * @param dirname a UTF-8 string with the name of the directory that you
118 * want to create.
119 * @param mode directory permissions
120 * @return 0 on success, -1 on error (see errno).
122 int vlc_mkdir (const char *dirname, mode_t mode)
124 return mkdir (dirname, mode);
128 * Opens a DIR pointer.
130 * @param dirname UTF-8 representation of the directory name
131 * @return a pointer to the DIR struct, or NULL in case of error.
132 * Release with standard closedir().
134 DIR *vlc_opendir (const char *dirname)
136 return opendir (dirname);
140 * Reads the next file name from an open directory.
142 * @param dir The directory that is being read
144 * @return a UTF-8 string of the directory entry. Use free() to release it.
145 * If there are no more entries in the directory, NULL is returned.
146 * If an error occurs, errno is set and NULL is returned.
148 char *vlc_readdir( DIR *dir )
150 /* Beware that readdir_r() assumes <buf> is large enough to hold the result
151 * dirent including the file name. A buffer overflow could occur otherwise.
152 * In particular, pathconf() and _POSIX_NAME_MAX cannot be used here. */
153 struct dirent *ent;
154 char *path = NULL;
156 long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
157 #if !defined(__OS2__) || !defined(__INNOTEK_LIBC__)
158 #ifdef NAME_MAX
159 /* POSIX says there shall we room for NAME_MAX bytes at all times */
160 if (/*len == -1 ||*/ len < NAME_MAX)
161 len = NAME_MAX;
162 #else
163 /* OS is broken. Lets assume there is no files left. */
164 if (len == -1)
165 return NULL;
166 #endif
167 len += offsetof (struct dirent, d_name) + 1;
168 #else /* __OS2__ && __INNOTEK_LIBC__ */
169 /* In the implementation of Innotek LIBC, aka kLIBC on OS/2,
170 * fpathconf (_PC_NAME_MAX) is broken, and d_name is not the last member
171 * of struct dirent.
172 * So just allocate as many as the size of struct dirent. */
173 len = sizeof (struct dirent);
174 #endif
176 struct dirent *buf = malloc (len);
177 if (unlikely(buf == NULL))
178 return NULL;
180 int val = readdir_r (dir, buf, &ent);
181 if (val != 0)
182 errno = val;
183 else if (ent != NULL)
184 path = strdup (ent->d_name);
185 free (buf);
186 return path;
190 * Finds file/inode information, as stat().
191 * Consider using fstat() instead, if possible.
193 * @param filename UTF-8 file path
195 int vlc_stat (const char *filename, struct stat *buf)
197 return stat (filename, buf);
201 * Finds file/inode information, as lstat().
202 * Consider using fstat() instead, if possible.
204 * @param filename UTF-8 file path
206 int vlc_lstat (const char *filename, struct stat *buf)
208 return lstat (filename, buf);
212 * Removes a file.
214 * @param filename a UTF-8 string with the name of the file you want to delete.
215 * @return A 0 return value indicates success. A -1 return value indicates an
216 * error, and an error code is stored in errno
218 int vlc_unlink (const char *filename)
220 return unlink (filename);
224 * Moves a file atomically. This only works within a single file system.
226 * @param oldpath path to the file before the move
227 * @param newpath intended path to the file after the move
228 * @return A 0 return value indicates success. A -1 return value indicates an
229 * error, and an error code is stored in errno
231 int vlc_rename (const char *oldpath, const char *newpath)
233 return rename (oldpath, newpath);
237 * Determines the current working directory.
239 * @return the current working directory (must be free()'d)
240 * or NULL on error
242 char *vlc_getcwd (void)
244 /* Try $PWD */
245 const char *pwd = getenv ("PWD");
246 if (pwd != NULL)
248 struct stat s1, s2;
249 /* Make sure $PWD is correct */
250 if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
251 && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
252 return strdup (pwd);
255 /* Otherwise iterate getcwd() until the buffer is big enough */
256 long path_max = pathconf (".", _PC_PATH_MAX);
257 size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
259 for (;; size *= 2)
261 char *buf = malloc (size);
262 if (unlikely(buf == NULL))
263 break;
265 if (getcwd (buf, size) != NULL)
266 return buf;
267 free (buf);
269 if (errno != ERANGE)
270 break;
272 return NULL;
276 * Duplicates a file descriptor. The new file descriptor has the close-on-exec
277 * descriptor flag set.
278 * @return a new file descriptor or -1
280 int vlc_dup (int oldfd)
282 int newfd;
284 #ifdef F_DUPFD_CLOEXEC
285 newfd = fcntl (oldfd, F_DUPFD_CLOEXEC);
286 if (unlikely(newfd == -1 && errno == EINVAL))
287 #endif
289 newfd = dup (oldfd);
290 if (likely(newfd != -1))
291 fcntl (newfd, F_SETFD, FD_CLOEXEC);
293 return newfd;
296 #ifdef __ANDROID__ /* && we support android < 2.3 */
297 /* pipe2() is declared and available since android-9 NDK,
298 * although it is available in libc.a since android-3
299 * We redefine the function here in order to be able to run
300 * on versions of Android older than 2.3
302 #include <sys/syscall.h>
303 //#include <sys/linux-syscalls.h> // fucking brokeness
304 int pipe2(int fds[2], int flags)
306 return syscall(/*__NR_pipe2 */ 359, fds, flags);
308 #endif /* __ANDROID__ */
311 * Creates a pipe (see "man pipe" for further reference).
313 int vlc_pipe (int fds[2])
315 #ifdef HAVE_PIPE2
316 if (pipe2 (fds, O_CLOEXEC) == 0)
317 return 0;
318 if (errno != ENOSYS)
319 return -1;
320 #endif
322 if (pipe (fds))
323 return -1;
325 fcntl (fds[0], F_SETFD, FD_CLOEXEC);
326 fcntl (fds[1], F_SETFD, FD_CLOEXEC);
327 return 0;
330 #include <vlc_network.h>
333 * Creates a socket file descriptor. The new file descriptor has the
334 * close-on-exec flag set.
335 * @param pf protocol family
336 * @param type socket type
337 * @param proto network protocol
338 * @param nonblock true to create a non-blocking socket
339 * @return a new file descriptor or -1
341 int vlc_socket (int pf, int type, int proto, bool nonblock)
343 int fd;
345 #ifdef SOCK_CLOEXEC
346 type |= SOCK_CLOEXEC;
347 if (nonblock)
348 type |= SOCK_NONBLOCK;
349 fd = socket (pf, type, proto);
350 if (fd != -1 || errno != EINVAL)
351 return fd;
353 type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
354 #endif
356 fd = socket (pf, type, proto);
357 if (fd == -1)
358 return -1;
360 fcntl (fd, F_SETFD, FD_CLOEXEC);
361 if (nonblock)
362 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
363 return fd;
367 * Accepts an inbound connection request on a listening socket.
368 * The new file descriptor has the close-on-exec flag set.
369 * @param lfd listening socket file descriptor
370 * @param addr pointer to the peer address or NULL [OUT]
371 * @param alen pointer to the length of the peer address or NULL [OUT]
372 * @param nonblock whether to put the new socket in non-blocking mode
373 * @return a new file descriptor, or -1 on error.
375 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
377 #ifdef HAVE_ACCEPT4
378 int flags = SOCK_CLOEXEC;
379 if (nonblock)
380 flags |= SOCK_NONBLOCK;
384 int fd = accept4 (lfd, addr, alen, flags);
385 if (fd != -1)
386 return fd;
388 while (errno == EINTR);
390 if (errno != ENOSYS)
391 return -1;
392 #endif
396 int fd = accept (lfd, addr, alen);
397 if (fd != -1)
399 fcntl (fd, F_SETFD, FD_CLOEXEC);
400 if (nonblock)
401 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
402 return fd;
405 while (errno == EINTR);
407 return -1;