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

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