Skip to content

Instantly share code, notes, and snippets.

@adanselm
Created October 16, 2018 10:01
Show Gist options
  • Save adanselm/a6337d19ec3af76cd884757364e0be8a to your computer and use it in GitHub Desktop.
Save adanselm/a6337d19ec3af76cd884757364e0be8a to your computer and use it in GitHub Desktop.
Find files from ref dir that are not present in target dir
defmodule CompareDirs do
def compare(ref, target) do
ref_list = md5_dir(ref)
target_list = md5_dir(target)
Enum.reduce(ref_list, [], fn({path, hash}, acc) ->
case Enum.find(target_list, fn({_t_path, t_hash}) -> t_hash == hash end) do
true -> acc
_ -> acc ++ [path]
end
end)
end
def md5_dir(path) do
ls_r(path)
|> Enum.map(fn x -> {x, md5_file(x)} end)
end
def ls_r(path \\ ".") do
cond do
File.regular?(path) -> [path]
File.dir?(path) ->
File.ls!(path)
|> Enum.map(&Path.join(path, &1))
|> Enum.map(&ls_r/1)
|> Enum.concat
true -> []
end
end
def md5_file(path) do
File.stream!(path,[],2048)
|> Enum.reduce(:crypto.hash_init(:md5), fn(line, acc) -> :crypto.hash_update(acc,line) end)
|> :crypto.hash_final
|> Base.encode16
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment