]> git.sesse.net Git - vlc/blobdiff - src/text/filesystem.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / text / filesystem.c
index b57a27d868abbfd4a5cd7d21b279e4d27231eea9..f83a4fcff803311f47628c7d1ded6dbed8c2f644 100644 (file)
 #include <assert.h>
 
 #include <stdio.h>
+#include <limits.h> /* NAME_MAX */
 #include <errno.h>
 #include <sys/types.h>
-#ifdef HAVE_DIRENT_H
-#  include <dirent.h>
-#endif
-#ifdef UNDER_CE
-#  include <tchar.h>
-#endif
+#include <dirent.h>
 #ifdef HAVE_SYS_STAT_H
 # include <sys/stat.h>
 #endif
 #endif
 #ifdef WIN32
 # include <io.h>
+# include <winsock2.h>
 # ifndef UNDER_CE
 #  include <direct.h>
+# else
+#  include <tchar.h>
 # endif
 #else
 # include <unistd.h>
+# include <sys/socket.h>
 #endif
 
 #ifndef HAVE_LSTAT
@@ -224,7 +224,7 @@ int vlc_openat (int dir, const char *filename, int flags, ...)
         return -1;
     }
 
-#ifdef HAVE_FDOPENDIR
+#ifdef HAVE_OPENAT
     int fd = openat (dir, local_name, flags, mode);
 # ifdef HAVE_FCNTL
     if (fd != -1)
@@ -308,8 +308,9 @@ DIR *vlc_opendir( const char *dirname )
  *
  * @param dir The directory that is being read
  *
- * @return a UTF-8 string of the directory entry.
- * Use free() to free this memory.
+ * @return a UTF-8 string of the directory entry. Use free() to release it.
+ * If there are no more entries in the directory, NULL is returned.
+ * If an error occurs, errno is set and NULL is returned.
  */
 char *vlc_readdir( DIR *dir )
 {
@@ -320,13 +321,35 @@ char *vlc_readdir( DIR *dir )
 
     return FromWide (ent->d_name);
 #else
+    /* Beware that readdir_r() assumes <buf> is large enough to hold the result
+     * dirent including the file name. A buffer overflow could occur otherwise.
+     * In particular, pathconf() and _POSIX_NAME_MAX cannot be used here. */
     struct dirent *ent;
+    char *path = NULL;
+
+    long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
+    if (len == -1)
+    {
+#ifdef NAME_MAX
+        len = NAME_MAX;
+#else
+        errno = ENOMEM;
+        return NULL; // OS is broken. There is no sane way to fix this.
+#endif
+    }
+    len += offsetof (struct dirent, d_name) + 1;
 
-    ent = readdir( (DIR *)dir );
-    if( ent == NULL )
+    struct dirent *buf = malloc (len);
+    if (unlikely(buf == NULL))
         return NULL;
 
-    return vlc_fix_readdir( ent->d_name );
+    int val = readdir_r (dir, buf, &ent);
+    if (val != 0)
+        errno = val;
+    else if (ent != NULL)
+        path = vlc_fix_readdir (ent->d_name);
+    free (buf);
+    return path;
 #endif
 }
 
@@ -344,54 +367,59 @@ int vlc_loaddir( DIR *dir, char ***namelist,
                   int (*select)( const char * ),
                   int (*compar)( const char **, const char ** ) )
 {
-    if( select == NULL )
+    assert (dir);
+
+    if (select == NULL)
         select = dummy_select;
 
-    if( dir == NULL )
-        return -1;
-    else
-    {
-        char **tab = NULL;
-        char *entry;
-        unsigned num = 0;
+    char **tab = NULL;
+    unsigned num = 0;
 
-        rewinddir( dir );
+    rewinddir (dir);
 
-        while( ( entry = vlc_readdir( dir ) ) != NULL )
+    for (unsigned size = 0;;)
+    {
+        errno = 0;
+        char *entry = vlc_readdir (dir);
+        if (entry == NULL)
         {
-            char **newtab;
+            if (errno)
+                goto error;
+            break;
+        }
 
-            if( !select( entry ) )
-            {
-                free( entry );
-                continue;
-            }
+        if (!select (entry))
+        {
+            free (entry);
+            continue;
+        }
 
-            newtab = realloc( tab, sizeof( char * ) * (num + 1) );
-            if( newtab == NULL )
+        if (num >= size)
+        {
+            size = size ? (2 * size) : 16;
+            char **newtab = realloc (tab, sizeof (*tab) * (size));
+
+            if (unlikely(newtab == NULL))
             {
-                free( entry );
+                free (entry);
                 goto error;
             }
             tab = newtab;
-            tab[num++] = entry;
         }
 
-        if( compar != NULL )
-            qsort( tab, num, sizeof( tab[0] ),
-                   (int (*)( const void *, const void *))compar );
-
-        *namelist = tab;
-        return num;
+        tab[num++] = entry;
+    }
 
-    error:{
-        unsigned i;
+    if (compar != NULL)
+        qsort (tab, num, sizeof (*tab),
+               (int (*)( const void *, const void *))compar);
+    *namelist = tab;
+    return num;
 
-        for( i = 0; i < num; i++ )
-            free( tab[i] );
-        free( tab );
-        }
-    }
+error:
+    for (unsigned i = 0; i < num; i++)
+        free (tab[i]);
+    free (tab);
     return -1;
 }
 
