1 Rules for opening shared files
2 ==============================
4 File is already open, with share set to none:
9 File is already open for reading, with read share:
11 Can open for reading only, share must include read (can have write too)
14 File is already open for reading, with write share:
16 Can open for writing only, share must include read (can have write too)
19 File is already open for reading, with read + write share:
21 Can open for read, writing or both, share must include read (can have write too)
24 File is already open for writing, with read share:
26 Can open for reading only, share must include write (can have read too)
29 File is already open for writing, with write share:
31 Can open for writing only, share must include write (can have read too)
34 File is already open for writing, with read + write share:
36 Can open for reading, writing or both, share must include write (can have read too)
39 File is already open for reading + writing, with read share:
41 Can open for reading only, share must be read + write
44 File is already open for reading + writing, with write share:
46 Can open for for writing only, share must be read + write
49 File is already open for reading + writing, with read + write share:
51 Can open for read, writing or both, share must be read + write
58 Second open must have access within first share, must set second share to at least first access
73 GENERIC_READ | GENERIC_WRITE
76 char *access_names[] = {
85 FILE_SHARE_READ | FILE_SHARE_WRITE
88 char *share_names[] = {
91 "SHARE_READ|SHARE_WRITE"
94 void lockfiles(int access1, int share1, int access2, int share2)
99 if (access2 == 0 && share2 == 0) {
101 printf("%22.22s\n%22.22s", access_names[access1], share_names[share1]);
104 h1 = CreateFile("lockedfile",
107 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
108 if (h1 == INVALID_HANDLE_VALUE) {
109 printf("Open1 failed: %d\n", GetLastError());
113 h2 = CreateFile("lockedfile",
116 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
117 if (h2 == INVALID_HANDLE_VALUE) {
118 printf(" %4.4s", "");
120 printf(" %4.4s", "OK");
127 int main(int argc, char **argv)
131 printf("\t\t\t\t\t\t\tSecond Open\n");
132 printf("%22.22s G_RE G_RE G_RE G_WR G_WR G_WR G_RW G_RW G_RW\n", "");
133 printf("%22.22s S_RE S_WR S_RW S_RE S_WR S_RW S_RE S_WR S_RW", "First open --v ");
134 for (i = 0; i < 3; i++) {
135 for (j = 0; j < 3; j++) {
136 for (k = 0; k < 3; k++) {
137 for (l = 0; l < 3; l++) {
138 lockfiles(i, j, k, l);
153 G_RE G_RE G_RE G_WR G_WR G_WR G_RW G_RW G_RW
154 First open --v S_RE S_WR S_RW S_RE S_WR S_RW S_RE S_WR S_RW
160 SHARE_READ|SHARE_WRITE OK OK OK OK OK OK
166 SHARE_READ|SHARE_WRITE OK OK OK OK OK OK
172 SHARE_READ|SHARE_WRITE OK OK OK