[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.31

1.31    ! niallo      1: /*     $OpenBSD: buf.c,v 1.30 2006/01/15 19:25:32 niallo 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.28      xsa        27: #include "includes.h"
1.1       jfb        28:
                     29: #include "buf.h"
                     30: #include "log.h"
1.19      joris      31: #include "xmalloc.h"
1.1       jfb        32:
1.15      xsa        33: #define BUF_INCR       128
1.1       jfb        34:
                     35: struct cvs_buf {
1.15      xsa        36:        u_int   cb_flags;
1.1       jfb        37:
                     38:        /* buffer handle and size */
1.15      xsa        39:        u_char  *cb_buf;
                     40:        size_t   cb_size;
1.1       jfb        41:
                     42:        /* start and length of valid data in buffer */
1.15      xsa        43:        u_char  *cb_cur;
                     44:        size_t   cb_len;
1.1       jfb        45: };
                     46:
1.16      moritz     47: #define SIZE_LEFT(b)   (b->cb_size - (size_t)(b->cb_cur - b->cb_buf) \
                     48:                            - b->cb_len)
1.1       jfb        49:
1.15      xsa        50: static ssize_t cvs_buf_grow(BUF *, size_t);
1.1       jfb        51:
                     52: /*
                     53:  * cvs_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 cvs_buf_free(),
                     57:  * once the buffer is no longer needed.
                     58:  */
1.15      xsa        59: BUF *
1.1       jfb        60: cvs_buf_alloc(size_t len, u_int flags)
                     61: {
                     62:        BUF *b;
                     63:
1.19      joris      64:        b = (BUF *)xmalloc(sizeof(*b));
1.30      niallo     65:        /* Postpone creation of zero-sized buffers */
                     66:        if (len > 0) {
                     67:                b->cb_buf = xmalloc(len);
                     68:                memset(b->cb_buf, 0, len);
1.31    ! niallo     69:        } else
        !            70:                b->cb_buf = NULL;
1.1       jfb        71:
                     72:        b->cb_flags = flags;
                     73:        b->cb_size = len;
1.8       jfb        74:        b->cb_cur = b->cb_buf;
1.1       jfb        75:        b->cb_len = 0;
                     76:
                     77:        return (b);
                     78: }
                     79:
                     80: /*
                     81:  * cvs_buf_load()
                     82:  *
                     83:  * Open the file specified by <path> and load all of its contents into a
                     84:  * buffer.
1.20      xsa        85:  * Returns the loaded buffer on success.
1.1       jfb        86:  */
1.15      xsa        87: BUF *
1.1       jfb        88: cvs_buf_load(const char *path, u_int flags)
                     89: {
                     90:        int fd;
                     91:        ssize_t ret;
                     92:        size_t len;
1.8       jfb        93:        u_char *bp;
1.1       jfb        94:        struct stat st;
                     95:        BUF *buf;
                     96:
1.20      xsa        97:        if ((fd = open(path, O_RDONLY, 0600)) == -1)
                     98:                fatal("cvs_buf_load: open: `%s': %s", path, strerror(errno));
1.1       jfb        99:
1.20      xsa       100:        if (fstat(fd, &st) == -1)
                    101:                fatal("cvs_buf_load: fstat: %s", strerror(errno));
1.1       jfb       102:
1.25      joris     103:        buf = cvs_buf_alloc((size_t)st.st_size, flags);
1.1       jfb       104:        for (bp = buf->cb_cur; ; bp += (size_t)ret) {
1.16      moritz    105:                len = SIZE_LEFT(buf);
1.1       jfb       106:                ret = read(fd, bp, len);
                    107:                if (ret == -1) {
1.8       jfb       108:                        cvs_buf_free(buf);
1.20      xsa       109:                        fatal("cvs_buf_load: read: %s", strerror(errno));
1.5       deraadt   110:                } else if (ret == 0)
1.1       jfb       111:                        break;
                    112:
                    113:                buf->cb_len += (size_t)ret;
                    114:        }
                    115:
                    116:        (void)close(fd);
                    117:
                    118:        return (buf);
                    119: }
                    120:
                    121: /*
                    122:  * cvs_buf_free()
                    123:  *
                    124:  * Free the buffer <b> and all associated data.
                    125:  */
                    126: void
                    127: cvs_buf_free(BUF *b)
                    128: {
1.31    ! niallo    129:        if (b->cb_buf != NULL)
        !           130:                xfree(b->cb_buf);
1.19      joris     131:        xfree(b);
1.1       jfb       132: }
                    133:
                    134: /*
                    135:  * cvs_buf_release()
                    136:  *
                    137:  * Free the buffer <b>'s structural information but do not free the contents
                    138:  * of the buffer.  Instead, they are returned and should be freed later using
                    139:  * free().
                    140:  */
