未公开的ram_file 内存文件

Ram_file是erlang未公开的一个模块, 是在内存文件的一个实现,erts的内置驱动ram_file_drv提供底层快速的内存访问。它的用途是需要文件访问接口的模块如erl_tar之类的可以在内存里面提供高速的文件访问服务。 所以ram_file提供file所提供的正常接口以外,还支持以下接口:
%% Specialized file operations
-export([get_size/1, get_file/1, set_file/2, get_file_close/1]).
-export([compress/1, uncompress/1, uuencode/1, uudecode/1]).
主要用于zip压缩.

root@yufeng-desktop:~/otp_src_R13B/lib/stdlib/src# erl
Erlang R13B (erts-5.7.1) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.1 (abort with ^G)
1> erl_ddll:loaded_drivers().
{ok,["efile","tcp_inet","udp_inet","zlib_drv",
"ram_file_drv","tty_sl"]}
2> f(R),{ok,R}=ram_file:open("hello", []).
{ok,{file_descriptor,ram_file,#Port<0.453>}}
3> ram_file:get_size(R).
{ok,5}
4> ram_file:get_file(R).
{ok,"hello"}
5> ram_file:compress(R).
{ok,25}
6> ram_file:get_file(R).
{ok,[31,139,8,0,0,0,0,0,0,3,203,72,205,201,201,7,0,134,166,
16,54,5,0,0,0]}
7> ram_file:uncompress(R).
{ok,5}
8> ram_file:get_file(R).
{ok,"hello"}
9> ram_file:close(R).
ok

CTRL+C o可以看到ram_file的port的信息:

=port:#Port<0.456>
Slot: 456
Connected: <0.63.0>
Links: <0.63.0>
Port controls linked-in driver: ram_file_drv

注意ram_file:open的文件名实际上是个数据。

另外如果file:open(xxx, [ram]) 方式打开的话,其返回的实际上是ram_file的handle.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值