2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
27
* Open a file descriptor.
30
__os_open(dbenv, name, flags, mode, fhp)
38
u_int32_t log_size, pagesize, sectorsize;
39
int access, attr, oflags, share, createflag;
41
char *drive, dbuf[4]; /* <letter><colon><slosh><nul> */
45
(DB_OSO_CREATE | DB_OSO_DIRECT | DB_OSO_EXCL | DB_OSO_LOG | \
46
DB_OSO_RDONLY | DB_OSO_REGION | DB_OSO_SEQ | DB_OSO_TEMP | \
48
if ((ret = __db_fchk(dbenv, "__os_open", flags, OKFLAGS)) != 0)
53
* The "public" interface to the __os_open routine passes around POSIX
54
* 1003.1 flags, not DB flags. If the user has defined their own open
55
* interface, use the POSIX flags.
57
if (DB_GLOBAL(j_open) != NULL) {
58
oflags = O_BINARY | O_NOINHERIT;
60
if (LF_ISSET(DB_OSO_CREATE))
63
if (LF_ISSET(DB_OSO_EXCL))
66
if (LF_ISSET(DB_OSO_RDONLY))
71
if (LF_ISSET(DB_OSO_SEQ))
72
oflags |= _O_SEQUENTIAL;
76
if (LF_ISSET(DB_OSO_TEMP))
77
oflags |= _O_TEMPORARY;
79
if (LF_ISSET(DB_OSO_TRUNC))
82
ret = __os_openhandle(dbenv, name, oflags, mode, fhp);
84
fhp->handle = _get_osfhandle (fhp->fd);
91
if (LF_ISSET(DB_OSO_LOG))
92
log_size = fhp->log_size; /* XXX: Gag. */
94
pagesize = fhp->pagesize;
96
memset(fhp, 0, sizeof(*fhp));
100
* Otherwise, use the Windows/32 CreateFile interface so that we can
101
* play magic games with log files to get data flush effects similar
102
* to the POSIX O_DSYNC flag.
105
* We currently ignore the 'mode' argument. It would be possible
106
* to construct a set of security attributes that we could pass to
107
* CreateFile that would accurately represents the mode. In worst
108
* case, this would require looking up user and all group names and
109
* creating an entry for each. Alternatively, we could call the
110
* _chmod (partial emulation) function after file creation, although
111
* this leaves us with an obvious race. However, these efforts are
112
* largely meaningless on FAT, the most common file system, which
113
* only has a "readable" and "writeable" flag, applying to all users.
115
access = GENERIC_READ;
116
if (!LF_ISSET(DB_OSO_RDONLY))
117
access |= GENERIC_WRITE;
119
share = FILE_SHARE_READ | FILE_SHARE_WRITE;
120
attr = FILE_ATTRIBUTE_NORMAL;
123
* Reproduce POSIX 1003.1 semantics: if O_CREATE and O_EXCL are both
124
* specified, fail, returning EEXIST, unless we create the file.
126
if (LF_ISSET(DB_OSO_CREATE) && LF_ISSET(DB_OSO_EXCL))
127
createflag = CREATE_NEW; /* create only if !exist*/
128
else if (!LF_ISSET(DB_OSO_CREATE) && LF_ISSET(DB_OSO_TRUNC))
129
createflag = TRUNCATE_EXISTING; /* truncate, fail if !exist */
130
else if (LF_ISSET(DB_OSO_TRUNC))
131
createflag = CREATE_ALWAYS; /* create and truncate */
132
else if (LF_ISSET(DB_OSO_CREATE))
133
createflag = OPEN_ALWAYS; /* open or create */
135
createflag = OPEN_EXISTING; /* open only if existing */
137
if (LF_ISSET(DB_OSO_LOG)) {
138
F_SET(fhp, DB_FH_NOSYNC);
139
attr |= FILE_FLAG_WRITE_THROUGH;
142
if (LF_ISSET(DB_OSO_SEQ))
143
attr |= FILE_FLAG_SEQUENTIAL_SCAN;
145
attr |= FILE_FLAG_RANDOM_ACCESS;
147
if (LF_ISSET(DB_OSO_TEMP))
148
attr |= FILE_FLAG_DELETE_ON_CLOSE;
151
* We can turn filesystem buffering off if the page size is a
152
* multiple of the disk's sector size. To find the sector size,
153
* we call GetDiskFreeSpace, which expects a drive name like "d:\\"
154
* or NULL for the current disk (i.e., a relative path)
156
if (LF_ISSET(DB_OSO_DIRECT) && pagesize != 0 && name[0] != '\0') {
157
if (name[1] == ':') {
159
snprintf(dbuf, sizeof(dbuf), "%c:\\", name[0]);
163
if (GetDiskFreeSpace(drive, NULL, §orsize, NULL, NULL) &&
164
pagesize % sectorsize == 0)
165
attr |= FILE_FLAG_NO_BUFFERING;
168
for (nrepeat = 1;; ++nrepeat) {
170
CreateFile(name, access, share, NULL, createflag, attr, 0);
171
if (fhp->handle == INVALID_HANDLE_VALUE) {
173
* If it's a "temporary" error, we retry up to 3 times,
174
* waiting up to 12 seconds. While it's not a problem
175
* if we can't open a database, an inability to open a
176
* log file is cause for serious dismay.
178
ret = __os_win32_errno();
179
if ((ret != ENFILE && ret != EMFILE && ret != ENOSPC) ||
183
(void)__os_sleep(dbenv, nrepeat * 2, 0);
189
* Special handling needed for log files. To get Windows to not update
190
* the MFT metadata on each write, extend the file to its maximum size.
191
* Windows will allocate all the data blocks and store them in the MFT
192
* (inode) area. In addition, flush the MFT area to disk.
193
* This strategy only works for Win/NT; Win/9X does not
194
* guarantee that the logs will be zero filled.
196
if (LF_ISSET(DB_OSO_LOG) && log_size != 0 && __os_is_winnt()) {
197
if (SetFilePointer(fhp->handle,
198
log_size - 1, NULL, FILE_BEGIN) == (DWORD)-1)
200
if (WriteFile(fhp->handle, "\x00", 1, &bytesWritten, NULL) == 0)
202
if (bytesWritten != 1)
204
if (SetEndOfFile(fhp->handle) == 0)
207
fhp->handle, 0, NULL, FILE_BEGIN) == (DWORD)-1)
209
if (FlushFileBuffers(fhp->handle) == 0)
213
F_SET(fhp, DB_FH_VALID);
217
ret = __os_win32_errno();
218
if (fhp->handle != INVALID_HANDLE_VALUE)
219
(void)CloseHandle(fhp->handle);