file-share-modes 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. Rules for opening shared files
  2. ==============================
  3. File is already open, with share set to none:
  4. Can not open again
  5. File is already open for reading, with read share:
  6. Can open for reading only, share must include read (can have write too)
  7. File is already open for reading, with write share:
  8. Can open for writing only, share must include read (can have write too)
  9. File is already open for reading, with read + write share:
  10. Can open for read, writing or both, share must include read (can have write too)
  11. File is already open for writing, with read share:
  12. Can open for reading only, share must include write (can have read too)
  13. File is already open for writing, with write share:
  14. Can open for writing only, share must include write (can have read too)
  15. File is already open for writing, with read + write share:
  16. Can open for reading, writing or both, share must include write (can have read too)
  17. File is already open for reading + writing, with read share:
  18. Can open for reading only, share must be read + write
  19. File is already open for reading + writing, with write share:
  20. Can open for for writing only, share must be read + write
  21. File is already open for reading + writing, with read + write share:
  22. Can open for read, writing or both, share must be read + write
  23. Executive Summary
  24. -----------------
  25. Second open must have access within first share, must set second share to at least first access
  26. Documenting code
  27. ----------------
  28. #include <stdio.h>
  29. #include <windows.h>
  30. int access[] = {
  31. GENERIC_READ,
  32. GENERIC_WRITE,
  33. GENERIC_READ | GENERIC_WRITE
  34. };
  35. char *access_names[] = {
  36. "G_READ",
  37. "G_WRITE",
  38. "G_READ|G_WRITE"
  39. };
  40. int share[] = {
  41. FILE_SHARE_READ,
  42. FILE_SHARE_WRITE,
  43. FILE_SHARE_READ | FILE_SHARE_WRITE
  44. };
  45. char *share_names[] = {
  46. "SHARE_READ",
  47. "SHARE_WRITE",
  48. "SHARE_READ|SHARE_WRITE"
  49. };
  50. void lockfiles(int access1, int share1, int access2, int share2)
  51. {
  52. HANDLE h1, h2;
  53. BOOL ret;
  54. if (access2 == 0 && share2 == 0) {
  55. printf("\n");
  56. printf("%22.22s\n%22.22s", access_names[access1], share_names[share1]);
  57. }
  58. h1 = CreateFile("lockedfile",
  59. access[access1],
  60. share[share1],
  61. NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  62. if (h1 == INVALID_HANDLE_VALUE) {
  63. printf("Open1 failed: %d\n", GetLastError());
  64. return;
  65. }
  66. h2 = CreateFile("lockedfile",
  67. access[access2],
  68. share[share2],
  69. NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  70. if (h2 == INVALID_HANDLE_VALUE) {
  71. printf(" %4.4s", "");
  72. } else {
  73. printf(" %4.4s", "OK");
  74. CloseHandle(h2);
  75. }
  76. CloseHandle(h1);
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. int i, j, k, l;
  81. printf("\t\t\t\t\t\t\tSecond Open\n");
  82. printf("%22.22s G_RE G_RE G_RE G_WR G_WR G_WR G_RW G_RW G_RW\n", "");
  83. printf("%22.22s S_RE S_WR S_RW S_RE S_WR S_RW S_RE S_WR S_RW", "First open --v ");
  84. for (i = 0; i < 3; i++) {
  85. for (j = 0; j < 3; j++) {
  86. for (k = 0; k < 3; k++) {
  87. for (l = 0; l < 3; l++) {
  88. lockfiles(i, j, k, l);
  89. }
  90. }
  91. }
  92. }
  93. return(0);
  94. }
  95. Code output
  96. -----------
  97. Second Open
  98. G_RE G_RE G_RE G_WR G_WR G_WR G_RW G_RW G_RW
  99. First open --v S_RE S_WR S_RW S_RE S_WR S_RW S_RE S_WR S_RW
  100. G_READ
  101. SHARE_READ OK OK
  102. G_READ
  103. SHARE_WRITE OK OK
  104. G_READ
  105. SHARE_READ|SHARE_WRITE OK OK OK OK OK OK
  106. G_WRITE
  107. SHARE_READ OK OK
  108. G_WRITE
  109. SHARE_WRITE OK OK
  110. G_WRITE
  111. SHARE_READ|SHARE_WRITE OK OK OK OK OK OK
  112. G_READ|G_WRITE
  113. SHARE_READ OK
  114. G_READ|G_WRITE
  115. SHARE_WRITE OK
  116. G_READ|G_WRITE
  117. SHARE_READ|SHARE_WRITE OK OK OK