Annotation of src/usr.bin/rcs/buf.c, Revision 1.5
1.5 ! niallo 1: /* $OpenBSD: buf.c,v 1.4 2006/06/01 21:28:24 niallo 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: */
181: ssize_t
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: */
254: ssize_t
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: */
283: ssize_t
284: rcs_buf_fappend(BUF *b, const char *fmt, ...)
285: {
286: ssize_t ret;
287: char *str;
288: va_list vap;
289:
290: va_start(vap, fmt);
291: ret = vasprintf(&str, fmt, vap);
292: va_end(vap);
293:
294: if (ret == -1)
295: errx(1, "rcs_buf_fappend: failed to format data");
296:
297: ret = rcs_buf_append(b, str, (size_t)ret);
298: xfree(str);
299: return (ret);
300: }
301:
302: /*
303: * rcs_buf_len()
304: *
305: * Returns the size of the buffer that is being used.
306: */
307: size_t
308: rcs_buf_len(BUF *b)
309: {
310: return (b->cb_len);
311: }
312:
313: /*
314: * rcs_buf_write_fd()
315: *
316: * Write the contents of the buffer <b> to the specified <fd>
317: */
318: int
319: rcs_buf_write_fd(BUF *b, int fd)
320: {
321: u_char *bp;
322: size_t len;
323: ssize_t ret;
324:
325: len = b->cb_len;
326: bp = b->cb_cur;
327:
328: do {
329: ret = write(fd, bp, len);
330: if (ret == -1) {
331: if (errno == EINTR || errno == EAGAIN)
332: continue;
333: return (-1);
334: }
335:
336: len -= (size_t)ret;
337: bp += (size_t)ret;
338: } while (len > 0);
339:
340: return (0);
341: }
342:
343: /*
344: * rcs_buf_write()
345: *
346: * Write the contents of the buffer <b> to the file whose path is given in
347: * <path>. If the file does not exist, it is created with mode <mode>.
348: */
349: int
350: rcs_buf_write(BUF *b, const char *path, mode_t mode)
351: {
352: int fd;
353: open:
354: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
355: if (errno == EACCES && unlink(path) != -1)
356: goto open;
357: else
1.3 xsa 358: err(1, "%s", path);
1.1 joris 359: }
360:
361: if (rcs_buf_write_fd(b, fd) == -1) {
362: (void)unlink(path);
363: errx(1, "rcs_buf_write: rcs_buf_write_fd: `%s'", path);
364: }
365:
366: if (fchmod(fd, mode) < 0)
367: warn("permissions not set on file %s", path);
368:
369: (void)close(fd);
370:
371: return (0);
372: }
373:
374: /*
375: * rcs_buf_write_stmp()
376: *
377: * Write the contents of the buffer <b> to a temporary file whose path is
378: * specified using <template> (see mkstemp.3). NB. This function will modify
379: * <template>, as per mkstemp
380: */
381: void
382: rcs_buf_write_stmp(BUF *b, char *template, mode_t mode)
383: {
384: int fd;
385:
386: if ((fd = mkstemp(template)) == -1)
1.3 xsa 387: err(1, "%s", template);
1.1 joris 388:
389: rcs_worklist_add(template, &rcs_temp_files);
390:
391: if (rcs_buf_write_fd(b, fd) == -1) {
392: (void)unlink(template);
393: errx(1, "rcs_buf_write_stmp: rcs_buf_write_fd: `%s'", template);
394: }
395:
396: if (fchmod(fd, mode) < 0)
397: warn("permissions not set on temporary file %s",
398: template);
399:
400: (void)close(fd);
401: }
402:
403: /*
404: * rcs_buf_grow()
405: *
406: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
407: * operation regardless of the result.
408: */
409: static void
410: rcs_buf_grow(BUF *b, size_t len)
411: {
412: void *tmp;
413: size_t diff;
414:
415: diff = b->cb_cur - b->cb_buf;
416: tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
417: b->cb_buf = tmp;
418: b->cb_size += len;
419:
420: /* readjust pointers in case the buffer moved in memory */
421: b->cb_cur = b->cb_buf + diff;
422: }