2008-12-09 Mark Mason <mmason@upwardaccess.com>
[mono.git] / docs / file-share-modes
blob7be1919912de7f241e655040ded94725d7485bd9
1 Rules for opening shared files
2 ==============================
4 File is already open, with share set to none:
6         Can not open again
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
55 Executive Summary
56 -----------------
58         Second open must have access within first share, must set second share to at least first access
63 Documenting code
64 ----------------
67 #include <stdio.h>
68 #include <windows.h>
70 int access[] = {
71         GENERIC_READ,
72         GENERIC_WRITE,
73         GENERIC_READ | GENERIC_WRITE
76 char *access_names[] = {
77         "G_READ",
78         "G_WRITE",
79         "G_READ|G_WRITE"
82 int share[] = {
83         FILE_SHARE_READ,
84         FILE_SHARE_WRITE,
85         FILE_SHARE_READ | FILE_SHARE_WRITE
88 char *share_names[] = {
89         "SHARE_READ",
90         "SHARE_WRITE",
91         "SHARE_READ|SHARE_WRITE"
94 void lockfiles(int access1, int share1, int access2, int share2)
96         HANDLE h1, h2;
97         BOOL ret;
99         if (access2 == 0 && share2 == 0) {
100                 printf("\n");
101                 printf("%22.22s\n%22.22s", access_names[access1], share_names[share1]);
102         }
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         }
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         }
124         CloseHandle(h1);
127 int main(int argc, char **argv)
129         int i, j, k, l;
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         }
144         return(0);
149 Code output
150 -----------
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