File: vtestream-file.h

package info (click to toggle)
vte2.91 0.38.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,340 kB
  • ctags: 4,751
  • sloc: ansic: 28,687; sh: 11,929; makefile: 593; xml: 120
file content (496 lines) | stat: -rw-r--r-- 9,636 bytes parent folder | download | duplicates (2)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * Copyright (C) 2009,2010 Red Hat, Inc.
 * Copyright (C) 2013 Google, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Red Hat Author(s): Behdad Esfahbod
 * Google Author(s): Behdad Esfahbod
 */

#include <errno.h>

#include "vteutils.h"


/*
 * We provide two file implementations: one based on raw
 * syscalls, and one based on stdio.  The syscall one has
 * got more than enough testing in the past.  The stdio
 * is newer, but has a default cache, which greatly reduces
 * number of syscalls.
 *
 * Change the following conditional to 0 for syscalls, and
 * to 1 for stdio.  Please test both branches before
 * making changes to _file_t semantics or implementation.
 */

#if 0


/*
 * File implementation using Unix syscalls.
 */

#include <unistd.h>

#ifndef HAVE_PREAD
#define pread _pread
static inline gsize
pread (int fd, char *data, gsize len, gsize offset)
{
	if (-1 == lseek (fd, offset, SEEK_SET))
		return -1;
	return read (fd, data, len);
}
#endif

#ifndef HAVE_PWRITE
#define pwrite _pwrite
static inline gsize
pwrite (int fd, char *data, gsize len, gsize offset)
{
	if (-1 == lseek (fd, offset, SEEK_SET))
		return -1;
	return write (fd, data, len);
}
#endif


typedef struct
{
	int fd;
} _file_t;

static inline void
_file_init (_file_t *f)
{
	f->fd = -1;
}

static inline void
_file_open (_file_t *f, int fd)
{
	f->fd = fd;
}

static inline gboolean
_file_isopen (_file_t *f)
{
	return f->fd != -1;
}

static inline void
_file_close (_file_t *f)
{
	if (G_UNLIKELY (!_file_isopen (f)))
		return;

	close (f->fd);
}


static gboolean
_file_try_truncate (_file_t *f, gsize offset)
{
	int ret;

	if (G_UNLIKELY (!_file_isopen (f)))
		return;

	do {
		ret = ftruncate (f->fd, offset);
	} while (ret == -1 && errno == EINTR);

	return !ret;
}

static void
_file_reset (_file_t *f)
{
	_file_try_truncate (f, 0);
}

static gsize
_file_read (_file_t *f, char *data, gsize len, gsize offset)
{
	gsize ret, total = 0;

	if (G_UNLIKELY (!_file_isopen (f)))
		return 0;

	while (len) {
		ret = pread (f->fd, data, len, offset);
		if (G_UNLIKELY (ret == (gsize) -1)) {
			if (errno == EINTR)
				continue;
			else
				break;
		}
		if (G_UNLIKELY (ret == 0))
			break;
		data += ret;
		len -= ret;
		offset += ret;
		total += ret;
	}
	return total;
}

static void
_file_write (_file_t *f, const char *data, gsize len, gsize offset)
{
	gsize ret;
	gboolean truncated = FALSE;

	g_assert (_file_isopen (f) || !len);

	while (len) {
		ret = pwrite (f->fd, data, len, offset);
		if (G_UNLIKELY (ret == (gsize) -1)) {
			if (errno == EINTR)
				continue;
			else if (errno == EINVAL && !truncated)
			{
				/* Perhaps previous writes failed and now we are
				 * seeking past end of file.  Try extending it
				 * and retry.  This allows recovering from a
				 * "/tmp is full" error.
				 */
				_file_try_truncate (f, offset);
				truncated = TRUE;
				continue;
			}
			else
				break;
		}
		if (G_UNLIKELY (ret == 0))
			break;
		data += ret;
		len -= ret;
		offset += ret;
	}
}


#else


/*
 * File implementation using stdio.
 */

#include <stdio.h>

/* Use unlocked versions if available. */
#ifdef HAVE_FWRITE_UNLOCKED
#	define fwrite	fwrite_unlocked
#	define fread	fread_unlocked
#	define fflush	fflush_unlocked
#endif

typedef struct
{
	FILE *fp;
	gsize pos;
} _file_t;

static inline void
_file_init (_file_t *f)
{
	f->fp = NULL;
	f->pos = -1;
}

static inline void
_file_open (_file_t *f, int fd)
{
	f->fp = fdopen (fd, "w+");
	f->pos = 0;
}

static inline gboolean
_file_isopen (_file_t *f)
{
	return f->fp != NULL;
}

static inline void
_file_close (_file_t *f)
{
	if (G_UNLIKELY (!_file_isopen (f)))
		return;

	fclose (f->fp);
}


static gboolean
_file_try_truncate (_file_t *f, gsize offset)
{
	if (G_UNLIKELY (!_file_isopen (f)))
		return TRUE;

	/* We don't have to truncate.  The bytestream works
	 * without a working truncate (hence the "try").
	 * Here's a fallback implementation.  Things should
	 * work with or without this.
	 */
	if (1)
	{
		int ret;

		fflush (f->fp);

		do {
			ret = ftruncate (fileno (f->fp), offset);
		} while (ret == -1 && errno == EINTR);

		return !ret;
	}

	return FALSE;
}

static void
_file_reset (_file_t *f)
{
	int fd;

	if (_file_try_truncate (f, 0))
		return;

	if (G_UNLIKELY (!_file_isopen (f)))
		return;

	/* Reopen file to truncate it. */

	fd = dup (fileno (f->fp));

	_file_close (f);
	_file_open (f, fd);
}

