Commit ab29a459 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

filesystem: add vlc_memfd() helper

parent cc8bb2ed
...@@ -536,7 +536,7 @@ need_libc=false ...@@ -536,7 +536,7 @@ need_libc=false
dnl Check for usual libc functions dnl Check for usual libc functions
AC_CHECK_DECLS([nanosleep],,,[#include <time.h>]) AC_CHECK_DECLS([nanosleep],,,[#include <time.h>])
AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mmap open_memstream openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale pthread_cond_timedwait_monotonic_np pthread_condattr_setclock]) AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mkostemp mmap open_memstream openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale pthread_cond_timedwait_monotonic_np pthread_condattr_setclock])
AC_REPLACE_FUNCS([atof atoll dirfd fdopendir ffsll flockfile fsync getdelim getpid lldiv nrand48 poll posix_memalign rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strsep strtof strtok_r strtoll swab tdestroy strverscmp]) AC_REPLACE_FUNCS([atof atoll dirfd fdopendir ffsll flockfile fsync getdelim getpid lldiv nrand48 poll posix_memalign rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strsep strtof strtok_r strtoll swab tdestroy strverscmp])
AC_CHECK_FUNCS(fdatasync,, AC_CHECK_FUNCS(fdatasync,,
[AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.]) [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
......
...@@ -93,6 +93,19 @@ VLC_API int vlc_dup(int) VLC_USED; ...@@ -93,6 +93,19 @@ VLC_API int vlc_dup(int) VLC_USED;
*/ */
VLC_API int vlc_pipe(int [2]) VLC_USED; VLC_API int vlc_pipe(int [2]) VLC_USED;
/**
* Creates an anonymous regular file descriptor, i.e. a descriptor for a
* temporary file.
*
* The file is initially empty. The storage space is automatically reclaimed
* when all file descriptors referencing it are closed.
*
* The new file descriptor has the close-on-exec flag preset.
*
* @return a file descriptor on success, -1 on error (see errno)
*/
VLC_API int vlc_memfd(void) VLC_USED;
/** /**
* Writes data to a file descriptor. Unlike write(), if EPIPE error occurs, * Writes data to a file descriptor. Unlike write(), if EPIPE error occurs,
* this function does not generate a SIGPIPE signal. * this function does not generate a SIGPIPE signal.
......
...@@ -450,6 +450,7 @@ vlc_mkdir ...@@ -450,6 +450,7 @@ vlc_mkdir
vlc_mkstemp vlc_mkstemp
vlc_open vlc_open
vlc_openat vlc_openat
vlc_memfd
vlc_opendir vlc_opendir
vlc_readdir vlc_readdir
vlc_scandir vlc_scandir
......
...@@ -79,6 +79,12 @@ int vlc_openat (int dir, const char *filename, int flags, ...) ...@@ -79,6 +79,12 @@ int vlc_openat (int dir, const char *filename, int flags, ...)
return -1; return -1;
} }
int vlc_memfd (void)
{
errno = ENOSYS;
return -1;
}
int vlc_mkdir (const char *dirname, mode_t mode) int vlc_mkdir (const char *dirname, mode_t mode)
{ {
char *locname = ToLocaleDup (dirname); char *locname = ToLocaleDup (dirname);
......
...@@ -98,6 +98,35 @@ int vlc_openat (int dir, const char *filename, int flags, ...) ...@@ -98,6 +98,35 @@ int vlc_openat (int dir, const char *filename, int flags, ...)
return fd; return fd;
} }
int vlc_memfd (void)
{
int fd;
#ifdef O_TMPFILE
fd = vlc_open ("/tmp", O_RDWR|O_TMPFILE, S_IRUSR|S_IWUSR);
if (fd != -1)
return fd;
/* ENOENT means either /tmp is missing (!) or the kernel does not support
* O_TMPFILE. EISDIR means /tmp exists but the kernel does not support
* O_TMPFILE. EOPNOTSUPP means the kernel supports O_TMPFILE but the /tmp
* filesystem does not. Do not fallback on other errors. */
if (errno != ENOENT && errno != EISDIR && errno != EOPNOTSUPP)
return -1;
#endif
char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
#ifdef HAVE_MKOSTEMP
fd = mkostemp (bufpath, O_CLOEXEC);
#else
fd = mkstemp (bufpath);
#endif
if (fd != -1)
{
fcntl (fd, F_SETFD, FD_CLOEXEC);
unlink (bufpath);
}
return fd;
}
int vlc_mkdir (const char *dirname, mode_t mode) int vlc_mkdir (const char *dirname, mode_t mode)
{ {
......
...@@ -104,6 +104,26 @@ int vlc_openat (int dir, const char *filename, int flags, ...) ...@@ -104,6 +104,26 @@ int vlc_openat (int dir, const char *filename, int flags, ...)
return -1; return -1;
} }
int vlc_memfd (void)
{
#if 0
int fd, err;
FILE *stream = tmpfile();
if (stream == NULL)
return -1;
fd = vlc_dup(fileno(stream));
err = errno;
fclose(stream);
errno = err;
return fd;
#else /* Not currently used */
errno = ENOSYS;
return -1;
#endif
}
int vlc_mkdir( const char *dirname, mode_t mode ) int vlc_mkdir( const char *dirname, mode_t mode )
{ {
wchar_t *wpath = widen_path (dirname); wchar_t *wpath = widen_path (dirname);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment