From c7e5cf8e259956120c07206c4e7df235b129cb56 Mon Sep 17 00:00:00 2001 From: Richard Carlsson Date: Tue, 22 Nov 2016 15:17:04 +0100 Subject: Restructure code:which() and where_is_file() --- lib/kernel/src/code.erl | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'lib/kernel/src/code.erl') diff --git a/lib/kernel/src/code.erl b/lib/kernel/src/code.erl index 1b149bbb4e..bb19d6716d 100644 --- a/lib/kernel/src/code.erl +++ b/lib/kernel/src/code.erl @@ -719,29 +719,14 @@ start_get_mode() -> which(Module) when is_atom(Module) -> case is_loaded(Module) of false -> - File = atom_to_list(Module) ++ objfile_extension(), - which(File, get_path()); + which(Module, get_path()); {file, File} -> File end. --spec which(file:filename(), [file:filename()]) -> - 'non_existing' | file:filename(). - -which(_, []) -> - non_existing; -which(File, [Path|Tail]) -> - case erl_prim_loader:list_dir(Path) of - {ok,Files} -> - case lists:member(File,Files) of - true -> - filename:append(Path, File); - false -> - which(File, Tail) - end; - _Error -> - which(File, Tail) - end. +which(Module, Path) when is_atom(Module) -> + File = atom_to_list(Module) ++ objfile_extension(), + where_is_file(Path, File). %% Search the code path for a specific file. Try to locate %% it in the code path cache if possible. @@ -751,13 +736,28 @@ which(File, [Path|Tail]) -> Absname :: file:filename(). where_is_file(File) when is_list(File) -> Path = get_path(), - which(File, Path). + where_is_file(Path, File). -spec where_is_file(Path :: file:filename(), Filename :: file:filename()) -> file:filename() | 'non_existing'. -where_is_file(Path, File) when is_list(Path), is_list(File) -> - which(File, Path). +where_is_file([], _) -> + non_existing; +where_is_file([Path|Tail], File) -> + case erl_prim_loader:list_dir(Path) of + {ok,Files} -> + where_is_file(Tail, File, Path, Files); + _Error -> + where_is_file(Tail, File) + end. + +where_is_file(Tail, File, Path, Files) -> + case lists:member(File, Files) of + true -> + filename:append(Path, File); + false -> + where_is_file(Tail, File) + end. -spec set_primary_archive(ArchiveFile :: file:filename(), ArchiveBin :: binary(), -- cgit v1.2.3