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

1.59    ! ray         1: /*     $OpenBSD: buf.c,v 1.58 2007/02/19 11:40:00 otto 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:
1.57      ray        40:        /* buffer handle, buffer size, and data length */
1.15      xsa        41:        u_char  *cb_buf;
                     42:        size_t   cb_size;
                     43:        size_t   cb_len;
1.1       jfb        44: };
                     45:
1.57      ray        46: #define SIZE_LEFT(b)   (b->cb_size - b->cb_len)
1.1       jfb        47:
1.46      ray        48: static void    cvs_buf_grow(BUF *, size_t);
1.1       jfb        49:
                     50: /*
                     51:  * cvs_buf_alloc()
                     52:  *
                     53:  * Create a new buffer structure and return a pointer to it.  This structure
                     54:  * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
                     55:  * once the buffer is no longer needed.
                     56:  */
1.15      xsa        57: BUF *
1.1       jfb        58: cvs_buf_alloc(size_t len, u_int flags)
                     59: {
                     60:        BUF *b;
                     61:
1.42      ray        62:        b = xmalloc(sizeof(*b));
1.30      niallo     63:        /* Postpone creation of zero-sized buffers */
1.47      ray        64:        if (len > 0)
                     65:                b->cb_buf = xcalloc(1, len);
                     66:        else
1.31      niallo     67:                b->cb_buf = NULL;
1.1       jfb        68:
                     69:        b->cb_flags = flags;
                     70:        b->cb_size = len;
                     71:        b->cb_len = 0;
                     72:
                     73:        return (b);
                     74: }
                     75:
1.15      xsa        76: BUF *
1.1       jfb        77: cvs_buf_load(const char *path, u_int flags)
                     78: {
                     79:        int fd;
1.54      joris      80:        BUF *bp;
                     81:
                     82:        if ((fd = open(path, O_RDONLY, 0600)) == -1)
                     83:                fatal("cvs_buf_load: failed to load '%s' : %s", path,
                     84:                    strerror(errno));
                     85:
                     86:        bp = cvs_buf_load_fd(fd, flags);
                     87:        (void)close(fd);
                     88:        return (bp);
                     89: }
                     90:
                     91: BUF *
                     92: cvs_buf_load_fd(int fd, u_int flags)
                     93: {
1.1       jfb        94:        ssize_t ret;
                     95:        size_t len;
1.8       jfb        96:        u_char *bp;
1.1       jfb        97:        struct stat st;
                     98:        BUF *buf;
                     99:
1.54      joris     100:        if (fstat(fd, &st) == -1)
                    101:                fatal("cvs_buf_load_fd: fstat: %s", strerror(errno));
1.1       jfb       102:
1.54      joris     103:        if (lseek(fd, 0, SEEK_SET) == -1)
                    104:                fatal("cvs_buf_load_fd: lseek: %s", strerror(errno));
1.1       jfb       105:
1.51      joris     106:        buf = cvs_buf_alloc(st.st_size, flags);
1.57      ray       107:        for (bp = buf->cb_buf; ; bp += (size_t)ret) {
1.16      moritz    108:                len = SIZE_LEFT(buf);
1.1       jfb       109:                ret = read(fd, bp, len);
1.51      joris     110:                if (ret == -1)
1.20      xsa       111:                        fatal("cvs_buf_load: read: %s", strerror(errno));
1.51      joris     112:                else if (ret == 0)
1.1       jfb       113:                        break;
                    114:
                    115:                buf->cb_len += (size_t)ret;
                    116:        }
                    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.58      otto      141: u_char *
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.1       jfb       160:        b->cb_len = 0;
                    161: }
                    162:
                    163: /*
                    164:  * cvs_buf_putc()
                    165:  *
                    166:  * Append a single character <c> to the end of the buffer <b>.
                    167:  */
1.39      xsa       168: void
1.1       jfb       169: cvs_buf_putc(BUF *b, int c)
                    170: {
                    171:        u_char *bp;
                    172:
1.57      ray       173:        bp = b->cb_buf + b->cb_len;
1.1       jfb       174:        if (bp == (b->cb_buf + b->cb_size)) {
                    175:                /* extend */
1.46      ray       176:                if (b->cb_flags & BUF_AUTOEXT)
                    177:                        cvs_buf_grow(b, (size_t)BUF_INCR);
                    178:                else
1.21      xsa       179:                        fatal("cvs_buf_putc failed");
1.1       jfb       180:
                    181:                /* the buffer might have been moved */
1.57      ray       182:                bp = b->cb_buf + b->cb_len;
1.1       jfb       183:        }
                    184:        *bp = (u_char)c;
                    185:        b->cb_len++;
1.29      niallo    186: }
                    187:
                    188: /*
                    189:  * cvs_buf_getc()
                    190:  *
                    191:  * Return u_char at buffer position <pos>.
                    192:  *
                    193:  */
                    194: u_char
