aboutsummaryrefslogtreecommitdiff
path: root/lib/file_hash.nom
diff options
context:
space:
mode:
Diffstat (limited to 'lib/file_hash.nom')
-rw-r--r--lib/file_hash.nom46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/file_hash.nom b/lib/file_hash.nom
new file mode 100644
index 0000000..4d99e39
--- /dev/null
+++ b/lib/file_hash.nom
@@ -0,0 +1,46 @@
+use "core"
+
+%hash_to_filename <- {}
+
+lua> ".."
+ local Hash = require("openssl.digest");
+ local function sha1(x)
+ local hash = Hash.new("sha1"):final(x);
+ local hex = hash:gsub('.', function(c) return string.format('%02x', string.byte(c)) end)
+ return hex
+ end
+ local lfs = require('lfs');
+ local function attrdir(path)
+ for filename in lfs.dir(path) do
+ if filename ~= "." and filename ~= ".." and filename:sub(1,1) ~= "." then
+ local filename = path..'/'..filename
+ local attr = lfs.attributes(filename);
+ if attr.mode == "directory" then
+ attrdir(filename);
+ elseif filename:match(".*%.nom") then
+ local file = io.open(filename);
+ local hash = sha1(file:read("*a"));
+ file:close();
+ \%hash_to_filename[hash] = filename
+ end
+ end
+ end
+ end
+ attrdir(".");
+
+action [sha1 %]:
+ lua> "return sha1(\%);"
+
+action [file with hash %hash]:
+ %file <- (%hash in %hash_to_filename)
+ assume %file or barf "File with SHA1 hash \%hash not found!"
+ return %file
+
+action [hash of file %filename]:
+ lua> ".."
+ local f = io.open(\%filename);
+ local hash = sha1(f:read("*a"));
+ f:close();
+ return hash;
+
+parse [use file with hash %hash] as: use (file with hash %hash)