summaryrefslogtreecommitdiff
path: root/src/os_common/os_fstream.c
blob: fc0daf1c2117cbe0cce549c01fe743cc7b705462 (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
/*-
 * 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 *fs)
{
	WT_DECL_RET;

	if (!F_ISSET(fs, WT_STREAM_READ))
		WT_TRET(fs->flush(session, fs));

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

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

	return (0);
}

/*
 * __fstream_flush_notsup --
 *	Stream flush unsupported.
 */
static int
__fstream_flush_notsup(WT_SESSION_IMPL *session, WT_FSTREAM *fs)
{
	WT_RET_MSG(session, ENOTSUP, "%s: flush", fs->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 *fs, 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(fs->buf.data, fs->buf.mem) >= fs->buf.size) {
			len = WT_MIN(WT_STREAM_BUFSIZE,
			    (size_t)(fs->size - fs->off));
			if (len == 0)
				break; /* EOF */
			WT_RET(__wt_buf_initsize(session, &fs->buf, len));
			WT_RET(__wt_read(
			    session, fs->fh, fs->off, len, fs->buf.mem));
			fs->off += (wt_off_t)len;
		}

		c = *(p = fs->buf.data);
		fs->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 *fs, WT_ITEM *buf)
{
	WT_UNUSED(buf);
	WT_RET_MSG(session, ENOTSUP, "%s: getline", fs->name);
}

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

	buf = &fs->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;
		len = (size_t)vsnprintf(p, space, fmt, ap_copy);
		va_end(ap_copy);

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

			return (buf->size >= WT_STREAM_BUFSIZE ?
			    __wt_fflush(session, fs) : 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 *fs, const char *fmt, va_list ap)
{
	WT_UNUSED(fmt);
	WT_UNUSED(ap);
	WT_RET_MSG(session, ENOTSUP, "%s: printf", fs->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 **fsp)
{
	WT_DECL_RET;
	WT_FH *fh;
	WT_FSTREAM *fs;

	fs = NULL;

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

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

	fs->close = __fstream_close;
	WT_ERR(__wt_filesize(session, fh, &fs->size));
	if (LF_ISSET(WT_STREAM_APPEND))
		fs->off = fs->size;
	if (LF_ISSET(WT_STREAM_APPEND | WT_STREAM_WRITE)) {
		fs->flush = __fstream_flush;
		fs->getline = __fstream_getline_notsup;
		fs->printf = __fstream_printf;
	} else {
		WT_ASSERT(session, LF_ISSET(WT_STREAM_READ));
		fs->flush = __fstream_flush_notsup;
		fs->getline = __fstream_getline;
		fs->printf = __fstream_printf_notsup;
	}
	*fsp = fs;
	return (0);

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