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

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