static gboolean
_file_seek (_file_t *f, gsize offset)
{
	if (f->pos != offset)
	{
		if (-1 == fseek (f->fp, offset, SEEK_SET))
			return FALSE;
		f->pos = offset;
	}
	return TRUE;
}

static gsize
_file_read (_file_t *f, char *data, gsize len, gsize offset)
{
	gsize ret;

	if (G_UNLIKELY (!_file_isopen (f)))
		return 0;

	if (!_file_seek (f, offset))
		return 0;

	ret = fread (data, 1, len, f->fp);
	f->pos += ret;
	return ret;
}

static void
_file_write (_file_t *f, const char *data, gsize len, gsize offset)
{
	gsize ret;

	g_assert (_file_isopen (f) || !len);

	if (!_file_seek (f, offset))
		return;

	ret = fwrite (data, 1, len, f->fp);
	f->pos += ret;
}


#endif


/*
 * VteFileStream: A file-based stream
 */

typedef struct _VteFileStream {
	VteStream parent;

	/* The first file/offset is for the write head, second is for last page */
	_file_t file[2];
	gsize offset[2];
	gsize head;
} VteFileStream;

typedef VteStreamClass VteFileStreamClass;

static GType _vte_file_stream_get_type (void);
#define VTE_TYPE_FILE_STREAM _vte_file_stream_get_type ()

G_DEFINE_TYPE (VteFileStream, _vte_file_stream, VTE_TYPE_STREAM)

static void
_vte_file_stream_init (VteFileStream *stream)
{
	_file_init (&stream->file[0]);
	_file_init (&stream->file[1]);
}

VteStream *
_vte_file_stream_new (void)
{
	return (VteStream *) g_object_new (VTE_TYPE_FILE_STREAM, NULL);
}

static void
_vte_file_stream_finalize (GObject *object)
{
	VteFileStream *stream = (VteFileStream *) object;

	_file_close (&stream->file[0]);
	_file_close (&stream->file[1]);

	G_OBJECT_CLASS (_vte_file_stream_parent_class)->finalize(object);
}

static inline void
_vte_file_stream_ensure_file0 (VteFileStream *stream)
{
	int fd;

	if (G_LIKELY (_file_isopen (&stream->file[0])))
		return;

        fd = _vte_mkstemp ();
        if (fd == -1)
                return;

        _file_open (&stream->file[0], fd);
}

static void
_vte_file_stream_reset (VteStream *astream, gsize offset)
{
	VteFileStream *stream = (VteFileStream *) astream;

	_file_reset (&stream->file[0]);
	_file_reset (&stream->file[1]);

	stream->head = stream->offset[0] = stream->offset[1] = offset;
}

static void
_vte_file_stream_append (VteStream *astream, const char *data, gsize len)
{
	VteFileStream *stream = (VteFileStream *) astream;

	_vte_file_stream_ensure_file0 (stream);

	_file_write (&stream->file[0], data, len, stream->head - stream->offset[0]);
	stream->head += len;
}

static gboolean
_vte_file_stream_read (VteStream *astream, gsize offset, char *data, gsize len)
{
	VteFileStream *stream = (VteFileStream *) astream;
	gsize l;

	if (G_UNLIKELY (offset < stream->offset[1]))
		return FALSE;

	if (offset < stream->offset[0]) {
		l = _file_read (&stream->file[1], data, len, offset - stream->offset[1]);
		offset += l; data += l; len -= l; if (!len) return TRUE;
	}

	l = _file_read (&stream->file[0], data, len, offset - stream->offset[0]);
	offset += l; data += l; len -= l; if (!len) return TRUE;

	return FALSE;
}

static void
_vte_file_stream_swap_fds (VteFileStream *stream)
{
	_file_t f;

	f = stream->file[0]; stream->file[0] = stream->file[1]; stream->file[1] = f;
}

static void
_vte_file_stream_truncate (VteStream *astream, gsize offset)
{
	VteFileStream *stream = (VteFileStream *) astream;

	if (G_UNLIKELY (offset < stream->offset[1])) {
		_file_reset (&stream->file[1]);
		stream->offset[1] = offset;
	}

	if (G_UNLIKELY (offset < stream->offset[0])) {
		_file_reset (&stream->file[0]);
		stream->offset[0] = stream->offset[1];
		_vte_file_stream_swap_fds (stream);
	} else {
		_file_try_truncate (&stream->file[0], offset - stream->offset[0]);
	}

	stream->head = offset;
}

static void
_vte_file_stream_advance_tail (VteStream *astream, gsize offset)
{
	VteFileStream *stream = (VteFileStream *) astream;

	if (offset >= stream->offset[0]) {
		stream->offset[1] = stream->offset[0];
		stream->offset[0] = stream->head;
		_vte_file_stream_swap_fds (stream);
		_file_reset (&stream->file[0]);
	}
}

static gsize
_vte_file_stream_head (VteStream *astream)
{
	VteFileStream *stream = (VteFileStream *) astream;

	return stream->head;
}

static void
_vte_file_stream_class_init (VteFileStreamClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	gobject_class->finalize = _vte_file_stream_finalize;

	klass->reset = _vte_file_stream_reset;
	klass->append = _vte_file_stream_append;
	klass->read = _vte_file_stream_read;
	klass->truncate = _vte_file_stream_truncate;
	klass->advance_tail = _vte_file_stream_advance_tail;
	klass->head = _vte_file_stream_head;
}