1.40      ray       195: cvs_buf_getc(BUF *b, size_t pos)
1.29      niallo    196: {
1.57      ray       197:        return (b->cb_buf[pos]);
1.1       jfb       198: }
                    199:
                    200: /*
                    201:  * cvs_buf_append()
                    202:  *
                    203:  * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
                    204:  * buffer is too small to accept all data, it will attempt to append as much
                    205:  * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
                    206:  * will get resized to an appropriate size to accept all data.
1.27      joris     207:  * Returns the number of bytes successfully appended to the buffer.
1.1       jfb       208:  */
                    209: ssize_t
                    210: cvs_buf_append(BUF *b, const void *data, size_t len)
                    211: {
                    212:        size_t left, rlen;
1.8       jfb       213:        u_char *bp, *bep;
1.1       jfb       214:
1.57      ray       215:        bp = b->cb_buf + b->cb_len;
1.1       jfb       216:        bep = b->cb_buf + b->cb_size;
                    217:        left = bep - bp;
                    218:        rlen = len;
                    219:
                    220:        if (left < len) {
                    221:                if (b->cb_flags & BUF_AUTOEXT) {
1.46      ray       222:                        cvs_buf_grow(b, len - left);
1.57      ray       223:                        bp = b->cb_buf + b->cb_len;
1.5       deraadt   224:                } else
1.1       jfb       225:                        rlen = bep - bp;
                    226:        }
                    227:
                    228:        memcpy(bp, data, rlen);
                    229:        b->cb_len += rlen;
                    230:
                    231:        return (rlen);
                    232: }
                    233:
                    234: /*
                    235:  * cvs_buf_fappend()
                    236:  *
                    237:  */
1.37      ray       238: ssize_t
1.1       jfb       239: cvs_buf_fappend(BUF *b, const char *fmt, ...)
                    240: {
1.37      ray       241:        ssize_t ret;
1.1       jfb       242:        char *str;
                    243:        va_list vap;
                    244:
                    245:        va_start(vap, fmt);
1.4       pat       246:        ret = vasprintf(&str, fmt, vap);
                    247:        va_end(vap);
1.1       jfb       248:
1.20      xsa       249:        if (ret == -1)
                    250:                fatal("cvs_buf_fappend: failed to format data");
1.1       jfb       251:
1.18      xsa       252:        ret = cvs_buf_append(b, str, (size_t)ret);
1.19      joris     253:        xfree(str);
1.1       jfb       254:        return (ret);
                    255: }
                    256:
                    257: /*
1.16      moritz    258:  * cvs_buf_len()
1.1       jfb       259:  *
                    260:  * Returns the size of the buffer that is being used.
                    261:  */
                    262: size_t
1.16      moritz    263: cvs_buf_len(BUF *b)
1.1       jfb       264: {
                    265:        return (b->cb_len);
                    266: }
                    267:
                    268: /*
1.7       djm       269:  * cvs_buf_write_fd()
1.1       jfb       270:  *
1.7       djm       271:  * Write the contents of the buffer <b> to the specified <fd>
1.1       jfb       272:  */
                    273: int
1.7       djm       274: cvs_buf_write_fd(BUF *b, int fd)
1.1       jfb       275: {
                    276:        u_char *bp;
                    277:        size_t len;
                    278:        ssize_t ret;
                    279:
                    280:        len = b->cb_len;
1.57      ray       281:        bp = b->cb_buf;
1.1       jfb       282:
                    283:        do {
1.16      moritz    284:                ret = write(fd, bp, len);
1.1       jfb       285:                if (ret == -1) {
1.7       djm       286:                        if (errno == EINTR || errno == EAGAIN)
                    287:                                continue;
1.22      xsa       288:                        return (-1);
1.1       jfb       289:                }
                    290:
                    291:                len -= (size_t)ret;
                    292:                bp += (size_t)ret;
                    293:        } while (len > 0);
                    294:
1.7       djm       295:        return (0);
                    296: }
                    297:
                    298: /*
                    299:  * cvs_buf_write()
                    300:  *
                    301:  * Write the contents of the buffer <b> to the file whose path is given in
                    302:  * <path>.  If the file does not exist, it is created with mode <mode>.
                    303:  */
                    304: int
                    305: cvs_buf_write(BUF *b, const char *path, mode_t mode)
                    306: {
1.22      xsa       307:        int fd;
1.32      niallo    308:  open:
                    309:        if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
1.49      deraadt   310:                if (errno == EACCES && unlink(path) != -1)
1.32      niallo    311:                        goto open;
                    312:                else
                    313:                        fatal("open: `%s': %s", path, strerror(errno));
                    314:        }
