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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Appendix B: The Backup Call Back Interface
</TITLE>
<SCRIPT type="text/javascript" src="../../../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="9"><!-- Empty --></A>
<H2>9 Appendix B: The Backup Call Back Interface
</H2>
<A NAME="9.1"><!-- Empty --></A>
<H3>9.1 mnesia_backup callback behavior</H3>
<P>
<PRE>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This module contains one implementation of callback functions
%% used by Mnesia at backup and restore. The user may however
%% write an own module the same interface as mnesia_backup and
%% configure Mnesia so the alternate module performs the actual
%% accesses to the backup media. This means that the user may put
%% the backup on medias that Mnesia does not know about, possibly
%% on hosts where Erlang is not running.
%%
%% The OpaqueData argument is never interpreted by other parts of
%% Mnesia. It is the property of this module. Alternate implementations
%% of this module may have different interpretations of OpaqueData.
%% The OpaqueData argument given to open_write/1 and open_read/1
%% are forwarded directly from the user.
%%
%% All functions must return {ok, NewOpaqueData} or {error, Reason}.
%%
%% The NewOpaqueData arguments returned by backup callback functions will
%% be given as input when the next backup callback function is invoked.
%% If any return value does not match {ok, _} the backup will be aborted.
%%
%% The NewOpaqueData arguments returned by restore callback functions will
%% be given as input when the next restore callback function is invoked
%% If any return value does not match {ok, _} the restore will be aborted.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(mnesia_backup).
-include_lib("kernel/include/file.hrl").
-export([
%% Write access
open_write/1,
write/2,
commit_write/1,
abort_write/1,
%% Read access
open_read/1,
read/1,
close_read/1
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Backup callback interface
-record(backup, {tmp_file, file, file_desc}).
%% Opens backup media for write
%%
%% Returns {ok, OpaqueData} or {error, Reason}
open_write(OpaqueData) ->
File = OpaqueData,
Tmp = lists:concat([File,".BUPTMP"]),
file:delete(Tmp),
file:delete(File),
case disk_log:open([{name, make_ref()},
{file, Tmp},
{repair, false},
{linkto, self()}]) of
{ok, Fd} ->
{ok, #backup{tmp_file = Tmp, file = File, file_desc = Fd}};
{error, Reason} ->
{error, Reason}
end.
%% Writes BackupItems to the backup media
%%
%% Returns {ok, OpaqueData} or {error, Reason}
write(OpaqueData, BackupItems) ->
B = OpaqueData,
case disk_log:log_terms(B#backup.file_desc, BackupItems) of
ok ->
{ok, B};
{error, Reason} ->
abort_write(B),
{error, Reason}
end.
%% Closes the backup media after a successful backup
%%
%% Returns {ok, ReturnValueToUser} or {error, Reason}
commit_write(OpaqueData) ->
B = OpaqueData,
case disk_log:sync(B#backup.file_desc) of
ok ->
case disk_log:close(B#backup.file_desc) of
ok ->
case file:rename(B#backup.tmp_file, B#backup.file) of
ok ->
{ok, B#backup.file};
{error, Reason} ->
{error, Reason}
end;
{error, Reason} ->
{error, Reason}
end;
{error, Reason} ->
{error, Reason}
end.
%% Closes the backup media after an interrupted backup
%%
%% Returns {ok, ReturnValueToUser} or {error, Reason}
abort_write(BackupRef) ->
Res = disk_log:close(BackupRef#backup.file_desc),
file:delete(BackupRef#backup.tmp_file),
case Res of
ok ->
{ok, BackupRef#backup.file};
{error, Reason} ->
{error, Reason}
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Restore callback interface
-record(restore, {file, file_desc, cont}).
%% Opens backup media for read
%%
%% Returns {ok, OpaqueData} or {error, Reason}
open_read(OpaqueData) ->
File = OpaqueData,
case file:read_file_info(File) of
{error, Reason} ->
{error, Reason};
_FileInfo -> %% file exists
case disk_log:open([{file, File},
{name, make_ref()},
{repair, false},
{mode, read_only},
{linkto, self()}]) of
{ok, Fd} ->
{ok, #restore{file = File, file_desc = Fd, cont = start}};
{repaired, Fd, _, {badbytes, 0}} ->
{ok, #restore{file = File, file_desc = Fd, cont = start}};
{repaired, Fd, _, _} ->
{ok, #restore{file = File, file_desc = Fd, cont = start}};
{error, Reason} ->
{error, Reason}
end
end.
%% Reads BackupItems from the backup media
%%
%% Returns {ok, OpaqueData, BackupItems} or {error, Reason}
%%
%% BackupItems == [] is interpreted as eof
read(OpaqueData) ->
R = OpaqueData,
Fd = R#restore.file_desc,
case disk_log:chunk(Fd, R#restore.cont) of
{error, Reason} ->
{error, {"Possibly truncated", Reason}};
eof ->
{ok, R, []};
{Cont, []} ->
read(R#restore{cont = Cont});
{Cont, BackupItems, _BadBytes} ->
{ok, R#restore{cont = Cont}, BackupItems};
{Cont, BackupItems} ->
{ok, R#restore{cont = Cont}, BackupItems}
end.
%% Closes the backup media after restore
%%
%% Returns {ok, ReturnValueToUser} or {error, Reason}
close_read(OpaqueData) ->
R = OpaqueData,
case disk_log:close(R#restore.file_desc) of
ok -> {ok, R#restore.file};
{error, Reason} -> {error, Reason}
end.
</PRE>
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|