Improve vlc_readdir documentation
[vlc/asuraparaju-public.git] / src / text / filesystem.c
blobff08e1e934a05d1136bf8db2a6238fbade11099e
1 /*****************************************************************************
2 * filesystem.c: 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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include <vlc_fs.h>
34 #include "libvlc.h" /* vlc_mkdir */
35 #include <vlc_rand.h>
37 #include <assert.h>
39 #include <stdio.h>
40 #include <limits.h> /* NAME_MAX */
41 #if !defined(NAME_MAX) && defined(_POSIX_NAME_MAX)
42 # define NAME_MAX _POSIX_NAME_MAX
43 #endif
44 #include <errno.h>
45 #include <sys/types.h>
46 #include <dirent.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
49 #endif
50 #ifdef HAVE_FCNTL_H
51 # include <fcntl.h>
52 #endif
53 #ifdef WIN32
54 # include <io.h>
55 # include <winsock2.h>
56 # ifndef UNDER_CE
57 # include <direct.h>
58 # else
59 # include <tchar.h>
60 # endif
61 #else
62 # include <unistd.h>
63 # include <sys/socket.h>
64 #endif
66 #ifndef HAVE_LSTAT
67 # define lstat( a, b ) stat(a, b)
68 #endif
70 #ifdef WIN32
71 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
73 if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
75 errno = ENOENT;
76 return -1;
78 wpath[MAX_PATH] = L'\0';
79 return 0;
81 # define CONVERT_PATH(path, wpath, err) \
82 wchar_t wpath[MAX_PATH+1]; \
83 if (convert_path (path, wpath)) \
84 return (err)
85 #endif
87 /**
88 * Opens a system file handle.
90 * @param filename file path to open (with UTF-8 encoding)
91 * @param flags open() flags, see the C library open() documentation
92 * @return a file handle on success, -1 on error (see errno).
93 * @note Contrary to standard open(), this function returns file handles
94 * with the close-on-exec flag enabled.
96 int vlc_open (const char *filename, int flags, ...)
98 unsigned int mode = 0;
99 va_list ap;
101 va_start (ap, flags);
102 if (flags & O_CREAT)
103 mode = va_arg (ap, unsigned int);
104 va_end (ap);
106 #ifdef O_CLOEXEC
107 flags |= O_CLOEXEC;
108 #endif
110 #ifdef UNDER_CE
111 /*_open translates to wchar internally on WinCE*/
112 return _open (filename, flags, mode);
113 #elif defined (WIN32)
115 * open() cannot open files with non-“ANSI” characters on Windows.
116 * We use _wopen() instead. Same thing for mkdir() and stat().
118 CONVERT_PATH(filename, wpath, -1);
119 return _wopen (wpath, flags, mode);
121 #endif
122 const char *local_name = ToLocale (filename);
124 if (local_name == NULL)
126 errno = ENOENT;
127 return -1;
130 int fd = open (local_name, flags, mode);
131 #ifdef HAVE_FCNTL
132 if (fd != -1)
133 fcntl (fd, F_SETFD, FD_CLOEXEC);
134 #endif
136 LocaleFree (local_name);
137 return fd;
141 * Opens a FILE pointer.
142 * @param filename file path, using UTF-8 encoding
143 * @param mode fopen file open mode
144 * @return NULL on error, an open FILE pointer on success.
146 FILE *vlc_fopen (const char *filename, const char *mode)
148 int rwflags = 0, oflags = 0;
149 bool append = false;
151 for (const char *ptr = mode; *ptr; ptr++)
153 switch (*ptr)
155 case 'r':
156 rwflags = O_RDONLY;
157 break;
159 case 'a':
160 rwflags = O_WRONLY;
161 oflags |= O_CREAT;
162 append = true;
163 break;
165 case 'w':
166 rwflags = O_WRONLY;
167 oflags |= O_CREAT | O_TRUNC;
168 break;
170 case '+':
171 rwflags = O_RDWR;
172 break;
174 #ifdef O_TEXT
175 case 't':
176 oflags |= O_TEXT;
177 break;
178 #endif
182 int fd = vlc_open (filename, rwflags | oflags, 0666);
183 if (fd == -1)
184 return NULL;
186 if (append && (lseek (fd, 0, SEEK_END) == -1))
188 close (fd);
189 return NULL;
192 FILE *stream = fdopen (fd, mode);
193 if (stream == NULL)
194 close (fd);
196 return stream;
200 * Opens a system file handle relative to an existing directory handle.
202 * @param dir directory file descriptor
203 * @param filename file path to open (with UTF-8 encoding)
204 * @param flags open() flags, see the C library open() documentation
205 * @return a file handle on success, -1 on error (see errno).
206 * @note Contrary to standard open(), this function returns file handles
207 * with the close-on-exec flag enabled.
209 int vlc_openat (int dir, const char *filename, int flags, ...)
211 unsigned int mode = 0;
212 va_list ap;
214 va_start (ap, flags);
215 if (flags & O_CREAT)
216 mode = va_arg (ap, unsigned int);
217 va_end (ap);
219 #ifdef O_CLOEXEC
220 flags |= O_CLOEXEC;
221 #endif
223 const char *local_name = ToLocale (filename);
224 if (local_name == NULL)
226 errno = ENOENT;
227 return -1;
230 #ifdef HAVE_OPENAT
231 int fd = openat (dir, local_name, flags, mode);
232 # ifdef HAVE_FCNTL
233 if (fd != -1)
234 fcntl (fd, F_SETFD, FD_CLOEXEC);
235 # endif
236 #else
237 int fd = -1;
238 errno = ENOSYS;
239 #endif
241 LocaleFree (local_name);
242 return fd;
247 * Creates a directory using UTF-8 paths.
249 * @param dirname a UTF-8 string with the name of the directory that you
250 * want to create.
251 * @param mode directory permissions
252 * @return 0 on success, -1 on error (see errno).
254 int vlc_mkdir( const char *dirname, mode_t mode )
256 #if defined (UNDER_CE)
257 (void) mode;
258 /* mkdir converts internally to wchar */
259 return _mkdir(dirname);
260 #elif defined (WIN32)
261 (void) mode;
262 CONVERT_PATH (dirname, wpath, -1);
263 return _wmkdir (wpath);
265 #else
266 char *locname = ToLocale( dirname );
267 int res;
269 if( locname == NULL )
271 errno = ENOENT;
272 return -1;
274 res = mkdir( locname, mode );
276 LocaleFree( locname );
277 return res;
278 #endif
282 * Opens a DIR pointer.
284 * @param dirname UTF-8 representation of the directory name
285 * @return a pointer to the DIR struct, or NULL in case of error.
286 * Release with standard closedir().
288 DIR *vlc_opendir( const char *dirname )
290 #ifdef WIN32
291 CONVERT_PATH (dirname, wpath, NULL);
292 return (DIR *)vlc_wopendir (wpath);
294 #else
295 const char *local_name = ToLocale( dirname );
297 if( local_name != NULL )
299 DIR *dir = opendir( local_name );
300 LocaleFree( local_name );
301 return dir;
303 #endif
305 errno = ENOENT;
306 return NULL;
310 * Reads the next file name from an open directory.
312 * @param dir The directory that is being read
314 * @return a UTF-8 string of the directory entry. Use free() to release it.
315 * If there are no more entries in the directory, NULL is returned.
316 * If an error occurs, errno is set and NULL is returned.
318 char *vlc_readdir( DIR *dir )
320 #ifdef WIN32
321 struct _wdirent *ent = vlc_wreaddir (dir);
322 if (ent == NULL)
323 return NULL;
325 return FromWide (ent->d_name);
326 #else
327 struct dirent *ent;
328 struct
330 struct dirent ent;
331 char buf[NAME_MAX + 1];
332 } buf;
333 int val = readdir_r (dir, &buf.ent, &ent);
334 if (val)
336 errno = val;
337 return NULL;
339 return ent ? vlc_fix_readdir( ent->d_name ) : NULL;
340 #endif
343 static int dummy_select( const char *str )
345 (void)str;
346 return 1;
350 * Does the same as vlc_scandir(), but takes an open directory pointer
351 * instead of a directory path.
353 int vlc_loaddir( DIR *dir, char ***namelist,
354 int (*select)( const char * ),
355 int (*compar)( const char **, const char ** ) )
357 assert (dir);
359 if (select == NULL)
360 select = dummy_select;
362 char **tab = NULL;
363 unsigned num = 0;
365 rewinddir (dir);
367 for (unsigned size = 0;;)
369 errno = 0;
370 char *entry = vlc_readdir (dir);
371 if (entry == NULL)
373 if (errno)
374 goto error;
375 break;
378 if (!select (entry))
380 free (entry);
381 continue;
384 if (num >= size)
386 size = size ? (2 * size) : 16;
387 char **newtab = realloc (tab, sizeof (*tab) * (size));
389 if (unlikely(newtab == NULL))
391 free (entry);
392 goto error;
394 tab = newtab;
397 tab[num++] = entry;
400 if (compar != NULL)
401 qsort (tab, num, sizeof (*tab),
402 (int (*)( const void *, const void *))compar);
403 *namelist = tab;
404 return num;
406 error:
407 for (unsigned i = 0; i < num; i++)
408 free (tab[i]);
409 free (tab);
410 return -1;
414 * Selects file entries from a directory, as GNU C scandir().
416 * @param dirname UTF-8 diretory path
417 * @param pointer [OUT] pointer set, on successful completion, to the address
418 * of a table of UTF-8 filenames. All filenames must be freed with free().
419 * The table itself must be freed with free() as well.
421 * @return How many file names were selected (possibly 0),
422 * or -1 in case of error.
424 int vlc_scandir( const char *dirname, char ***namelist,
425 int (*select)( const char * ),
426 int (*compar)( const char **, const char ** ) )
428 DIR *dir = vlc_opendir (dirname);
429 int val = -1;
431 if (dir != NULL)
433 val = vlc_loaddir (dir, namelist, select, compar);
434 closedir (dir);
436 return val;
439 static int vlc_statEx( const char *filename, struct stat *buf,
440 bool deref )
442 #ifdef UNDER_CE
443 /*_stat translates to wchar internally on WinCE*/
444 return _stat( filename, buf );
445 #elif defined (WIN32)
446 CONVERT_PATH (filename, wpath, -1);
447 return _wstati64 (wpath, buf);
449 #endif
450 #ifdef HAVE_SYS_STAT_H
451 const char *local_name = ToLocale( filename );
453 if( local_name != NULL )
455 int res = deref ? stat( local_name, buf )
456 : lstat( local_name, buf );
457 LocaleFree( local_name );
458 return res;
460 errno = ENOENT;
461 #endif
462 return -1;
466 * Finds file/inode information, as stat().
467 * Consider using fstat() instead, if possible.
469 * @param filename UTF-8 file path
471 int vlc_stat( const char *filename, struct stat *buf)
473 return vlc_statEx( filename, buf, true );
477 * Finds file/inode information, as lstat().
478 * Consider using fstat() instead, if possible.
480 * @param filename UTF-8 file path
482 int vlc_lstat( const char *filename, struct stat *buf)
484 return vlc_statEx( filename, buf, false );
488 * Removes a file.
490 * @param filename a UTF-8 string with the name of the file you want to delete.
491 * @return A 0 return value indicates success. A -1 return value indicates an
492 * error, and an error code is stored in errno
494 int vlc_unlink( const char *filename )
496 #ifdef UNDER_CE
497 /*_open translates to wchar internally on WinCE*/
498 return _unlink( filename );
499 #elif defined (WIN32)
500 CONVERT_PATH (filename, wpath, -1);
501 return _wunlink (wpath);
503 #endif
504 const char *local_name = ToLocale( filename );
506 if( local_name == NULL )
508 errno = ENOENT;
509 return -1;
512 int ret = unlink( local_name );
513 LocaleFree( local_name );
514 return ret;
518 * Moves a file atomically. This only works within a single file system.
520 * @param oldpath path to the file before the move
521 * @param newpath intended path to the file after the move
522 * @return A 0 return value indicates success. A -1 return value indicates an
523 * error, and an error code is stored in errno
525 int vlc_rename (const char *oldpath, const char *newpath)
527 #if defined (WIN32)
528 CONVERT_PATH (oldpath, wold, -1);
529 CONVERT_PATH (newpath, wnew, -1);
530 # ifdef UNDER_CE
531 /* FIXME: errno support */
532 if (MoveFileW (wold, wnew))
533 return 0;
534 else
535 return -1;
536 #else
537 if (_wrename (wold, wnew) && errno == EACCES)
538 { /* Windows does not allow atomic file replacement */
539 if (_wremove (wnew))
541 errno = EACCES; /* restore errno */
542 return -1;
544 if (_wrename (wold, wnew))
545 return -1;
547 return 0;
548 #endif
550 #endif
551 const char *lo = ToLocale (oldpath);
552 if (lo == NULL)
553 goto error;
555 const char *ln = ToLocale (newpath);
556 if (ln == NULL)
558 LocaleFree (lo);
559 error:
560 errno = ENOENT;
561 return -1;
564 int ret = rename (lo, ln);
565 LocaleFree (lo);
566 LocaleFree (ln);
567 return ret;
570 int vlc_mkstemp( char *template )
572 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
573 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
575 /* */
576 assert( template );
578 /* Check template validity */
579 const size_t i_length = strlen( template );
580 char *psz_rand = &template[i_length-6];
582 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
584 errno = EINVAL;
585 return -1;
588 /* */
589 for( int i = 0; i < 256; i++ )
591 /* Create a pseudo random file name */
592 uint8_t pi_rand[6];
594 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
595 for( int j = 0; j < 6; j++ )
596 psz_rand[j] = digits[pi_rand[j] % i_digits];
598 /* */
599 int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
600 if( fd >= 0 )
601 return fd;
602 if( errno != EEXIST )
603 return -1;
606 errno = EEXIST;
607 return -1;
610 #ifdef UNDER_CE
611 # define dup(fd) (fd, -1)
612 #endif
615 * Duplicates a file descriptor. The new file descriptor has the close-on-exec
616 * descriptor flag set.
617 * @return a new file descriptor or -1
619 int vlc_dup (int oldfd)
621 int newfd;
623 #ifdef HAVE_DUP3
624 /* Unfortunately, dup3() works like dup2(), not like plain dup(). So we
625 * need such contortion to find the new file descriptor while preserving
626 * thread safety of the file descriptor table. */
627 newfd = vlc_open ("/dev/null", O_RDONLY);
628 if (likely(newfd != -1))
630 if (likely(dup3 (oldfd, newfd, O_CLOEXEC) == newfd))
631 return newfd;
632 close (newfd);
634 #endif
636 newfd = dup (oldfd);
637 #ifdef HAVE_FCNTL
638 if (likely(newfd != -1))
639 fcntl (newfd, F_SETFD, FD_CLOEXEC);
640 #endif
641 return newfd;
644 #include <vlc_network.h>
647 * Creates a socket file descriptor. The new file descriptor has the
648 * close-on-exec flag set.
649 * @param pf protocol family
650 * @param type socket type
651 * @param proto network protocol
652 * @param nonblock true to create a non-blocking socket
653 * @return a new file descriptor or -1
655 int vlc_socket (int pf, int type, int proto, bool nonblock)
657 int fd;
659 #ifdef SOCK_CLOEXEC
660 type |= SOCK_CLOEXEC;
661 if (nonblock)
662 type |= SOCK_NONBLOCK;
663 fd = socket (pf, type, proto);
664 if (fd != -1 || errno != EINVAL)
665 return fd;
667 type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
668 #endif
670 fd = socket (pf, type, proto);
671 if (fd == -1)
672 return -1;
674 #ifndef WIN32
675 fcntl (fd, F_SETFD, FD_CLOEXEC);
676 if (nonblock)
677 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
678 #else
679 if (nonblock)
680 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
681 #endif
682 return fd;
686 * Accepts an inbound connection request on a listening socket.
687 * The new file descriptor has the close-on-exec flag set.
688 * @param lfd listening socket file descriptor
689 * @param addr pointer to the peer address or NULL [OUT]
690 * @param alen pointer to the length of the peer address or NULL [OUT]
691 * @param nonblock whether to put the new socket in non-blocking mode
692 * @return a new file descriptor, or -1 on error.
694 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
696 #ifdef HAVE_ACCEPT4
697 int flags = SOCK_CLOEXEC;
698 if (nonblock)
699 flags |= SOCK_NONBLOCK;
703 int fd = accept4 (lfd, addr, alen, flags);
704 if (fd != -1)
705 return fd;
707 while (errno == EINTR);
709 if (errno != ENOSYS)
710 return -1;
711 #endif
712 #ifdef WIN32
713 errno = 0;
714 #endif
718 int fd = accept (lfd, addr, alen);
719 if (fd != -1)
721 #ifndef WIN32
722 fcntl (fd, F_SETFD, FD_CLOEXEC);
723 if (nonblock)
724 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
725 #else
726 if (nonblock)
727 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
728 #endif
729 return fd;
732 while (errno == EINTR);
734 return -1;