From e22fd25c2028e1a3c030829de02954717570a030 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 26 Mar 2023 12:08:00 +0200 Subject: Redo dir_hash with new file_hash functionality --- lib/util.bash | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/util.bash b/lib/util.bash index c36b576..fac492c 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -63,9 +63,19 @@ die() { # in a directory. It can be used to check whether contents changed after # templating files in a given directory. dir_hash() { - find "$1" -type f -exec sha1sum {} \; \ - | sha1sum \ - | awk '{ print $1 }' + local path + + path="$1" ; shift + + for entry in "$path"/* + do + if [[ -d "$entry" ]] + then + dir_hash "$entry" + fi + + file_hash "$entry" + done | file_hash - } # Fetch a file from an URL. Using this function introduces a dependency on curl. @@ -108,6 +118,25 @@ fetch_http_wget() { wget --quiet --output-document "$2" "$1" } +# Hash a given file. This is a convenience function to work around different +# systems calling their file hashing programs differently, and generating +# different output. This function only expects 1 file as argument, and only +# outputs the hash of this particular file. +file_hash() { + file_hash_md5 "$@" +} + +file_hash_md5() { + local file + + file="$1" ; shift + + case "${BASHTARD_PLATFORM[key]}" in + freebsd) md5 "$file" | awk '{ print $NF }' ;; + linux-*) md5sum "$file" | awk '{ print $1 }' ;; + esac +} + # A very simple means of templating a file, using sed and awk. The template # file is assumed to exist within the share directory of the current playbook. # Variables are passed as key=value pairs to this function. Inside the -- cgit v1.1