/lib/mnesia/test/mnesia_config_backup.erl

https://github.com/dustin/otp · Erlang · 105 lines · 40 code · 11 blank · 54 comment · 3 complexity · 7b4a0c92eefde53984fc7c37f76c3ce5 MD5 · raw file

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