1.17      xsa       141: void *
1.1       jfb       142: cvs_buf_release(BUF *b)
                    143: {
1.8       jfb       144:        u_char *tmp;
1.6       tedu      145:
1.1       jfb       146:        tmp = b->cb_buf;
1.19      joris     147:        xfree(b);
1.1       jfb       148:        return (tmp);
                    149: }
                    150:
                    151: /*
                    152:  * cvs_buf_empty()
                    153:  *
                    154:  * Empty the contents of the buffer <b> and reset pointers.
                    155:  */
                    156: void
                    157: cvs_buf_empty(BUF *b)
                    158: {
1.16      moritz    159:        memset(b->cb_buf, 0, b->cb_size);
1.8       jfb       160:        b->cb_cur = b->cb_buf;
1.1       jfb       161:        b->cb_len = 0;
                    162: }
                    163:
                    164: /*
                    165:  * cvs_buf_copy()
                    166:  *
                    167:  * Copy the first <len> bytes of data in the buffer <b> starting at offset
                    168:  * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
                    169:  * Returns the number of bytes successfully copied, or -1 on failure.
                    170:  */
                    171: ssize_t
                    172: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
                    173: {
                    174:        size_t rc;
                    175:
                    176:        if (off > b->cb_len)
1.21      xsa       177:                fatal("cvs_buf_copy failed");
1.1       jfb       178:
                    179:        rc = MIN(len, (b->cb_len - off));
1.16      moritz    180:        memcpy(dst, b->cb_buf + off, rc);
1.1       jfb       181:
                    182:        return (ssize_t)rc;
                    183: }
                    184:
                    185: /*
                    186:  * cvs_buf_set()
                    187:  *
1.16      moritz    188:  * Set the contents of the buffer <b> at offset <off> to the first <len>
                    189:  * bytes of data found at <src>.  If the buffer was not created with
                    190:  * BUF_AUTOEXT, as many bytes as possible will be copied in the buffer.
1.1       jfb       191:  */
1.16      moritz    192: ssize_t
1.1       jfb       193: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
                    194: {
1.23      xsa       195:        size_t rlen = 0;
1.1       jfb       196:
                    197:        if (b->cb_size < (len + off)) {
1.20      xsa       198:                if ((b->cb_flags & BUF_AUTOEXT) &&
                    199:                    (cvs_buf_grow(b, len + off - b->cb_size) < 0))
                    200:                        fatal("cvs_buf_set failed");
1.1       jfb       201:                else
                    202:                        rlen = b->cb_size - off;
1.5       deraadt   203:        } else
1.1       jfb       204:                rlen = len;
                    205:
                    206:        memcpy((b->cb_buf + off), src, rlen);
                    207:
                    208:        if (b->cb_len == 0) {
                    209:                b->cb_cur = b->cb_buf + off;
                    210:                b->cb_len = rlen;
                    211:        }
                    212:
1.16      moritz    213:        return (rlen);
1.1       jfb       214: }
                    215:
                    216: /*
                    217:  * cvs_buf_putc()
                    218:  *
                    219:  * Append a single character <c> to the end of the buffer <b>.
1.21      xsa       220:  * Returns 0 on success.
1.1       jfb       221:  */
                    222: int
                    223: cvs_buf_putc(BUF *b, int c)
                    224: {
                    225:        u_char *bp;
                    226:
                    227:        bp = b->cb_cur + b->cb_len;
                    228:        if (bp == (b->cb_buf + b->cb_size)) {
                    229:                /* extend */
                    230:                if (!(b->cb_flags & BUF_AUTOEXT) ||
1.18      xsa       231:                    (cvs_buf_grow(b, (size_t)BUF_INCR) < 0))
1.21      xsa       232:                        fatal("cvs_buf_putc failed");
1.1       jfb       233:
                    234:                /* the buffer might have been moved */
                    235:                bp = b->cb_cur + b->cb_len;
                    236:        }
                    237:        *bp = (u_char)c;
                    238:        b->cb_len++;
                    239:
                    240:        return (0);
1.29      niallo    241: }
                    242:
                    243: /*
                    244:  * cvs_buf_getc()
                    245:  *
                    246:  * Return u_char at buffer position <pos>.
                    247:  *
                    248:  */
                    249: u_char
                    250: cvs_buf_getc(BUF *b, u_int pos)
                    251: {
                    252:        return (b->cb_cur[pos]);
1.1       jfb       253: }
                    254:
                    255: /*
                    256:  * cvs_buf_append()
                    257:  *
                    258:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    259:  * buffer is too small to accept all data, it will attempt to append as much
                    260:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    261:  * will get resized to an appropriate size to accept all data.
1.27      joris     262:  * Returns the number of bytes successfully appended to the buffer.
1.1       jfb       263:  */
                    264: ssize_t
                    265: cvs_buf_append(BUF *b, const void *data, size_t len)
                    266: {
                    267:        size_t left, rlen;
1.8       jfb       268:        u_char *bp, *bep;
1.1       jfb       269:
                    270:        bp = b->cb_cur + b->cb_len;
                    271:        bep = b->cb_buf + b->cb_size;
                    272:        left = bep - bp;
                    273:        rlen = len;
                    274:
                    275:        if (left < len) {
                    276:                if (b->cb_flags & BUF_AUTOEXT) {
                    277:                        if (cvs_buf_grow(b, len - left) < 0)
1.20      xsa       278:                                fatal("cvs_buf_append failed");
1.1       jfb       279:                        bp = b->cb_cur + b->cb_len;
1.5       deraadt   280:                } else
1.1       jfb       281:                        rlen = bep - bp;
                    282:        }
                    283:
                    284:        memcpy(bp, data, rlen);
                    285:        b->cb_len += rlen;
                    286:
                    287:        return (rlen);
                    288: }
                    289:
                    290: /*
                    291:  * cvs_buf_fappend()
                    292:  *
                    293:  */
                    294: int
                    295: cvs_buf_fappend(BUF *b, const char *fmt, ...)
                    296: {
                    297:        int ret;
                    298:        char *str;
                    299:        va_list vap;
                    300:
                    301:        va_start(vap, fmt);
1.4       pat       302:        ret = vasprintf(&str, fmt, vap);
                    303:        va_end(vap);
1.1       jfb       304:
1.20      xsa       305:        if (ret == -1)
                    306:                fatal("cvs_buf_fappend: failed to format data");
1.1       jfb       307:
1.18      xsa       308:        ret = cvs_buf_append(b, str, (size_t)ret);
1.19      joris     309:        xfree(str);
1.1       jfb       310:        return (ret);
                    311: }
                    312:
                    313: /*
1.16      moritz    314:  * cvs_buf_len()
1.1       jfb       315:  *
                    316:  * Returns the size of the buffer that is being used.
                    317:  */
                    318: size_t
