Skip to content

Instantly share code, notes, and snippets.

@mprymek
Created February 23, 2015 01:02
Show Gist options
  • Save mprymek/6b30368f8de6d2b5efbd to your computer and use it in GitHub Desktop.
Save mprymek/6b30368f8de6d2b5efbd to your computer and use it in GitHub Desktop.
# I have an "mtree" file which has records for every file in the filesystem: it's name, uid, git...
# from this file I make a structures like this:
# %Fact.File{flags: [:uarch, :schg], gid: 0, mode: 365, path: "/usr/sbin/sshd", sha256: <<129, 155, 30, 219, 55, 53, 33, 134, 243, 253, 172, 143, 182, 16, 16, 212, 0, 140, 249, 59, 112, 147, 206, 166, 6, 54, 21, 199, 133, 191, 209, 174>>, uid: 0}
# ... and then insert all the metadata as facts into Erlog db:
def from_mtree_data(data,set) do
e = Exlog.new
e = data |> Enum.reduce(e,fn
f,e ->
e |> assertz!( uid(f.path,f.uid) <- [fileset(set)] )
|> assertz!( gid(f.path,f.gid) <- [fileset(set)] )
|> assertz!( mode(f.path,f.mode) <- [fileset(set)] )
# add sha if present
case Map.get(f,:sha256) do
nil -> e
sha -> e |> assertz!( sha256(f.path,sha) <- [fileset(set)] )
end
end
)
e
end
# note assertz!/2 is a macro which translates some elixirish pseudo-prolog into erlog structures
# now I can make queries...
fs = [:freebsd,10,1,5,:amd64]
e = e |> assertz!( fileset(fs) <- [os(:freebsd,"10.1-RELEASE-p5"),arch(:amd64)] )
e = e |> assert!( os(:freebsd,"10.1-RELEASE-p5") )
{e,res} = e |> prove_all( os(X,Y) )
assert res == [[X: :freebsd, Y: "10.1-RELEASE-p5"]]
fname = "/var/db/etcupdate/current/etc/hosts"
sha = HexStr.decode "10c5d5e434664d5b9370e1e41836e219be91219b801130c2abf90b110059c682"
{us,{e,result}} = :timer.tc(fn ->
e |> prove_all( sha256(fname,S) )
end)
assert result == [[S: sha]]
IO.puts "Checksum lookup: #{us/1000000}s"
{us,{e,_result}} = :timer.tc(fn ->
e |> prove_all( sha256(F,S) )
end)
IO.puts "Getting all shas: #{us/1000000}s"
{us,{e,result}} = :timer.tc(fn ->
e |> prove_all( sha256(F,sha) )
end)
assert result == [[F: "/var/db/etcupdate/current/etc/hosts"], [F: "/usr/share/examples/etc/hosts"], [F: "/etc/hosts"]]
IO.puts "Getting files with given sha: #{us/1000000}s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment