⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readwrite.c

📁 Apache官方在今天放出产品系列2.2的最新版本2.2.11的源码包 最流行的HTTP服务器软件之一
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include "apr_arch_file_io.h"#include "apr_file_io.h"#include "apr_general.h"#include "apr_strings.h"#include "apr_lib.h"#include "apr_errno.h"#include <malloc.h>#include "apr_arch_atime.h"#include "apr_arch_misc.h"/* * read_with_timeout()  * Uses async i/o to emulate unix non-blocking i/o with timeouts. */static apr_status_t read_with_timeout(apr_file_t *file, void *buf, apr_size_t len_in, apr_size_t *nbytes){    apr_status_t rv;    DWORD res;    DWORD len = (DWORD)len_in;    DWORD bytesread = 0;    /* Handle the zero timeout non-blocking case */    if (file->timeout == 0) {        /* Peek at the pipe. If there is no data available, return APR_EAGAIN.         * If data is available, go ahead and read it.         */        if (file->pipe) {            DWORD bytes;            if (!PeekNamedPipe(file->filehand, NULL, 0, NULL, &bytes, NULL)) {                rv = apr_get_os_error();                if (rv == APR_FROM_OS_ERROR(ERROR_BROKEN_PIPE)) {                    rv = APR_EOF;                }                *nbytes = 0;                return rv;            }            else {                if (bytes == 0) {                    *nbytes = 0;                    return APR_EAGAIN;                }                if (len > bytes) {                    len = bytes;                }            }        }        else {            /* ToDo: Handle zero timeout non-blocking file i/o              * This is not needed until an APR application needs to             * timeout file i/o (which means setting file i/o non-blocking)             */        }    }    if (file->pOverlapped && !file->pipe) {        file->pOverlapped->Offset     = (DWORD)file->filePtr;        file->pOverlapped->OffsetHigh = (DWORD)(file->filePtr >> 32);    }    if (ReadFile(file->filehand, buf, len,                  &bytesread, file->pOverlapped)) {        rv = APR_SUCCESS;    }    else {        rv = apr_get_os_error();        if (rv == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {            /* Wait for the pending i/o, timeout converted from us to ms             * Note that we loop if someone gives up the event, since             * folks suggest that WAIT_ABANDONED isn't actually a result             * but an alert that ownership of the event has passed from             * one owner to a new proc/thread.             */            do {                res = WaitForSingleObject(file->pOverlapped->hEvent,                                           (file->timeout > 0)                                            ? (DWORD)(file->timeout/1000)                                            : ((file->timeout == -1)                                                  ? INFINITE : 0));            } while (res == WAIT_ABANDONED);            /* There is one case that represents entirely             * successful operations, otherwise we will cancel             * the operation in progress.             */            if (res != WAIT_OBJECT_0) {                CancelIo(file->filehand);            }            /* Ignore any failures above.  Attempt to complete             * the overlapped operation and use only _its_ result.             * For example, CancelIo or WaitForSingleObject can             * fail if the handle is closed, yet the read may have             * completed before we attempted to CancelIo...             */            if (GetOverlappedResult(file->filehand, file->pOverlapped,                                     &bytesread, TRUE)) {                rv = APR_SUCCESS;            }            else {                rv = apr_get_os_error();                if (((rv == APR_FROM_OS_ERROR(ERROR_IO_INCOMPLETE))                        || (rv == APR_FROM_OS_ERROR(ERROR_OPERATION_ABORTED)))                    && (res == WAIT_TIMEOUT))                    rv = APR_TIMEUP;            }        }        if (rv == APR_FROM_OS_ERROR(ERROR_BROKEN_PIPE)) {            /* Assume ERROR_BROKEN_PIPE signals an EOF reading from a pipe */            rv = APR_EOF;        } else if (rv == APR_FROM_OS_ERROR(ERROR_HANDLE_EOF)) {            /* Did we hit EOF reading from the handle? */            rv = APR_EOF;        }    }        /* OK and 0 bytes read ==> end of file */    if (rv == APR_SUCCESS && bytesread == 0)        rv = APR_EOF;        if (rv == APR_SUCCESS && file->pOverlapped && !file->pipe) {        file->filePtr += bytesread;    }    *nbytes = bytesread;    return rv;}APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *len){    apr_status_t rv;    DWORD bytes_read = 0;    if (*len <= 0) {        *len = 0;        return APR_SUCCESS;    }    /* If the file is open for xthread support, allocate and     * initialize the overlapped and io completion event (hEvent).      * Threads should NOT share an apr_file_t or its hEvent.     */    if ((thefile->flags & APR_XTHREAD) && !thefile->pOverlapped ) {        thefile->pOverlapped = (OVERLAPPED*) apr_pcalloc(thefile->pool,                                                          sizeof(OVERLAPPED));        thefile->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);        if (!thefile->pOverlapped->hEvent) {            rv = apr_get_os_error();            return rv;        }    }    /* Handle the ungetchar if there is one */    if (thefile->ungetchar != -1) {        bytes_read = 1;        *(char *)buf = (char)thefile->ungetchar;        buf = (char *)buf + 1;        (*len)--;        thefile->ungetchar = -1;        if (*len == 0) {            *len = bytes_read;            return APR_SUCCESS;        }    }    if (thefile->buffered) {        char *pos = (char *)buf;        apr_size_t blocksize;        apr_size_t size = *len;        apr_thread_mutex_lock(thefile->mutex);        if (thefile->direction == 1) {            rv = apr_file_flush(thefile);            if (rv != APR_SUCCESS) {                apr_thread_mutex_unlock(thefile->mutex);                return rv;            }            thefile->bufpos = 0;            thefile->direction = 0;            thefile->dataRead = 0;        }        rv = 0;        while (rv == 0 && size > 0) {            if (thefile->bufpos >= thefile->dataRead) {                apr_size_t read;                rv = read_with_timeout(thefile, thefile->buffer,                                        thefile->bufsize, &read);                if (read == 0) {                    if (rv == APR_EOF)                        thefile->eof_hit = TRUE;                    break;                }                else {                    thefile->dataRead = read;                    thefile->filePtr += thefile->dataRead;                    thefile->bufpos = 0;                }            }            blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;            memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);            thefile->bufpos += blocksize;            pos += blocksize;            size -= blocksize;        }        *len = pos - (char *)buf;        if (*len) {            rv = APR_SUCCESS;        }        apr_thread_mutex_unlock(thefile->mutex);    } else {          /* Unbuffered i/o */        apr_size_t nbytes;        rv = read_with_timeout(thefile, buf, *len, &nbytes);        if (rv == APR_EOF)            thefile->eof_hit = TRUE;        *len = nbytes;    }    return rv;}APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes){    apr_status_t rv;    DWORD bwrote;    /* If the file is open for xthread support, allocate and     * initialize the overlapped and io completion event (hEvent).      * Threads should NOT share an apr_file_t or its hEvent.     */    if ((thefile->flags & APR_XTHREAD) && !thefile->pOverlapped ) {        thefile->pOverlapped = (OVERLAPPED*) apr_pcalloc(thefile->pool,                                                          sizeof(OVERLAPPED));        thefile->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);        if (!thefile->pOverlapped->hEvent) {            rv = apr_get_os_error();            return rv;        }    }    if (thefile->buffered) {        char *pos = (char *)buf;        apr_size_t blocksize;        apr_size_t size = *nbytes;        apr_thread_mutex_lock(thefile->mutex);        if (thefile->direction == 0) {            // Position file pointer for writing at the offset we are logically reading from            apr_off_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;            DWORD offlo = (DWORD)offset;            DWORD offhi = (DWORD)(offset >> 32);            if (offset != thefile->filePtr)                SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);            thefile->bufpos = thefile->dataRead = 0;            thefile->direction = 1;        }        rv = 0;        while (rv == 0 && size > 0) {            if (thefile->bufpos == thefile->bufsize)   // write buffer is full                rv = apr_file_flush(thefile);            blocksize = size > thefile->bufsize - thefile->bufpos ?                                      thefile->bufsize - thefile->bufpos : size;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -