]> git.sesse.net Git - vlc/blobdiff - src/posix/filesystem.c
vout: add window owner structure and resize event
[vlc] / src / posix / filesystem.c
index 1c26099327dd6cf69e1e79c05da78062bd9d01d7..45df501fc80837b984bcb9a63579b19934d8eefa 100644 (file)
@@ -1,50 +1,49 @@
 /*****************************************************************************
  * filesystem.c: POSIX file system helpers
  *****************************************************************************
- * Copyright (C) 2005-2006 the VideoLAN team
+ * Copyright (C) 2005-2006 VLC authors and VideoLAN
  * Copyright © 2005-2008 Rémi Denis-Courmont
  *
  * Authors: Rémi Denis-Courmont <rem # videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#include <vlc_common.h>
-#include <vlc_charset.h>
-#include <vlc_fs.h>
-#include "libvlc.h" /* vlc_mkdir */
-
 #include <assert.h>
 
 #include <stdio.h>
 #include <limits.h> /* NAME_MAX */
 #include <errno.h>
+
 #include <sys/types.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#ifndef HAVE_LSTAT
+# define lstat(a, b) stat(a, b)
+#endif
 #include <dirent.h>
 #include <sys/socket.h>
 
-#ifndef HAVE_LSTAT
-# define lstat( a, b ) stat(a, b)
-#endif
+#include <vlc_common.h>
+#include <vlc_fs.h>
+#include "libvlc.h" /* vlc_mkdir */
 
 /**
  * Opens a system file handle.
@@ -69,19 +68,9 @@ int vlc_open (const char *filename, int flags, ...)
     flags |= O_CLOEXEC;
 #endif
 
-    const char *local_name = ToLocale (filename);
-
-    if (local_name == NULL)
-    {
-        errno = ENOENT;
-        return -1;
-    }
-
-    int fd = open (local_name, flags, mode);
+    int fd = open (filename, flags, mode);
     if (fd != -1)
         fcntl (fd, F_SETFD, FD_CLOEXEC);
-
-    LocaleFree (local_name);
     return fd;
 }
 
@@ -109,23 +98,18 @@ int vlc_openat (int dir, const char *filename, int flags, ...)
     flags |= O_CLOEXEC;
 #endif
 
-    const char *local_name = ToLocale (filename);
-    if (local_name == NULL)
-    {
-        errno = ENOENT;
-        return -1;
-    }
-
 #ifdef HAVE_OPENAT
-    int fd = openat (dir, local_name, flags, mode);
+    int fd = openat (dir, filename, flags, mode);
     if (fd != -1)
         fcntl (fd, F_SETFD, FD_CLOEXEC);
 #else
+       VLC_UNUSED (dir);
+       VLC_UNUSED (filename);
+       VLC_UNUSED (mode);
+
     int fd = -1;
     errno = ENOSYS;
 #endif
-
-    LocaleFree (local_name);
     return fd;
 }
 
@@ -140,16 +124,7 @@ int vlc_openat (int dir, const char *filename, int flags, ...)
  */
 int vlc_mkdir (const char *dirname, mode_t mode)
 {
-    char *locname = ToLocale (dirname);
-    if (unlikely(locname == NULL))
-    {
-        errno = ENOENT;
-        return -1;
-    }
-
-    int res = mkdir (locname, mode);
-    LocaleFree (locname);
-    return res;
+    return mkdir (dirname, mode);
 }
 
 /**
@@ -161,77 +136,24 @@ int vlc_mkdir (const char *dirname, mode_t mode)
  */
 DIR *vlc_opendir (const char *dirname)
 {
-    const char *local_name = ToLocale (dirname);
-    if (unlikely(local_name == NULL))
-    {
-        errno = ENOENT;
-        return NULL;
-    }
-
-    DIR *dir = opendir (local_name);
-    LocaleFree (local_name);
-    return dir;
+    return opendir (dirname);
 }
 
 /**
  * Reads the next file name from an open directory.
  *
- * @param dir The directory that is being read
+ * @param dir directory handle as returned by vlc_opendir()
+ *            (must not be used by another thread concurrently)
  *
- * @return a UTF-8 string of the directory entry. Use free() to release it.
+ * @return a UTF-8 string of the directory entry. The string is valid until
+ * the next call to vlc_readdir() or closedir() on the handle.
  * 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 )
 {
-    /* 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);
-#ifdef NAME_MAX
-    /* POSIX says there shall we room for NAME_MAX bytes at all times */
-    if (/*len == -1 ||*/ len < NAME_MAX)
-        len = NAME_MAX;
-#else
-    /* OS is broken. Lets assume there is no files left. */
-    if (len == -1)
-        return NULL;
-#endif
-    len += offsetof (struct dirent, d_name) + 1;
-
-    struct dirent *buf = malloc (len);
-    if (unlikely(buf == NULL))
-        return NULL;
-
-    int val = readdir_r (dir, buf, &ent);
-    if (val != 0)
-        errno = val;
-    else if (ent != NULL)
-#ifndef __APPLE__
-        path = strdup (ent->d_name);
-#else
-        path = FromCharset ("UTF-8-MAC", ent->d_name, strlen (ent->d_name));
-#endif
-    free (buf);
-    return path;
-}
-
-static int vlc_statEx (const char *filename, struct stat *buf, bool deref)
-{
-    const char *local_name = ToLocale (filename);
-    if (unlikely(local_name == NULL))
-    {
-        errno = ENOENT;
-        return -1;
-    }
-
-    int res = deref ? stat (local_name, buf)
-                    : lstat (local_name, buf);
-    LocaleFree (local_name);
-    return res;
+    struct dirent *ent = readdir (dir);
+    return (ent != NULL) ? ent->d_name : NULL;
 }
 
 /**
@@ -242,7 +164,7 @@ static int vlc_statEx (const char *filename, struct stat *buf, bool deref)
  */
 int vlc_stat (const char *filename, struct stat *buf)
 {
-    return vlc_statEx (filename, buf, true);
+    return stat (filename, buf);
 }
 
 /**
@@ -253,7 +175,7 @@ int vlc_stat (const char *filename, struct stat *buf)
  */
 int vlc_lstat (const char *filename, struct stat *buf)
 {
-    return vlc_statEx (filename, buf, false);
+    return lstat (filename, buf);
 }
 
 /**
@@ -265,16 +187,7 @@ int vlc_lstat (const char *filename, struct stat *buf)
  */
 int vlc_unlink (const char *filename)
 {
-    const char *local_name = ToLocale (filename);
-    if (unlikely(local_name == NULL))
-    {
-        errno = ENOENT;
-        return -1;
-    }
-
-    int ret = unlink (local_name);
-    LocaleFree (local_name);
-    return ret;
+    return unlink (filename);
 }
 
 /**
@@ -287,23 +200,7 @@ int vlc_unlink (const char *filename)
  */
 int vlc_rename (const char *oldpath, const char *newpath)
 {
-    const char *lo = ToLocale (oldpath);
-    if (lo == NULL)
-        goto error;
-
-    const char *ln = ToLocale (newpath);
-    if (ln == NULL)
-    {
-        LocaleFree (lo);
-error:
-        errno = ENOENT;
-        return -1;
-    }
-
-    int ret = rename (lo, ln);
-    LocaleFree (lo);
-    LocaleFree (ln);
-    return ret;
+    return rename (oldpath, newpath);
 }
 
 /**
@@ -314,18 +211,6 @@ error:
  */
 char *vlc_getcwd (void)
 {
-    /* Try $PWD */
-    const char *pwd = getenv ("PWD");
-    if (pwd != NULL)
-    {
-        struct stat s1, s2;
-        /* Make sure $PWD is correct */
-        if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
-         && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
-            return ToLocaleDup (pwd);
-    }
-
-    /* Otherwise iterate getcwd() until the buffer is big enough */
     long path_max = pathconf (".", _PC_PATH_MAX);
     size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
 
@@ -336,15 +221,7 @@ char *vlc_getcwd (void)
             break;
 
         if (getcwd (buf, size) != NULL)
-#ifdef ASSUME_UTF8
             return buf;
-#else
-        {
-            char *ret = ToLocaleDup (buf);
-            free (buf);
-            return ret; /* success */
-        }
-#endif
         free (buf);
 
         if (errno != ERANGE)
@@ -363,7 +240,7 @@ int vlc_dup (int oldfd)
     int newfd;
 
 #ifdef F_DUPFD_CLOEXEC
-    newfd = fcntl (oldfd, F_DUPFD_CLOEXEC);
+    newfd = fcntl (oldfd, F_DUPFD_CLOEXEC, 0);
     if (unlikely(newfd == -1 && errno == EINVAL))
 #endif
     {
@@ -374,20 +251,6 @@ int vlc_dup (int oldfd)
     return newfd;
 }
 
-#ifdef __ANDROID__ /* && we support android < 2.3 */
-/* pipe2() is declared and available since android-9 NDK,
- * although it is available in libc.a since android-3
- * We redefine the function here in order to be able to run
- * on versions of Android older than 2.3
- */
-#include <sys/syscall.h>
-//#include <sys/linux-syscalls.h> // fucking brokeness
-int pipe2(int fds[2], int flags)
-{
-    return syscall(/*__NR_pipe2 */ 359, fds, flags);
-}
-#endif /* __ANDROID__ */
-
 /**
  * Creates a pipe (see "man pipe" for further reference).
  */