        -:    0:Source:/home/dsg/apr_trunk/file_io/unix/readwrite.c
        -:    0:Graph:./file_io/unix/.libs/readwrite.gcno
        -:    0:Data:./file_io/unix/.libs/readwrite.gcda
        -:    0:Runs:346
        -:    1:/* Licensed to the Apache Software Foundation (ASF) under one or more
        -:    2: * contributor license agreements.  See the NOTICE file distributed with
        -:    3: * this work for additional information regarding copyright ownership.
        -:    4: * The ASF licenses this file to You under the Apache License, Version 2.0
        -:    5: * (the "License"); you may not use this file except in compliance with
        -:    6: * the License.  You may obtain a copy of the License at
        -:    7: *
        -:    8: *     http://www.apache.org/licenses/LICENSE-2.0
        -:    9: *
        -:   10: * Unless required by applicable law or agreed to in writing, software
        -:   11: * distributed under the License is distributed on an "AS IS" BASIS,
        -:   12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        -:   13: * See the License for the specific language governing permissions and
        -:   14: * limitations under the License.
        -:   15: */
        -:   16:
        -:   17:#include "apr_arch_file_io.h"
        -:   18:#include "apr_strings.h"
        -:   19:#include "apr_thread_mutex.h"
        -:   20:#include "apr_support.h"
        -:   21:#include "apr_time.h"
        -:   22:#include "apr_file_info.h"
        -:   23:
        -:   24:/* The only case where we don't use wait_for_io_or_timeout is on
        -:   25: * pre-BONE BeOS, so this check should be sufficient and simpler */
        -:   26:#if !defined(BEOS_R5)
        -:   27:#define USE_WAIT_FOR_IO
        -:   28:#endif
        -:   29:
    86205:   30:static apr_status_t file_read_buffered(apr_file_t *thefile, void *buf,
        -:   31:                                       apr_size_t *nbytes)
        -:   32:{
        -:   33:    apr_ssize_t rv;
    86205:   34:    char *pos = (char *)buf;
        -:   35:    apr_uint64_t blocksize;
    86205:   36:    apr_uint64_t size = *nbytes;
        -:   37:
    86205:   38:    if (thefile->direction == 1) {
        2:   39:        rv = apr_file_flush_locked(thefile);
        2:   40:        if (rv) {
        2:   41:            return rv;
        -:   42:        }
    #####:   43:        thefile->bufpos = 0;
    #####:   44:        thefile->direction = 0;
    #####:   45:        thefile->dataRead = 0;
        -:   46:    }
        -:   47:
    86203:   48:    rv = 0;
    86203:   49:    if (thefile->ungetchar != -1) {
    #####:   50:        *pos = (char)thefile->ungetchar;
    #####:   51:        ++pos;
    #####:   52:        --size;
    #####:   53:        thefile->ungetchar = -1;
        -:   54:    }
   172494:   55:    while (rv == 0 && size > 0) {
    86325:   56:        if (thefile->bufpos >= thefile->dataRead) {
     4785:   57:            int bytesread = read(thefile->filedes, thefile->buffer,
        -:   58:                                 thefile->bufsize);
     4785:   59:            if (bytesread == 0) {
       34:   60:                thefile->eof_hit = TRUE;
       34:   61:                rv = APR_EOF;
       34:   62:                break;
        -:   63:            }
     4751:   64:            else if (bytesread == -1) {
    #####:   65:                rv = errno;
    #####:   66:                break;
        -:   67:            }
     4751:   68:            thefile->dataRead = bytesread;
     4751:   69:            thefile->filePtr += thefile->dataRead;
     4751:   70:            thefile->bufpos = 0;
        -:   71:        }
        -:   72:
    86291:   73:        blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
    86291:   74:        memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
    86291:   75:        thefile->bufpos += blocksize;
    86291:   76:        pos += blocksize;
    86291:   77:        size -= blocksize;
        -:   78:    }
        -:   79:
    86203:   80:    *nbytes = pos - (char *)buf;
    86203:   81:    if (*nbytes) {
    86171:   82:        rv = 0;
        -:   83:    }
    86203:   84:    return rv;
        -:   85:}
        -:   86:
   228290:   87:APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
        -:   88:{
        -:   89:    apr_ssize_t rv;
        -:   90:    apr_size_t bytes_read;
        -:   91:
   228290:   92:    if (*nbytes <= 0) {
        2:   93:        *nbytes = 0;
        2:   94:        return APR_SUCCESS;
        -:   95:    }
        -:   96:
   228288:   97:    if (thefile->buffered) {
   86189*:   98:        file_lock(thefile);
    86189:   99:        rv = file_read_buffered(thefile, buf, nbytes);
   86189*:  100:        file_unlock(thefile);
    86189:  101:        return rv;
        -:  102:    }
        -:  103:    else {
   142099:  104:        bytes_read = 0;
   142099:  105:        if (thefile->ungetchar != -1) {
        6:  106:            bytes_read = 1;
        6:  107:            *(char *)buf = (char)thefile->ungetchar;
        6:  108:            buf = (char *)buf + 1;
        6:  109:            (*nbytes)--;
        6:  110:            thefile->ungetchar = -1;
        6:  111:            if (*nbytes == 0) {
        6:  112:                *nbytes = bytes_read;
        6:  113:                return APR_SUCCESS;
        -:  114:            }
        -:  115:        }
        -:  116:
        -:  117:        do {
   142093:  118:            rv = read(thefile->filedes, buf, *nbytes);
   142091:  119:        } while (rv == -1 && errno == EINTR);
        -:  120:#ifdef USE_WAIT_FOR_IO
   142091:  121:        if (rv == -1 &&
       19:  122:            (errno == EAGAIN || errno == EWOULDBLOCK) &&
        5:  123:            thefile->timeout != 0) {
        3:  124:            apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 1);
        3:  125:            if (arv != APR_SUCCESS) {
        2:  126:                *nbytes = bytes_read;
        2:  127:                return arv;
        -:  128:            }
        -:  129:            else {
        -:  130:                do {
        1:  131:                    rv = read(thefile->filedes, buf, *nbytes);
       1*:  132:                } while (rv == -1 && errno == EINTR);
        -:  133:            }
        -:  134:        }
        -:  135:#endif
   142089:  136:        *nbytes = bytes_read;
   142089:  137:        if (rv == 0) {
       64:  138:            thefile->eof_hit = TRUE;
       64:  139:            return APR_EOF;
        -:  140:        }
   142025:  141:        if (rv > 0) {
   142009:  142:            *nbytes += rv;
   142009:  143:            return APR_SUCCESS;
        -:  144:        }
       16:  145:        return errno;
        -:  146:    }
        -:  147:}
        -:  148:
    #####:  149:static apr_status_t do_rotating_check(apr_file_t *thefile, apr_time_t now)
        -:  150:{
    #####:  151:    apr_size_t rv = APR_SUCCESS;
        -:  152:
    #####:  153:    if ((now - thefile->rotating->lastcheck) >= thefile->rotating->timeout) {
        -:  154:        apr_finfo_t new_finfo;
        -:  155:        apr_pool_t *tmp_pool;
        -:  156:
    #####:  157:        apr_pool_create(&tmp_pool, thefile->pool);
        -:  158:
    #####:  159:        rv = apr_stat(&new_finfo, thefile->fname,
        -:  160:                      APR_FINFO_DEV|APR_FINFO_INODE, tmp_pool);
        -:  161:
    #####:  162:        if (rv != APR_SUCCESS ||
    #####:  163:            new_finfo.inode != thefile->rotating->finfo.inode ||
    #####:  164:            new_finfo.device != thefile->rotating->finfo.device)  {
        -:  165:
    #####:  166:            if (thefile->buffered) {
    #####:  167:                apr_file_flush(thefile);
        -:  168:            }
        -:  169:
    #####:  170:            close(thefile->filedes);
    #####:  171:            thefile->filedes = -1;
        -:  172:
    #####:  173:            if (thefile->rotating->perm == APR_FPROT_OS_DEFAULT) {
    #####:  174:                thefile->filedes = open(thefile->fname,
    #####:  175:                                        thefile->rotating->oflags,
        -:  176:                                        0666);
        -:  177:            }
        -:  178:            else {
    #####:  179:                thefile->filedes = open(thefile->fname,
    #####:  180:                                        thefile->rotating->oflags,
    #####:  181:                                        apr_unix_perms2mode(thefile->rotating->perm));
        -:  182:            }
        -:  183:
    #####:  184:            if (thefile->filedes < 0) {
    #####:  185:                rv = errno;
        -:  186:            }
        -:  187:            else {
    #####:  188:                rv = apr_file_info_get(&thefile->rotating->finfo,
        -:  189:                                       APR_FINFO_DEV|APR_FINFO_INODE,
        -:  190:                                       thefile);
        -:  191:            }
        -:  192:        }
        -:  193:
    #####:  194:        apr_pool_destroy(tmp_pool);
    #####:  195:        thefile->rotating->lastcheck = now;
        -:  196:    }
    #####:  197:    return rv;
        -:  198:}
        -:  199:
    68862:  200:static apr_status_t file_rotating_check(apr_file_t *thefile)
        -:  201:{
   68862*:  202:    if (thefile->rotating && thefile->rotating->manual == 0) {
    #####:  203:        apr_time_t now = apr_time_sec(apr_time_now());
    #####:  204:        return do_rotating_check(thefile, now);
        -:  205:    }
    68862:  206:    return APR_SUCCESS;
        -:  207:}
        -:  208:
    #####:  209:APR_DECLARE(apr_status_t) apr_file_rotating_check(apr_file_t *thefile)
        -:  210:{
    #####:  211:    if (thefile->rotating) {
    #####:  212:        apr_time_t now = apr_time_sec(apr_time_now());
    #####:  213:        return do_rotating_check(thefile, now);
        -:  214:    }
    #####:  215:    return APR_SUCCESS;
        -:  216:}
        -:  217:
    #####:  218:APR_DECLARE(apr_status_t) apr_file_rotating_manual_check(apr_file_t *thefile, apr_time_t n)
        -:  219:{
    #####:  220:    if (thefile->rotating) {
    #####:  221:        return do_rotating_check(thefile, n);
        -:  222:    }
    #####:  223:    return APR_SUCCESS;
        -:  224:}
        -:  225:
    68852:  226:APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes)
        -:  227:{
        -:  228:    apr_size_t rv;
        -:  229:
    68852:  230:    rv = file_rotating_check(thefile);
    68852:  231:    if (rv != APR_SUCCESS) {
    #####:  232:        return rv;
        -:  233:    }
        -:  234:
    68852:  235:    if (thefile->buffered) {
       32:  236:        char *pos = (char *)buf;
        -:  237:        int blocksize;
       32:  238:        int size = *nbytes;
        -:  239:
      32*:  240:        file_lock(thefile);
        -:  241:
       32:  242:        if ( thefile->direction == 0 ) {
        -:  243:            /* Position file pointer for writing at the offset we are
        -:  244:             * logically reading from
        -:  245:             */
       22:  246:            apr_int64_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
       22:  247:            if (offset != thefile->filePtr) {
    #####:  248:                thefile->filePtr = lseek(thefile->filedes, offset, SEEK_SET);
    #####:  249:                if (thefile->filePtr == -1) rv = errno;
        -:  250:            }
       22:  251:            thefile->bufpos = thefile->dataRead = 0;
       22:  252:            thefile->direction = 1;
        -:  253:        }
        -:  254:
      180:  255:        while (rv == APR_SUCCESS && size > 0) {
      148:  256:            if (thefile->bufpos == thefile->bufsize)   /* write buffer is full*/
      118:  257:                rv = apr_file_flush_locked(thefile);
        -:  258:
      148:  259:            blocksize = size > thefile->bufsize - thefile->bufpos ?
      148:  260:                        thefile->bufsize - thefile->bufpos : size;
      148:  261:            memcpy(thefile->buffer + thefile->bufpos, pos, blocksize);
      148:  262:            thefile->bufpos += blocksize;
      148:  263:            pos += blocksize;
      148:  264:            size -= blocksize;
        -:  265:        }
        -:  266:
      32*:  267:        file_unlock(thefile);
        -:  268:
       32:  269:        return rv;
        -:  270:    }
        -:  271:    else {
        -:  272:        do {
    68820:  273:            rv = write(thefile->filedes, buf, *nbytes);
    68820:  274:        } while (rv == (apr_size_t)-1 && errno == EINTR);
        -:  275:#ifdef USE_WAIT_FOR_IO
    68820:  276:        if (rv == (apr_size_t)-1 &&
     767*:  277:            (errno == EAGAIN || errno == EWOULDBLOCK) &&
      767:  278:            thefile->timeout != 0) {
      765:  279:            apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 0);
      765:  280:            if (arv != APR_SUCCESS) {
    #####:  281:                *nbytes = 0;
    #####:  282:                return arv;
        -:  283:            }
        -:  284:            else {
    #####:  285:                do {
        -:  286:                    do {
      765:  287:                        rv = write(thefile->filedes, buf, *nbytes);
     765*:  288:                    } while (rv == (apr_size_t)-1 && errno == EINTR);
     765*:  289:                    if (rv == (apr_size_t)-1 &&
    #####:  290:                        (errno == EAGAIN || errno == EWOULDBLOCK)) {
    #####:  291:                        *nbytes /= 2; /* yes, we'll loop if kernel lied
        -:  292:                                       * and we can't even write 1 byte
        -:  293:                                       */
        -:  294:                    }
        -:  295:                    else {
        -:  296:                        break;
        -:  297:                    }
        -:  298:                } while (1);
        -:  299:            }
        -:  300:        }
        -:  301:#endif
    68820:  302:        if (rv == (apr_size_t)-1) {
        2:  303:            (*nbytes) = 0;
        2:  304:            return errno;
        -:  305:        }
    68818:  306:        *nbytes = rv;
    68818:  307:        return APR_SUCCESS;
        -:  308:    }
        -:  309:}
        -:  310:
       10:  311:APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile, const struct iovec *vec,
        -:  312:                                          apr_size_t nvec, apr_size_t *nbytes)
        -:  313:{
        -:  314:#ifdef HAVE_WRITEV
        -:  315:    apr_status_t rv;
        -:  316:    apr_ssize_t bytes;
        -:  317:
       10:  318:    if (thefile->buffered) {
       4*:  319:        file_lock(thefile);
        -:  320:
        4:  321:        rv = apr_file_flush_locked(thefile);
        4:  322:        if (rv != APR_SUCCESS) {
    #####:  323:            file_unlock(thefile);
    #####:  324:            return rv;
        -:  325:        }
        4:  326:        if (thefile->direction == 0) {
        -:  327:            /* Position file pointer for writing at the offset we are
        -:  328:             * logically reading from
        -:  329:             */
        2:  330:            apr_int64_t offset = thefile->filePtr - thefile->dataRead +
        2:  331:                                 thefile->bufpos;
        2:  332:            if (offset != thefile->filePtr) {
        2:  333:                thefile->filePtr = lseek(thefile->filedes, offset, SEEK_SET);
       2*:  334:                if (thefile->filePtr == -1) rv = errno;
        -:  335:            }
        2:  336:            thefile->bufpos = thefile->dataRead = 0;
        -:  337:        }
        -:  338:
       4*:  339:        file_unlock(thefile);
       4*:  340:        if (rv) return rv;
        -:  341:    }
        -:  342:
       10:  343:    rv = file_rotating_check(thefile);
       10:  344:    if (rv != APR_SUCCESS) {
    #####:  345:        return rv;
        -:  346:    }
        -:  347:
       10:  348:    if ((bytes = writev(thefile->filedes, vec, nvec)) < 0) {
    #####:  349:        *nbytes = 0;
    #####:  350:        rv = errno;
        -:  351:    }
        -:  352:    else {
       10:  353:        *nbytes = bytes;
       10:  354:        rv = APR_SUCCESS;
        -:  355:    }
       10:  356:    return rv;
        -:  357:#else
        -:  358:    /**
        -:  359:     * The problem with trying to output the entire iovec is that we cannot
        -:  360:     * maintain the behaviour that a real writev would have.  If we iterate
        -:  361:     * over the iovec one at a time, we lose the atomic properties of
        -:  362:     * writev().  The other option is to combine the entire iovec into one
        -:  363:     * buffer that we could then send in one call to write().  This is not
        -:  364:     * reasonable since we do not know how much data an iovec could contain.
        -:  365:     *
        -:  366:     * The only reasonable option, that maintains the semantics of a real
        -:  367:     * writev(), is to only write the first iovec.  Callers of file_writev()
        -:  368:     * must deal with partial writes as they normally would. If you want to
        -:  369:     * ensure an entire iovec is written, use apr_file_writev_full().
        -:  370:     */
        -:  371:
        -:  372:    *nbytes = vec[0].iov_len;
        -:  373:    return apr_file_write(thefile, vec[0].iov_base, nbytes);
        -:  374:#endif
        -:  375:}
        -:  376:
       12:  377:APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile)
        -:  378:{
       12:  379:    apr_size_t nbytes = 1;
        -:  380:
       12:  381:    return apr_file_write(thefile, &ch, &nbytes);
        -:  382:}
        -:  383:
        6:  384:APR_DECLARE(apr_status_t) apr_file_ungetc(char ch, apr_file_t *thefile)
        -:  385:{
        6:  386:    thefile->ungetchar = (unsigned char)ch;
        6:  387:    return APR_SUCCESS;
        -:  388:}
        -:  389:
       24:  390:APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile)
        -:  391:{
       24:  392:    apr_size_t nbytes = 1;
        -:  393:
       24:  394:    return apr_file_read(thefile, ch, &nbytes);
        -:  395:}
        -:  396:
    20046:  397:APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile)
        -:  398:{
    20046:  399:    return apr_file_write_full(thefile, str, strlen(str), NULL);
        -:  400:}
        -:  401:
      218:  402:apr_status_t apr_file_flush_locked(apr_file_t *thefile)
        -:  403:{
      218:  404:    apr_status_t rv = APR_SUCCESS;
        -:  405:
      218:  406:    if (thefile->direction == 1 && thefile->bufpos) {
      146:  407:        apr_ssize_t written = 0, ret;
        -:  408:
        -:  409:        do {
      146:  410:            ret = write(thefile->filedes, thefile->buffer + written,
      146:  411:                        thefile->bufpos - written);
      146:  412:            if (ret > 0)
      134:  413:                written += ret;
      146:  414:        } while (written < thefile->bufpos &&
       12:  415:                 (ret > 0 || (ret == -1 && errno == EINTR)));
      146:  416:        if (ret == -1) {
       12:  417:            rv = errno;
        -:  418:        } else {
      134:  419:            thefile->filePtr += written;
      134:  420:            thefile->bufpos = 0;
        -:  421:        }
        -:  422:    }
        -:  423:
      218:  424:    return rv;
        -:  425:}
        -:  426:
       80:  427:APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile)
        -:  428:{
       80:  429:    apr_status_t rv = APR_SUCCESS;
        -:  430:
       80:  431:    if (thefile->buffered) {
       80:  432:        file_lock(thefile);
       80:  433:        rv = apr_file_flush_locked(thefile);
       80:  434:        file_unlock(thefile);
        -:  435:    }
        -:  436:    /* There isn't anything to do if we aren't buffering the output
        -:  437:     * so just return success.
        -:  438:     */
       80:  439:    return rv;
        -:  440:}
        -:  441:
    #####:  442:APR_DECLARE(apr_status_t) apr_file_sync(apr_file_t *thefile)
        -:  443:{
    #####:  444:    apr_status_t rv = APR_SUCCESS;
        -:  445:
    #####:  446:    file_lock(thefile);
        -:  447:
    #####:  448:    if (thefile->buffered) {
    #####:  449:        rv = apr_file_flush_locked(thefile);
        -:  450:
    #####:  451:        if (rv != APR_SUCCESS) {
    #####:  452:            file_unlock(thefile);
    #####:  453:            return rv;
        -:  454:        }
        -:  455:    }
        -:  456:
    #####:  457:    if (fsync(thefile->filedes)) {
    #####:  458:        rv = apr_get_os_error();
        -:  459:    }
        -:  460:
    #####:  461:    file_unlock(thefile);
        -:  462:
    #####:  463:    return rv;
        -:  464:}
        -:  465:
        4:  466:APR_DECLARE(apr_status_t) apr_file_datasync(apr_file_t *thefile)
        -:  467:{
        4:  468:    apr_status_t rv = APR_SUCCESS;
        4:  469:    int os_status = 0;
        -:  470:
       4*:  471:    file_lock(thefile);
        -:  472:
        4:  473:    if (thefile->buffered) {
    #####:  474:        rv = apr_file_flush_locked(thefile);
        -:  475:
    #####:  476:        if (rv != APR_SUCCESS) {
    #####:  477:            file_unlock(thefile);
    #####:  478:            return rv;
        -:  479:        }
        -:  480:    }
        -:  481:
        -:  482:#ifdef HAVE_FDATASYNC
        4:  483:    os_status = fdatasync(thefile->filedes);
        -:  484:#elif defined(F_FULLFSYNC)
        -:  485:    os_status = fcntl(thefile->filedes, F_FULLFSYNC);
        -:  486:    if (os_status) {
        -:  487:        /* Fall back to fsync() if the device doesn't support F_FULLFSYNC. */
        -:  488:        os_status = fsync(thefile->filedes);
        -:  489:    }
        -:  490:#else
        -:  491:    os_status = fsync(thefile->filedes);
        -:  492:#endif
        4:  493:    if (os_status) {
        2:  494:        rv = apr_get_os_error();
        -:  495:    }
        -:  496:
       4*:  497:    file_unlock(thefile);
        -:  498:
        4:  499:    return rv;
        -:  500:}
        -:  501:
       58:  502:APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len, apr_file_t *thefile)
        -:  503:{
       58:  504:    apr_status_t rv = APR_SUCCESS; /* get rid of gcc warning */
        -:  505:    apr_size_t nbytes;
       58:  506:    const char *str_start = str;
       58:  507:    char *final = str + len - 1;
        -:  508:
       58:  509:    if (len <= 1) {
        -:  510:        /* sort of like fgets(), which returns NULL and stores no bytes
        -:  511:         */
    #####:  512:        return APR_SUCCESS;
        -:  513:    }
        -:  514:
        -:  515:    /* If we have an underlying buffer, we can be *much* more efficient
        -:  516:     * and skip over the apr_file_read calls.
        -:  517:     */
       58:  518:    if (thefile->buffered) {
       16:  519:        file_lock(thefile);
        -:  520:
       16:  521:        if (thefile->direction == 1) {
        2:  522:            rv = apr_file_flush_locked(thefile);
        2:  523:            if (rv) {
       2*:  524:                file_unlock(thefile);
        2:  525:                return rv;
        -:  526:            }
        -:  527:
    #####:  528:            thefile->direction = 0;
    #####:  529:            thefile->bufpos = 0;
    #####:  530:            thefile->dataRead = 0;
        -:  531:        }
        -:  532:
     8262:  533:        while (str < final) { /* leave room for trailing '\0' */
        -:  534:            /* Force ungetc leftover to call apr_file_read. */
     8262:  535:            if (thefile->bufpos < thefile->dataRead &&
     8246:  536:                thefile->ungetchar == -1) {
     8246:  537:                *str = thefile->buffer[thefile->bufpos++];
        -:  538:            }
        -:  539:            else {
       16:  540:                nbytes = 1;
       16:  541:                rv = file_read_buffered(thefile, str, &nbytes);
       16:  542:                if (rv != APR_SUCCESS) {
       10:  543:                    break;
        -:  544:                }
        -:  545:            }
     8252:  546:            if (*str == '\n') {
        4:  547:                ++str;
        4:  548:                break;
        -:  549:            }
     8248:  550:            ++str;
        -:  551:        }
       14:  552:        file_unlock(thefile);
        -:  553:    }
        -:  554:    else {
      156:  555:        while (str < final) { /* leave room for trailing '\0' */
      152:  556:            nbytes = 1;
      152:  557:            rv = apr_file_read(thefile, str, &nbytes);
      152:  558:            if (rv != APR_SUCCESS) {
       24:  559:                break;
        -:  560:            }
      128:  561:            if (*str == '\n') {
       14:  562:                ++str;
       14:  563:                break;
        -:  564:            }
      114:  565:            ++str;
        -:  566:        }
        -:  567:    }
        -:  568:
        -:  569:    /* We must store a terminating '\0' if we've stored any chars. We can
        -:  570:     * get away with storing it if we hit an error first.
        -:  571:     */
       56:  572:    *str = '\0';
       56:  573:    if (str > str_start) {
        -:  574:        /* we stored chars; don't report EOF or any other errors;
        -:  575:         * the app will find out about that on the next call
        -:  576:         */
       28:  577:        return APR_SUCCESS;
        -:  578:    }
       28:  579:    return rv;
        -:  580:}
        -:  581:
        -:  582:
        -:  583:
        4:  584:APR_DECLARE(apr_status_t) apr_file_pipe_wait(apr_file_t *thepipe, apr_wait_type_t direction)
        -:  585:{
        4:  586:    return apr_wait_for_io_or_timeout(thepipe, NULL, direction == APR_WAIT_READ);
        -:  587:}
