diff options
author | Matthew Sackman <matthew@rabbitmq.com> | 2010-11-25 14:50:14 +0000 |
---|---|---|
committer | Matthew Sackman <matthew@rabbitmq.com> | 2010-11-25 14:50:14 +0000 |
commit | b273afc13a115a56bde8224f29b2f15aa8c6d66b (patch) | |
tree | b1b3620c6e613c88040e5d3c4a53a9b79a3432cf | |
parent | a3bddd01bfe6e9b0c17dbb6e43b034cf97101311 (diff) | |
download | rabbitmq-server-bug23547.tar.gz |
Abstract out file locking, and revert to not using the R14-ism exclusive flagbug23547
-rw-r--r-- | src/rabbit_misc.erl | 13 | ||||
-rw-r--r-- | src/rabbit_upgrade.erl | 9 |
2 files changed, 16 insertions, 6 deletions
diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 0067a410..f549dfe0 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -66,6 +66,7 @@ -export([get_options/2]). -export([all_module_attributes/1, build_acyclic_graph/3]). -export([now_ms/0]). +-export([lock_file/1]). -import(mnesia). -import(lists). @@ -200,6 +201,7 @@ {bad_edge, [digraph:vertex()]}), digraph:vertex(), digraph:vertex()})). -spec(now_ms/0 :: () -> non_neg_integer()). +-spec(lock_file/1 :: (file:filename()) -> rabbit_types:ok_or_error('eexist')). -endif. @@ -811,3 +813,14 @@ build_acyclic_graph(VertexFun, EdgeFun, Graph) -> true = digraph:delete(G), {error, Reason} end. + +%% TODO: When we stop supporting Erlang prior to R14, this should be +%% replaced with file:open [write, exclusive] +lock_file(Path) -> + case filelib:is_file(Path) of + true -> + {error, eexist}; + false -> + {ok, Lock} = file:open(Path, [write]), + ok = file:close(Lock) + end. diff --git a/src/rabbit_upgrade.erl b/src/rabbit_upgrade.erl index 64d0c251..97a07514 100644 --- a/src/rabbit_upgrade.erl +++ b/src/rabbit_upgrade.erl @@ -127,9 +127,8 @@ heads(G) -> apply_upgrades(Upgrades) -> LockFile = lock_filename(dir()), - case file:open(LockFile, [write, exclusive]) of - {ok, Lock} -> - ok = file:close(Lock), + case rabbit_misc:lock_file(LockFile) of + ok -> BackupDir = dir() ++ "-upgrade-backup", info("Upgrades: ~w to apply~n", [length(Upgrades)]), case rabbit_mnesia:copy_db(BackupDir) of @@ -155,9 +154,7 @@ apply_upgrades(Upgrades) -> throw({could_not_back_up_mnesia_dir, E}) end; {error, eexist} -> - throw({error, previous_upgrade_failed}); - {error, _} = Error -> - throw(Error) + throw({error, previous_upgrade_failed}) end. apply_upgrade({M, F}) -> |