1 /*****************************************************************************
2 * filesystem.c: POSIX file system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_charset.h>
31 #include "libvlc.h" /* vlc_mkdir */
36 #include <limits.h> /* NAME_MAX */
38 #include <sys/types.h>
43 #include <sys/socket.h>
46 # define lstat( a, b ) stat(a, b)
50 * Opens a system file handle.
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.
58 int vlc_open (const char *filename, int flags, ...)
60 unsigned int mode = 0;
65 mode = va_arg (ap, unsigned int);
72 const char *local_name = ToLocale (filename);
74 if (local_name == NULL)
80 int fd = open (local_name, flags, mode);
82 fcntl (fd, F_SETFD, FD_CLOEXEC);
84 LocaleFree (local_name);
89 * Opens a system file handle relative to an existing directory handle.
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.
98 int vlc_openat (int dir, const char *filename, int flags, ...)
100 unsigned int mode = 0;
103 va_start (ap, flags);
105 mode = va_arg (ap, unsigned int);
112 const char *local_name = ToLocale (filename);
113 if (local_name == NULL)
120 int fd = openat (dir, local_name, flags, mode);
122 fcntl (fd, F_SETFD, FD_CLOEXEC);
128 LocaleFree (local_name);
134 * Creates a directory using UTF-8 paths.
136 * @param dirname a UTF-8 string with the name of the directory that you
138 * @param mode directory permissions
139 * @return 0 on success, -1 on error (see errno).
141 int vlc_mkdir (const char *dirname, mode_t mode)
143 char *locname = ToLocale (dirname);
144 if (unlikely(locname == NULL))
150 int res = mkdir (locname, mode);
151 LocaleFree (locname);
156 * Opens a DIR pointer.
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().
162 DIR *vlc_opendir (const char *dirname)
164 const char *local_name = ToLocale (dirname);
165 if (unlikely(local_name == NULL))
171 DIR *dir = opendir (local_name);
172 LocaleFree (local_name);
177 * Reads the next file name from an open directory.
179 * @param dir The directory that is being read
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.
185 char *vlc_readdir( DIR *dir )
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. */
193 long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
200 return NULL; // OS is broken. There is no sane way to fix this.
203 len += offsetof (struct dirent, d_name) + 1;
205 struct dirent *buf = malloc (len);
206 if (unlikely(buf == NULL))
209 int val = readdir_r (dir, buf, &ent);
212 else if (ent != NULL)
214 path = strdup (ent->d_name);
216 path = FromCharset ("UTF-8-MAC", ent->d_name, strlen (ent->d_name));
222 static int vlc_statEx (const char *filename, struct stat *buf, bool deref)
224 const char *local_name = ToLocale (filename);
225 if (unlikely(local_name == NULL))
231 int res = deref ? stat (local_name, buf)
232 : lstat (local_name, buf);
233 LocaleFree (local_name);
238 * Finds file/inode information, as stat().
239 * Consider using fstat() instead, if possible.
241 * @param filename UTF-8 file path
243 int vlc_stat (const char *filename, struct stat *buf)
245 return vlc_statEx (filename, buf, true);
249 * Finds file/inode information, as lstat().
250 * Consider using fstat() instead, if possible.
252 * @param filename UTF-8 file path
254 int vlc_lstat (const char *filename, struct stat *buf)
256 return vlc_statEx (filename, buf, false);
262 * @param filename a UTF-8 string with the name of the file you want to delete.
263 * @return A 0 return value indicates success. A -1 return value indicates an
264 * error, and an error code is stored in errno
266 int vlc_unlink (const char *filename)
268 const char *local_name = ToLocale (filename);
269 if (unlikely(local_name == NULL))
275 int ret = unlink (local_name);
276 LocaleFree (local_name);
281 * Moves a file atomically. This only works within a single file system.
283 * @param oldpath path to the file before the move
284 * @param newpath intended path to the file after the move
285 * @return A 0 return value indicates success. A -1 return value indicates an
286 * error, and an error code is stored in errno
288 int vlc_rename (const char *oldpath, const char *newpath)
290 const char *lo = ToLocale (oldpath);
294 const char *ln = ToLocale (newpath);
303 int ret = rename (lo, ln);
310 * Duplicates a file descriptor. The new file descriptor has the close-on-exec
311 * descriptor flag set.
312 * @return a new file descriptor or -1
314 int vlc_dup (int oldfd)
319 /* Unfortunately, dup3() works like dup2(), not like plain dup(). So we
320 * need such contortion to find the new file descriptor while preserving
321 * thread safety of the file descriptor table. */
322 newfd = vlc_open ("/dev/null", O_RDONLY);
323 if (likely(newfd != -1))
325 if (likely(dup3 (oldfd, newfd, O_CLOEXEC) == newfd))
332 if (likely(newfd != -1))
333 fcntl (newfd, F_SETFD, FD_CLOEXEC);
338 * Creates a pipe (see "man pipe" for further reference).
340 int vlc_pipe (int fds[2])
343 if (pipe2 (fds, O_CLOEXEC) == 0)
352 fcntl (fds[0], F_SETFD, FD_CLOEXEC);
353 fcntl (fds[1], F_SETFD, FD_CLOEXEC);
357 #include <vlc_network.h>
360 * Creates a socket file descriptor. The new file descriptor has the
361 * close-on-exec flag set.
362 * @param pf protocol family
363 * @param type socket type
364 * @param proto network protocol
365 * @param nonblock true to create a non-blocking socket
366 * @return a new file descriptor or -1
368 int vlc_socket (int pf, int type, int proto, bool nonblock)
373 type |= SOCK_CLOEXEC;
375 type |= SOCK_NONBLOCK;
376 fd = socket (pf, type, proto);
377 if (fd != -1 || errno != EINVAL)
380 type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
383 fd = socket (pf, type, proto);
387 fcntl (fd, F_SETFD, FD_CLOEXEC);
389 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
394 * Accepts an inbound connection request on a listening socket.
395 * The new file descriptor has the close-on-exec flag set.
396 * @param lfd listening socket file descriptor
397 * @param addr pointer to the peer address or NULL [OUT]
398 * @param alen pointer to the length of the peer address or NULL [OUT]
399 * @param nonblock whether to put the new socket in non-blocking mode
400 * @return a new file descriptor, or -1 on error.
402 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
405 int flags = SOCK_CLOEXEC;
407 flags |= SOCK_NONBLOCK;
411 int fd = accept4 (lfd, addr, alen, flags);
415 while (errno == EINTR);
423 int fd = accept (lfd, addr, alen);
426 fcntl (fd, F_SETFD, FD_CLOEXEC);
428 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
432 while (errno == EINTR);