1.16      moritz    319: cvs_buf_len(BUF *b)
1.1       jfb       320: {
                    321:        return (b->cb_len);
                    322: }
                    323:
                    324: /*
                    325:  * cvs_buf_peek()
                    326:  *
                    327:  * Peek at the contents of the buffer <b> at offset <off>.
                    328:  */
1.17      xsa       329: const void *
1.1       jfb       330: cvs_buf_peek(BUF *b, size_t off)
                    331: {
                    332:        if (off >= b->cb_len)
                    333:                return (NULL);
                    334:
                    335:        return (b->cb_buf + off);
                    336: }
                    337:
                    338: /*
1.7       djm       339:  * cvs_buf_write_fd()
1.1       jfb       340:  *
1.7       djm       341:  * Write the contents of the buffer <b> to the specified <fd>
1.1       jfb       342:  */
                    343: int
1.7       djm       344: cvs_buf_write_fd(BUF *b, int fd)
1.1       jfb       345: {
                    346:        u_char *bp;
                    347:        size_t len;
                    348:        ssize_t ret;
                    349:
                    350:        len = b->cb_len;
                    351:        bp = b->cb_cur;
                    352:
                    353:        do {
1.16      moritz    354:                ret = write(fd, bp, len);
1.1       jfb       355:                if (ret == -1) {
1.7       djm       356:                        if (errno == EINTR || errno == EAGAIN)
                    357:                                continue;
1.22      xsa       358:                        return (-1);
1.1       jfb       359:                }
                    360:
                    361:                len -= (size_t)ret;
                    362:                bp += (size_t)ret;
                    363:        } while (len > 0);
                    364:
1.7       djm       365:        return (0);
                    366: }
                    367:
                    368: /*
                    369:  * cvs_buf_write()
                    370:  *
                    371:  * Write the contents of the buffer <b> to the file whose path is given in
                    372:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    373:  */
                    374: int
                    375: cvs_buf_write(BUF *b, const char *path, mode_t mode)
                    376: {
1.22      xsa       377:        int fd;
1.7       djm       378:
1.20      xsa       379:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1)
                    380:                fatal("open: `%s': %s", path, strerror(errno));
