forked from mono/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-share-modes
173 lines (117 loc) · 3.93 KB
/
file-share-modes
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Rules for opening shared files
==============================
File is already open, with share set to none:
Can not open again
File is already open for reading, with read share:
Can open for reading only, share must include read (can have write too)
File is already open for reading, with write share:
Can open for writing only, share must include read (can have write too)
File is already open for reading, with read + write share:
Can open for read, writing or both, share must include read (can have write too)
File is already open for writing, with read share:
Can open for reading only, share must include write (can have read too)
File is already open for writing, with write share:
Can open for writing only, share must include write (can have read too)
File is already open for writing, with read + write share:
Can open for reading, writing or both, share must include write (can have read too)
File is already open for reading + writing, with read share:
Can open for reading only, share must be read + write
File is already open for reading + writing, with write share:
Can open for for writing only, share must be read + write
File is already open for reading + writing, with read + write share:
Can open for read, writing or both, share must be read + write
Executive Summary
-----------------
Second open must have access within first share, must set second share to at least first access
Documenting code
----------------
#include <stdio.h>
#include <windows.h>
int access[] = {
GENERIC_READ,
GENERIC_WRITE,
GENERIC_READ | GENERIC_WRITE
};
char *access_names[] = {
"G_READ",
"G_WRITE",
"G_READ|G_WRITE"
};
int share[] = {
FILE_SHARE_READ,
FILE_SHARE_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE
};
char *share_names[] = {
"SHARE_READ",
"SHARE_WRITE",
"SHARE_READ|SHARE_WRITE"
};
void lockfiles(int access1, int share1, int access2, int share2)
{
HANDLE h1, h2;
BOOL ret;
if (access2 == 0 && share2 == 0) {
printf("\n");
printf("%22.22s\n%22.22s", access_names[access1], share_names[share1]);
}
h1 = CreateFile("lockedfile",
access[access1],
share[share1],
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (h1 == INVALID_HANDLE_VALUE) {
printf("Open1 failed: %d\n", GetLastError());
return;
}
h2 = CreateFile("lockedfile",
access[access2],
share[share2],
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (h2 == INVALID_HANDLE_VALUE) {
printf(" %4.4s", "");
} else {
printf(" %4.4s", "OK");
CloseHandle(h2);
}
CloseHandle(h1);
}
int main(int argc, char **argv)
{
int i, j, k, l;
printf("\t\t\t\t\t\t\tSecond Open\n");
printf("%22.22s G_RE G_RE G_RE G_WR G_WR G_WR G_RW G_RW G_RW\n", "");
printf("%22.22s S_RE S_WR S_RW S_RE S_WR S_RW S_RE S_WR S_RW", "First open --v ");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
for (l = 0; l < 3; l++) {
lockfiles(i, j, k, l);
}
}
}
}
return(0);
}
Code output
-----------
Second Open
G_RE G_RE G_RE G_WR G_WR G_WR G_RW G_RW G_RW
First open --v S_RE S_WR S_RW S_RE S_WR S_RW S_RE S_WR S_RW
G_READ
SHARE_READ OK OK
G_READ
SHARE_WRITE OK OK
G_READ
SHARE_READ|SHARE_WRITE OK OK OK OK OK OK
G_WRITE
SHARE_READ OK OK
G_WRITE
SHARE_WRITE OK OK
G_WRITE
SHARE_READ|SHARE_WRITE OK OK OK OK OK OK
G_READ|G_WRITE
SHARE_READ OK
G_READ|G_WRITE
SHARE_WRITE OK
G_READ|G_WRITE
SHARE_READ|SHARE_WRITE OK OK OK