~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/os_win32/os_open.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1997-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 */
7
 
 
8
 
#include "db_config.h"
9
 
 
10
 
#ifndef lint
11
 
static const char revid[] = "$Id$";
12
 
#endif /* not lint */
13
 
 
14
 
#ifndef NO_SYSTEM_INCLUDES
15
 
#include <sys/types.h>
16
 
 
17
 
#include <fcntl.h>
18
 
#include <signal.h>
19
 
#include <string.h>
20
 
#include <unistd.h>
21
 
#endif
22
 
 
23
 
#include "db_int.h"
24
 
 
25
 
/*
26
 
 * __os_open --
27
 
 *      Open a file descriptor.
28
 
 */
29
 
int
30
 
__os_open(dbenv, name, flags, mode, fhp)
31
 
        DB_ENV *dbenv;
32
 
        const char *name;
33
 
        u_int32_t flags;
34
 
        int mode;
35
 
        DB_FH *fhp;
36
 
{
37
 
        DWORD bytesWritten;
38
 
        u_int32_t log_size, pagesize, sectorsize;
39
 
        int access, attr, oflags, share, createflag;
40
 
        int ret, nrepeat;
41
 
        char *drive, dbuf[4]; /* <letter><colon><slosh><nul> */
42
 
 
43
 
#ifdef DIAGNOSTIC
44
 
#define OKFLAGS                                                         \
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 |     \
47
 
         DB_OSO_TRUNC)
48
 
        if ((ret = __db_fchk(dbenv, "__os_open", flags, OKFLAGS)) != 0)
49
 
                return (ret);
50
 
#endif
51
 
 
52
 
        /*
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.
56
 
         */
57
 
        if (DB_GLOBAL(j_open) != NULL) {
58
 
                oflags = O_BINARY | O_NOINHERIT;
59
 
 
60
 
                if (LF_ISSET(DB_OSO_CREATE))
61
 
                        oflags |= O_CREAT;
62
 
 
63
 
                if (LF_ISSET(DB_OSO_EXCL))
64
 
                        oflags |= O_EXCL;
65
 
 
66
 
                if (LF_ISSET(DB_OSO_RDONLY))
67
 
                        oflags |= O_RDONLY;
68
 
                else
69
 
                        oflags |= O_RDWR;
70
 
 
71
 
                if (LF_ISSET(DB_OSO_SEQ))
72
 
                        oflags |= _O_SEQUENTIAL;
73
 
                else
74
 
                        oflags |= _O_RANDOM;
75
 
 
76
 
                if (LF_ISSET(DB_OSO_TEMP))
77
 
                        oflags |= _O_TEMPORARY;
78
 
 
79
 
                if (LF_ISSET(DB_OSO_TRUNC))
80
 
                        oflags |= O_TRUNC;
81
 
 
82
 
                ret = __os_openhandle(dbenv, name, oflags, mode, fhp);
83
 
                if (ret == 0)
84
 
                        fhp->handle = _get_osfhandle (fhp->fd);
85
 
 
86
 
                return ret;
87
 
        }
88
 
 
89
 
        ret = 0;
90
 
 
91
 
        if (LF_ISSET(DB_OSO_LOG))
92
 
                log_size = fhp->log_size;                       /* XXX: Gag. */
93
 
 
94
 
        pagesize = fhp->pagesize;
95
 
 
96
 
        memset(fhp, 0, sizeof(*fhp));
97
 
        fhp->fd = -1;
98
 
 
99
 
        /*
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.
103
 
         *
104
 
         * !!!
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.
114
 
         */
115
 
        access = GENERIC_READ;
116
 
        if (!LF_ISSET(DB_OSO_RDONLY))
117
 
                access |= GENERIC_WRITE;
118
 
 
119
 
        share = FILE_SHARE_READ | FILE_SHARE_WRITE;
120
 
        attr = FILE_ATTRIBUTE_NORMAL;
121
 
 
122
 
        /*
123
 
         * Reproduce POSIX 1003.1 semantics: if O_CREATE and O_EXCL are both
124
 
         * specified, fail, returning EEXIST, unless we create the file.
125
 
         */
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 */
134
 
        else
135
 
                createflag = OPEN_EXISTING;     /* open only if existing */
136
 
 
137
 
        if (LF_ISSET(DB_OSO_LOG)) {
138
 
                F_SET(fhp, DB_FH_NOSYNC);
139
 
                attr |= FILE_FLAG_WRITE_THROUGH;
140
 
        }
141
 
 
142
 
        if (LF_ISSET(DB_OSO_SEQ))
143
 
                attr |= FILE_FLAG_SEQUENTIAL_SCAN;
144
 
        else
145
 
                attr |= FILE_FLAG_RANDOM_ACCESS;
146
 
 
147
 
        if (LF_ISSET(DB_OSO_TEMP))
148
 
                attr |= FILE_FLAG_DELETE_ON_CLOSE;
149
 
 
150
 
        /*
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)
155
 
         */
156
 
        if (LF_ISSET(DB_OSO_DIRECT) && pagesize != 0 && name[0] != '\0') {
157
 
                if (name[1] == ':') {
158
 
                        drive = dbuf;
159
 
                        snprintf(dbuf, sizeof(dbuf), "%c:\\", name[0]);
160
 
                } else
161
 
                        drive = NULL;
162
 
 
163
 
                if (GetDiskFreeSpace(drive, NULL, &sectorsize, NULL, NULL) &&
164
 
                    pagesize % sectorsize == 0)
165
 
                        attr |= FILE_FLAG_NO_BUFFERING;
166
 
        }
167
 
 
168
 
        for (nrepeat = 1;; ++nrepeat) {
169
 
                fhp->handle =
170
 
                    CreateFile(name, access, share, NULL, createflag, attr, 0);
171
 
                if (fhp->handle == INVALID_HANDLE_VALUE) {
172
 
                        /*
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.
177
 
                         */
178
 
                        ret = __os_win32_errno();
179
 
                        if ((ret != ENFILE && ret != EMFILE && ret != ENOSPC) ||
180
 
                            nrepeat > 3)
181
 
                                goto err;
182
 
 
183
 
                        (void)__os_sleep(dbenv, nrepeat * 2, 0);
184
 
                } else
185
 
                        break;
186
 
        }
187
 
 
188
 
        /*
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.
195
 
         */
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)
199
 
                        goto err;
200
 
                if (WriteFile(fhp->handle, "\x00", 1, &bytesWritten, NULL) == 0)
201
 
                        goto err;
202
 
                if (bytesWritten != 1)
203
 
                        goto err;
204
 
                if (SetEndOfFile(fhp->handle) == 0)
205
 
                        goto err;
206
 
                if (SetFilePointer(
207
 
                    fhp->handle, 0, NULL, FILE_BEGIN) == (DWORD)-1)
208
 
                        goto err;
209
 
                if (FlushFileBuffers(fhp->handle) == 0)
210
 
                        goto err;
211
 
        }
212
 
 
213
 
        F_SET(fhp, DB_FH_VALID);
214
 
        return (0);
215
 
 
216
 
err:    if (ret == 0)
217
 
                ret = __os_win32_errno();
218
 
        if (fhp->handle != INVALID_HANDLE_VALUE)
219
 
                (void)CloseHandle(fhp->handle);
220
 
        return (ret);
221
 
}