[BACK]Return to buf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/buf.c, Revision 1.77

1.77    ! zinovik     1: /*     $OpenBSD: buf.c,v 1.76 2010/07/23 21:46:05 ray 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.1       jfb        41:
1.76      ray        42: struct buf {
1.15      xsa        43:        u_char  *cb_buf;
                     44:        size_t   cb_size;
                     45:        size_t   cb_len;
1.1       jfb        46: };
                     47:
1.77    ! zinovik    48: #define SIZE_LEFT(b)   (b->cb_size - b->cb_len)
        !            49:
        !            50: static void    buf_grow(BUF *, size_t);
        !            51:
1.15      xsa        52: BUF *
1.76      ray        53: buf_alloc(size_t len)
1.1       jfb        54: {
                     55:        BUF *b;
                     56:
1.42      ray        57:        b = xmalloc(sizeof(*b));
1.76      ray        58:        /* Postpone creation of zero-sized buffers */
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.76      ray        71: 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.76      ray        77:                fatal("buf_load: failed to load '%s' : %s", path,
1.54      joris      78:                    strerror(errno));
                     79:
1.76      ray        80:        bp = buf_load_fd(fd);
1.54      joris      81:        (void)close(fd);
                     82:        return (bp);
                     83: }
                     84:
                     85: BUF *
1.76      ray        86: 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)
1.76      ray        92:                fatal("buf_load_fd: fstat: %s", strerror(errno));
1.1       jfb        93:
1.54      joris      94:        if (lseek(fd, 0, SEEK_SET) == -1)
1.76      ray        95:                fatal("buf_load_fd: lseek: %s", strerror(errno));
1.1       jfb        96:
1.71      tobias     97:        if (st.st_size > SIZE_MAX)
1.76      ray        98:                fatal("buf_load_fd: file size too big");
                     99:        buf = buf_alloc(st.st_size);
1.65      tobias    100:        if (atomicio(read, fd, buf->cb_buf, buf->cb_size) != buf->cb_size)
1.76      ray       101:                fatal("buf_load_fd: read: %s", strerror(errno));
1.65      tobias    102:        buf->cb_len = buf->cb_size;
1.1       jfb       103:
                    104:        return (buf);
                    105: }
                    106:
                    107: void
1.76      ray       108: buf_free(BUF *b)
1.1       jfb       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.76      ray       116: buf_release(BUF *b)
1.1       jfb       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.76      ray       126: buf_putc(BUF *b, int c)
1.1       jfb       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.77    ! zinovik   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
1.76      ray       140: buf_puts(BUF *b, const char *str)
1.72      joris     141: {
1.76      ray       142:        buf_append(b, str, strlen(str));
1.29      niallo    143: }
                    144:
1.68      tobias    145: void
1.76      ray       146: buf_append(BUF *b, const void *data, size_t len)
1.1       jfb       147: {
1.68      tobias    148:        size_t left;
1.77    ! zinovik   149:        u_char *bp;
1.1       jfb       150:
1.77    ! zinovik   151:        left = SIZE_LEFT(b);
1.1       jfb       152:
1.77    ! zinovik   153:        if (left < len)
        !           154:                buf_grow(b, len - left);
1.1       jfb       155:
1.77    ! zinovik   156:        bp = b->cb_buf + b->cb_len;
1.68      tobias    157:        memcpy(bp, data, len);
                    158:        b->cb_len += len;
1.1       jfb       159: }
                    160:
                    161: size_t
1.76      ray       162: buf_len(BUF *b)
1.1       jfb       163: {
                    164:        return (b->cb_len);
                    165: }
                    166:
                    167: int
1.76      ray       168: buf_write_fd(BUF *b, int fd)
1.1       jfb       169: {
1.65      tobias    170:        if (atomicio(vwrite, fd, b->cb_buf, b->cb_len) != b->cb_len)
                    171:                return (-1);
1.7       djm       172:        return (0);
                    173: }
                    174:
                    175: int
1.76      ray       176: buf_write(BUF *b, const char *path, mode_t mode)
1.7       djm       177: {
1.22      xsa       178:        int fd;
1.74      joris     179: open:
1.32      niallo    180:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
1.49      deraadt   181:                if (errno == EACCES && unlink(path) != -1)
1.32      niallo    182:                        goto open;
                    183:                else
                    184:                        fatal("open: `%s': %s", path, strerror(errno));
                    185:        }
1.7       djm       186:
1.76      ray       187:        if (buf_write_fd(b, fd) == -1) {
1.7       djm       188:                (void)unlink(path);
1.76      ray       189:                fatal("buf_write: buf_write_fd: `%s'", path);
1.7       djm       190:        }
1.44      niallo    191:
                    192:        if (fchmod(fd, mode) < 0)
1.51      joris     193:                cvs_log(LP_ERR, "permissions not set on file %s", path);
1.44      niallo    194:
1.1       jfb       195:        (void)close(fd);
1.24      joris     196:
1.22      xsa       197:        return (0);
1.1       jfb       198: }
                    199:
1.69      joris     200: int
1.76      ray       201: buf_write_stmp(BUF *b, char *template, struct timeval *tv)
1.7       djm       202: {
1.22      xsa       203:        int fd;
1.7       djm       204:
1.20      xsa       205:        if ((fd = mkstemp(template)) == -1)
                    206:                fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7       djm       207:
1.76      ray       208:        if (buf_write_fd(b, fd) == -1) {
1.7       djm       209:                (void)unlink(template);
1.76      ray       210:                fatal("buf_write_stmp: buf_write_fd: `%s'", template);
1.7       djm       211:        }
1.44      niallo    212:
1.51      joris     213:        if (tv != NULL) {
                    214:                if (futimes(fd, tv) == -1)
1.76      ray       215:                        fatal("buf_write_stmp: futimes failed");
1.51      joris     216:        }
                    217:
1.75      ray       218:        worklist_add(template, &temp_files);
1.69      joris     219:
1.70      joris     220:        if (lseek(fd, 0, SEEK_SET) < 0)
1.76      ray       221:                fatal("buf_write_stmp: lseek: %s", strerror(errno));
1.51      joris     222:
1.69      joris     223:        return (fd);
1.7       djm       224: }
1.1       jfb       225:
1.74      joris     226: u_char *
1.76      ray       227: buf_get(BUF *bp)
1.1       jfb       228: {
1.74      joris     229:        return (bp->cb_buf);
1.52      joris     230: }
                    231:
                    232: int
1.76      ray       233: buf_differ(const BUF *b1, const BUF *b2)
1.52      joris     234: {
1.59      ray       235:        if (b1->cb_len != b2->cb_len)
1.52      joris     236:                return (1);
                    237:
1.59      ray       238:        return (memcmp(b1->cb_buf, b2->cb_buf, b1->cb_len));
1.77    ! zinovik   239: }
        !           240:
        !           241: /*
        !           242:  * buf_grow()
        !           243:  *
        !           244:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
        !           245:  * operation regardless of the result.
        !           246:  */
        !           247: static void
        !           248: buf_grow(BUF *b, size_t len)
        !           249: {
        !           250:        b->cb_buf = xrealloc(b->cb_buf, 1, b->cb_size + len);
        !           251:        b->cb_size += len;
1.43      xsa       252: }