Created
October 16, 2018 10:01
-
-
Save adanselm/a6337d19ec3af76cd884757364e0be8a to your computer and use it in GitHub Desktop.
Find files from ref dir that are not present in target dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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