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

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

本文简介:

#if APR_HAS_THREADS
        if (thefile->thlock) {
            apr_thread_mutex_unlock(thefile->thlock);
        }
#endif
        return rv;
    }
 
在apr_file_read函数的基础之上,APR提供了额外的几个辅助函数:
APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile);
用以从文件中获取一个字符
■ APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *thefile, void *buf,
                                             apr_size_t nbytes,
                                             apr_size_t *bytes_read)
apr_file_read函数通常用于读取指定字节的数据,并且永远不会超过这个指定值,但是实际的读取字节数则可以少于这个值。
而apr_file_read_full与apr_file_read非常类似,主要的区别就是,对于指定的字节数nbytes,函数必须全部读取完毕才肯罢休,任何时候如果发现读取得字节数小于nbytes,函数都将继续等待。这种读取方式我们称之为“全字节读取方式”。
4.5.2字符串读取
 
4.5.3文件写入
文件写入通过函数apr_file_write实现,与标准的写入函数相同,它也具有三个参数:
APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes)
thefile是写入的文件描述符,buf是写入的源目标字符串,nbytes是需要写入的字节总数目。
与读取类似,写入也分为缓冲写入和非缓冲写入两种。对于非缓冲写入,所有的数据是直接写入到文件中,而对于缓冲写入,所有的数据先写入到文件缓冲区中,然后再从缓冲区中写入到文件中。
对于非缓冲写入的核心代码如下所示:
        apr_size_t rv;
 
        do {
            rv = write(thefile->filedes, buf, *nbytes);
        } while (rv == (apr_size_t)-1 && errno == EINTR);
#ifdef USE_WAIT_FOR_IO
        if (rv == (apr_size_t)-1 &&
            (errno == EAGAIN || errno == EWOULDBLOCK) &&
            thefile->timeout != 0) {
            apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 0);
            if (arv != APR_SUCCESS) {
                *nbytes = 0;
                return arv;
            }
            else {

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

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

go top