summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/buf.c
blob: 2ae596a84bebda2adf524579036b36c97d6c3f53 (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
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
497
/*	$OpenBSD: buf.c,v 1.10 2005/05/26 21:46:36 jfb Exp $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/stat.h>

#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include "buf.h"
#include "cvs.h"
#include "log.h"


#define BUF_INCR   128


struct cvs_buf {
	u_int    cb_flags;

	/* buffer handle and size */
	u_char  *cb_buf;
	size_t   cb_size;

	/* start and length of valid data in buffer */
	u_char  *cb_cur;
	size_t   cb_len;
};



#define SIZE_LEFT(b)  ((size_t)(b->cb_buf - b->cb_cur) + b->cb_size)


static ssize_t   cvs_buf_grow (BUF *, size_t);



/*
 * cvs_buf_alloc()
 *
 * Create a new buffer structure and return a pointer to it.  This structure
 * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
 * once the buffer is no longer needed.
 */
BUF*
cvs_buf_alloc(size_t len, u_int flags)
{
	BUF *b;

	b = (BUF *)malloc(sizeof(*b));
	if (b == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate buffer");
		return (NULL);
	}

	b->cb_buf = malloc(len);
	if (b->cb_buf == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate buffer");
		free(b);
		return (NULL);
	}
	memset(b->cb_buf, 0, len);

	b->cb_flags = flags;
	b->cb_size = len;
	b->cb_cur = b->cb_buf;
	b->cb_len = 0;

	return (b);
}


/*
 * cvs_buf_load()
 *
 * Open the file specified by <path> and load all of its contents into a
 * buffer.
 * Returns the loaded buffer on success, or NULL on failure.
 */
BUF*
cvs_buf_load(const char *path, u_int flags)
{
	int fd;
	ssize_t ret;
	size_t len;
	u_char *bp;
	struct stat st;
	BUF *buf;

	fd = open(path, O_RDONLY, 0600);
	if (fd == -1) {
		cvs_log(LP_ERRNO, "failed to open buffer source");
		return (NULL);
	}

	if (fstat(fd, &st) == -1) {
		cvs_log(LP_ERRNO, "failed to stat buffer source");
		(void)close(fd);
		return (NULL);
	}

	buf = cvs_buf_alloc((size_t)st.st_size, flags);
	if (buf == NULL) {
		(void)close(fd);
		return (NULL);
	}

	for (bp = buf->cb_cur; ; bp += (size_t)ret) {
		len = MIN(SIZE_LEFT(buf), 4096);
		ret = read(fd, bp, len);
		if (ret == -1) {
			cvs_log(LP_ERRNO, "read failed from buffer source");
			(void)close(fd);
			cvs_buf_free(buf);
			return (NULL);
		} else if (ret == 0)
			break;

		buf->cb_len += (size_t)ret;
	}

	(void)close(fd);

	return (buf);
}


/*
 * cvs_buf_free()
 *
 * Free the buffer <b> and all associated data.
 */
void
cvs_buf_free(BUF *b)
{
	free(b->cb_buf);
	free(b);
}


/*
 * cvs_buf_release()
 *
 * Free the buffer <b>'s structural information but do not free the contents
 * of the buffer.  Instead, they are returned and should be freed later using
 * free().
 */
void*
cvs_buf_release(BUF *b)
{
	u_char *tmp;

	tmp = b->cb_buf;
	free(b);
	return (tmp);
}


/*
 * cvs_buf_empty()
 *
 * Empty the contents of the buffer <b> and reset pointers.
 */
void
cvs_buf_empty(BUF *b)
{
	b->cb_cur = b->cb_buf;
	b->cb_len = 0;
}


/*
 * cvs_buf_copy()
 *
 * Copy the first <len> bytes of data in the buffer <b> starting at offset
 * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
 * Returns the number of bytes successfully copied, or -1 on failure.
 */
ssize_t
cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
{
	size_t rc;

	if (off > b->cb_len)
		return (-1);

	rc = MIN(len, (b->cb_len - off));
	memcpy(dst, b->cb_buf, rc);

	return (ssize_t)rc;
}


/*
 * cvs_buf_set()
 *
 * Set the contents of the buffer <b> to the first <len> bytes of data found
 * at <src>.  If the buffer was not created with BUF_AUTOEXT, as many bytes
 * as possible will be copied in the buffer.
 */
int
cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
{
	size_t rlen;

	if (b->cb_size < (len + off)) {
		if ((b->cb_flags & BUF_AUTOEXT) && (cvs_buf_grow(b,
		    len + off - b->cb_size) < 0))
			return (-1);
		else
			rlen = b->cb_size - off;
	} else
		rlen = len;

	memcpy((b->cb_buf + off), src, rlen);

	if (b->cb_len == 0) {
		b->cb_cur = b->cb_buf + off;
		b->cb_len = rlen;
	}

	return (int)rlen;
}


/*
 * cvs_buf_putc()
 *
 * Append a single character <c> to the end of the buffer <b>.
 * Returns 0 on success, or -1 on failure.
 */