1.7       djm       381:
1.22      xsa       382:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       383:                (void)unlink(path);
1.22      xsa       384:                fatal("cvs_buf_write: cvs_buf_write_fd: `%s'", path);
1.7       djm       385:        }
1.1       jfb       386:        (void)close(fd);
1.24      joris     387:
                    388:        if (chmod(path, mode) < 0)
                    389:                fatal("cvs_buf_write: chmod failed: %s", strerror(errno));
1.1       jfb       390:
1.22      xsa       391:        return (0);
1.1       jfb       392: }
                    393:
1.7       djm       394: /*
                    395:  * cvs_buf_write_stmp()
                    396:  *
1.13      joris     397:  * Write the contents of the buffer <b> to a temporary file whose path is
1.7       djm       398:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    399:  * <template>, as per mkstemp
                    400:  */
                    401: int
                    402: cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
                    403: {
1.22      xsa       404:        int fd;
1.7       djm       405:
1.20      xsa       406:        if ((fd = mkstemp(template)) == -1)
                    407:                fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7       djm       408:
1.22      xsa       409:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       410:                (void)unlink(template);
1.22      xsa       411:                fatal("cvs_buf_write_stmp: cvs_buf_write_fd: `%s'", template);
1.7       djm       412:        }
                    413:        (void)close(fd);
                    414:
1.22      xsa       415:        return (0);
1.7       djm       416: }
1.1       jfb       417:
                    418: /*
                    419:  * cvs_buf_grow()
                    420:  *
                    421:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    422:  * operation regardless of the result.
                    423:  * Returns the new size on success, or -1 on failure.
                    424:  */
                    425: static ssize_t
                    426: cvs_buf_grow(BUF *b, size_t len)
                    427: {
                    428:        void *tmp;
                    429:        size_t diff;
                    430:
1.8       jfb       431:        diff = b->cb_cur - b->cb_buf;
1.30      niallo    432:        /* Buffer not allocated yet */
                    433:        if (b->cb_size == 0)
                    434:                tmp = xmalloc(len);
                    435:        else
                    436:                tmp = xrealloc(b->cb_buf, b->cb_size + len);
1.1       jfb       437:        b->cb_buf = (u_char *)tmp;
                    438:        b->cb_size += len;
                    439:
                    440:        /* readjust pointers in case the buffer moved in memory */
                    441:        b->cb_cur = b->cb_buf + diff;
                    442:
                    443:        return (ssize_t)b->cb_size;
                    444: }