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

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