summaryrefslogtreecommitdiff
path: root/src/os_common/os_fstream.c
blob: 744da732d844be7761c8326e4b260f3791059041 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*-
 * Copyright (c) 2014-2016 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/* Buffer size for streamed reads/writes. */
#define	WT_STREAM_BUFSIZE	8192

/*
 * __fstream_close --
 *	Close a stream handle.
 */
static int
__fstream_close(WT_SESSION_IMPL *session, WT_FSTREAM *fstr)
{
	WT_DECL_RET;

	if (!F_ISSET(fstr, WT_STREAM_READ))
		WT_TRET(fstr->fstr_flush(session, fstr));

	WT_TRET(__wt_close(session, &fstr->fh));
	__wt_buf_free(session, &fstr->buf);
	__wt_free(session, fstr);
	return (ret);
}

/*
 * __fstream_flush --
 *	Flush the data from a stream.
 */
static int
__fstream_flush(WT_SESSION_IMPL *session, WT_FSTREAM *fstr)
{
	if (fstr->buf.size > 0) {
		WT_RET(__wt_write(session,
		    fstr->fh, fstr->off, fstr->buf.size, fstr->buf.data));
		fstr->off += (wt_off_t)fstr->buf.size;
		fstr->buf.size = 0;
	}

	return (0);
}

/*
 * __fstream_flush_notsup --
 *	Stream flush unsupported.
 */
static int
__fstream_flush_notsup(WT_SESSION_IMPL *session, WT_FSTREAM *fstr)
{
	WT_RET_MSG(session, ENOTSUP, "%s: flush", fstr->name);
}

/*
 * __fstream_getline --
 *	Get a line from a stream.
 *
 * Implementation of the POSIX getline or BSD fgetln functions (finding the
 * function in a portable way is hard, it's simple enough to write it instead).
 *
 * Note: Unlike the standard getline calls, this function doesn't include the
 * trailing newline character in the returned buffer and discards empty lines
 * (so the caller's EOF marker is a returned line length of 0).
 */
static int
__fstream_getline(WT_SESSION_IMPL *session, WT_FSTREAM *fstr, WT_ITEM *buf)
{
	const char *p;
	size_t len;
	char c;

	/*
	 * We always NUL-terminate the returned string (even if it's empty),
	 * make sure there's buffer space for a trailing NUL in all cases.
	 */
	WT_RET(__wt_buf_init(session, buf, 100));

	for (;;) {
		/* Check if we need to refill the buffer. */
		if (WT_PTRDIFF(fstr->buf.data, fstr->buf.mem) >=
		    fstr->buf.size) {
			len = WT_MIN(WT_STREAM_BUFSIZE,
			    (size_t)(fstr->size - fstr->off));
			if (len == 0)
				break; /* EOF */
			WT_RET(__wt_buf_initsize(session, &fstr->buf, len));
			WT_RET(__wt_read(
			    session, fstr->fh, fstr->off, len, fstr->buf.mem));
			fstr->off += (wt_off_t)len;
		}

		c = *(p = fstr->buf.data);
		fstr->buf.data = ++p;

		/* Leave space for a trailing NUL. */
		WT_RET(__wt_buf_extend(session, buf, buf->size + 2));
		if (c == '\n') {
			if (buf->size == 0)
				continue;
			break;
		}
		((char *)buf->mem)[buf->size++] = c;
	}

	((char *)buf->mem)[buf->size] = '\0';

	return (0);
}

/*
 * __fstream_getline_notsup --
 *	Stream getline unsupported.
 */
static int
__fstream_getline_notsup(
    WT_SESSION_IMPL *session, WT_FSTREAM *fstr, WT_ITEM *buf)
{
	WT_UNUSED(buf);
	WT_RET_MSG(session, ENOTSUP, "%s: getline", fstr->name);
}

/*
 * __fstream_printf --
 *	ANSI C vfprintf.
 */
static int
__fstream_printf(
    WT_SESSION_IMPL *session, WT_FSTREAM *fstr, const char *fmt, va_list ap)
{
	WT_ITEM *buf;
	va_list ap_copy;
	size_t len, space;
	char *p;

	buf = &fstr->buf;

	for (;;) {
		va_copy(ap_copy, ap);
		p = (char *)((uint8_t *)buf->mem + buf->size);
		WT_ASSERT(session, buf->memsize >= buf->size);
		space = buf->memsize - buf->size;
		WT_RET(__wt_vsnprintf_len_set(p, space, &len, fmt, ap_copy));
		va_end(ap_copy);

		if (len < space) {
			buf->size += len;

			return (buf->size >= WT_STREAM_BUFSIZE ?
			    __wt_fflush(session, fstr) : 0);
		}
		WT_RET(__wt_buf_extend(session, buf, buf->size + len + 1));
	}
}

/*
 * __fstream_printf_notsup --
 *	ANSI C vfprintf unsupported.
 */
static int
__fstream_printf_notsup(
    WT_SESSION_IMPL *session, WT_FSTREAM *fstr, const char *fmt, va_list ap)
{
	WT_UNUSED(fmt);
	WT_UNUSED(ap);
	WT_RET_MSG(session, ENOTSUP, "%s: printf", fstr->name);
}

/*
 * __wt_fopen --
 *	Open a stream handle.
 */
int
__wt_fopen(WT_SESSION_IMPL *session,
    const char *name, uint32_t open_flags, uint32_t flags, WT_FSTREAM **fstrp)
{
	WT_DECL_RET;
	WT_FH *fh;
	WT_FSTREAM *fstr;

	*fstrp = NULL;

	fstr = NULL;

	WT_RET(__wt_open(
	    session, name, WT_FS_OPEN_FILE_TYPE_REGULAR, open_flags, &fh));

	WT_ERR(__wt_calloc_one(session, &fstr));
	fstr->fh = fh;
	fstr->name = fh->name;
	fstr->flags = flags;

	fstr->close = __fstream_close;
	WT_ERR(__wt_filesize(session, fh, &fstr->size));
	if (LF_ISSET(WT_STREAM_APPEND))
		fstr->off = fstr->size;
	if (LF_ISSET(WT_STREAM_APPEND | WT_STREAM_WRITE)) {
		fstr->fstr_flush = __fstream_flush;
		fstr->fstr_getline = __fstream_getline_notsup;
		fstr->fstr_printf = __fstream_printf;
	} else {
		WT_ASSERT(session, LF_ISSET(WT_STREAM_READ));
		fstr->fstr_flush = __fstream_flush_notsup;
		fstr->fstr_getline = __fstream_getline;
		fstr->fstr_printf = __fstream_printf_notsup;
	}
	*fstrp = fstr;
	return (0);

err:	WT_TRET(__wt_close(session, &fh));
	__wt_free(session, fstr);
	return (ret);
}