@@ -399,7 +427,7 @@ int vlc_loaddir( DIR *dir, char ***namelist,
  * Selects file entries from a directory, as GNU C scandir().
  *
  * @param dirname UTF-8 diretory path
- * @param pointer [OUT] pointer set, on succesful completion, to the address
+ * @param pointer [OUT] pointer set, on successful completion, to the address
  * of a table of UTF-8 filenames. All filenames must be freed with free().
  * The table itself must be freed with free() as well.
  *
@@ -448,7 +476,7 @@ static int vlc_statEx( const char *filename, struct stat *buf,
 }
 
 /**
- * Finds file/inode informations, as stat().
+ * Finds file/inode information, as stat().
  * Consider using fstat() instead, if possible.
  *
  * @param filename UTF-8 file path
@@ -459,7 +487,7 @@ int vlc_stat( const char *filename, struct stat *buf)
 }
 
 /**
- * Finds file/inode informations, as lstat().
+ * Finds file/inode information, as lstat().
  * Consider using fstat() instead, if possible.
  *
  * @param filename UTF-8 file path
@@ -519,7 +547,17 @@ int vlc_rename (const char *oldpath, const char *newpath)
     else
         return -1;
 #else
-    return _wrename (wold, wnew);
+    if (_wrename (wold, wnew) && errno == EACCES)
+    {   /* Windows does not allow atomic file replacement */
+        if (_wremove (wnew))
+        {
+            errno = EACCES; /* restore errno */
+            return -1;
+        }
+        if (_wrename (wold, wnew))
+            return -1;
+    }
+    return 0;
 #endif
 
 #endif
@@ -615,3 +653,96 @@ int vlc_dup (int oldfd)
 #endif
     return newfd;
 }
+
+#include <vlc_network.h>
+
+/**
+ * Creates a socket file descriptor. The new file descriptor has the
+ * close-on-exec flag set.
+ * @param pf protocol family
+ * @param type socket type
+ * @param proto network protocol
+ * @param nonblock true to create a non-blocking socket
+ * @return a new file descriptor or -1
+ */
+int vlc_socket (int pf, int type, int proto, bool nonblock)
+{
+    int fd;
+
+#ifdef SOCK_CLOEXEC
+    type |= SOCK_CLOEXEC;
+    if (nonblock)
+        type |= SOCK_NONBLOCK;
+    fd = socket (pf, type, proto);
+    if (fd != -1 || errno != EINVAL)
+        return fd;
+
+    type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
+#endif
+
+    fd = socket (pf, type, proto);
+    if (fd == -1)
+        return -1;
+
+#ifndef WIN32
+    fcntl (fd, F_SETFD, FD_CLOEXEC);
+    if (nonblock)
+        fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
+#else
+    if (nonblock)
+        ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
+#endif
+    return fd;
+}
+
+/**
+ * Accepts an inbound connection request on a listening socket.
+ * The new file descriptor has the close-on-exec flag set.
+ * @param lfd listening socket file descriptor
+ * @param addr pointer to the peer address or NULL [OUT]
+ * @param alen pointer to the length of the peer address or NULL [OUT]
+ * @param nonblock whether to put the new socket in non-blocking mode
+ * @return a new file descriptor, or -1 on error.
+ */
+int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
+{
+#ifdef HAVE_ACCEPT4
+    int flags = SOCK_CLOEXEC;
+    if (nonblock)
+        flags |= SOCK_NONBLOCK;
+
+    do
+    {
+        int fd = accept4 (lfd, addr, alen, flags);
+        if (fd != -1)
+            return fd;
+    }
+    while (errno == EINTR);
+
+    if (errno != ENOSYS)
+        return -1;
+#endif
+#ifdef WIN32
+    errno = 0;
+#endif
+
+    do
+    {
+        int fd = accept (lfd, addr, alen);
+        if (fd != -1)
+        {
+#ifndef WIN32
+            fcntl (fd, F_SETFD, FD_CLOEXEC);
+            if (nonblock)
+                fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
+#else
+            if (nonblock)
+                ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
+#endif
+            return fd;
+        }
+    }
+    while (errno == EINTR);
+
+    return -1;
+}