/ In Insert mode, just open the file for append. Otherwise */ create the mapping file object. The map handle can be released */ immediately because they will not be used anymore. */ If del is true in DELETE mode, then delete the whole file. */ Returns the file handle that can be used by caller. */ /
| 23 | /* Returns the file handle that can be used by caller. */ |
| 24 | /***********************************************************************/ |
| 25 | HANDLE CreateFileMap(PGLOBAL g, LPCSTR filename, |
| 26 | MEMMAP *mm, MODE mode, bool del) |
| 27 | { |
| 28 | HANDLE hFile; |
| 29 | HANDLE hFileMap; |
| 30 | DWORD access, share, disposition; |
| 31 | |
| 32 | memset(mm, 0, sizeof(MEMMAP)); |
| 33 | *g->Message = '\0'; |
| 34 | |
| 35 | switch (mode) { |
| 36 | case MODE_READ: |
| 37 | access = GENERIC_READ; |
| 38 | share = FILE_SHARE_READ; |
| 39 | disposition = OPEN_EXISTING; |
| 40 | break; |
| 41 | case MODE_UPDATE: |
| 42 | case MODE_DELETE: |
| 43 | access = GENERIC_READ | GENERIC_WRITE; |
| 44 | share = 0; |
| 45 | disposition = (del) ? TRUNCATE_EXISTING : OPEN_EXISTING; |
| 46 | break; |
| 47 | case MODE_INSERT: |
| 48 | access = GENERIC_WRITE; |
| 49 | share = 0; |
| 50 | disposition = OPEN_ALWAYS; |
| 51 | break; |
| 52 | default: |
| 53 | snprintf(g->Message, sizeof(g->Message), MSG(BAD_FUNC_MODE), "CreateFileMap", mode); |
| 54 | return INVALID_HANDLE_VALUE; |
| 55 | } // endswitch |
| 56 | |
| 57 | hFile = CreateFile(filename, access, share, NULL, disposition, |
| 58 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 59 | |
| 60 | if (hFile != INVALID_HANDLE_VALUE) |
| 61 | if (mode != MODE_INSERT) { |
| 62 | /*****************************************************************/ |
| 63 | /* Create the file-mapping object. */ |
| 64 | /*****************************************************************/ |
| 65 | access = (mode == MODE_READ) ? PAGE_READONLY : PAGE_READWRITE; |
| 66 | hFileMap = CreateFileMapping(hFile, NULL, access, 0, 0, NULL); |
| 67 | |
| 68 | if (!hFileMap) { |
| 69 | DWORD ler = GetLastError(); |
| 70 | |
| 71 | if (ler && ler != 1006) { |
| 72 | snprintf(g->Message, sizeof(g->Message), MSG(FILE_MAP_ERROR), filename, ler); |
| 73 | CloseHandle(hFile); |
| 74 | return INVALID_HANDLE_VALUE; |
| 75 | } else { |
| 76 | snprintf(g->Message, sizeof(g->Message), MSG(FILE_IS_EMPTY), filename); |
| 77 | return hFile; |
| 78 | } // endif ler |
| 79 | |
| 80 | } // endif hFileMap |
| 81 | |
| 82 | access = (mode == MODE_READ) ? FILE_MAP_READ : FILE_MAP_WRITE; |
no test coverage detected