This is an automated email from the git hooks/post-receive script.

Git pushed a commit to branch master
in repository ffmpeg.

commit 4a9a180d2662506573cd8d382f11bd5fbf9983e6
Author:     Kacper Michajłow <[email protected]>
AuthorDate: Sun Sep 13 02:13:39 2026 +0200
Commit:     Kacper Michajłow <[email protected]>
CommitDate: Wed Sep 23 16:50:46 2026 +0200

    avformat/os_support: add pread(), pwrite() and flock() for Windows
    
    Positional reads and writes and whole file locks have no CRT
    equivalent on Windows. off_t is 32 bits wide with the Windows SDK and
    ssize_t does not exist there, so define both.
    
    Also provide a fallback O_BINARY for the platforms without it.
    
    Signed-off-by: Kacper Michajłow <[email protected]>
---
 libavformat/os_support.h | 96 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 1 deletion(-)

diff --git a/libavformat/os_support.h b/libavformat/os_support.h
index 4fa5ca3c52..e3aac6f9b8 100644
--- a/libavformat/os_support.h
+++ b/libavformat/os_support.h
@@ -29,8 +29,13 @@
 
 #include "config.h"
 
+#include <fcntl.h>
 #include <sys/stat.h>
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 #ifdef _WIN32
 #if HAVE_DIRECT_H
 #include <direct.h>
@@ -41,8 +46,12 @@
 #endif
 
 #ifdef _WIN32
-#  include <fcntl.h>
 #  include <stdint.h>
+#  include <sys/types.h>
+#  ifdef off_t
+#   undef off_t
+#  endif
+#  define off_t int64_t
 #  ifdef lseek
 #   undef lseek
 #  endif
@@ -171,11 +180,18 @@ int ff_poll(struct pollfd *fds, nfds_t numfds, int 
timeout);
 #endif /* CONFIG_NETWORK */
 
 #ifdef _WIN32
+#include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <windows.h>
 #include "libavutil/mem.h"
 #include "libavutil/wchar_filename.h"
 
+#ifndef _SSIZE_T_DEFINED
+#define _SSIZE_T_DEFINED
+typedef SSIZE_T ssize_t;
+#endif
+
 #define DEF_FS_FUNCTION(name, wfunc, afunc)               \
 static inline int win32_##name(const char *filename_utf8) \
 {                                                         \
@@ -307,11 +323,89 @@ fallback:
     return ret;
 }
 
+/*
+ * Positional I/O and file locking. Unlike pread() and pwrite() the Windows
+ * versions move the file position of the descriptor. A flock() lock covers
+ * only the first byte of the file, ReadFile() and WriteFile() of that byte
+ * from other processes block or fail while the lock is held. Mapped
+ * views of the file are not affected.
+ */
+#ifndef LOCK_SH
+#define LOCK_SH 1
+#define LOCK_EX 2
+#define LOCK_NB 4
+#define LOCK_UN 8
+#endif
+
+static inline ssize_t win32_pread(int fd, void *buf, size_t size, off_t offset)
+{
+    HANDLE fh = (HANDLE)_get_osfhandle(fd);
+    OVERLAPPED ov = { .Offset = offset, .OffsetHigh = offset >> 32 };
+    DWORD n;
+
+    if (fh == INVALID_HANDLE_VALUE)
+        return -1;
+    if (size > INT_MAX)
+        size = INT_MAX;
+    if (!ReadFile(fh, buf, size, &n, &ov)) {
+        if (GetLastError() == ERROR_HANDLE_EOF)
+            return 0;
+        errno = EIO;
+        return -1;
+    }
+    return n;
+}
+
+static inline ssize_t win32_pwrite(int fd, const void *buf, size_t size, off_t 
offset)
+{
+    HANDLE fh = (HANDLE)_get_osfhandle(fd);
+    OVERLAPPED ov = { .Offset = offset, .OffsetHigh = offset >> 32 };
+    DWORD n;
+
+    if (fh == INVALID_HANDLE_VALUE)
+        return -1;
+    if (size > INT_MAX)
+        size = INT_MAX;
+    if (!WriteFile(fh, buf, size, &n, &ov)) {
+        errno = GetLastError() == ERROR_DISK_FULL ? ENOSPC : EIO;
+        return -1;
+    }
+    return n;
+}
+
+static inline int win32_flock(int fd, int op)
+{
+    HANDLE fh = (HANDLE)_get_osfhandle(fd);
+    OVERLAPPED ov = { 0 };
+    BOOL ok;
+
+    if (fh == INVALID_HANDLE_VALUE)
+        return -1;
+    if (op & LOCK_UN) {
+        ok = UnlockFileEx(fh, 0, 1, 0, &ov);
+    } else {
+        DWORD flags = 0;
+        if (op & LOCK_EX)
+            flags |= LOCKFILE_EXCLUSIVE_LOCK;
+        if (op & LOCK_NB)
+            flags |= LOCKFILE_FAIL_IMMEDIATELY;
+        ok = LockFileEx(fh, flags, 0, 1, 0, &ov);
+    }
+    if (!ok) {
+        errno = GetLastError() == ERROR_LOCK_VIOLATION ? EWOULDBLOCK : EIO;
+        return -1;
+    }
+    return 0;
+}
+
 #define mkdir(a, b) win32_mkdir(a)
 #define rename      win32_rename
 #define rmdir       win32_rmdir
 #define unlink      win32_unlink
 #define access      win32_access
+#define pread       win32_pread
+#define pwrite      win32_pwrite
+#define flock       win32_flock
 
 #endif
 

-- 
To stop receiving notification emails like this one, please contact
[email protected].
_______________________________________________
ffmpeg-cvslog mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to