aboutsummaryrefslogtreecommitdiff
path: root/lib/file_hash.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-05-24 15:33:06 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-05-24 15:33:12 -0700
commitba76a35e84d384673f4633d59003df91ce6a2d79 (patch)
treefee12e07224e7f6c4e4ceb228d28e7b0c38f2537 /lib/file_hash.nom
parent2e345e271f27147051b8ce1f2981ba728b14394a (diff)
Fixed up file hash lib.
Diffstat (limited to 'lib/file_hash.nom')
-rw-r--r--lib/file_hash.nom56
1 files changed, 17 insertions, 39 deletions
diff --git a/lib/file_hash.nom b/lib/file_hash.nom
index 0d7e523..9a707c3 100644
--- a/lib/file_hash.nom
+++ b/lib/file_hash.nom
@@ -1,46 +1,24 @@
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
+action [file with hash %hash]
+ lua> ".."
+ local Hash = require("openssl.digest")
+ for filename in io.popen('find -L . -type f -name "*.nom"'):lines() do
+ local file = io.open(filename)
+ local contents = file:read("*a")
+ file:close()
+ local hash = Hash.new("sha1"):final(contents)
+ local hex = hash:gsub('.', function(c) return string.format('%02x', string.byte(c)) end)
+ if hex == \%hash then
+ return filename
end
end
- end
- attrdir(".");
-action [sha1 %]
- lua> "return sha1(\%);"
+action [hash %, sha1 %]
+ %hashlib <- (=lua "require('openssl.digest')")
+ %hash <- (=lua "\%hashlib.new('sha1'):final(\%)")
+ return: =lua "\%hash:gsub('.', function(c) return string.format('%02x', string.byte(c)) end)"
-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 [hash of file %filename] as
+ sha1: =lua "io.open(\%filename):read('*a')"
-parse [use file with hash %hash] as: use (file with hash %hash)