-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_store_ops.sh
executable file
·95 lines (78 loc) · 2.7 KB
/
hash_store_ops.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
############################################################
#
# Author: Georg Schnabel
# Email: [email protected]
# Date: 2022/01/11
# Institution: IAEA
#
# This script can do two things:
# (1) It can store a file in a directory
# with its name changed to the sha256 cryptographic
# hash of its content. The destination directory can
# function as a content-addressable storage.
# The file in the source directory is not altered.
#
# (2) It can associate a symbolic link in a git-annex
# repository with a weburl that points to a directory
# with files as created in (1).
#
# Usage:
# ./hash_store_ops.sh store <hashstore-dir> <filepath>
# ./hash_store_ops.sh associate <weburl> <filepath>
#
# <hashstore-dir>: directory to be used as hashstore
# <weburl>: url pointing to a directory with a hashstore
# <filepath>: if mode 'store', path to the file that
# should be stored in the hashstore.
# if mode 'associate', path to the symbolic
# link that should be associated with the
# corresponding weburl
#
############################################################
mode=$1
hashdir="$2"
filepath="$3"
if [ "$mode" == "store" ]; then
filename=$(basename $filepath)
filehash="sha256-$(sha256sum $filepath | cut -d' ' -f1)"
outfilepath="$hashdir/$filehash"
outlogfile="$hashdir/hashinfo.txt"
if [ -f "$outfilepath" ]; then
existhash="sha256-$(sha256sum $outfilepath | cut -d' ' -f1)"
if [ "$existhash" != "$filehash" ]; then
echo "FATAL ERROR: Inconsistent file $existhash in hashstore"
exit 2
else
echo $filehash $filename >> $outlogfile
echo "skipped '$outfilepath' because already in hashstore"
exit 0
fi
fi
cp $filepath $outfilepath && \
echo $filehash $filename >> $outlogfile
retcode="$?"
chmod 444 $outfilepath
if [ "$retcode" -eq 0 ]; then
echo "stored '$filepath' as $filehash"
else
echo "error: could not store $filepath"
fi
exit $retcode
elif [ "$mode" == "associate" ]; then
filehash="sha256-$(ls -la $filepath | sed -e 's/^.*--\([0-9a-f]*\).*$/\1/')"
url="${hashdir}${filehash}"
curdir=`pwd`
filename="$(basename $filepath)"
cd $(dirname $filepath)
git annex addurl --file="$filename" "$url"
cd $curdir
elif [ "$mode" == "print_association" ]; then
filehash="sha256-$(ls -la $filepath | sed -e 's/^.*--\([0-9a-f]*\).*$/\1/')"
url="${hashdir}${filehash}"
echo $url $filepath
else
echo "ERROR: Unsupported mode $mode".
echo " Use either 'store' or 'associate'"
exit 1
fi