Option --forceflush inoperative on Windows #150

Closed
opened 2026-01-29 16:36:27 +00:00 by claunia · 1 comment
Owner

Originally created by @lelegard on GitHub (May 30, 2016).

Originally assigned to: @cfsmp3 on GitHub.

Hi,

I would like to report an invisible bug with -ff / --forceflush on Windows. It is "invisible" in the sense that it has no symptom, no crash, but failed to work properly, ie. it does not flush the file.

Here is a small patch to fix the problem: ccextractor.0.80.winfsync.patch.zip

The problem is simple. Flush is done using fync(). This function does not exist on Windows. In file lib_ccx/ccx_encoders_common.c, there is a replacement function fsync(int fd) for WIN32. This function calls the native Win32 function FlushFileBuffers(fd). But the problem is that FlushFileBuffers uses a HANDLE as parameter, not an int, the traditional UNIX file descriptor.

So, it is only by chance that FlushFileBuffers does not crash the application. Adding, some traces, we can see that FlushFileBuffers quite logically fails with error ERROR_INVALID_HANDLE.

The valid way to emulate fsync() on Windows is FlushFileBuffers((HANDLE)_get_osfhandle(fd)). This is what the patch does and FlushFileBuffers no longer fails.

Originally created by @lelegard on GitHub (May 30, 2016). Originally assigned to: @cfsmp3 on GitHub. Hi, I would like to report an invisible bug with `-ff` / `--forceflush` on Windows. It is "invisible" in the sense that it has no symptom, no crash, but failed to work properly, ie. it does not flush the file. Here is a small patch to fix the problem: [ccextractor.0.80.winfsync.patch.zip](https://github.com/CCExtractor/ccextractor/files/289873/ccextractor.0.80.winfsync.patch.zip) The problem is simple. Flush is done using `fync()`. This function does not exist on Windows. In file `lib_ccx/ccx_encoders_common.c`, there is a replacement function `fsync(int fd)` for `WIN32`. This function calls the native Win32 function `FlushFileBuffers(fd)`. But the problem is that `FlushFileBuffers` uses a `HANDLE` as parameter, not an `int`, the traditional UNIX file descriptor. So, it is only by chance that `FlushFileBuffers` does not crash the application. Adding, some traces, we can see that `FlushFileBuffers` quite logically fails with error `ERROR_INVALID_HANDLE`. The valid way to emulate `fsync()` on Windows is `FlushFileBuffers((HANDLE)_get_osfhandle(fd))`. This is what the patch does and `FlushFileBuffers` no longer fails.
Author
Owner

@cfsmp3 commented on GitHub (Jun 9, 2016):

Merged.

@cfsmp3 commented on GitHub (Jun 9, 2016): Merged.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#150