1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2017. All Rights Reserved.
%%
%% Licensed 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.
%%
%% %CopyrightEnd%
%%
-module(raw_file_io).
-export([open/2]).
open(Filename, Modes) ->
%% Layers are applied in this order, and the listed modules will call this
%% function again as necessary. eg. a raw compressed delayed file in list
%% mode will walk through [_list -> _compressed -> _delayed].
ModuleOrder = [{raw_file_io_list, fun match_list/1},
{raw_file_io_compressed, fun match_compressed/1},
{raw_file_io_delayed, fun match_delayed/1}],
open_1(ModuleOrder, Filename, add_implicit_modes(Modes)).
open_1([], Filename, Modes) ->
prim_file:open(Filename, Modes);
open_1([{Module, Match} | Rest], Filename, Modes) ->
case lists:any(Match, Modes) of
true ->
{Options, ChildModes} =
lists:partition(fun(Mode) -> Match(Mode) end, Modes),
Module:open_layer(Filename, ChildModes, Options);
false ->
open_1(Rest, Filename, Modes)
end.
%% 'read' and 'list' mode are enabled unless disabled by another option, so
%% we'll explicitly add them to avoid duplicating this logic in child layers.
add_implicit_modes(Modes0) ->
Modes1 = add_unless_matched(Modes0, fun match_writable/1, read),
add_unless_matched(Modes1, fun match_binary/1, list).
add_unless_matched(Modes, Match, Default) ->
case lists:any(Match, Modes) of
false -> [Default | Modes];
true -> Modes
end.
match_list(list) -> true;
match_list(_Other) -> false.
match_compressed(compressed) -> true;
match_compressed(_Other) -> false.
match_delayed({delayed_write, _Size, _Timeout}) -> true;
match_delayed(delayed_write) -> true;
match_delayed(_Other) -> false.
match_writable(write) -> true;
match_writable(append) -> true;
match_writable(exclusive) -> true;
match_writable(_Other) -> false.
match_binary(binary) -> true;
match_binary(_Other) -> false.
|