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 *****************************************************************************/
31 #include <limits.h> /* NAME_MAX */
34 #include <sys/types.h>
39 # define lstat(a, b) stat(a, b)
42 #include <sys/socket.h>
44 #include <vlc_common.h>
46 #include "libvlc.h" /* vlc_mkdir */
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;
64 mode = va_arg (ap, unsigned int);
71 int fd = open (filename, flags, mode);
73 fcntl (fd, F_SETFD, FD_CLOEXEC);
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;
94 mode = va_arg (ap, unsigned int);
102 int fd = openat (dir, filename, flags, mode);
104 fcntl (fd, F_SETFD, FD_CLOEXEC);
115 * Creates a directory using UTF-8 paths.
117 * @param dirname a UTF-8 string with the name of the directory that you
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. */
156 long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
157 /* POSIX says there shall we room for NAME_MAX bytes at all times */
158 if (/*len == -1 ||*/ len < NAME_MAX)
160 len += offsetof (struct dirent, d_name) + 1;
162 struct dirent *buf = malloc (len);
163 if (unlikely(buf == NULL))
166 int val = readdir_r (dir, buf, &ent);
169 else if (ent != NULL)
170 path = strdup (ent->d_name);
176 * Finds file/inode information, as stat().
177 * Consider using fstat() instead, if possible.
179 * @param filename UTF-8 file path
181 int vlc_stat (const char *filename, struct stat *buf)
183 return stat (filename, buf);
187 * Finds file/inode information, as lstat().
188 * Consider using fstat() instead, if possible.
190 * @param filename UTF-8 file path
192 int vlc_lstat (const char *filename, struct stat *buf)
194 return lstat (filename, buf);
200 * @param filename a UTF-8 string with the name of the file you want to delete.
201 * @return A 0 return value indicates success. A -1 return value indicates an
202 * error, and an error code is stored in errno
204 int vlc_unlink (const char *filename)
206 return unlink (filename);
210 * Moves a file atomically. This only works within a single file system.
212 * @param oldpath path to the file before the move
213 * @param newpath intended path to the file after the move
214 * @return A 0 return value indicates success. A -1 return value indicates an
215 * error, and an error code is stored in errno
217 int vlc_rename (const char *oldpath, const char *newpath)
219 return rename (oldpath, newpath);
223 * Determines the current working directory.
225 * @return the current working directory (must be free()'d)
228 char *vlc_getcwd (void)
231 const char *pwd = getenv ("PWD");
235 /* Make sure $PWD is correct */
236 if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
237 && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
241 /* Otherwise iterate getcwd() until the buffer is big enough */
242 long path_max = pathconf (".", _PC_PATH_MAX);
243 size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
247 char *buf = malloc (size);
248 if (unlikely(buf == NULL))
251 if (getcwd (buf, size) != NULL)
262 * Duplicates a file descriptor. The new file descriptor has the close-on-exec
263 * descriptor flag set.
264 * @return a new file descriptor or -1
266 int vlc_dup (int oldfd)
270 #ifdef F_DUPFD_CLOEXEC
271 newfd = fcntl (oldfd, F_DUPFD_CLOEXEC);
272 if (unlikely(newfd == -1 && errno == EINVAL))
276 if (likely(newfd != -1))
277 fcntl (newfd, F_SETFD, FD_CLOEXEC);
283 * Creates a pipe (see "man pipe" for further reference).
285 int vlc_pipe (int fds[2])
288 if (pipe2 (fds, O_CLOEXEC) == 0)
297 fcntl (fds[0], F_SETFD, FD_CLOEXEC);
298 fcntl (fds[1], F_SETFD, FD_CLOEXEC);
302 #include <vlc_network.h>
305 * Creates a socket file descriptor. The new file descriptor has the
306 * close-on-exec flag set.
307 * @param pf protocol family
308 * @param type socket type
309 * @param proto network protocol
310 * @param nonblock true to create a non-blocking socket
311 * @return a new file descriptor or -1
313 int vlc_socket (int pf, int type, int proto, bool nonblock)
318 type |= SOCK_CLOEXEC;
320 type |= SOCK_NONBLOCK;
321 fd = socket (pf, type, proto);
322 if (fd != -1 || errno != EINVAL)
325 type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
328 fd = socket (pf, type, proto);
332 fcntl (fd, F_SETFD, FD_CLOEXEC);
334 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
339 * Accepts an inbound connection request on a listening socket.
340 * The new file descriptor has the close-on-exec flag set.
341 * @param lfd listening socket file descriptor
342 * @param addr pointer to the peer address or NULL [OUT]
343 * @param alen pointer to the length of the peer address or NULL [OUT]
344 * @param nonblock whether to put the new socket in non-blocking mode
345 * @return a new file descriptor, or -1 on error.
347 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
350 int flags = SOCK_CLOEXEC;
352 flags |= SOCK_NONBLOCK;
356 int fd = accept4 (lfd, addr, alen, flags);
360 while (errno == EINTR);
368 int fd = accept (lfd, addr, alen);
371 fcntl (fd, F_SETFD, FD_CLOEXEC);
373 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
377 while (errno == EINTR);