|
1 #!/bin/bash |
|
2 |
|
3 # Relational pipes |
|
4 # Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
5 # |
|
6 # This program is free software: you can redistribute it and/or modify |
|
7 # it under the terms of the GNU General Public License as published by |
|
8 # the Free Software Foundation, version 3 of the License. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
18 |
|
19 # This streamlet computes hashes of given files. |
|
20 # Default algorithm is sha256. |
|
21 # |
|
22 # Any supported hash algorithm can be specified by e.g. --option "attribute" "sha512" |
|
23 # The hash command is derived from the algorithm name by adding "sum" suffix and must be available at $PATH. |
|
24 # |
|
25 # Multiple algorithms can be specified (just repeat the --option). |
|
26 # But single streamlet instance will run them sequentially. |
|
27 # When parallell processing is needed (usually faster) then multiple scriptlet instances should be used: |
|
28 # --scriptlet hash --option "sha1" --as "sha1" --scriptlet hash --option "sha256" --as "sha256" |
|
29 # instead of: |
|
30 # --scriptlet hash --option "sha1" --as "sha1" --option "sha256" --as "sha256" |
|
31 |
|
32 |
|
33 . "$(dirname $0)/streamlet-common.sh" |
|
34 |
|
35 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() { |
|
36 hashTypes=() |
|
37 hashCommands=() |
|
38 |
|
39 for (( i=0; i<${#optionNames[@]}; i++)); do |
|
40 if [[ "x${optionNames[$i]}" == "xattribute" ]]; then |
|
41 if type "${optionValues[$i]}sum" > /dev/null; then |
|
42 hashTypes+=("${optionValues[$i]}"); |
|
43 hashCommands+=("${optionValues[$i]}sum"); |
|
44 else |
|
45 echo "Unsupported attribute: ${optionValues[$i]}" >&2 |
|
46 fi |
|
47 else |
|
48 echo "Unsupported option: ${optionNames[$i]}" >&2 |
|
49 fi |
|
50 done |
|
51 |
|
52 if [[ -z "$hashTypes" ]]; then |
|
53 hashTypes=("sha256") |
|
54 hashCommands=("sha256sum") |
|
55 fi |
|
56 |
|
57 for (( i=0; i<${#hashTypes[@]}; i++)); do |
|
58 send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-${hashTypes[$i]}}" "string" |
|
59 done |
|
60 |
|
61 send WAITING_FOR_INPUT_ATTRIBUTES |
|
62 } |
|
63 |
|
64 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() { |
|
65 for (( i=0; i<${#hashTypes[@]}; i++)); do |
|
66 value=$("${hashCommands[$i]}" "$currentFile" | cut -d" " -f1) 2>/dev/null; |
|
67 if [[ -z "$value" ]]; then isNull="true"; else isNull="false"; fi |
|
68 send OUTPUT_ATTRIBUTE "$value" "$isNull"; |
|
69 done |
|
70 |
|
71 send WAITING_FOR_INPUT_ATTRIBUTES; |
|
72 } |
|
73 |
|
74 initialize |
|
75 processMessages |