Annotation of src/usr.bin/cvs/buf.c, Revision 1.86
1.86 ! deraadt 1: /* $OpenBSD: buf.c,v 1.85 2019/06/28 13:35:00 deraadt Exp $ */
1.1 jfb 2: /*
1.2 jfb 3: * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
1.1 jfb 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:
1.60 otto 27: #include <sys/stat.h>
1.67 tobias 28: #include <sys/time.h>
1.60 otto 29:
30: #include <errno.h>
31: #include <fcntl.h>
1.82 millert 32: #include <stdint.h>
1.66 chl 33: #include <stdlib.h>
1.60 otto 34: #include <string.h>
35: #include <unistd.h>
1.1 jfb 36:
1.65 tobias 37: #include "atomicio.h"
1.51 joris 38: #include "cvs.h"
1.1 jfb 39: #include "buf.h"
40:
1.15 xsa 41: #define BUF_INCR 128
1.1 jfb 42:
1.76 ray 43: struct buf {
1.80 nicm 44: /* buffer handle, buffer size, and data length */
1.15 xsa 45: u_char *cb_buf;
46: size_t cb_size;
47: size_t cb_len;
1.1 jfb 48: };
49:
1.77 zinovik 50: #define SIZE_LEFT(b) (b->cb_size - b->cb_len)
51:
52: static void buf_grow(BUF *, size_t);
53:
1.80 nicm 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 buf_free(), once
57: * the buffer is no longer needed.
58: */
1.15 xsa 59: BUF *
1.76 ray 60: buf_alloc(size_t len)
1.1 jfb 61: {
62: BUF *b;
63:
1.42 ray 64: b = xmalloc(sizeof(*b));
1.76 ray 65: /* Postpone creation of zero-sized buffers */
1.47 ray 66: if (len > 0)
67: b->cb_buf = xcalloc(1, len);
68: else
1.31 niallo 69: b->cb_buf = NULL;
1.1 jfb 70:
71: b->cb_size = len;
72: b->cb_len = 0;
73:
74: return (b);
75: }
76:
1.80 nicm 77: /*
78: * Open the file specified by <path> and load all of its contents into a
79: * buffer.
80: * Returns the loaded buffer.
81: */
1.15 xsa 82: BUF *
1.76 ray 83: buf_load(const char *path)
1.1 jfb 84: {
85: int fd;
1.54 joris 86: BUF *bp;
87:
1.86 ! deraadt 88: if ((fd = open(path, O_RDONLY)) == -1)
1.76 ray 89: fatal("buf_load: failed to load '%s' : %s", path,
1.54 joris 90: strerror(errno));
91:
1.76 ray 92: bp = buf_load_fd(fd);
1.54 joris 93: (void)close(fd);
94: return (bp);
95: }
96:
97: BUF *
1.76 ray 98: buf_load_fd(int fd)
1.54 joris 99: {
1.1 jfb 100: struct stat st;
101: BUF *buf;
102:
1.54 joris 103: if (fstat(fd, &st) == -1)
1.76 ray 104: fatal("buf_load_fd: fstat: %s", strerror(errno));
1.1 jfb 105:
1.54 joris 106: if (lseek(fd, 0, SEEK_SET) == -1)
1.76 ray 107: fatal("buf_load_fd: lseek: %s", strerror(errno));
1.1 jfb 108:
1.84 millert 109: if ((uintmax_t)st.st_size > SIZE_MAX)
1.76 ray 110: fatal("buf_load_fd: file size too big");
111: buf = buf_alloc(st.st_size);
1.65 tobias 112: if (atomicio(read, fd, buf->cb_buf, buf->cb_size) != buf->cb_size)
1.76 ray 113: fatal("buf_load_fd: read: %s", strerror(errno));
1.65 tobias 114: buf->cb_len = buf->cb_size;
1.1 jfb 115:
116: return (buf);
117: }
118:
119: void
1.76 ray 120: buf_free(BUF *b)
1.1 jfb 121: {
1.83 nicm 122: if (b == NULL)
123: return;
124: free(b->cb_buf);
125: free(b);
1.1 jfb 126: }
127:
1.80 nicm 128: /*
129: * Free the buffer <b>'s structural information but do not free the contents
1.83 nicm 130: * of the buffer. Instead, they are returned and should be freed later.
1.80 nicm 131: */
1.78 zinovik 132: void *
1.76 ray 133: buf_release(BUF *b)
1.1 jfb 134: {
1.78 zinovik 135: void *tmp;
1.6 tedu 136:
1.1 jfb 137: tmp = b->cb_buf;
1.83 nicm 138: free(b);
1.1 jfb 139: return (tmp);
140: }
141:
1.80 nicm 142: /*
143: * Append a single character <c> to the end of the buffer <b>.
144: */
1.39 xsa 145: void
1.76 ray 146: buf_putc(BUF *b, int c)
1.1 jfb 147: {
148: u_char *bp;
149:
1.79 tobias 150: if (SIZE_LEFT(b) == 0)
151: buf_grow(b, BUF_INCR);
1.57 ray 152: bp = b->cb_buf + b->cb_len;
1.1 jfb 153: *bp = (u_char)c;
154: b->cb_len++;
1.72 joris 155: }
156:
1.80 nicm 157: /*
158: * Append a C-string <str> to the end of the buffer <b>.
159: */
1.72 joris 160: void
1.76 ray 161: buf_puts(BUF *b, const char *str)
1.72 joris 162: {
1.76 ray 163: buf_append(b, str, strlen(str));
1.29 niallo 164: }
165:
1.80 nicm 166: /*
167: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
168: * buffer is too small to accept all data, it will get resized to an
169: * appropriate size to accept all data.
170: */
1.68 tobias 171: void
1.76 ray 172: buf_append(BUF *b, const void *data, size_t len)
1.1 jfb 173: {
1.68 tobias 174: size_t left;
1.77 zinovik 175: u_char *bp;
1.1 jfb 176:
1.77 zinovik 177: left = SIZE_LEFT(b);
1.1 jfb 178:
1.77 zinovik 179: if (left < len)
180: buf_grow(b, len - left);
1.1 jfb 181:
1.77 zinovik 182: bp = b->cb_buf + b->cb_len;
1.68 tobias 183: memcpy(bp, data, len);
184: b->cb_len += len;
1.1 jfb 185: }
186:
1.80 nicm 187: /*
188: * Returns the size of the buffer that is being used.
189: */
1.1 jfb 190: size_t
1.76 ray 191: buf_len(BUF *b)
1.1 jfb 192: {
193: return (b->cb_len);
194: }
195:
1.80 nicm 196: /*
197: * Write the contents of the buffer <b> to the specified <fd>
198: */
1.1 jfb 199: int
1.76 ray 200: buf_write_fd(BUF *b, int fd)
1.1 jfb 201: {
1.65 tobias 202: if (atomicio(vwrite, fd, b->cb_buf, b->cb_len) != b->cb_len)
203: return (-1);
1.7 djm 204: return (0);
205: }
206:
1.80 nicm 207: /*
208: * Write the contents of the buffer <b> to the file whose path is given in
209: * <path>. If the file does not exist, it is created with mode <mode>.
210: */
1.7 djm 211: int
1.76 ray 212: buf_write(BUF *b, const char *path, mode_t mode)
1.7 djm 213: {
1.22 xsa 214: int fd;
1.74 joris 215: open:
1.32 niallo 216: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
1.49 deraadt 217: if (errno == EACCES && unlink(path) != -1)
1.32 niallo 218: goto open;
219: else
220: fatal("open: `%s': %s", path, strerror(errno));
221: }
1.7 djm 222:
1.76 ray 223: if (buf_write_fd(b, fd) == -1) {
1.7 djm 224: (void)unlink(path);
1.76 ray 225: fatal("buf_write: buf_write_fd: `%s'", path);
1.7 djm 226: }
1.44 niallo 227:
1.85 deraadt 228: if (fchmod(fd, mode) == -1)
1.51 joris 229: cvs_log(LP_ERR, "permissions not set on file %s", path);
1.44 niallo 230:
1.1 jfb 231: (void)close(fd);
1.24 joris 232:
1.22 xsa 233: return (0);
1.1 jfb 234: }
235:
1.80 nicm 236: /*
237: * Write the contents of the buffer <b> to a temporary file whose path is
238: * specified using <template> (see mkstemp.3). If <tv> is specified file
239: * access and modification time is set to <tv>.
240: * NB. This function will modify <template>, as per mkstemp
241: */
1.69 joris 242: int
1.76 ray 243: buf_write_stmp(BUF *b, char *template, struct timeval *tv)
1.7 djm 244: {
1.22 xsa 245: int fd;
1.7 djm 246:
1.20 xsa 247: if ((fd = mkstemp(template)) == -1)
248: fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7 djm 249:
1.76 ray 250: if (buf_write_fd(b, fd) == -1) {
1.7 djm 251: (void)unlink(template);
1.76 ray 252: fatal("buf_write_stmp: buf_write_fd: `%s'", template);
1.7 djm 253: }
1.44 niallo 254:
1.51 joris 255: if (tv != NULL) {
256: if (futimes(fd, tv) == -1)
1.76 ray 257: fatal("buf_write_stmp: futimes failed");
1.51 joris 258: }
259:
1.75 ray 260: worklist_add(template, &temp_files);
1.69 joris 261:
1.85 deraadt 262: if (lseek(fd, 0, SEEK_SET) == -1)
1.76 ray 263: fatal("buf_write_stmp: lseek: %s", strerror(errno));
1.51 joris 264:
1.69 joris 265: return (fd);
1.7 djm 266: }
1.1 jfb 267:
1.74 joris 268: u_char *
1.76 ray 269: buf_get(BUF *bp)
1.1 jfb 270: {
1.74 joris 271: return (bp->cb_buf);
1.52 joris 272: }
273:
274: int
1.76 ray 275: buf_differ(const BUF *b1, const BUF *b2)
1.52 joris 276: {
1.59 ray 277: if (b1->cb_len != b2->cb_len)
1.52 joris 278: return (1);
279:
1.59 ray 280: return (memcmp(b1->cb_buf, b2->cb_buf, b1->cb_len));
1.77 zinovik 281: }
282:
283: /*
284: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
285: * operation regardless of the result.
286: */
287: static void
288: buf_grow(BUF *b, size_t len)
289: {
1.81 deraadt 290: b->cb_buf = xreallocarray(b->cb_buf, 1, b->cb_size + len);
1.77 zinovik 291: b->cb_size += len;
1.43 xsa 292: }