1 /*-------------------------------------------------------------------------
4 * Win32 open() replacement
7 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
9 * $PostgreSQL: pgsql/src/port/open.c,v 1.4 2004/08/29 05:07:02 momjian Exp $
11 *-------------------------------------------------------------------------
22 openFlagsToCreateFileFlags(int openFlags)
24 switch (openFlags & (O_CREAT | O_TRUNC | O_EXCL))
34 case O_TRUNC | O_EXCL:
35 return TRUNCATE_EXISTING;
37 case O_CREAT | O_TRUNC:
40 case O_CREAT | O_EXCL:
41 case O_CREAT | O_TRUNC | O_EXCL:
45 /* will never get here */
50 * - file attribute setting, based on fileMode?
51 * - handle other flags? (eg FILE_FLAG_NO_BUFFERING/FILE_FLAG_WRITE_THROUGH)
54 win32_open(const char *fileName, int fileFlags,...)
58 SECURITY_ATTRIBUTES sa;
60 /* Check that we can handle the request */
61 assert((fileFlags & ((O_RDONLY | O_WRONLY | O_RDWR) | O_APPEND |
62 (O_RANDOM | O_SEQUENTIAL | O_TEMPORARY) |
64 (O_CREAT | O_TRUNC | O_EXCL) | (O_TEXT | O_BINARY))) == fileFlags);
66 sa.nLength = sizeof(sa);
67 sa.bInheritHandle = TRUE;
68 sa.lpSecurityDescriptor = NULL;
70 if ((h = CreateFile(fileName,
71 /* cannot use O_RDONLY, as it == 0 */
72 (fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
73 ((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
74 (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
76 openFlagsToCreateFileFlags(fileFlags),
77 FILE_ATTRIBUTE_NORMAL |
78 ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
79 ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
80 ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
81 ((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0),
82 NULL)) == INVALID_HANDLE_VALUE)
84 switch (GetLastError())
86 /* EMFILE, ENFILE should not occur from CreateFile. */
87 case ERROR_PATH_NOT_FOUND:
88 case ERROR_FILE_NOT_FOUND:
91 case ERROR_FILE_EXISTS:
94 case ERROR_ACCESS_DENIED:
103 /* _open_osfhandle will, on error, set errno accordingly */
104 if ((fd = _open_osfhandle((long) h, fileFlags & O_APPEND)) < 0 ||
105 (fileFlags & (O_TEXT | O_BINARY) && (_setmode(fd, fileFlags & (O_TEXT | O_BINARY)) < 0)))
106 CloseHandle(h); /* will not affect errno */