-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathflock_win.h
78 lines (69 loc) · 2.3 KB
/
flock_win.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
typedef ULONGLONG ipc_flock_off_t;
static void ipc_flock_error( char* buf, size_t len, int code ) {
if( len > 0 ) {
if( 0 == FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
0,
buf,
len,
NULL ) ) {
strncpy( buf, "unknown error", len-1 );
buf[ len-1 ] = '\0';
} else { /* Windows puts an extra newline in there! */
size_t n = strlen( buf );
while( n > 0 && isspace( (unsigned char)buf[ --n ] ) )
buf[ n ] = '\0';
}
}
}
static int ipc_flock_lock( FILE* f, int is_wlock, int* could_lock,
ipc_flock_off_t start,
ipc_flock_off_t len ) {
HANDLE fh = (HANDLE)_get_osfhandle( _fileno( f ) );
DWORD flags = is_wlock ? LOCKFILE_EXCLUSIVE_LOCK : 0;
DWORD lenlo = (DWORD)len, lenhi = (DWORD)(len >> 32);
OVERLAPPED ov;
if( fh == (HANDLE)INVALID_HANDLE_VALUE )
return IPC_ERR( ERROR_INVALID_HANDLE );
if( could_lock != NULL )
flags |= LOCKFILE_FAIL_IMMEDIATELY;
ov.Offset = (DWORD)start;
ov.OffsetHigh = (DWORD)(start >> 32);
ov.hEvent = NULL;
if( len == 0 )
lenhi = lenlo = (DWORD)-1;
if( !LockFileEx( fh, flags, 0, lenlo, lenhi, &ov ) ) {
int code = GetLastError();
if( could_lock != NULL &&
(code == ERROR_LOCK_VIOLATION || code == ERROR_IO_PENDING) ) {
*could_lock = 0;
return 0;
}
return IPC_ERR( code );
}
if( could_lock != NULL )
*could_lock = 1;
return 0;
}
static int ipc_flock_unlock( FILE* f, ipc_flock_off_t start,
ipc_flock_off_t len ) {
HANDLE fh = (HANDLE)_get_osfhandle( _fileno( f ) );
DWORD lenlo = (DWORD)len, lenhi = (DWORD)(len >> 32);
DWORD offlo = (DWORD)start, offhi = (DWORD)(start >> 32);
if( fh == (HANDLE)INVALID_HANDLE_VALUE )
return IPC_ERR( ERROR_INVALID_HANDLE );
if( len == 0 )
lenhi = lenlo = (DWORD)-1;
if( !UnlockFile( fh, offlo, offhi, lenlo, lenhi ) )
return IPC_ERR( GetLastError() );
return 0;
}