实现流
php的流最强力的特性之一是它可以访问众多数据源: 普通文件, 压缩文件, 网络透明通道, 加密网络, 命名管道以及域套接字, 它们对于用户空间以及内部都是统一的api.
php流的表象之下
对于给定的流实例, 比如文件流和网络流, 它们的不同在于上一章你使用的流创建函数返回的php_stream结构体中的ops成员.
typedef struct _php_stream {
...
php_stream_ops *ops;
...
} php_stream;
php_stream_ops结构体定义的是一个函数指针集合以及一个描述标记.
typedef struct _php_stream_ops {
size_t (*write)(php_stream *stream, const char *buf,
size_t count tsrmls_dc);
size_t (*read)(php_stream *stream, char *buf,
size_t count tsrmls_dc);
int (*close)(php_stream *stream, int close_handle
tsrmls_dc);
int (*flush)(php_stream *stream tsrmls_dc);
const char *label;
int (*seek)(php_stream *stream, off_t offset, int whence,
off_t *newoffset tsrmls_dc);
int (*cast)(php_stream *stream, int castas, void **ret
tsrmls_dc);
int (*stat)(php_stream *stream, php_stream_statbuf *ssb
tsrmls_dc);
int (*set_option)(php_stream *stream, int option,int value,
void *ptrparam tsrmls_dc);
} php_stream_ops;
当流访问函数比如php_stream_read()被调用时, 流包装层实际上解析调用了stream->ops中对应的函数, 这样实际调用的就是当前流类型特有的read实现. 比如, 普通文件的流ops结构体中的read函数实现如下(实际的该实现比下面的示例复杂一点):
size_t php_stdio_read(php_stream *stream, char *buf,
size_t count tsrmls_dc)
{
php_stdio_stream_data *data =
(php_stdio_stream_data*)stream->abstract;
return read(data->fd, buf, count);
}
而compress.zlib流使用的ops结构体中则read则指向的是如下的函数:
size_t php_zlib_read(php_stream *stream, char *buf,
size_t count tsrmls_dc)
{
struct php_gz_stream_data_t *data =
(struct php_gz_stream_data_t *) stream->abstract;
return gzread(data->gz_file, buf, count);
}
这里第一点需要注意的是ops结构体指向的函数指针常常是对数据源真正的读取函数的一个瘦代理. 在上面两个例子中, 标准i/o流使用posix的read()函数, 而zlib流使用的是libz的gzread()函数.
你可能还注意到了, 这里使用了stream->abstract元素. 这是流实现的一个便利指针, 它可以被用于获取各种相关的捆绑信息. 在上面的例子中, 指向自定义结构体的指针, 用于存储底层read函数要使用的文件描述符.
还有一件你可能注意到的事情是php_stream_ops结构体中的每个函数都期望一个已有的流实例, 但是怎样得到实例呢? abstract成员是怎样设置的以及什么时候流指示使用哪个ops结构体? 答案就在你在上一章使用过的第一个打开流的函数(php_stream_open_wrapper())中.
当这个函数被调用时, php的流包装层尝试基于传递的url中的scheme://部分确定请求的是什么协议. 这样它就可以在已注册的php包装器中查找对应的php_stream_wrapper项. 每个php_stream_wrapper结构体都可以取到自己的ops元素, 它指向一个php_stream_wrapper_ops结构体:
typedef struct _php_stream_wrapper_ops {
php_stream *(*stream_opener)(php_stream_wrapper *wrapper,
char *filename, char *mode,
int options, char **opened_path,
php_stream_context *context
streams_dc tsrmls_dc);
int (*stream_closer)(php_stream_wrapper *wrapper,
php_stream *stream tsrmls_dc);
int (*stream_stat)(php_stream_wrapper *wrapper,
php_stream *stream,
php_stream_statbuf *ssb
tsrmls_dc);
int (*url_stat)(php_stream_wrapper *wrapper,
char *url, int flags,
php_stream_statbuf *ssb,
php_stream_context *context
tsrmls_dc);
php_stream *(*dir_opener)(php_stream_wrapper *wrapper,
char *filename, char *mode,
int options, char **opened_path,
php_stream_context *context
streams_dc tsrmls_dc);
const char *label;
int (*unlink)(php_stream_wrapper *wrapper, char *url,
int options,
php_stream_context *context
tsrmls_dc);
int (*rename)(php_stream_wrapper *wrapper,
char *url_from, char *url_to,
int options,
php_stream_context *context
tsrmls_dc);
int (*stream_mkdir)(php_stream_wrapper *wrapper,
char *url, int mode, int options,
php_stream_context *context
tsrmls_dc);
int (*stream_rmdir)(php_stream_wrapper *wrapper, char *url,
int options,
php_stream_context *context
tsrmls_dc);
} php_stream_wrapper_ops;
这里, 流包装层调用wrapper->ops->stream_opener(), 它将执行包装器特有的操作创建流实例, 赋值恰当的php_stream_ops结构体, 绑定相关的抽象数据.
dir_opener()函数和stream_opener()提供相同的基础服务; 不过, 它是对php_stream_opendir()这个api调用的响应, 并且通常会绑定一个不同的php_stream_ops结构体到返回的实例. stat()和close()函数在这一层上是重复的, 这样做是为了给包装器的这些操作增加协议特有的逻辑.
其他的函数则允许执行静态流操作而不用实际的创建流实例. 回顾这些流api调用, 它们并不实际返回php_stream对象, 你马上就会看到它们的细节.
尽管在php 4.3中引入流包装层时, url_stat在内部作为一个包装器的ops函数存在, 但直到php 5.0它才开始被使用. 此外, 最后的3个函数, rename(), stream_mkdir()以及stream_rmdir()一直到php 5.0才引入, 在这个版本之前, 它们并不在包装器的ops结构中.
包装器操作
除了url_stat()函数, 包装器操作中在const char *label元素之前的每个操作都可以用于激活的流实例上. 每个函数的意义如下:
stream_opener()实例化一个流实例. 当某个用户空间的fopen()函数被调用时, 这个函
数将被调用. 这个函数返回的php_stream实例是fopen()函数返回的
文件资源句柄的内部表示. 集成函数比如file(), file_get_contents(),
file_put_contents(), readfile()等等, 在请求包装资源时, 都使用这个包
装器ops.
stream_closer()当一个流实例结束其生命周期时这个函数被调用. stream_opener()时
分配的所有资源都应该在这个函数中被释放.
stream_stat()类似于用户空间的fstat()函数, 这个函数应该填充ssb结构体(实际上
只包含一个struct statbuf sb结构体成员),
dir_opener()和stream_opener()行为一致, 不过它是调用opendir()一族的用户空间
函数时被调用的. 目录流使用的底层流实现和文件流遵循相同的规则;
不过目录流只需要返回包含在打开的目录中找到的文件名的记录, 它
的大小为struct dirent这个结构体的大小.
静态包装器操作
包装器操作函数中的其他函数是在uri路径上执行原子操作, 具体取决于包装器协议. 在php4.3的php_stream_wrapper_ops结构体中只有url_stat()和unlink(); 其他的方式是到php 5.0后才定义的, 编码时应该适时的使用#ifdef块说明.
url_stat()stat()族函数使用, 返回文件元数据, 比如访问授权, 大小, 类型; 以及
访问, 修改,创建时间. 尽管这个函数是在php 4.3引入流包装层时出现
在php_stream_wrapper_ops结构体中的, 但直到php 5.0才被用户空
间的stat()函数使用.
unlink()和posix文件系统的同名函数语义相同, 它执行文件删除. 如果对于当
前的包装器删除没有意义, 比如内建的http://包装器, 这个函数应该被
定义为null, 以便内核去引发适当的错误消息.
rename()当用户空间的rename()函数的参数$from和$to参数指向的是相同的
底层包装器实现, php则将这个重命名请求分发到包装器的rename函
数.
mkdir() & rmdir()这两个函数直接映射到对应的用户空间函数.
实现一个包装器
为了演示包装器和流操作的内部工作原理, 我们需要重新实现php手册的stream_wrapper_register()一页示例中的var://包装器.
此刻, 首先从下面功能完整的变量流包装实现开始. 构建他, 并开始检查每一块的工作原理.
译注: 为了方便大家阅读, 对代码的注释进行了适量补充调整, 此外, 由于phpapi的调整, 原著中的代码不能直接在译者使用的php-5.4.10中运行, 进行了适当的修改. 因此下面代码结构可能和原著略有不同, 请参考阅读.(下面opendir的例子也进行了相应的修改)
config.m4
php_arg_enable(varstream,whether to enable varstream support,
[ enable-varstream enable varstream support])
if test "$php_varstream" = "yes"; then
ac_define(have_varstream,1,[whether you want varstream])
php_new_extension(varstream, varstream.c, $ext_shared)
fi
php_varstream.h
#ifndef php_varstream_h
#define php_varstream_h
extern zend_module_entry varstream_module_entry;
#define phpext_varstream_ptr &varstream_module_entry
#ifdef php_win32
# define php_varstream_api __declspec(dllexport)
#elif defined(__gnuc__) && __gnuc__ >= 4
# define php_varstream_api __attribute__ ((visibility("default")))
#else
# define php_varstream_api
#endif
#ifdef zts
#include "tsrm.h"
#endif
php_minit_function(varstream);
php_mshutdown_function(varstream);
#define php_varstream_wrapper "var"
#define php_varstream_streamtype "varstream"
/* 变量流的抽象数据结构 */
typedef struct _php_varstream_data {
off_t position;
char *varname;
int varname_len;
} php_varstream_data;
#ifdef zts
#define varstream_g(v) tsrmg(varstream_globals_id, zend_varstream_globals *, v)
#else
#define varstream_g(v) (varstream_globals.v)
#endif
#endif
varstream.c
#ifdef have_config_h
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/url.h"
#include "php_varstream.h"
static size_t php_varstream_write(php_stream *stream,
const char *buf, size_t count tsrmls_dc)
{
php_varstream_data *data = stream->abstract;
zval **var;
size_t newlen;
/* 查找变量 */
if (zend_hash_find(&eg(symbol_table), data->varname,
data->varname_len + 1,(void**)&var) == failure) {
/* 变量不存在, 直接创建一个字符串类型的变量, 并保存新传递进来的内容 */
zval *newval;
make_std_zval(newval);
zval_stringl(newval, buf, count, 1);
/* 将新的zval *放到变量中 */
zend_hash_add(&eg(symbol_table), data->varname,
data->varname_len + 1, (void*)&newval,
sizeof(zval*), null);
return count;
}
/* 如果需要, 让变量可写. 这里实际上处理的是写时复制 */
separate_zval_if_not_ref(var);
/* 转换为字符串类型 */
convert_to_string_ex(var);
/* 重置偏移量(译注: 相比于正常的文件系统, 这里的处理实际上不支持文件末尾的空洞创建, 读者如果熟悉*nix文件系统, 应该了解译者所说, 否则请略过) */
if (data->position > z_strlen_pp(var)) {
data->position = z_strlen_pp(var);
}
/* 计算新的字符串长度 */
newlen = data->position + count;
if (newlen < z_strlen_pp(var)) {
/* 总长度不变 */
newlen = z_strlen_pp(var);
} else if (newlen > z_strlen_pp(var)) {
/* 重新调整缓冲区大小以保存新内容 */
z_strval_pp(var) =erealloc(z_strval_pp(var),newlen+1);
/* 更新字符串长度 */
z_strlen_pp(var) = newlen;
/* 确保字符串null终止 */
z_strval_pp(var)[newlen] = 0;
}
/* 将数据写入到变量中 */
memcpy(z_strval_pp(var) + data->position, buf, count);
data->position += count;
return count;
}
static size_t php_varstream_read(php_stream *stream,
char *buf, size_t count tsrmls_dc)
{
php_varstream_data *data = stream->abstract;
zval **var, copyval;
int got_copied = 0;
size_t toread = count;
if (zend_hash_find(&eg(symbol_table), data->varname,
data->varname_len + 1, (void**)&var) == failure) {
/* 变量不存在, 读不到数据, 返回0字节长度 */
return 0;
}
copyval = **var;
if (z_type(copyval) != is_string) {
/* 对于非字符串类型变量, 创建一个副本进行读, 这样对于只读的变量, 就不会改变其原始类型 */
zval_copy_ctor(©val);
init_pzval(©val);
got_copied = 1;
}
if (data->position > z_strlen(copyval)) {
data->position = z_strlen(copyval);
}
if ((z_strlen(copyval) - data->position) < toread) {
/* 防止读取到变量可用缓冲区外的内容 */
toread = z_strlen(copyval) - data->position;
}
/* 设置缓冲区 */
memcpy(buf, z_strval(copyval) + data->position, toread);
data->position += toread;
/* 如果创建了副本, 则释放副本 */
if (got_copied) {
zval_dtor(©val);
}
/* 返回设置到缓冲区的字节数 */
return toread;
}
static int php_varstream_closer(php_stream *stream,
int close_handle tsrmls_dc)
{
php_varstream_data *data = stream->abstract;
/* 释放内部结构避免泄露 */
efree(data->varname);
efree(data);
return 0;
}
static int php_varstream_flush(php_stream *stream tsrmls_dc)
{
php_varstream_data *data = stream->abstract;
zval **var;
/* 根据不同情况, 重置偏移量 */
if (zend_hash_find(&eg(symbol_table), data->varname,
data->varname_len + 1, (void**)&var)
== success) {
if (z_type_pp(var) == is_string) {
data->position = z_strlen_pp(var);
} else {
zval copyval = **var;
zval_copy_ctor(©val);
convert_to_string(©val);
data->position = z_strlen(copyval);
zval_dtor(©val);
}
} else {
data->position = 0;
}
return 0;
}
static int php_varstream_seek(php_stream *stream, off_t offset,
int whence, off_t *newoffset tsrmls_dc)
{
php_varstream_data *data = stream->abstract;
switch (whence) {
case seek_set:
data->position = offset;
break;
case seek_cur:
data->position += offset;
break;
case seek_end:
{
zval **var;
size_t curlen = 0;
if (zend_hash_find(&eg(symbol_table),
data->varname, data->varname_len + 1,
(void**)&var) == success) {
if (z_type_pp(var) == is_string) {
curlen = z_strlen_pp(var);
} else {
zval copyval = **var;
zval_copy_ctor(©val);
convert_to_string(©val);
curlen = z_strlen(copyval);
zval_dtor(©val);
}
}
data->position = curlen + offset;
break;
}
}
/* 防止随机访问指针移动到缓冲区开始位置之前 */
if (data->position < 0) {
data->position = 0;
}
if (newoffset) {
*newoffset = data->position;
}
return 0;
}
static php_stream_ops php_varstream_ops = {
php_varstream_write,
php_varstream_read,
php_varstream_closer,
php_varstream_flush,
php_varstream_streamtype,
php_varstream_seek,
null, /* cast */
null, /* stat */
null, /* set_option */
};
/* define the wrapper operations */
static php_stream *php_varstream_opener(
php_stream_wrapper *wrapper,
char *filename, char *mode, int options,
char **opened_path, php_stream_context *context
streams_dc tsrmls_dc)
{
php_varstream_data *data;
php_url *url;
if (options & stream_open_persistent) {
/* 按照变量流的定义, 是不能持久化的
* 因为变量在请求结束后将被释放
*/
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unable to open %s persistently",
filename);
return null;
}
/* 标准url解析: scheme://user:pass@host:port/path?query#fragment */
url = php_url_parse(filename);
if (!url) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unexpected error parsing url");
return null;
}
/* 检查是否有变量流url必须的元素host, 以及scheme是否是var */
if (!url->host || (url->host[0] == 0) ||
strcasecmp("var", url->scheme) != 0) {
/* bad url or wrong wrapper */
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "invalid url, must be in the form: "
"var://variablename");
php_url_free(url);
return null;
}
/* 创建一个数据结构保存协议信息(变量流协议重要是变量名, 变量名长度, 当前偏移量) */
data = emalloc(sizeof(php_varstream_data));
data->position = 0;
data->varname_len = strlen(url->host);
data->varname = estrndup(url->host, data->varname_len + 1);
/* 释放前面解析出来的url占用的内存 */
php_url_free(url);
/* 实例化一个流, 为其赋予恰当的流ops, 绑定抽象数据 */
return php_stream_alloc(&php_varstream_ops, data, 0, mode);
}
static php_stream_wrapper_ops php_varstream_wrapper_ops = {
php_varstream_opener, /* 调用php_stream_open_wrapper(sprintf("%s://xxx", php_varstream_wrapper))时执行 */
null, /* stream_close */
null, /* stream_stat */
null, /* url_stat */
null, /* dir_opener */
php_varstream_wrapper,
null, /* unlink */
#if php_major_version >= 5
/* php >= 5.0 only */
null, /* rename */
null, /* mkdir */
null, /* rmdir */
#endif
};
static php_stream_wrapper php_varstream_wrapper = {
&php_varstream_wrapper_ops,
null, /* abstract */
0, /* is_url */
};
php_minit_function(varstream)
{
/* 注册流包装器:
* 1. 检查流包装器名字是否正确(符合这个正则: /^[a-za-z0-9+.-]+$/)
* 2. 将传入的php_varstream_wrapper增加到url_stream_wrappers_hash这个hashtable中, key为php_varstream_wrapper
*/
if (php_register_url_stream_wrapper(php_varstream_wrapper,
&php_varstream_wrapper tsrmls_cc)==failure) {
return failure;
}
return success;
}
php_mshutdown_function(varstream)
{
/* 卸载流包装器: 从url_stream_wrappers_hash中删除 */
if (php_unregister_url_stream_wrapper(php_varstream_wrapper
tsrmls_cc) == failure) {
return failure;
}
return success;
}
zend_module_entry varstream_module_entry = {
#if zend_module_api_no >= 20010901
standard_module_header,
#endif
"varstream",
null,
php_minit(varstream),
php_mshutdown(varstream),
null,
null,
null,
#if zend_module_api_no >= 20010901
"0.1",
#endif
standard_module_properties
};
#ifdef compile_dl_varstream
zend_get_module(varstream)
#endif
在构建加载扩展后, php就可以处理以var://开始的url的请求, 它的行为和手册中用户空间实现的行为一致.
内部实现
首先你注意到的可能是这个扩展完全没有暴露用户空间函数. 它所做的只是在minit函数中调用了一个核心phpapi的钩子, 将var协议和我们定义的包装器关联起来:
static php_stream_wrapper php_varstream_wrapper = {
&php_varstream_wrapper_ops,
null, /* abstract */
0, /* is_url */
}
很明显, 最重要的元素就是ops, 它提供了访问特定流包装器的创建以及检查函数. 你可以安全的忽略abstract属性, 它仅在运行时使用, 在初始化定义时, 它只是作为一个占位符. 第三个元素is_url, 它告诉php在使用这个包装器时是否考虑php.ini中的allow_url_fopen选项. 如果这个值非0, 并且将allow_url_fopen设置为false, 则这个包装器不能被脚本使用.
在本章前面你已经知道, 调用用户空间函数比如fopen将通过这个包装器的ops元素得到php_varstream_wrapper_ops, 这样去调用流的打开函数php_varstream_opener.
这个函数的第一块代码检查是否请求持久化的流:
if (options & stream_open_persistent) {
对于很多包装器这样的请求是合法的. 然而目前的情况这个行为没有意义. 一方面用户空间变量的定义就是临时的, 另一方面, varstream的实例化代价很低, 这就使得持久化的优势很小.
像流包装层报告错误很简单, 只需要返回一个null值而不是流实例即可. 流包装层透出到用户空间的失败消息并不会说明具体的错误, 只是说明不能打开url. 要想给开发者暴露更多的错误信息, 可以在返回之前使用php_stream_wrapper_log_error()函数.
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unable to open %s persistently",
filename);
return null;
url解析
实例化varstream的下一步需要一个人类可读的url, 将它分块放入到一个易管理的结构体中. 幸运的是它使用了和用户空间url_parse()函数相同的机制. 如果url成功解析, 将会分配一个php_url结构体并设置合适的值. 如果在url中没有某些值, 在返回的php_url中对应的将被设置为null. 这个结构体必须在离开php_varstream_opener函数之前被显式释放, 否则它的内存将会泄露:
typedef struct php_url {
/* scheme://user:pass@host:port/path?query#fragment */
char *scheme;
char *user;
char *pass;
char *host;
unsigned short port;
char *path;
char *query;
char *fragment;
} php_url;
最后, varstream包装器创建了一个数据结构, 保存了流指向的变量名, 读取时的当前位置. 这个结构体将在流的读取和写入函数中用于获取变量, 并且将在流结束使用时由php_varstream_close函数释放.
opendir()
读写变量内容的实现可以再次进行扩展. 这里可以加入一个新的特性, 允许使用目录函数读取数组中的key. 在你的php_varstream_wrapper_ops结构体之前增加下面的代码:
static size_t php_varstream_readdir(php_stream *stream,
char *buf, size_t count tsrmls_dc)
{
php_stream_dirent *ent = (php_stream_dirent*)buf;
php_varstream_dirdata *data = stream->abstract;
char *key;
int type, key_len;
long idx;
/* 查找数组中的key */
type = zend_hash_get_current_key_ex(z_arrval_p(data->arr),
&key, &key_len, &idx, 0, &(data->pos));
/* 字符串key */
if (type == hash_key_is_string) {
if (key_len >= sizeof(ent->d_name)) {
/* truncate long keys to maximum length */
key_len = sizeof(ent->d_name) - 1;
}
/* 设置到目录结构上 */
memcpy(ent->d_name, key, key_len);
ent->d_name[key_len] = 0;
/* 数值key */
} else if (type == hash_key_is_long) {
/* 设置到目录结构上 */
snprintf(ent->d_name, sizeof(ent->d_name), "%ld",idx);
} else {
/* 迭代结束 */
return 0;
}
/* 移动数组指针(位置记录到流的抽象结构中) */
zend_hash_move_forward_ex(z_arrval_p(data->arr),
&data->pos);
return sizeof(php_stream_dirent);
}
static int php_varstream_closedir(php_stream *stream,
int close_handle tsrmls_dc)
{
php_varstream_dirdata *data = stream->abstract;
zval_ptr_dtor(&(data->arr));
efree(data);
return 0;
}
static int php_varstream_dirseek(php_stream *stream,
off_t offset, int whence,
off_t *newoffset tsrmls_dc)
{
php_varstream_dirdata *data = stream->abstract;
if (whence == seek_set && offset == 0) {
/* 重置数组指针 */
zend_hash_internal_pointer_reset_ex(
z_arrval_p(data->arr), &(data->pos));
if (newoffset) {
*newoffset = 0;
}
return 0;
}
/* 不支持其他类型的随机访问 */
return -1;
}
static php_stream_ops php_varstream_dirops = {
null, /* write */
php_varstream_readdir,
php_varstream_closedir,
null, /* flush */
php_varstream_dirstreamtype,
php_varstream_dirseek,
null, /* cast */
null, /* stat */
null, /* set_option */
};
static php_stream *php_varstream_opendir(
php_stream_wrapper *wrapper,
char *filename, char *mode, int options,
char **opened_path, php_stream_context *context
streams_dc tsrmls_dc)
{
php_varstream_dirdata *data;
php_url *url;
zval **var;
/* 不支持持久化流 */
if (options & stream_open_persistent) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unable to open %s persistently",
filename);
return null;
}
/* 解析url */
url = php_url_parse(filename);
if (!url) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unexpected error parsing url");
return null;
}
/* 检查请求url的正确性 */
if (!url->host || (url->host[0] == 0) ||
strcasecmp("var", url->scheme) != 0) {
/* bad url or wrong wrapper */
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "invalid url, must be in the form: "
"var://variablename");
php_url_free(url);
return null;
}
/* 查找变量 */
if (zend_hash_find(&eg(symbol_table), url->host,
strlen(url->host) + 1, (void**)&var) == failure) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "variable $%s not found", url->host);
php_url_free(url);
return null;
}
/* 检查变量类型 */
if (z_type_pp(var) != is_array) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "$%s is not an array", url->host);
php_url_free(url);
return null;
}
/* 释放前面分配的url结构 */
php_url_free(url);
/* 分配抽象数据结构 */
data = emalloc(sizeof(php_varstream_dirdata));
if ( z_isref_pp(var) && z_refcount_pp(var) > 1) {
/* 全拷贝 */
make_std_zval(data->arr);
*(data->arr) = **var;
zval_copy_ctor(data->arr);
init_pzval(data->arr);
} else {
/* 写时拷贝 */
data->arr = *var;
z_set_refcount_p(data->arr, z_refcount_p(data->arr) + 1);
}
/* 重置数组指针 */
zend_hash_internal_pointer_reset_ex(z_arrval_p(data->arr),
&data->pos);
return php_stream_alloc(&php_varstream_dirops,data,0,mode);
}
现在, 将你的php_varstream_wrapper_ops结构体中的dir_opener的null替换成你的php_varstream_opendir函数. 最后, 将下面新定义的类型放入到你的php_varstream.h文件的php_varstream_data定义下面:
#define php_varstream_dirstreamtype "varstream directory"
typedef struct _php_varstream_dirdata {
zval *arr;
hashposition pos;
} php_varstream_dirdata;
在你基于fopen()实现的varstream包装器中, 你直接使用持久变量名, 每次执行读写操作时从符号表中获取变量. 而这里, opendir()的实现中获取变量时处理了变量不存在或者类型错误的异常. 你还有一个数组变量的拷贝, 这就说明原数组的改变并不会影响后续的readdir()调用的结果. 原来存储变量名的方式也可以正常工作, 这里只是给出另外一种选择作为演示示例.
由于目录访问是基于成块的目录条目, 而不是字符, 因此这里需要一套独立的流操作. 这个版本中, write没有意义, 因此保持它为null. read的实现使用zend_hash_get_current_key_ex()函数将数组映射到目录名. 而随机访问也只是对seek_set有效, 用来响应rewinddir()跳转到数组开始位置.
实际上, 目录流并没有使用seek_cur, seek_end, 或者除了0之外的偏移量. 在实现目录流操作时, 最好还是涉及你的函数能以某种方式处理这些情况, 以使得在流包装层变化时能够适应其目录随机访问.
操纵
5个静态包装器操作中的4个用来处理不是基于i/o的流资源操作. 你已经看到过它们并了解它们的原型; 现在我们看看varstream包装器框架中它们的实现:
unlink
在你的wrapper_ops结构体中增加下面的函数, 它可以让unlink()通过varstream包装器, 拥有和unset()一样的行为:
static int php_varstream_unlink(php_stream_wrapper *wrapper,
char *filename, int options,
php_stream_context *context
tsrmls_dc)
{
php_url *url;
url = php_url_parse(filename);
if (!url) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unexpected error parsing url");
return -1;
}
if (!url->host || (url->host[0] == 0) ||
strcasecmp("var", url->scheme) != 0) {
/* url不合法 */
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "invalid url, must be in the form: "
"var://variablename");
php_url_free(url);
return -1;
}
/* 从符号表删除变量 */
//zend_hash_del(&eg(symbol_table), url->host, strlen(url->host) + 1);
zend_delete_global_variable(url->host, strlen(url->host) + 1 tsrmls_cc);
php_url_free(url);
return 0;
}
这个函数的编码量和php_varstream_opener差不多. 唯一的不同在于这里你需要传递变量名给zend_hash_del()去删除变量.
译注: 译者的php-5.4.10环境中, 使用unlink()删除变量后, 在用户空间再次读取该变量名的值会导致core dump. 因此上面代码中译者进行了修正, 删除变量时使用了zend_delete_global_variable(), 请读者参考阅读zend_delete_global_variable()函数源代码, 考虑为什么直接用zend_hash_del()删除, 会导致core dump. 下面是译者测试用的用户空间代码:
<?php
$fp = fopen('var://hello', 'r');
fwrite($fp, 'world');
var_dump($hello);
unlink('var://hello');
$a = $hello;
这个函数的代码量应该和php_varstream_opener差不多. 唯一的不同是这里是传递变量名给zend_hash_del()去删除变量.
rename, mkdir, rmdir
为了一致性, 下面给出rename, mkdir, rmdir函数的实现:
static int php_varstream_rename(php_stream_wrapper *wrapper,
char *url_from, char *url_to, int options,
php_stream_context *context tsrmls_dc)
{
php_url *from, *to;
zval **var;
/* 来源url解析 */
from = php_url_parse(url_from);
if (!from) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unexpected error parsing source");
return -1;
}
/* 查找变量 */
if (zend_hash_find(&eg(symbol_table), from->host,
strlen(from->host) + 1,
(void**)&var) == failure) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "$%s does not exist", from->host);
php_url_free(from);
return -1;
}
/* 目标url解析 */
to = php_url_parse(url_to);
if (!to) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unexpected error parsing dest");
php_url_free(from);
return -1;
}
/* 变量的改名 */
z_set_refcount_pp(var, z_refcount_pp(var) + 1);
zend_hash_update(&eg(symbol_table), to->host,
strlen(to->host) + 1, (void*)var,
sizeof(zval*), null);
zend_hash_del(&eg(symbol_table), from->host,
strlen(from->host) + 1);
php_url_free(from);
php_url_free(to);
return 0;
}
static int php_varstream_mkdir(php_stream_wrapper *wrapper,
char *url_from, int mode, int options,
php_stream_context *context tsrmls_dc)
{
php_url *url;
/* url解析 */
url = php_url_parse(url_from);
if (!url) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "unexpected error parsing url");
return -1;
}
/* 检查变量是否存在 */
if (zend_hash_exists(&eg(symbol_table), url->host,
strlen(url->host) + 1)) {
php_stream_wrapper_log_error(wrapper, options
tsrmls_cc, "$%s already exists", url->host);
php_url_free(url);
return -1;
}
/* eg(uninitialized_zval_ptr)通常是is_null的zval *, 引用计数无限大 */
zend_hash_add(&eg(symbol_table), url->host,
strlen(url->host) + 1,
(void*)&eg(uninitialized_zval_ptr),
sizeof(zval*), null);
php_url_free(url);
return 0;
}
static int php_varstream_rmdir(php_stream_wrapper *wrapper,
char *url, int options,
php_stream_context *context tsrmls_dc)
{
/* 行为等价于unlink() */
wrapper->wops->unlink(wrapper, url, options,
context tsrmls_cc);
}
检查
并不是所有的流操作都涉及到资源的操纵. 有时候也需要查看活动的流在某个时刻的状态, 或检查潜在可打开的资源的状态.
这一节流和包装器的ops函数都是在相同的数据结构php_stream_statbuf上工作的, 它只有一个元素: posix标准的struct statbuf. 当本节的某个函数被调用时, 将尝试填充尽可能多的statbuf元素的成员.
stat
如果设置, 当请求激活流实例的信息时, 将会调用wrapper->ops->stream_stat(). 如果没有设置, 则对应的stream->ops->stat()将会被调用. 无论哪个函数被调用, 都应该尽可能多的向返回的statbuf结构体ssb->sb中填充尽可能多流实例的有用信息. 在普通文件i/o的用法中, 它对应fstat()的标准i/o调用.
url_stat
在流实例外部调用wrapper->ops->url_stat()取到流资源的元数据. 通常来说, 符号链接和重定向都应该被解析, 直到找到一个真正的资源, 对其通过stat()系统调用这样的机制读取统计信息. url_stat的flags参数允许是下面php_stream_url_stat_*系列的常量值(省略php_stream_url_stat_前缀):
link不解析符号链接和重定向. 而是报告它碰到的第一个节点的信息, 无论是连
接还是真正的资源.
quiet不报告错误. 注意, 这和许多其他流函数中的report_errors逻辑恰恰
相反.
小结
无论是暴露远程网络i/o还是本地数据源的流资源, 都允许你的扩展在核心数据上挂在操纵函数的钩子, 避免重新实现单调的描述符管理和i/o缓冲区工作. 这使得它在用户空间环境中更加有用, 更加强大.
下一章将通过对过滤器和上下文的学习结束流包装层的学习, 过滤器和上下文可以用于选择默认的流行为, 甚至过程中修改数据.
以上就是[翻译][php扩展开发和嵌入式]第15章-php中流的实现的内容。