Reactos
1//
2// commit.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _commit(), which flushes a file buffer to disk.
7//
8#include <corecrt_internal_lowio.h>
9
10
11
12// Flushes the buffer for the given file to disk.
13//
14// On success, 0 is returned. On failure, -1 is returned and errno is set.
15extern "C" int __cdecl _commit(int const fh)
16{
17 _CHECK_FH_RETURN(fh, EBADF, -1);
18 _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
19 _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
20
21 return __acrt_lowio_lock_fh_and_call(fh, [&]()
22 {
23 if (_osfile(fh) & FOPEN)
24 {
25 if (FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(fh))))
26 return 0; // Success
27
28 _doserrno = GetLastError();
29 }
30
31 errno = EBADF;
32
33 _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
34 return -1;
35 });
36}