1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2008-2011 WiredTiger, Inc.
* All rights reserved.
*/
#include "wt_internal.h"
/*
* __wt_open --
* Open a file handle.
*/
int
__wt_open(WT_SESSION_IMPL *session,
const char *name, mode_t mode, int ok_create, WT_FH **fhp)
{
const char *path;
WT_CONNECTION_IMPL *conn;
WT_FH *fh;
int f, fd, matched, ret;
conn = S2C(session);
fh = NULL;
fd = -1;
ret = 0;
WT_VERBOSE(session, FILEOPS, "fileops: %s: open", name);
/* Increment the reference count if we already have the file open. */
matched = 0;
__wt_lock(session, conn->mtx);
TAILQ_FOREACH(fh, &conn->fhqh, q) {
if (strcmp(name, fh->name) == 0) {
++fh->refcnt;
*fhp = fh;
matched = 1;
break;
}
}
__wt_unlock(session, conn->mtx);
if (matched)
return (0);
WT_RET(__wt_filename(session, name, &path));
f = O_RDWR;
#ifdef O_BINARY
/* Windows clones: we always want to treat the file as a binary. */
f |= O_BINARY;
#endif
if (ok_create)
f |= O_CREAT;
for (;;) {
if ((fd = open(path, f, mode)) != -1)
break;
switch (errno) {
case EAGAIN:
case EBUSY:
case EINTR:
case EMFILE:
case ENFILE:
case ENOSPC:
__wt_sleep(1L, 0L);
break;
default:
__wt_err(session, errno, "%s", name);
WT_ERR(errno);
}
}
WT_ERR(__wt_calloc(session, 1, sizeof(WT_FH), &fh));
WT_ERR(__wt_strdup(session, name, &fh->name));
#if defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
/*
* Security:
* The application may spawn a new process, and we don't want another
* process to have access to our file handles. There's an obvious
* race here...
*/
if ((f = fcntl(fd, F_GETFD)) == -1 ||
fcntl(fd, F_SETFD, f | FD_CLOEXEC) == -1) {
__wt_err(session, errno, "%s: fcntl", name);
WT_ERR(errno);
}
#endif
fh->fd = fd;
fh->refcnt = 1;
/* Set the file's size. */
WT_ERR(__wt_filesize(session, fh, &fh->file_size));
/* Link onto the environment's list of files. */
__wt_lock(session, conn->mtx);
TAILQ_INSERT_TAIL(&conn->fhqh, fh, q);
__wt_unlock(session, conn->mtx);
*fhp = fh;
if (0) {
err: if (fh != NULL) {
__wt_free(session, fh->name);
__wt_free(session, fh);
}
if (fd != -1)
(void)close(fd);
}
__wt_free(session, path);
return (ret);
}
/*
* __wt_close --
* Close a file handle.
*/
int
__wt_close(WT_SESSION_IMPL *session, WT_FH *fh)
{
WT_CONNECTION_IMPL *conn;
int ret;
conn = S2C(session);
ret = 0;
if (fh == NULL || fh->refcnt == 0 || --fh->refcnt > 0)
return (0);
/* Remove from the list and discard the memory. */
__wt_lock(session, conn->mtx);
TAILQ_REMOVE(&conn->fhqh, fh, q);
__wt_unlock(session, conn->mtx);
if (close(fh->fd) != 0) {
__wt_err(session, errno, "%s", fh->name);
ret = WT_ERROR;
}
__wt_free(session, fh->name);
__wt_free(session, fh);
return (ret);
}
|