summaryrefslogtreecommitdiff
path: root/src/libs/uio/fileblock.c
blob: bb4f466c25b47c916c92c169a018de35837d34bc (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Copyright (C) 2003  Serge van den Boom
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * Nota bene: later versions of the GNU General Public License do not apply
 * to this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#define uio_INTERNAL_FILEBLOCK

#include "iointrn.h"
#include "fileblock.h"
#include "uioport.h"

#include <errno.h>

static uio_FileBlock *uio_FileBlock_new(uio_Handle *handle, int flags,
		off_t offset, size_t blockSize, char *buffer, size_t bufSize,
		off_t bufOffset, size_t bufFill, size_t readAheadBufSize);
static inline uio_FileBlock *uio_FileBlock_alloc(void);
static void uio_FileBlock_free(uio_FileBlock *block);

// caller should uio_Handle_ref(handle) (unless it doesn't need it's own
// reference anymore).
static uio_FileBlock *
uio_FileBlock_new(uio_Handle *handle, int flags, off_t offset,
		size_t blockSize, char *buffer, size_t bufSize, off_t bufOffset,
		size_t bufFill, size_t readAheadBufSize) {
	uio_FileBlock *result;
	
	result = uio_FileBlock_alloc();
	result->handle = handle;
	result->flags = flags;
	result->offset = offset;
	result->blockSize = blockSize;
	result->buffer = buffer;
	result->bufSize = bufSize;
	result->bufOffset = bufOffset;
	result->bufFill = bufFill;
	result->readAheadBufSize = readAheadBufSize;
	return result;
}

static inline uio_FileBlock *
uio_FileBlock_alloc(void) {
	return uio_malloc(sizeof (uio_FileBlock));
}

static inline void
uio_FileBlock_free(uio_FileBlock *block) {
	uio_free(block);
}

uio_FileBlock *
uio_openFileBlock(uio_Handle *handle) {
	// TODO: if mmap support is available, and it is available natively
	//       for the filesystem (make some sort of sysctl for filesystems
	//       to check this?), use mmap.
	//       mmap the entire file if it's small enough.
	//       N.B. Keep in mind streams of which the size is not known in
	//       advance.
	struct stat statBuf;
	if (uio_fstat(handle, &statBuf) == -1) {
		// errno is set
		return NULL;
	}
	uio_Handle_ref(handle);
	return uio_FileBlock_new(handle, 0, 0, statBuf.st_size, NULL, 0, 0, 0, 0);
}

uio_FileBlock *
uio_openFileBlock2(uio_Handle *handle, off_t offset, size_t size) {
	// TODO: mmap (see uio_openFileBlock)

	// TODO: check if offset and size are acceptable.
	//       Need to handle streams of which the size is unknown.
#if 0
	if (uio_stat(handle, &statBuf) == -1) {
		// errno is set
		return NULL;
	}
	if (statBuf.st_size > offset || (statBuf.st_size - offset > size)) {
		// NOT: 'if (statBuf.st_size > offset + size)', to protect
		// against overflow.

	}
#endif
	uio_Handle_ref(handle);
	return uio_FileBlock_new(handle, 0, offset, size, NULL, 0, 0, 0, 0);
}

static inline ssize_t
uio_accessFileBlockMmap(uio_FileBlock *block, off_t offset, size_t length,
		char **buffer) {
	// TODO
	errno = ENOSYS;
	(void) block;
	(void) offset;
	(void) length;
	(void) buffer;
	return -1;
}

static inline ssize_t
uio_accessFileBlockNoMmap(uio_FileBlock *block, off_t offset, size_t length,
		char **buffer) {
	ssize_t numRead;
	off_t start;
	off_t end;
	size_t bufSize;
	char *oldBuffer;
	//size_t oldBufSize;

	// Don't go beyond the end of the block.
	if (offset > (off_t) block->blockSize) {
		*buffer = block->buffer;
		return 0;
	}
	if (length > block->blockSize - offset)
		length = block->blockSize - offset;

	if (block->buffer != NULL) {
		// Check whether the requested data is already in the buffer.
		if (offset >= block->bufOffset &&
				(offset - block->bufOffset) + length < block->bufFill) {
			*buffer = block->buffer + (offset - block->bufOffset);
			return length;
		}
	}

	if (length < block->readAheadBufSize &&
			(block->flags & uio_FB_USAGE_MASK) != 0) {
		// We can buffer more data.
		switch (block->flags & uio_FB_USAGE_MASK) {
			case uio_FB_USAGE_FORWARD:
				// Read extra data after the requested data.
				start = offset;
				end = (block->readAheadBufSize > block->blockSize - offset) ?
						block->blockSize : offset + block->readAheadBufSize;
				break;
			case uio_FB_USAGE_BACKWARD:
				// Read extra data before the requested data.
				end = offset + length;
				start = (end <= (off_t) block->blockSize) ?
						0 : end - block->bufSize;
				break;
			case uio_FB_USAGE_FORWARD | uio_FB_USAGE_BACKWARD: {
				// Read extra data both before and after the requested data.
				off_t extraBefore = (block->readAheadBufSize - length) / 2;
				start = (offset < extraBefore) ? 0 : offset - extraBefore;

				end = (block->readAheadBufSize > block->blockSize - start) ?
						block->blockSize : start + block->readAheadBufSize;
				break;
			}
		}
	} else {
		start = offset;
		end = offset + length;
	}
	bufSize = (length > block->readAheadBufSize) ?
			length : block->readAheadBufSize;

	// Start contains the start index in the block of the data we're going
	// to read.
	// End contains the end index.
	// bufSize contains the size of the buffer. bufSize >= end - start.

	oldBuffer = block->buffer;
	//oldBufSize = block->bufSize;
	if (block->buffer != NULL || block->bufSize != bufSize) {
		// We don't have a buffer, or we have one, but of the wrong size.
		block->buffer = uio_malloc(bufSize);
		block->bufSize = bufSize;
	}

	if (oldBuffer != NULL) {
		// TODO: If we have part of the data still in the old buffer, we
		// can keep that.
		// memmove(...)

		if (oldBuffer != block->buffer)
			uio_free(oldBuffer);
	}
	block->bufFill = 0;
	block->bufOffset = start;

	// TODO: lock handle
	if (uio_lseek(block->handle, block->offset + start, SEEK_SET) ==
			(off_t) -1) {
		// errno is set
		return -1;
	}
	
	numRead = uio_read(block->handle, block->buffer, end - start);
	if (numRead == -1) {
		// errno is set
		// TODO: unlock handle
		return -1;
	}
	// TODO: unlock handle

	block->bufFill = numRead;
	*buffer = block->buffer + (offset - block->bufOffset);
	if (numRead <= (offset - block->bufOffset))
		return 0;
	if ((size_t) numRead >= length)
		return length;
	return numRead - offset;
}

// block remains usable until the next call to uio_accessFileBlock
// with the same block as argument, or until uio_closeFileBlock with
// that block as argument.
// The 'offset' parameter is wrt. the start of the block.
// Requesting access to data beyond the file block is not an error. The
// FileBlock is meant to be used as a replacement of seek() and read(), and
// as with those functions, trying to go beyond the end of a file just
// goes to the end. The return value is the number of bytes in the buffer.
ssize_t
uio_accessFileBlock(uio_FileBlock *block, off_t offset, size_t length,
		char **buffer) {
	if (block->flags & uio_FB_USE_MMAP) {
		return uio_accessFileBlockMmap(block, offset, length, buffer);
	} else {
		return uio_accessFileBlockNoMmap(block, offset, length, buffer);
	}
}

int
uio_copyFileBlock(uio_FileBlock *block, off_t offset, char *buffer,
		size_t length) {
	if (block->flags & uio_FB_USE_MMAP) {
		// TODO
		errno = ENOSYS;
		return -1;
	} else {
		ssize_t numCopied = 0;
		ssize_t readResult;

		// Don't go beyond the end of the block.
		if (offset > (off_t) block->blockSize)
			return 0;
		if (length > block->blockSize - offset)
			length = block->blockSize - offset;
		
		// Check whether (part of) the requested data is already in our
		// own buffer.
		if (block->buffer != NULL && offset >= block->bufOffset
				&& offset < block->bufOffset + (off_t) block->bufFill) {
			size_t toCopy = block->bufFill - offset;
			if (toCopy > length)
				toCopy = length;
			memcpy(buffer, block->buffer + (offset - block->bufOffset),
					toCopy);
			numCopied += toCopy;
			length -= toCopy;
			if (length == 0)
				return numCopied;
			buffer += toCopy;
			offset += toCopy;
		}

		// TODO: lock handle
		if (uio_lseek(block->handle, block->offset + offset, SEEK_SET) ==
				(off_t) -1) {
			// errno is set
			return -1;
		}
		
		readResult = uio_read(block->handle, buffer, length);
		// TODO: unlock handle
		if (readResult == -1) {
			// errno is set
			return -1;
		}
		numCopied += readResult;

		return numCopied;
	}
}

int
uio_closeFileBlock(uio_FileBlock *block) {
	if (block->flags & uio_FB_USE_MMAP) {
#if 0
		if (block->buffer != NULL)
			uio_mmunmap(block->buffer);
#endif
	} else {
		if (block->buffer != NULL)
			uio_free(block->buffer);
	}
	uio_Handle_unref(block->handle);
	uio_FileBlock_free(block);
	return 0;
}

// Usage is the or'ed value of zero or more of uio_FB_USAGE_FORWARD,
// and uio_FB_USAGE_BACKWARD.
void
uio_setFileBlockUsageHint(uio_FileBlock *block, int usage,
		size_t readAheadBufSize) {
	block->flags = (block->flags & ~uio_FB_USAGE_MASK) |
			(usage & uio_FB_USAGE_MASK);
	block->readAheadBufSize = readAheadBufSize;
}

// Call if you want the memory used by the fileblock to be released, but
// still want to use the fileblock later. If you don't need the fileblock,
// call uio_closeFileBlock() instead.
void
uio_clearFileBlockBuffers(uio_FileBlock *block) {
	if (block->buffer != NULL) {
		uio_free(block->buffer);
		block->buffer = NULL;
	}
}