]> git.sesse.net Git - vlc/blob - src/text/filesystem.c
Distribute libvlc_version.h.in
[vlc] / src / text / filesystem.c
1 /*****************************************************************************
2  * filesystem.c: File system helpers
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
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
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
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>
36
37 #include <assert.h>
38
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
65
66 #ifndef HAVE_LSTAT
67 # define lstat( a, b ) stat(a, b)
68 #endif
69
70 #ifdef WIN32
71 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
72 {
73     if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
74     {
75         errno = ENOENT;
76         return -1;
77     }
78     wpath[MAX_PATH] = L'\0';
79     return 0;
80 }
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
86
87 /**
88  * Opens a system file handle.
89  *
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.
95  */
96 int vlc_open (const char *filename, int flags, ...)
97 {
98     unsigned int mode = 0;
99     va_list ap;
100
101     va_start (ap, flags);
102     if (flags & O_CREAT)
103         mode = va_arg (ap, unsigned int);
104     va_end (ap);
105
106 #ifdef O_CLOEXEC
107     flags |= O_CLOEXEC;
108 #endif
109
110 #ifdef UNDER_CE
111     /*_open translates to wchar internally on WinCE*/
112     return _open (filename, flags, mode);
113 #elif defined (WIN32)
114     /*
115      * open() cannot open files with non-“ANSI” characters on Windows.
116      * We use _wopen() instead. Same thing for mkdir() and stat().
117      */
118     CONVERT_PATH(filename, wpath, -1);
119     return _wopen (wpath, flags, mode);
120
121 #endif
122     const char *local_name = ToLocale (filename);
123
124     if (local_name == NULL)
125     {
126         errno = ENOENT;
127         return -1;
128     }
129
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
135
136     LocaleFree (local_name);
137     return fd;
138 }
139
140 /**
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.
145  */
146 FILE *vlc_fopen (const char *filename, const char *mode)
147 {
148     int rwflags = 0, oflags = 0;
149     bool append = false;
150
151     for (const char *ptr = mode; *ptr; ptr++)
152     {
153         switch (*ptr)
154         {
155             case 'r':
156                 rwflags = O_RDONLY;
157                 break;
158
159             case 'a':
160                 rwflags = O_WRONLY;
161                 oflags |= O_CREAT;
162                 append = true;
163                 break;
164
165             case 'w':
166                 rwflags = O_WRONLY;
167                 oflags |= O_CREAT | O_TRUNC;
168                 break;
169
170             case '+':
171                 rwflags = O_RDWR;
172                 break;
173
174 #ifdef O_TEXT
175             case 't':
176                 oflags |= O_TEXT;
177                 break;
178 #endif
179         }
180     }
181
182     int fd = vlc_open (filename, rwflags | oflags, 0666);
183     if (fd == -1)
184         return NULL;
185
186     if (append && (lseek (fd, 0, SEEK_END) == -1))
187     {
188         close (fd);
189         return NULL;
190     }
191
192     FILE *stream = fdopen (fd, mode);
193     if (stream == NULL)
194         close (fd);
195
196     return stream;
197 }
198
199 /**
200  * Opens a system file handle relative to an existing directory handle.
201  *
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.
208  */
209 int vlc_openat (int dir, const char *filename, int flags, ...)
210 {
211     unsigned int mode = 0;
212     va_list ap;
213
214     va_start (ap, flags);
215     if (flags & O_CREAT)
216         mode = va_arg (ap, unsigned int);
217     va_end (ap);
218
219 #ifdef O_CLOEXEC
220     flags |= O_CLOEXEC;
221 #endif
222
223     const char *local_name = ToLocale (filename);
224     if (local_name == NULL)
225     {
226         errno = ENOENT;
227         return -1;
228     }
229
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
240
241     LocaleFree (local_name);
242     return fd;
243 }
244
245
246 /**
247  * Creates a directory using UTF-8 paths.
248  *
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).
253  */
254 int vlc_mkdir( const char *dirname, mode_t mode )
255 {
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);
264
265 #else
266     char *locname = ToLocale( dirname );
267     int res;
268
269     if( locname == NULL )
270     {
271         errno = ENOENT;
272         return -1;
273     }
274     res = mkdir( locname, mode );
275
276     LocaleFree( locname );
277     return res;
278 #endif
279 }
280
281 /**
282  * Opens a DIR pointer.
283  *
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().
287  */
288 DIR *vlc_opendir( const char *dirname )
289 {
290 #ifdef WIN32
291     CONVERT_PATH (dirname, wpath, NULL);
292     return (DIR *)vlc_wopendir (wpath);
293
294 #else
295     const char *local_name = ToLocale( dirname );
296
297     if( local_name != NULL )
298     {
299         DIR *dir = opendir( local_name );
300         LocaleFree( local_name );
301         return dir;
302     }
303 #endif
304
305     errno = ENOENT;
306     return NULL;
307 }
308
309 /**
310  * Reads the next file name from an open directory.
311  *
312  * @param dir The directory that is being read
313  *
314  * @return a UTF-8 string of the directory entry.
315  * Use free() to free this memory.
316  */
317 char *vlc_readdir( DIR *dir )
318 {
319 #ifdef WIN32
320     struct _wdirent *ent = vlc_wreaddir (dir);
321     if (ent == NULL)
322         return NULL;
323
324     return FromWide (ent->d_name);
325 #else
326     struct dirent *ent;
327     struct
328     {
329         struct dirent ent;
330         char buf[NAME_MAX + 1];
331     } buf;
332     int val = readdir_r (dir, &buf.ent, &ent);
333     if (val)
334     {
335         errno = val;
336         return NULL;
337     }
338     return ent ? vlc_fix_readdir( ent->d_name ) : NULL;
339 #endif
340 }
341
342 static int dummy_select( const char *str )
343 {
344     (void)str;
345     return 1;
346 }
347
348 /**
349  * Does the same as vlc_scandir(), but takes an open directory pointer
350  * instead of a directory path.
351  */
352 int vlc_loaddir( DIR *dir, char ***namelist,
353                   int (*select)( const char * ),
354                   int (*compar)( const char **, const char ** ) )
355 {
356     assert (dir);
357
358     if (select == NULL)
359         select = dummy_select;
360
361     char **tab = NULL;
362     unsigned num = 0;
363
364     rewinddir (dir);
365
366     for (unsigned size = 0;;)
367     {
368         errno = 0;
369         char *entry = vlc_readdir (dir);
370         if (entry == NULL)
371         {
372             if (errno)
373                 goto error;
374             break;
375         }
376
377         if (!select (entry))
378         {
379             free (entry);
380             continue;
381         }
382
383         if (num >= size)
384         {
385             size = size ? (2 * size) : 16;
386             char **newtab = realloc (tab, sizeof (*tab) * (size));
387
388             if (unlikely(newtab == NULL))
389             {
390                 free (entry);
391                 goto error;
392             }
393             tab = newtab;
394         }
395
396         tab[num++] = entry;
397     }
398
399     if (compar != NULL)
400         qsort (tab, num, sizeof (*tab),
401                (int (*)( const void *, const void *))compar);
402     *namelist = tab;
403     return num;
404
405 error:
406     for (unsigned i = 0; i < num; i++)
407         free (tab[i]);
408     free (tab);
409     return -1;
410 }
411
412 /**
413  * Selects file entries from a directory, as GNU C scandir().
414  *
415  * @param dirname UTF-8 diretory path
416  * @param pointer [OUT] pointer set, on successful completion, to the address
417  * of a table of UTF-8 filenames. All filenames must be freed with free().
418  * The table itself must be freed with free() as well.
419  *
420  * @return How many file names were selected (possibly 0),
421  * or -1 in case of error.
422  */
423 int vlc_scandir( const char *dirname, char ***namelist,
424                   int (*select)( const char * ),
425                   int (*compar)( const char **, const char ** ) )
426 {
427     DIR *dir = vlc_opendir (dirname);
428     int val = -1;
429
430     if (dir != NULL)
431     {
432         val = vlc_loaddir (dir, namelist, select, compar);
433         closedir (dir);
434     }
435     return val;
436 }
437
438 static int vlc_statEx( const char *filename, struct stat *buf,
439                         bool deref )
440 {
441 #ifdef UNDER_CE
442     /*_stat translates to wchar internally on WinCE*/
443     return _stat( filename, buf );
444 #elif defined (WIN32)
445     CONVERT_PATH (filename, wpath, -1);
446     return _wstati64 (wpath, buf);
447
448 #endif
449 #ifdef HAVE_SYS_STAT_H
450     const char *local_name = ToLocale( filename );
451
452     if( local_name != NULL )
453     {
454         int res = deref ? stat( local_name, buf )
455                        : lstat( local_name, buf );
456         LocaleFree( local_name );
457         return res;
458     }
459     errno = ENOENT;
460 #endif
461     return -1;
462 }
463
464 /**
465  * Finds file/inode information, as stat().
466  * Consider using fstat() instead, if possible.
467  *
468  * @param filename UTF-8 file path
469  */
470 int vlc_stat( const char *filename, struct stat *buf)
471 {
472     return vlc_statEx( filename, buf, true );
473 }
474
475 /**
476  * Finds file/inode information, as lstat().
477  * Consider using fstat() instead, if possible.
478  *
479  * @param filename UTF-8 file path
480  */
481 int vlc_lstat( const char *filename, struct stat *buf)
482 {
483     return vlc_statEx( filename, buf, false );
484 }
485
486 /**
487  * Removes a file.
488  *
489  * @param filename a UTF-8 string with the name of the file you want to delete.
490  * @return A 0 return value indicates success. A -1 return value indicates an
491  *        error, and an error code is stored in errno
492  */
493 int vlc_unlink( const char *filename )
494 {
495 #ifdef UNDER_CE
496     /*_open translates to wchar internally on WinCE*/
497     return _unlink( filename );
498 #elif defined (WIN32)
499     CONVERT_PATH (filename, wpath, -1);
500     return _wunlink (wpath);
501
502 #endif
503     const char *local_name = ToLocale( filename );
504
505     if( local_name == NULL )
506     {
507         errno = ENOENT;
508         return -1;
509     }
510
511     int ret = unlink( local_name );
512     LocaleFree( local_name );
513     return ret;
514 }
515
516 /**
517  * Moves a file atomically. This only works within a single file system.
518  *
519  * @param oldpath path to the file before the move
520  * @param newpath intended path to the file after the move
521  * @return A 0 return value indicates success. A -1 return value indicates an
522  *        error, and an error code is stored in errno
523  */
524 int vlc_rename (const char *oldpath, const char *newpath)
525 {
526 #if defined (WIN32)
527     CONVERT_PATH (oldpath, wold, -1);
528     CONVERT_PATH (newpath, wnew, -1);
529 # ifdef UNDER_CE
530     /* FIXME: errno support */
531     if (MoveFileW (wold, wnew))
532         return 0;
533     else
534         return -1;
535 #else
536     if (_wrename (wold, wnew) && errno == EACCES)
537     {   /* Windows does not allow atomic file replacement */
538         if (_wremove (wnew))
539         {
540             errno = EACCES; /* restore errno */
541             return -1;
542         }
543         if (_wrename (wold, wnew))
544             return -1;
545     }
546     return 0;
547 #endif
548
549 #endif
550     const char *lo = ToLocale (oldpath);
551     if (lo == NULL)
552         goto error;
553
554     const char *ln = ToLocale (newpath);
555     if (ln == NULL)
556     {
557         LocaleFree (lo);
558 error:
559         errno = ENOENT;
560         return -1;
561     }
562
563     int ret = rename (lo, ln);
564     LocaleFree (lo);
565     LocaleFree (ln);
566     return ret;
567 }
568
569 int vlc_mkstemp( char *template )
570 {
571     static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
572     static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
573
574     /* */
575     assert( template );
576
577     /* Check template validity */
578     const size_t i_length = strlen( template );
579     char *psz_rand = &template[i_length-6];
580
581     if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
582     {
583         errno = EINVAL;
584         return -1;
585     }
586
587     /* */
588     for( int i = 0; i < 256; i++ )
589     {
590         /* Create a pseudo random file name */
591         uint8_t pi_rand[6];
592
593         vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
594         for( int j = 0; j < 6; j++ )
595             psz_rand[j] = digits[pi_rand[j] % i_digits];
596
597         /* */
598         int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
599         if( fd >= 0 )
600             return fd;
601         if( errno != EEXIST )
602             return -1;
603     }
604
605     errno = EEXIST;
606     return -1;
607 }
608
609 #ifdef UNDER_CE
610 # define dup(fd) (fd, -1)
611 #endif
612
613 /**
614  * Duplicates a file descriptor. The new file descriptor has the close-on-exec
615  * descriptor flag set.
616  * @return a new file descriptor or -1
617  */
618 int vlc_dup (int oldfd)
619 {
620     int newfd;
621
622 #ifdef HAVE_DUP3
623     /* Unfortunately, dup3() works like dup2(), not like plain dup(). So we
624      * need such contortion to find the new file descriptor while preserving
625      * thread safety of the file descriptor table. */
626     newfd = vlc_open ("/dev/null", O_RDONLY);
627     if (likely(newfd != -1))
628     {
629         if (likely(dup3 (oldfd, newfd, O_CLOEXEC) == newfd))
630             return newfd;
631         close (newfd);
632     }
633 #endif
634
635     newfd = dup (oldfd);
636 #ifdef HAVE_FCNTL
637     if (likely(newfd != -1))
638         fcntl (newfd, F_SETFD, FD_CLOEXEC);
639 #endif
640     return newfd;
641 }
642
643 #include <vlc_network.h>
644
645 /**
646  * Creates a socket file descriptor. The new file descriptor has the
647  * close-on-exec flag set.
648  * @param pf protocol family
649  * @param type socket type
650  * @param proto network protocol
651  * @param nonblock true to create a non-blocking socket
652  * @return a new file descriptor or -1
653  */
654 int vlc_socket (int pf, int type, int proto, bool nonblock)
655 {
656     int fd;
657
658 #ifdef SOCK_CLOEXEC
659     type |= SOCK_CLOEXEC;
660     if (nonblock)
661         type |= SOCK_NONBLOCK;
662     fd = socket (pf, type, proto);
663     if (fd != -1 || errno != EINVAL)
664         return fd;
665
666     type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
667 #endif
668
669     fd = socket (pf, type, proto);
670     if (fd == -1)
671         return -1;
672
673 #ifndef WIN32
674     fcntl (fd, F_SETFD, FD_CLOEXEC);
675     if (nonblock)
676         fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
677 #else
678     if (nonblock)
679         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
680 #endif
681     return fd;
682 }
683
684 /**
685  * Accepts an inbound connection request on a listening socket.
686  * The new file descriptor has the close-on-exec flag set.
687  * @param lfd listening socket file descriptor
688  * @param addr pointer to the peer address or NULL [OUT]
689  * @param alen pointer to the length of the peer address or NULL [OUT]
690  * @param nonblock whether to put the new socket in non-blocking mode
691  * @return a new file descriptor, or -1 on error.
692  */
693 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
694 {
695 #ifdef HAVE_ACCEPT4
696     int flags = SOCK_CLOEXEC;
697     if (nonblock)
698         flags |= SOCK_NONBLOCK;
699
700     do
701     {
702         int fd = accept4 (lfd, addr, alen, flags);
703         if (fd != -1)
704             return fd;
705     }
706     while (errno == EINTR);
707
708     if (errno != ENOSYS)
709         return -1;
710 #endif
711 #ifdef WIN32
712     errno = 0;
713 #endif
714
715     do
716     {
717         int fd = accept (lfd, addr, alen);
718         if (fd != -1)
719         {
720 #ifndef WIN32
721             fcntl (fd, F_SETFD, FD_CLOEXEC);
722             if (nonblock)
723                 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
724 #else
725             if (nonblock)
726                 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
727 #endif
728             return fd;
729         }
730     }
731     while (errno == EINTR);
732
733     return -1;
734 }