Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / docs / file-share-modes
1 Rules for opening shared files
2 ==============================
3
4 File is already open, with share set to none:
5
6         Can not open again
7
8
9 File is already open for reading, with read share:
10
11         Can open for reading only, share must include read (can have write too)
12
13
14 File is already open for reading, with write share:
15
16         Can open for writing only, share must include read (can have write too)
17
18
19 File is already open for reading, with read + write share:
20
21         Can open for read, writing or both, share must include read (can have write too)
22
23
24 File is already open for writing, with read share:
25
26         Can open for reading only, share must include write (can have read too)
27
28
29 File is already open for writing, with write share:
30
31         Can open for writing only, share must include write (can have read too)
32
33
34 File is already open for writing, with read + write share:
35
36         Can open for reading, writing or both, share must include write (can have read too)
37
38
39 File is already open for reading + writing, with read share:
40
41         Can open for reading only, share must be read + write
42
43
44 File is already open for reading + writing, with write share:
45
46         Can open for for writing only, share must be read + write
47
48
49 File is already open for reading + writing, with read + write share:
50
51         Can open for read, writing or both, share must be read + write
52
53
54
55 Executive Summary
56 -----------------
57
58         Second open must have access within first share, must set second share to at least first access
59
60
61
62
63 Documenting code
64 ----------------
65
66
67 #include <stdio.h>
68 #include <windows.h>
69
70 int access[] = {
71         GENERIC_READ,
72         GENERIC_WRITE,
73         GENERIC_READ | GENERIC_WRITE
74 };
75
76 char *access_names[] = {
77         "G_READ",
78         "G_WRITE",
79         "G_READ|G_WRITE"
80 };
81
82 int share[] = {
83         FILE_SHARE_READ,
84         FILE_SHARE_WRITE,
85         FILE_SHARE_READ | FILE_SHARE_WRITE
86 };
87
88 char *share_names[] = {
89         "SHARE_READ",
90         "SHARE_WRITE",
91         "SHARE_READ|SHARE_WRITE"
92 };
93
94 void lockfiles(int access1, int share1, int access2, int share2)
95 {
96         HANDLE h1, h2;
97         BOOL ret;
98
99         if (access2 == 0 && share2 == 0) {
100                 printf("\n");
101                 printf("%22.22s\n%22.22s", access_names[access1], share_names[share1]);
102         }
103
104         h1 = CreateFile("lockedfile",
105                 access[access1],
106                 share[share1],
107                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
108         if (h1 == INVALID_HANDLE_VALUE) {
109                 printf("Open1 failed: %d\n", GetLastError());
110                 return;
111         }
112
113         h2 = CreateFile("lockedfile",
114                 access[access2],
115                 share[share2],
116                 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
117         if (h2 == INVALID_HANDLE_VALUE) {
118                 printf(" %4.4s", "");
119         } else {
120                 printf(" %4.4s", "OK");
121                 CloseHandle(h2);
122         }
123
124         CloseHandle(h1);
125 }
126
127 int main(int argc, char **argv)
128 {
129         int i, j, k, l;
130
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);
139                                 }
140                         }
141                 }
142         }
143
144         return(0);
145 }
146
147
148
149 Code output
150 -----------
151
152                                                         Second Open
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
155                 G_READ
156             SHARE_READ   OK        OK                              
157                 G_READ
158            SHARE_WRITE                  OK        OK               
159                 G_READ
160 SHARE_READ|SHARE_WRITE   OK        OK   OK        OK   OK        OK
161                G_WRITE
162             SHARE_READ        OK   OK                              
163                G_WRITE
164            SHARE_WRITE                       OK   OK               
165                G_WRITE
166 SHARE_READ|SHARE_WRITE        OK   OK        OK   OK        OK   OK
167         G_READ|G_WRITE
168             SHARE_READ             OK                              
169         G_READ|G_WRITE
170            SHARE_WRITE                            OK               
171         G_READ|G_WRITE
172 SHARE_READ|SHARE_WRITE             OK             OK             OK
173