Annotation of src/usr.bin/cvs/buf.c, Revision 1.74
1.74 ! joris 1: /* $OpenBSD: buf.c,v 1.73 2009/03/23 18:21:23 joris 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.66 chl 32: #include <stdlib.h>
1.60 otto 33: #include <string.h>
34: #include <unistd.h>
1.1 jfb 35:
1.65 tobias 36: #include "atomicio.h"
1.51 joris 37: #include "cvs.h"
1.1 jfb 38: #include "buf.h"
39:
1.15 xsa 40: #define BUF_INCR 128
1.74 ! joris 41: #define BUF_GROW(bp, len) \
! 42: do { \
! 43: b->cb_buf = xrealloc(b->cb_buf, 1, b->cb_size + len); \
! 44: b->cb_size += len; \
! 45: } while (0);
1.1 jfb 46:
47: struct cvs_buf {
1.15 xsa 48: u_char *cb_buf;
49: size_t cb_size;
50: size_t cb_len;
1.1 jfb 51: };
52:
1.15 xsa 53: BUF *
1.68 tobias 54: cvs_buf_alloc(size_t len)
1.1 jfb 55: {
56: BUF *b;
57:
1.42 ray 58: b = xmalloc(sizeof(*b));
1.47 ray 59: if (len > 0)
60: b->cb_buf = xcalloc(1, len);
61: else
1.31 niallo 62: b->cb_buf = NULL;
1.1 jfb 63:
64: b->cb_size = len;
65: b->cb_len = 0;
66:
67: return (b);
68: }
69:
1.15 xsa 70: BUF *
1.68 tobias 71: cvs_buf_load(const char *path)
1.1 jfb 72: {
73: int fd;
1.54 joris 74: BUF *bp;
75:
76: if ((fd = open(path, O_RDONLY, 0600)) == -1)
1.63 joris 77: fatal("cvs_buf_load: failed to load '%s' : %s", path,
1.54 joris 78: strerror(errno));
79:
1.68 tobias 80: bp = cvs_buf_load_fd(fd);
1.54 joris 81: (void)close(fd);
82: return (bp);
83: }
84:
85: BUF *
1.68 tobias 86: cvs_buf_load_fd(int fd)
1.54 joris 87: {
1.1 jfb 88: struct stat st;
89: BUF *buf;
90:
1.54 joris 91: if (fstat(fd, &st) == -1)
92: fatal("cvs_buf_load_fd: fstat: %s", strerror(errno));
1.1 jfb 93:
1.54 joris 94: if (lseek(fd, 0, SEEK_SET) == -1)
95: fatal("cvs_buf_load_fd: lseek: %s", strerror(errno));
1.1 jfb 96:
1.71 tobias 97: if (st.st_size > SIZE_MAX)
98: fatal("cvs_buf_load_fd: file size too big");
1.68 tobias 99: buf = cvs_buf_alloc(st.st_size);
1.65 tobias 100: if (atomicio(read, fd, buf->cb_buf, buf->cb_size) != buf->cb_size)
101: fatal("cvs_buf_load_fd: read: %s", strerror(errno));
102: buf->cb_len = buf->cb_size;
1.1 jfb 103:
104: return (buf);
105: }
106:
107: void
108: cvs_buf_free(BUF *b)
109: {
1.31 niallo 110: if (b->cb_buf != NULL)
111: xfree(b->cb_buf);
1.19 joris 112: xfree(b);
1.1 jfb 113: }
114:
1.58 otto 115: u_char *
1.1 jfb 116: cvs_buf_release(BUF *b)
117: {
1.8 jfb 118: u_char *tmp;
1.6 tedu 119:
1.1 jfb 120: tmp = b->cb_buf;
1.19 joris 121: xfree(b);
1.1 jfb 122: return (tmp);
123: }
124:
1.39 xsa 125: void
1.1 jfb 126: cvs_buf_putc(BUF *b, int c)
127: {
128: u_char *bp;
129:
1.57 ray 130: bp = b->cb_buf + b->cb_len;
1.1 jfb 131: if (bp == (b->cb_buf + b->cb_size)) {
1.74 ! joris 132: BUF_GROW(b, BUF_INCR);
1.57 ray 133: bp = b->cb_buf + b->cb_len;
1.1 jfb 134: }
135: *bp = (u_char)c;
136: b->cb_len++;
1.72 joris 137: }
138:
139: void
140: cvs_buf_puts(BUF *b, const char *str)
141: {
142: cvs_buf_append(b, str, strlen(str));
1.29 niallo 143: }
144:
1.68 tobias 145: void
1.1 jfb 146: cvs_buf_append(BUF *b, const void *data, size_t len)
147: {
1.68 tobias 148: size_t left;
1.8 jfb 149: u_char *bp, *bep;
1.1 jfb 150:
1.57 ray 151: bp = b->cb_buf + b->cb_len;
1.1 jfb 152: bep = b->cb_buf + b->cb_size;
153: left = bep - bp;
154:
155: if (left < len) {
1.74 ! joris 156: BUF_GROW(b, len - left);
1.68 tobias 157: bp = b->cb_buf + b->cb_len;
1.1 jfb 158: }
159:
1.68 tobias 160: memcpy(bp, data, len);
161: b->cb_len += len;
1.1 jfb 162: }
163:
164: size_t
1.16 moritz 165: cvs_buf_len(BUF *b)
1.1 jfb 166: {
167: return (b->cb_len);
168: }
169:
170: int
1.7 djm 171: cvs_buf_write_fd(BUF *b, int fd)
1.1 jfb 172: {
1.65 tobias 173: if (atomicio(vwrite, fd, b->cb_buf, b->cb_len) != b->cb_len)
174: return (-1);
1.7 djm 175: return (0);
176: }
177:
178: int
179: cvs_buf_write(BUF *b, const char *path, mode_t mode)
180: {
1.22 xsa 181: int fd;
1.74 ! joris 182: open:
1.32 niallo 183: if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
1.49 deraadt 184: if (errno == EACCES && unlink(path) != -1)
1.32 niallo 185: goto open;
186: else
187: fatal("open: `%s': %s", path, strerror(errno));
188: }
1.7 djm 189:
1.22 xsa 190: if (cvs_buf_write_fd(b, fd) == -1) {
1.7 djm 191: (void)unlink(path);
1.22 xsa 192: fatal("cvs_buf_write: cvs_buf_write_fd: `%s'", path);
1.7 djm 193: }
1.44 niallo 194:
195: if (fchmod(fd, mode) < 0)
1.51 joris 196: cvs_log(LP_ERR, "permissions not set on file %s", path);
1.44 niallo 197:
1.1 jfb 198: (void)close(fd);
1.24 joris 199:
1.22 xsa 200: return (0);
1.1 jfb 201: }
202:
1.69 joris 203: int
1.55 ray 204: cvs_buf_write_stmp(BUF *b, char *template, struct timeval *tv)
1.7 djm 205: {
1.22 xsa 206: int fd;
1.7 djm 207:
1.20 xsa 208: if ((fd = mkstemp(template)) == -1)
209: fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7 djm 210:
1.22 xsa 211: if (cvs_buf_write_fd(b, fd) == -1) {
1.7 djm 212: (void)unlink(template);
1.22 xsa 213: fatal("cvs_buf_write_stmp: cvs_buf_write_fd: `%s'", template);
1.7 djm 214: }
1.44 niallo 215:
1.51 joris 216: if (tv != NULL) {
217: if (futimes(fd, tv) == -1)
218: fatal("cvs_buf_write_stmp: futimes failed");
219: }
220:
1.69 joris 221: cvs_worklist_add(template, &temp_files);
222:
1.70 joris 223: if (lseek(fd, 0, SEEK_SET) < 0)
1.69 joris 224: fatal("cvs_buf_write_stmp: lseek: %s", strerror(errno));
1.51 joris 225:
1.69 joris 226: return (fd);
1.7 djm 227: }
1.1 jfb 228:
1.74 ! joris 229: u_char *
! 230: cvs_buf_get(BUF *bp)
1.1 jfb 231: {
1.74 ! joris 232: return (bp->cb_buf);
1.52 joris 233: }
234:
235: int
1.59 ray 236: cvs_buf_differ(const BUF *b1, const BUF *b2)
1.52 joris 237: {
1.59 ray 238: if (b1->cb_len != b2->cb_len)
1.52 joris 239: return (1);
240:
1.59 ray 241: return (memcmp(b1->cb_buf, b2->cb_buf, b1->cb_len));
1.43 xsa 242: }