1.7       djm       315:
1.22      xsa       316:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       317:                (void)unlink(path);
1.22      xsa       318:                fatal("cvs_buf_write: cvs_buf_write_fd: `%s'", path);
1.7       djm       319:        }
1.44      niallo    320:
                    321:        if (fchmod(fd, mode) < 0)
1.51      joris     322:                cvs_log(LP_ERR, "permissions not set on file %s", path);
1.44      niallo    323:
1.1       jfb       324:        (void)close(fd);
1.24      joris     325:
1.22      xsa       326:        return (0);
1.1       jfb       327: }
                    328:
1.7       djm       329: /*
                    330:  * cvs_buf_write_stmp()
                    331:  *
1.13      joris     332:  * Write the contents of the buffer <b> to a temporary file whose path is
1.7       djm       333:  * specified using <template> (see mkstemp.3). NB. This function will modify
                    334:  * <template>, as per mkstemp
                    335:  */
1.38      niallo    336: void
1.55      ray       337: cvs_buf_write_stmp(BUF *b, char *template, struct timeval *tv)
1.7       djm       338: {
1.22      xsa       339:        int fd;
1.7       djm       340:
1.20      xsa       341:        if ((fd = mkstemp(template)) == -1)
                    342:                fatal("mkstemp: `%s': %s", template, strerror(errno));
1.7       djm       343:
1.22      xsa       344:        if (cvs_buf_write_fd(b, fd) == -1) {
1.7       djm       345:                (void)unlink(template);
1.22      xsa       346:                fatal("cvs_buf_write_stmp: cvs_buf_write_fd: `%s'", template);
1.7       djm       347:        }
1.44      niallo    348:
1.51      joris     349:        if (tv != NULL) {
                    350:                if (futimes(fd, tv) == -1)
                    351:                        fatal("cvs_buf_write_stmp: futimes failed");
                    352:        }
                    353:
1.7       djm       354:        (void)close(fd);
1.51      joris     355:
                    356:        cvs_worklist_add(template, &temp_files);
1.7       djm       357: }
1.1       jfb       358:
                    359: /*
                    360:  * cvs_buf_grow()
                    361:  *
                    362:  * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
                    363:  * operation regardless of the result.
                    364:  */
1.46      ray       365: static void
1.1       jfb       366: cvs_buf_grow(BUF *b, size_t len)
                    367: {
                    368:        void *tmp;
                    369:
1.46      ray       370:        tmp = xrealloc(b->cb_buf, 1, b->cb_size + len);
                    371:        b->cb_buf = tmp;
1.1       jfb       372:        b->cb_size += len;
                    373: }
1.43      xsa       374:
                    375: /*
                    376:  * cvs_buf_copy()
                    377:  *
                    378:  * Copy the first <len> bytes of data in the buffer <b> starting at offset
                    379:  * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
                    380:  * Returns the number of bytes successfully copied, or -1 on failure.
                    381:  */
                    382: ssize_t
                    383: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
                    384: {
                    385:        size_t rc;
                    386:
                    387:        if (off > b->cb_len)
                    388:                fatal("cvs_buf_copy failed");
                    389:
                    390:        rc = MIN(len, (b->cb_len - off));
                    391:        memcpy(dst, b->cb_buf + off, rc);
                    392:
                    393:        return (ssize_t)rc;
                    394: }
                    395:
                    396: /*
                    397:  * cvs_buf_peek()
                    398:  *
                    399:  * Peek at the contents of the buffer <b> at offset <off>.
                    400:  */
1.58      otto      401: const u_char *
1.43      xsa       402: cvs_buf_peek(BUF *b, size_t off)
                    403: {
                    404:        if (off >= b->cb_len)
                    405:                return (NULL);
                    406:
                    407:        return (b->cb_buf + off);
1.52      joris     408: }
                    409:
                    410: int
1.59    ! ray       411: cvs_buf_differ(const BUF *b1, const BUF *b2)
1.52      joris     412: {
1.59    ! ray       413:        if (b1->cb_len != b2->cb_len)
1.52      joris     414:                return (1);
                    415:
1.59    ! ray       416:        return (memcmp(b1->cb_buf, b2->cb_buf, b1->cb_len));
1.43      xsa       417: }