1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1997-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%
22-module(mnesia_config_backup).
23-author('peterl@erix.ericsson.se').
24
25%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26%%
27%% This module is used for testing the backup module config parameter.
28%%
29%% This module is an impostor for the mnesia_backup module.
30%%
31%%
32%% Original doc below:
33%%
34%% This module contains one implementation of callback functions
35%% used by Mnesia at backup and restore. The user may however
36%% write an own module the same interface as mnesia_backup and
37%% configure Mnesia so the alternate module performs the actual
38%% accesses to the backup media. This means that the user may put
39%% the backup on medias that Mnesia does not know about, possibly
40%% on hosts where Erlang is not running.
41%%
42%% The OpaqueData argument is never interpreted by other parts of
43%% Mnesia. It is the property of this module. Alternate implementations
44%% of this module may have different interpretations of OpaqueData.
45%% The OpaqueData argument given to open_write/1 and open_read/1
46%% are forwarded directly from the user.
47%%
48%% All functions must return {ok, NewOpaqueData} or {error, Reason}.
49%%
50%% The NewOpaqueData arguments returned by backup callback functions will
51%% be given as input when the next backup callback function is invoked.
52%% If any return value does not match {ok, _} the backup will be aborted.
53%%
54%% The NewOpaqueData arguments returned by restore callback functions will
55%% be given as input when the next restore callback function is invoked
56%% If any return value does not match {ok, _} the restore will be aborted.
57%%
58%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59
60-export([
61         open_write/1, write/2, commit_write/1, abort_write/1,
62         open_read/1, read/1, close_read/1
63        ]).
64
65-record(backup, {name, mode, items}).
66
67open_write(Name) ->
68    file:delete(Name),
69    {ok, #backup{name = Name, mode = write, items = []}}.
70
71write(Opaque, Item) when Opaque#backup.mode == write ->
72    %% Build the list in reverse order
73    {ok, Opaque#backup{items = [Item | Opaque#backup.items]}}.
74
75commit_write(Opaque) when Opaque#backup.mode == write ->
76    Bin = term_to_binary(Opaque#backup.items),
77    case file:write_file(Opaque#backup.name, Bin) of
78	ok ->
79	    {ok, Opaque#backup{mode = closed, items = []}};
80	{error, Reason} ->
81	    {error, {commit_write, Reason}}
82    end.
83
84abort_write(Opaque) ->
85    {ok, Opaque#backup{mode = closed, items = []}}.
86
87open_read(Name) ->
88    case file:read_file(Name) of
89	{ok, Bin} ->
90	    ReverseList = binary_to_term(Bin),
91	    List = lists:reverse(ReverseList),
92	    {ok, #backup{name = Name, mode = read, items = List}};
93	{error, Reason} ->
94	    %% {error, {open_read, Reason}}
95	    {Reason, error} %% Testing error handling in mnesia
96    end.
97
98read(Opaque) when Opaque#backup.mode == read ->
99    case Opaque#backup.items of
100	[Head | Tail] ->
101	    {ok, Opaque#backup{items = Tail}, Head};
102	[] ->
103	    {ok, Opaque#backup{mode = closed}, []}
104    end.
105
106close_read(Opaque) ->
107    {ok, Opaque#backup{mode = closed, items = []}}.
108