int
cvs_buf_putc(BUF *b, int c)
{
	u_char *bp;

	bp = b->cb_cur + b->cb_len;
	if (bp == (b->cb_buf + b->cb_size)) {
		/* extend */
		if (!(b->cb_flags & BUF_AUTOEXT) ||
		    (cvs_buf_grow(b, BUF_INCR) < 0))
			return (-1);

		/* the buffer might have been moved */
		bp = b->cb_cur + b->cb_len;
	}
	*bp = (u_char)c;
	b->cb_len++;

	return (0);
}


/*
 * cvs_buf_append()
 *
 * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
 * buffer is too small to accept all data, it will attempt to append as much
 * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
 * will get resized to an appropriate size to accept all data.
 * Returns the number of bytes successfully appended to the buffer, or -1
 * on failure.
 */
ssize_t
cvs_buf_append(BUF *b, const void *data, size_t len)
{
	size_t left, rlen;
	u_char *bp, *bep;

	bp = b->cb_cur + b->cb_len;
	bep = b->cb_buf + b->cb_size;
	left = bep - bp;
	rlen = len;

	if (left < len) {
		if (b->cb_flags & BUF_AUTOEXT) {
			if (cvs_buf_grow(b, len - left) < 0)
				return (-1);
			bp = b->cb_cur + b->cb_len;
		} else
			rlen = bep - bp;
	}

	memcpy(bp, data, rlen);
	b->cb_len += rlen;

	return (rlen);
}


/*
 * cvs_buf_fappend()
 *
 */
int
cvs_buf_fappend(BUF *b, const char *fmt, ...)
{
	int ret;
	char *str;
	va_list vap;

	va_start(vap, fmt);
	ret = vasprintf(&str, fmt, vap);
	va_end(vap);

	if (ret == -1) {
		cvs_log(LP_ERRNO, "failed to format data");
		return (-1);
	}

	ret = cvs_buf_append(b, str, ret);
	free(str);
	return (ret);
}


/*
 * cvs_buf_size()
 *
 * Returns the size of the buffer that is being used.
 */
size_t
cvs_buf_size(BUF *b)
{
	return (b->cb_len);
}


/*
 * cvs_buf_peek()
 *
 * Peek at the contents of the buffer <b> at offset <off>.
 */
const void*
cvs_buf_peek(BUF *b, size_t off)
{
	if (off >= b->cb_len)
		return (NULL);

	return (b->cb_buf + off);
}


/*
 * cvs_buf_write_fd()
 *
 * Write the contents of the buffer <b> to the specified <fd>
 */
int
cvs_buf_write_fd(BUF *b, int fd)
{
	u_char *bp;
	size_t len;
	ssize_t ret;

	len = b->cb_len;
	bp = b->cb_cur;

	do {
		ret = write(fd, bp, MIN(len, 8192));
		if (ret == -1) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
			return (-1);
		}

		len -= (size_t)ret;
		bp += (size_t)ret;
	} while (len > 0);

	return (0);
}

/*
 * cvs_buf_write()
 *
 * Write the contents of the buffer <b> to the file whose path is given in
 * <path>.  If the file does not exist, it is created with mode <mode>.
 */

int
cvs_buf_write(BUF *b, const char *path, mode_t mode)
{
	int ret, fd;

	ret = 0;
	
	fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
	if (fd == -1) {
		cvs_log(LP_ERRNO, "failed to open file `%s'", path); 
		return (-1);
	}

	if (!cvs_noexec)
		ret = cvs_buf_write_fd(b, fd);

	if (ret == -1) {
		cvs_log(LP_ERRNO, "failed to write to file `%s'",  path);
		(void)unlink(path);
	}
	(void)close(fd);

	return (ret);
}

/*
 * cvs_buf_write_stmp()
 *
 * Write the contents of the buffer <b> to a temporary file whose path is 
 * specified using <template> (see mkstemp.3). NB. This function will modify
 * <template>, as per mkstemp
 */

int
cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
{
	int ret, fd;

	ret = 0;

	fd = mkstemp(template);
	if (fd == -1) {
		cvs_log(LP_ERRNO, "failed to mkstemp file `%s': %s", 
		    template, strerror(errno));
		return (-1);
	}

	if (!cvs_noexec)
		ret = cvs_buf_write_fd(b, fd);

	if (ret == -1) {
		cvs_log(LP_ERRNO, "failed to write to temp file `%s': %s", 
		    template, strerror(errno));
		(void)unlink(template);
	}
	(void)close(fd);

	return (ret);
}

/*
 * cvs_buf_grow()
 *
 * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
 * operation regardless of the result.
 * Returns the new size on success, or -1 on failure.
 */
static ssize_t
cvs_buf_grow(BUF *b, size_t len)
{
	void *tmp;
	size_t diff;

	diff = b->cb_cur - b->cb_buf;
	tmp = realloc(b->cb_buf, b->cb_size + len);
	if (tmp == NULL) {
		cvs_log(LP_ERRNO, "failed to grow buffer");
		return (-1);
	}
	b->cb_buf = (u_char *)tmp;
	b->cb_size += len;

	/* readjust pointers in case the buffer moved in memory */
	b->cb_cur = b->cb_buf + diff;

	return (ssize_t)b->cb_size;
}