-cache_ots_file() {
- # Desc: Scans and caches an OTS file for storing
- if fhash="$(must get_ots_filehash "$line")" && \
- block="$(must get_ots_oldestblock "$line")"; then
- fout="${fhash}_${block}.otsu"; # file name out
- pout="${pathDirOut1}/${fout}"; # file path out
- vbm "STATUS:Found OTS file at ${line} with hash ${fhash} and block ${block} and saving to ${pout}";
- must cp -n "$line" "$pout";
+store_ots_file() {
+ # Desc: Scans and stores an OTS file if none already stored
+ # Usage: store_ots_file FILE
+ # Example: store_ots_file foo.txt.ots
+ # Input: arg1 OTS file
+ # Output: exit code
+ local fin="$1";
+ vbm "STATUS:Starting store_ots_file()";
+
+ # Check if provided OTS file exists
+ if [[ ! -f "$fin" ]]; then die "FATAL:OTS file not found:$fin"; fi;
+
+ # Read file hash and oldest block from provided OTS file
+ if ! { fhash="$(must get_ots_filehash "$fin")" && \
+ block="$(must get_ots_oldestblock "$fin")"; }; then
+ yell "ERROR:Problem analyzing file with OpenTimestamps:${fin}";
+ return 1;
+ fi;
+ vbm "STATUS:The provided OTS file at ${fin} has digest ${fhash} and block ${block}.";
+
+ # Copy provided OTS if no matching OTS stored
+ fout="${fhash}_${block}.otsu"; # file name out
+ pout="${pathOtsStore}/${fout}"; # file path out
+ if [[ ! -f "$pout" ]]; then
+ vbm "STATUS:No matching stored OTS file found. Copying provided file to store at:${pout}";
+ must cp -n "$fin" "$pout";
+ return 0;
+ else
+ vbm "STATUS:Stored OTS file with matching file hash and block number in file name found.";
+ fi;
+
+ # Get block number for provided and stored OTS files.
+ if ! { blk_provid="$block"; blk_stored="$(get_ots_oldestblock "$pout"; )"; }; then
+ yell "ERROR:Could not read block numbers from OTS files: $(declare -p fhash block pout )";
+ fi;
+ re='[0-9]+';
+ if [[ ! "$blk_stored" =~ $re ]] || [[ ! "$blk_provid" =~ $re ]]; then
+ die "FATAL:Invalid block number(s):$(declare -p blk_stored blk_provid)";
+ fi;
+
+ # Copy provided OTS if matching OTS found stored but provided is older
+ if [[ "$blk_provid" -lt "$blk_stored" ]]; then
+ vbm "WARNING:Provided OTS file somehow older despite having same name. Previous error in storing OTS file?";
+ must mv "$pout" "${pout}--$(date +%s)";
+ must cp "$fin" "$pout";
+ return 0;