summaryrefslogtreecommitdiffstats
path: root/src/H5system.c
diff options
context:
space:
mode:
authorDana Robinson <43805+derobins@users.noreply.github.com>2021-03-22 16:32:40 (GMT)
committerGitHub <noreply@github.com>2021-03-22 16:32:40 (GMT)
commit60e14b826a22452934f7057196c1ce4d0054d97a (patch)
treecb33f8b4d8840a7d0e2c653bde8e73020837624d /src/H5system.c
parent05bc1905cb37d145c29c036bacb1caac63c7f0c9 (diff)
downloadhdf5-60e14b826a22452934f7057196c1ce4d0054d97a.zip
hdf5-60e14b826a22452934f7057196c1ce4d0054d97a.tar.gz
hdf5-60e14b826a22452934f7057196c1ce4d0054d97a.tar.bz2
File locks now work on Windows (#480)
* File locks now work on Windows Uses LockFileEx() and UnlockFileEx(). Fixes HDFFV-10191 (partial). * Committing clang-format changes * Committing clang-format changes * Fixes commenting in h5repack * Reworks H5Fis_accessible() H5Fis_accessible() created a new file handle and attempted to read through it, which will fail when a file has been opened with an exclusive lock on operating systems that have mandatory locks. This change uses the same scheme we use in H5Fopen() to check if the file is already open and only tries to read the file signature if the file has not already been opened. Also adds a test for this behavior. * Committing clang-format changes * Trivial change to force github to run actions Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'src/H5system.c')
-rw-r--r--src/H5system.c51
1 files changed, 27 insertions, 24 deletions
diff --git a/src/H5system.c b/src/H5system.c
index 1d94816..745476d 100644
--- a/src/H5system.c
+++ b/src/H5system.c
@@ -618,40 +618,43 @@ c99_vsnprintf(char *str, size_t size, const char *format, va_list ap)
*-------------------------------------------------------------------------
*/
int
-Wflock(int H5_ATTR_UNUSED fd, int H5_ATTR_UNUSED operation)
+Wflock(int fd, int operation)
{
-/* This is a no-op while we implement a Win32 VFD */
-#if 0
-int
-Wflock(int fd, int operation) {
-
- HANDLE hFile;
- DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
- DWORD dwReserved = 0;
- /* MAXDWORD for entire file */
- DWORD nNumberOfBytesToLockLow = MAXDWORD;
- DWORD nNumberOfBytesToLockHigh = MAXDWORD;
- /* Must initialize OVERLAPPED struct */
- OVERLAPPED overlapped = {0};
+ HANDLE hFile;
+ DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
+ DWORD dwReserved = 0;
+ /* MAXDWORD locks the entire file */
+ DWORD nNumberOfBytesToLockLow = MAXDWORD;
+ DWORD nNumberOfBytesToLockHigh = MAXDWORD;
+ /* Must initialize OVERLAPPED struct */
+ OVERLAPPED overlapped = {0};
/* Get Windows HANDLE */
- hFile = _get_osfhandle(fd);
+ if (INVALID_HANDLE_VALUE == (hFile = (HANDLE)_get_osfhandle(fd)))
+ return -1;
/* Convert to Windows flags */
- if(operation & LOCK_EX)
+ if (operation & LOCK_EX)
dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
/* Lock or unlock */
- if(operation & LOCK_UN)
- if(0 == UnlockFileEx(hFile, dwReserved, nNumberOfBytesToLockLow,
- nNumberOfBytesToLockHigh, &overlapped))
- return -1;
- else
- if(0 == LockFileEx(hFile, dwFlags, dwReserved, nNumberOfBytesToLockLow,
- nNumberOfBytesToLockHigh, &overlapped))
+ if (operation & LOCK_UN) {
+ if (0 ==
+ UnlockFileEx(hFile, dwReserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh, &overlapped)) {
+ /* Attempting to unlock an already unlocked file will fail and this can happen
+ * in H5Fstart_swmr_write(). For now, just ignore the "error" (error code: 0x9e / 158).
+ */
+ if (GetLastError() != 158)
+ return -1;
+ }
+ }
+ else {
+ if (0 == LockFileEx(hFile, dwFlags, dwReserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh,
+ &overlapped))
return -1;
-#endif /* 0 */
+ }
+
return 0;
} /* end Wflock() */