]> git.sesse.net Git - vlc/blob - src/posix/filesystem.c
Fix grammar / typo
[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 <assert.h>
29
30 #include <stdio.h>
31 #include <limits.h> /* NAME_MAX */
32 #include <errno.h>
33
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>
43
44 #include <vlc_common.h>
45 #include <vlc_fs.h>
46 #include "libvlc.h" /* vlc_mkdir */
47
48 /**
49  * Opens a system file handle.
50  *
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.
56  */
57 int vlc_open (const char *filename, int flags, ...)
58 {
59     unsigned int mode = 0;
60     va_list ap;
61
62     va_start (ap, flags);
63     if (flags & O_CREAT)
64         mode = va_arg (ap, unsigned int);
65     va_end (ap);
66
67 #ifdef O_CLOEXEC
68     flags |= O_CLOEXEC;
69 #endif
70
71     int fd = open (filename, flags, mode);
72     if (fd != -1)
73         fcntl (fd, F_SETFD, FD_CLOEXEC);
74     return fd;
75 }
76
77 /**
78  * Opens a system file handle relative to an existing directory handle.
79  *
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.
86  */
87 int vlc_openat (int dir, const char *filename, int flags, ...)
88 {
89     unsigned int mode = 0;
90     va_list ap;
91
92     va_start (ap, flags);
93     if (flags & O_CREAT)
94         mode = va_arg (ap, unsigned int);
95     va_end (ap);
96
97 #ifdef O_CLOEXEC
98     flags |= O_CLOEXEC;
99 #endif
100
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         VLC_UNUSED (dir);
107         VLC_UNUSED (filename);
108         VLC_UNUSED (mode);
109
110     int fd = -1;
111     errno = ENOSYS;
112 #endif
113     return fd;
114 }
115
116
117 /**
118  * Creates a directory using UTF-8 paths.
119  *
120  * @param dirname a UTF-8 string with the name of the directory that you
121  *        want to create.
122  * @param mode directory permissions
123  * @return 0 on success, -1 on error (see errno).
124  */
125 int vlc_mkdir (const char *dirname, mode_t mode)
126 {
127     return mkdir (dirname, mode);
128 }
129
130 /**
131  * Opens a DIR pointer.
132  *
133  * @param dirname UTF-8 representation of the directory name
134  * @return a pointer to the DIR struct, or NULL in case of error.
135  * Release with standard closedir().
136  */
137 DIR *vlc_opendir (const char *dirname)
138 {
139     return opendir (dirname);
140 }
141
142 /**
143  * Reads the next file name from an open directory.
144  *
145  * @param dir The directory that is being read
146  *
147  * @return a UTF-8 string of the directory entry. Use free() to release it.
148  * If there are no more entries in the directory, NULL is returned.
149  * If an error occurs, errno is set and NULL is returned.
150  */
151 char *vlc_readdir( DIR *dir )
152 {
153     /* Beware that readdir_r() assumes <buf> is large enough to hold the result
154      * dirent including the file name. A buffer overflow could occur otherwise.
155      * In particular, pathconf() and _POSIX_NAME_MAX cannot be used here. */
156     struct dirent *ent;
157     char *path = NULL;
158
159     long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
160     /* POSIX says there shall be room for NAME_MAX bytes at all times */
161     if (len == -1 || len < NAME_MAX)
162         len = NAME_MAX;
163     len += sizeof (*ent) + 1 - sizeof (ent->d_name);
164
165     struct dirent *buf = malloc (len);
166     if (unlikely(buf == NULL))
167         return NULL;
168
169     int val = readdir_r (dir, buf, &ent);
170     if (val != 0)
171         errno = val;
172     else if (ent != NULL)
173         path = strdup (ent->d_name);
174     free (buf);
175     return path;
176 }
177
178 /**
179  * Finds file/inode information, as stat().
180  * Consider using fstat() instead, if possible.
181  *
182  * @param filename UTF-8 file path
183  */
184 int vlc_stat (const char *filename, struct stat *buf)
185 {
186     return stat (filename, buf);
187 }
188
189 /**
190  * Finds file/inode information, as lstat().
191  * Consider using fstat() instead, if possible.
192  *
193  * @param filename UTF-8 file path
194  */
195 int vlc_lstat (const char *filename, struct stat *buf)
196 {
197     return lstat (filename, buf);
198 }
199
200 /**
201  * Removes a file.
202  *
203  * @param filename a UTF-8 string with the name of the file you want to delete.
204  * @return A 0 return value indicates success. A -1 return value indicates an
205  *        error, and an error code is stored in errno
206  */
207 int vlc_unlink (const char *filename)
208 {
209     return unlink (filename);
210 }
211
212 /**
213  * Moves a file atomically. This only works within a single file system.
214  *
215  * @param oldpath path to the file before the move
216  * @param newpath intended path to the file after the move
217  * @return A 0 return value indicates success. A -1 return value indicates an
218  *        error, and an error code is stored in errno
219  */
220 int vlc_rename (const char *oldpath, const char *newpath)
221 {
222     return rename (oldpath, newpath);
223 }
224
225 /**
226  * Determines the current working directory.
227  *
228  * @return the current working directory (must be free()'d)
229  *         or NULL on error
230  */
231 char *vlc_getcwd (void)
232 {
233     /* Try $PWD */
234     const char *pwd = getenv ("PWD");
235     if (pwd != NULL)
236     {
237         struct stat s1, s2;
238         /* Make sure $PWD is correct */
239         if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
240          && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
241             return strdup (pwd);
242     }
243
244     /* Otherwise iterate getcwd() until the buffer is big enough */
245     long path_max = pathconf (".", _PC_PATH_MAX);
246     size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
247
248     for (;; size *= 2)
249     {
250         char *buf = malloc (size);
251         if (unlikely(buf == NULL))
252             break;
253
254         if (getcwd (buf, size) != NULL)
255             return buf;
256         free (buf);
257
258         if (errno != ERANGE)
259             break;
260     }
261     return NULL;
262 }
263
264 /**
265  * Duplicates a file descriptor. The new file descriptor has the close-on-exec
266  * descriptor flag set.
267  * @return a new file descriptor or -1
268  */
269 int vlc_dup (int oldfd)
270 {
271     int newfd;
272
273 #ifdef F_DUPFD_CLOEXEC
274     newfd = fcntl (oldfd, F_DUPFD_CLOEXEC);
275     if (unlikely(newfd == -1 && errno == EINVAL))
276 #endif
277     {
278         newfd = dup (oldfd);
279         if (likely(newfd != -1))
280             fcntl (newfd, F_SETFD, FD_CLOEXEC);
281     }
282     return newfd;
283 }
284
285 /**
286  * Creates a pipe (see "man pipe" for further reference).
287  */
288 int vlc_pipe (int fds[2])
289 {
290 #ifdef HAVE_PIPE2
291     if (pipe2 (fds, O_CLOEXEC) == 0)
292         return 0;
293     if (errno != ENOSYS)
294         return -1;
295 #endif
296
297     if (pipe (fds))
298         return -1;
299
300     fcntl (fds[0], F_SETFD, FD_CLOEXEC);
301     fcntl (fds[1], F_SETFD, FD_CLOEXEC);
302     return 0;
303 }
304
305 #include <vlc_network.h>
306
307 /**
308  * Creates a socket file descriptor. The new file descriptor has the
309  * close-on-exec flag set.
310  * @param pf protocol family
311  * @param type socket type
312  * @param proto network protocol
313  * @param nonblock true to create a non-blocking socket
314  * @return a new file descriptor or -1
315  */
316 int vlc_socket (int pf, int type, int proto, bool nonblock)
317 {
318     int fd;
319
320 #ifdef SOCK_CLOEXEC
321     type |= SOCK_CLOEXEC;
322     if (nonblock)
323         type |= SOCK_NONBLOCK;
324     fd = socket (pf, type, proto);
325     if (fd != -1 || errno != EINVAL)
326         return fd;
327
328     type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
329 #endif
330
331     fd = socket (pf, type, proto);
332     if (fd == -1)
333         return -1;
334
335     fcntl (fd, F_SETFD, FD_CLOEXEC);
336     if (nonblock)
337         fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
338     return fd;
339 }
340
341 /**
342  * Accepts an inbound connection request on a listening socket.
343  * The new file descriptor has the close-on-exec flag set.
344  * @param lfd listening socket file descriptor
345  * @param addr pointer to the peer address or NULL [OUT]
346  * @param alen pointer to the length of the peer address or NULL [OUT]
347  * @param nonblock whether to put the new socket in non-blocking mode
348  * @return a new file descriptor, or -1 on error.
349  */
350 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
351 {
352 #ifdef HAVE_ACCEPT4
353     int flags = SOCK_CLOEXEC;
354     if (nonblock)
355         flags |= SOCK_NONBLOCK;
356
357     do
358     {
359         int fd = accept4 (lfd, addr, alen, flags);
360         if (fd != -1)
361             return fd;
362     }
363     while (errno == EINTR);
364
365     if (errno != ENOSYS)
366         return -1;
367 #endif
368
369     do
370     {
371         int fd = accept (lfd, addr, alen);
372         if (fd != -1)
373         {
374             fcntl (fd, F_SETFD, FD_CLOEXEC);
375             if (nonblock)
376                 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
377             return fd;
378         }
379     }
380     while (errno == EINTR);
381
382     return -1;
383 }