make mkdtemp a lot more secure (still not fully secure but more).

This commit is contained in:
Eric Merritt 2012-06-05 17:54:46 -05:00
parent 3e6357aea9
commit 10557e421e

View file

@ -109,14 +109,21 @@ is_symlink(Path) ->
%% function of the same name.
-spec mkdtemp() -> TmpDirPath::path().
mkdtemp() ->
UniqueNumber = integer_to_list(element(3, now())),
random:seed(now()),
UniqueNumber = erlang:integer_to_list(erlang:trunc(random:uniform() * 1000000000000)),
TmpDirPath =
filename:join([tmp(), lists:flatten([".tmp_dir", UniqueNumber])]),
case filelib:is_dir(TmpDirPath) of
true ->
throw(?UEX({mkdtemp_failed, file_exists}, "tmp directory exists", []));
false ->
try
ok = mkdir_path(TmpDirPath),
TmpDirPath
catch
_C:E -> throw(?UEX({mkdtemp_failed, E}, ?CHECK_PERMS_MSG, []))
end
end.