apache中的文件与目录(2)[14]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

            } while (written == -1 && errno == EINTR);
            if (written == -1) {
                return errno;
            }
            thefile->filePtr += written;
            thefile->bufpos = 0;
        }
    }
    return APR_SUCCESS;
}
函数中所作的无非就是不断调用write写入。写入成功后filePtr和缓冲区的开始位置,这两个步骤都被隐藏在该函数中,因此你在写入函数中看不到也就不奇怪了。
2)、缓冲区的空闲空间足够写入,即size <= thefile->bufsize - thefile->bufpos。此时,直接调用memcpy将用户缓冲区中的数据拷贝到文件缓冲区中,并调整用户缓冲区pos和文件缓冲区bufpos的位置。
3)、用户要求写入的数据空闲空间不够一次写入,此时将分为多次写入。如果写入过程中空间已满,使用1)的方法,否则使用2)的方法。
 
在该函数的基础之上,APR还提供了一些辅助扩充函数:
■ APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile);
用以向文件中写入一个字符。
■ APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile)
用以向文件中写入一个字符串。
■ APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile, const struct iovec *vec,
                                          apr_size_t nvec, apr_size_t *nbytes)
批量文件写入。需要批量写入的数据保存在vec中。如果系统中定义了writev函数,则调用writev函数批量写入。如果系统不支持writev函数,那么如果要写入整个iovec数组,可以使用两种变通策略:
第一种就是逐一遍历iovec数组中的每一个元素并将其写入到文件中。这种做法存在一个问题,就是原子性写入。Writev函数写入所有数据的时候是保持原子性的。而迭代则明显无法保持这种特性。
另一种可选策略就是首先将iovec数组中的数据集中写入到一个缓冲区中,然后再将该缓冲区写入到文件中。这种策略也是不合理的,因此你根本就不知道一个iovec数组中会包含多少数据,你也就无法确定缓冲区的大小。
为了保持writev的真正语义,最合理的策略就是仅写入iovec数组中的第一个数据,即vec[0]。Callers of file_writev() must deal with partial writes as they normally would. If you want to ensure an entire iovec is written, use apr_file_writev_full()。
■APR_DECLARE(apr_status_t) apr_file_write_full(apr_file_t *thefile,
                                              const void *buf,
                                              apr_size_t nbytes,
                                              apr_size_t *bytes_written)
与apr_file_read_full类似,该函数使得写入的字符串必须达到指定的nbytes,任何时候如果写入的字符数小于nbytes,函数都将等待。这种方式我们称之为“全字节写入方式”。
¢ APR_DECLARE(apr_status_t) apr_file_writev_full(apr_file_t *thefile,
                                               const struct iovec *vec,

本文关键:apache中的文件与目录(2)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top