Annotation of src/usr.bin/rcs/buf.c, Revision 1.7
1.7 ! ray 1: /* $OpenBSD: buf.c,v 1.6 2006/07/08 09:25:44 ray Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include "includes.h"
28:
29: #include "buf.h"
30: #include "xmalloc.h"
31: #include "worklist.h"
32:
33: #define BUF_INCR 128
34:
35: struct rcs_buf {
36: u_int cb_flags;
37:
38: /* buffer handle and size */
39: u_char *cb_buf;
40: size_t cb_size;
41:
42: /* start and length of valid data in buffer */
43: u_char *cb_cur;
44: size_t cb_len;
45: };
46:
47: #define SIZE_LEFT(b) (b->cb_size - (size_t)(b->cb_cur - b->cb_buf) \
48: - b->cb_len)
49:
50: static void rcs_buf_grow(BUF *, size_t);
51:
52: /*
53: * rcs_buf_alloc()
54: *
55: * Create a new buffer structure and return a pointer to it. This structure
56: * uses dynamically-allocated memory and must be freed with rcs_buf_free(),
57: * once the buffer is no longer needed.
58: */
59: BUF *
60: rcs_buf_alloc(size_t len, u_int flags)
61: {
62: BUF *b;
63:
64: b = xmalloc(sizeof(*b));
65: /* Postpone creation of zero-sized buffers */
66: if (len > 0)
67: b->cb_buf = xcalloc(1, len);
68: else
69: b->cb_buf = NULL;
70:
71: b->cb_flags = flags;
72: b->cb_size = len;
73: b->cb_cur = b->cb_buf;
74: b->cb_len = 0;
75:
76: return (b);
77: }
78:
79: /*
80: * rcs_buf_load()
81: *
82: * Open the file specified by <path> and load all of its contents into a
83: * buffer.
84: * Returns the loaded buffer on success.
85: */
86: BUF *
87: rcs_buf_load(const char *path, u_int flags)
88: {
89: int fd;
90: ssize_t ret;
91: size_t len;
92: u_char *bp;
93: struct stat st;
94: BUF *buf;
95:
96: if ((fd = open(path, O_RDONLY, 0600)) == -1) {
97: warn("%s", path);
98: return (NULL);
99: }
100:
101: if (fstat(fd, &st) == -1)
1.3 xsa 102: err(1, "%s", path);
1.1 joris 103:
104: buf = rcs_buf_alloc((size_t)st.st_size, flags);
105: for (bp = buf->cb_cur; ; bp += (size_t)ret) {
106: len = SIZE_LEFT(buf);
107: ret = read(fd, bp, len);
108: if (ret == -1) {
109: rcs_buf_free(buf);
1.3 xsa 110: err(1, "rcs_buf_load");
1.1 joris 111: } else if (ret == 0)
112: break;
113:
114: buf->cb_len += (size_t)ret;
115: }
116:
117: (void)close(fd);
118:
119: return (buf);
120: }
121:
122: /*
123: * rcs_buf_free()
124: *
125: * Free the buffer <b> and all associated data.
126: */
127: void
128: rcs_buf_free(BUF *b)
129: {
130: if (b->cb_buf != NULL)
131: xfree(b->cb_buf);
132: xfree(b);
133: }
134:
135: /*
136: * rcs_buf_release()
137: *
138: * Free the buffer <b>'s structural information but do not free the contents
139: * of the buffer. Instead, they are returned and should be freed later using
140: * free().
141: */
142: void *
143: rcs_buf_release(BUF *b)
144: {
1.4 niallo 145: void *tmp;
1.1 joris 146:
147: tmp = b->cb_buf;
148: xfree(b);
149: return (tmp);
1.5 niallo 150: }
151:
152: /*
153: * rcs_buf_get()
154: */
155: void *
156: rcs_buf_get(BUF *b)
157: {
158: return (b->cb_buf);
1.1 joris 159: }
160:
161: /*
162: * rcs_buf_empty()
163: *
164: * Empty the contents of the buffer <b> and reset pointers.
165: */
166: void
167: rcs_buf_empty(BUF *b)
168: {
169: memset(b->cb_buf, 0, b->cb_size);
170: b->cb_cur = b->cb_buf;
171: b->cb_len = 0;
172: }
173:
174: /*
175: * rcs_buf_set()
176: *
177: * Set the contents of the buffer <b> at offset <off> to the first <len>
178: * bytes of data found at <src>. If the buffer was not created with
179: * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
180: */
1.7 ! ray 181: size_t
1.1 joris 182: rcs_buf_set(BUF *b, const void *src, size_t len, size_t off)
183: {
184: size_t rlen = 0;
185:
186: if (b->cb_size < (len + off)) {
187: if ((b->cb_flags & BUF_AUTOEXT)) {
188: rcs_buf_grow(b, len + off - b->cb_size);
189: rlen = len + off;
190: } else {
191: rlen = b->cb_size - off;
192: }
193: } else {
194: rlen = len;
195: }
196:
197: b->cb_len = rlen;
198: memcpy((b->cb_buf + off), src, rlen);
199:
200: if (b->cb_len == 0) {
201: b->cb_cur = b->cb_buf + off;
202: b->cb_len = rlen;
203: }
204:
205: return (rlen);
206: }
207:
208: /*
209: * rcs_buf_putc()
210: *
211: * Append a single character <c> to the end of the buffer <b>.
212: */
213: void
214: rcs_buf_putc(BUF *b, int c)
215: {
216: u_char *bp;
217:
218: bp = b->cb_cur + b->cb_len;
219: if (bp == (b->cb_buf + b->cb_size)) {
220: /* extend */
221: if (b->cb_flags & BUF_AUTOEXT)
222: rcs_buf_grow(b, (size_t)BUF_INCR);
223: else
224: errx(1, "rcs_buf_putc failed");
225:
226: /* the buffer might have been moved */
227: bp = b->cb_cur + b->cb_len;
228: }
229: *bp = (u_char)c;
230: b->cb_len++;
231: }
232:
233: /*
234: * rcs_buf_getc()
235: *
236: * Return u_char at buffer position <pos>.
237: *
238: */
239: u_char
240: rcs_buf_getc(BUF *b, size_t pos)
241: {
242: return (b->cb_cur[pos]);
243: }
244:
245: /*
246: * rcs_buf_append()
247: *
248: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
249: * buffer is too small to accept all data, it will attempt to append as much
250: * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
251: * will get resized to an appropriate size to accept all data.
252: * Returns the number of bytes successfully appended to the buffer.
253: */
1.7 ! ray 254: size_t
1.1 joris 255: rcs_buf_append(BUF *b, const void *data, size_t len)
256: {
257: size_t left, rlen;
258: u_char *bp, *bep;
259:
260: bp = b->cb_cur + b->cb_len;
261: bep = b->cb_buf + b->cb_size;
262: left = bep - bp;
263: rlen = len;
264:
265: if (left < len) {
266: if (b->cb_flags & BUF_AUTOEXT) {
267: rcs_buf_grow(b, len - left);
268: bp = b->cb_cur + b->cb_len;
269: } else
270: rlen = bep - bp;
271: }
272:
273: memcpy(bp, data, rlen);
274: b->cb_len += rlen;
275:
276: return (rlen);
277: }
278:
279: /*
280: * rcs_buf_fappend()
281: *
282: */
1.7 ! ray 283: size_t
1.1 joris 284: rcs_buf_fappend(BUF *b, const char *fmt, ...)
285: {
1.7 ! ray 286: size_t ret;
! 287: int n;
1.1 joris 288: char *str;
289: va_list vap;
290:
291: va_start(vap, fmt);
1.7 ! ray 292: n = vasprintf(&str, fmt, vap);
1.1 joris 293: va_end(vap);
294:
1.7 ! ray 295: if (n == -1)
1.1 joris 296: errx(1, "rcs_buf_fappend: failed to format data");
297:
1.7 ! ray 298: ret = rcs_buf_append(b, str, n);
1.1 joris 299: xfree(str);
300: return (ret);
301: }
302:
303: /*
304: * rcs_buf_len()
305: *
306: * Returns the size of the buffer that is being used.
307: */
308: size_t
309: rcs_buf_len(BUF *b)
310: {
311: return (b->cb_len);
312: }
313:
314: /*
315: * rcs_buf_write_fd()
316: *
317: * Write the contents of the buffer <b> to the specified <fd>
318: */
319: int
320: rcs_buf_write_fd(BUF *b, int fd)
321: {
322: u_char *bp;
323: size_t len;
324: ssize_t ret;
325:
326: len = b->cb_len;
327: bp = b->cb_cur;
328:
329: do {
330: ret = write(fd, bp, len);
331: if (ret == -1) {
332: if (errno == EINTR || errno == EAGAIN)
333: continue;
334: return (-1);
335: }
336:
337: len -= (size_t)ret;
338: bp += (size_t)ret;
339: } while (len > 0);
340:
341: return (0);
342: }
343:
344: /*
345: * rcs_buf_write()
346: *
347: * Write the contents of the buffer <b> to the file whose path is given in
348: * <path>. If the file does not exist, it is created with mode <mode>.
349: */
350: int
351: rcs_buf_write(BUF *b, const char *path, mode_t mode)
352: {
353: int fd;
354: open:
355: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
356: if (errno == EACCES && unlink(path) != -1)
357: goto open;
358: else
1.3 xsa 359: err(1, "%s", path);
1.1 joris 360: }
361:
362: if (rcs_buf_write_fd(b, fd) == -1) {
363: (void)unlink(path);
364: errx(1, "rcs_buf_write: rcs_buf_write_fd: `%s'", path);
365: }
366:
367: if (fchmod(fd, mode) < 0)
368: warn("permissions not set on file %s", path);
369:
370: (void)close(fd);
371:
372: return (0);
373: }
374:
375: /*
376: * rcs_buf_write_stmp()
377: *
378: * Write the contents of the buffer <b> to a temporary file whose path is
379: * specified using <template> (see mkstemp.3). NB. This function will modify
380: * <template>, as per mkstemp
381: */
382: void
1.6 ray 383: rcs_buf_write_stmp(BUF *b, char *template)
1.1 joris 384: {
385: int fd;
386:
387: if ((fd = mkstemp(template)) == -1)
1.3 xsa 388: err(1, "%s", template);
1.1 joris 389:
390: rcs_worklist_add(template, &rcs_temp_files);
391:
392: if (rcs_buf_write_fd(b, fd) == -1) {
393: (void)unlink(template);
394: errx(1, "rcs_buf_write_stmp: rcs_buf_write_fd: `%s'", template);
395: }
396:
397: (void)close(fd);
398: }
399:
400: /*
401: * rcs_buf_grow()
402: *
403: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
404: * operation regardless of the result.
405: */
406: static void
407: rcs_buf_grow(BUF *b, size_t len)
408: {
409: void *tmp;
410: size_t diff;
411:
412: diff = b->cb_cur - b->cb_buf;
413: tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
414: b->cb_buf = tmp;
415: b->cb_size += len;
416:
417: /* readjust pointers in case the buffer moved in memory */
418: b->cb_cur = b->cb_buf + diff;
419: }