[vlc-devel] [PATCH 1/2] filesystem: add vlc_dup2() wrapper
remi at remlab.net
remi at remlab.net
Sun Nov 29 14:56:41 CET 2020
From: RĂ©mi Denis-Courmont <remi at remlab.net>
---
include/vlc_fs.h | 17 +++++++++++++++++
src/libvlccore.sym | 1 +
src/posix/filesystem.c | 12 ++++++++++++
src/win32/filesystem.c | 8 ++++++++
4 files changed, 38 insertions(+)
diff --git a/include/vlc_fs.h b/include/vlc_fs.h
index 4a50288be0..9ad2028053 100644
--- a/include/vlc_fs.h
+++ b/include/vlc_fs.h
@@ -110,6 +110,23 @@ VLC_API int vlc_mkstemp( char * );
*/
VLC_API int vlc_dup(int oldfd) VLC_USED;
+/**
+ * Replaces a file descriptor.
+ *
+ * This function duplicates a file descriptor to a specified file descriptor.
+ * This is primarily used to atomically replace a described file.
+ *
+ * @param oldfd source file descriptor to copy
+ * @param newfd destination file descriptor to replace
+ *
+ * @note Contrary to standard dup2(), the new file descriptor has the
+ * close-on-exec descriptor flag preset.
+ *
+ * @retval newfd success
+ * @retval -1 failure (see @c errno)
+ */
+VLC_API int vlc_dup2(int oldfd, int newfd);
+
/**
* Creates a pipe (see "man pipe" for further reference). The new file
* descriptors have the close-on-exec flag preset.
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index e3a2de9f6d..2cfe0d376c 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -448,6 +448,7 @@ vlc_unlink
vlc_rename
vlc_getcwd
vlc_dup
+vlc_dup2
vlc_pipe
vlc_write
vlc_writev
diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
index 60a997d9e0..18105a643c 100644
--- a/src/posix/filesystem.c
+++ b/src/posix/filesystem.c
@@ -178,6 +178,18 @@ int vlc_dup (int oldfd)
return fcntl (oldfd, F_DUPFD_CLOEXEC, 0);
}
+int vlc_dup2(int oldfd, int newfd)
+{
+#ifdef HAVE_DUP3
+ return dup3(oldfd, newfd, O_CLOEXEC);
+#else
+ int ret = dup2(oldfd, newfd);
+ if (ret >= 0)
+ vlc_cloexec(newfd);
+ return ret;
+#endif
+}
+
int vlc_pipe (int fds[2])
{
#ifdef HAVE_PIPE2
diff --git a/src/win32/filesystem.c b/src/win32/filesystem.c
index d8e3cc1127..09b8ec70d3 100644
--- a/src/win32/filesystem.c
+++ b/src/win32/filesystem.c
@@ -304,6 +304,14 @@ int vlc_dup (int oldfd)
return fd;
}
+int vlc_dup2(int oldfd, int newfd)
+{
+ int fd = dup2(oldfd, newfd);
+ if (fd != -1)
+ setmode(fd, O_BINARY);
+ return fd;
+}
+
int vlc_pipe (int fds[2])
{
#if VLC_WINSTORE_APP
--
2.29.2
More information about the vlc-devel
mailing list