]> zdv2.bktei.com Git - BK-2020-03.git/blobdiff - user/bkots
feat(user/gthumb/gthumb-analyze.sh):Get stats on selected images
[BK-2020-03.git] / user / bkots
index d37b9c22cf41a6bca959e0db49a05e031584fbe6..acea7f51e20b8daf00aaeec64fd904bf1475551b 100755 (executable)
@@ -1,6 +1,14 @@
 #!/usr/bin/env bash
+# Desc: Recursively timestamp files with OpenTimestamp
+# Usage: bkots [PATH]
+# Version: 3.0.0
 
 # Define variables
+declare -g max_job_count="2"; # default max job count
+declare -g max_batch_size="500"; # default xargs batch size for upgrading and stamping
+declare -g age_threshold="60"; # min age to add file; seconds;
+declare -g stamp_throttle="0.1"; # seconds delay between stamps
+declare -g stamp_throttle_fail="1"; # seconds delay if stamp errors
 declare -Ag appRollCall # Associative array for storing app status
 declare -Ag fileRollCall # Associative array for storing file status
 declare -Ag dirRollCall # Associative array for storing dir status
@@ -10,13 +18,12 @@ calendars+=("https://finney.calendar.eternitywall.com");
 calendars+=("https://btc.calendar.catallaxy.com");
 calendars+=("https://alice.btc.calendar.opentimestamps.org");
 calendars+=("https://bob.btc.calendar.opentimestamps.org");
-declare -a commands # array for storing assembled commands
-age_threshold="60"; # min age to add file; seconds;
+export OTS_CALS; OTS_CALS="$(printf '%s\n' "${calendars[@]}")";  # convert calendars array into string list
 
 # Declare functions
 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
-try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
+must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
 checkapp() {
     # Desc: If arg is a command, save result in assoc array 'appRollCall'
     # Usage: checkapp arg1 arg2 arg3 ...
@@ -200,8 +207,8 @@ showVersion() {
     vbm "DEBUG:showVersion function called."
 
     cat <<'EOF'
-bkots 1.0.0
-Copyright (C) 2022 Steven Baltakatei Sandoval
+bkots 3.0.0
+Copyright (C) 2026 Steven Baltakatei Sandoval
 License GPLv3: GNU GPL version 3
 This is free software; you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
@@ -239,8 +246,14 @@ showUsage() {
         --include-dotfiles
                 Include files and directories starting with '.' (not
                 included by default).
+        -j, --jobs
+                Specify simultaneous job count (default: 2)
+        -n, --batch-size
+                Specify file batch sizes for upgrade and stamp operations (default:500)
         -r, --recursive
                 Consider files in dirs recursively.
+        --verify
+                Verify OTS files.
         --version
                 Display script version.
         -v, --verbose
@@ -268,6 +281,32 @@ showUsage() {
       bkots foo.txt bar.pdf /home/username/Pictures/
 EOF
 } # Display information on how to use this script.
+count_jobs() {
+    # Desc: Count and return total number of jobs
+    # Usage: count_jobs
+    # Input: None.
+    # Output: stdout   integer number of jobs
+    # Depends: Bash 5.1.16
+    # Example: while [[$(count_jobs) -gt 0]]; do echo "Working..."; sleep 1; done;
+    # Version: 0.0.1
+
+    local job_count;
+    job_count="$(jobs -r | wc -l | tr -d ' ' )";
+    #yell "DEBUG:job_count:$job_count";
+    if [[ -z $job_count ]]; then job_count="0"; fi;
+    echo "$job_count";
+}; # Return number of background jobs
+wait_for_jobslot() {
+    # Desc: Does not return until count_jobs() falls below $max_job_count
+    # Input: var max_job_count
+    # Output: return code 0
+    # Depends: count_jobs(), yell();
+    while [[ $(count_jobs) -ge $max_job_count ]]; do
+        printf "\r%d:Working..." "$SECONDS";
+        sleep 1;
+    done;
+    return 0;
+};
 processArgs() {
     # Desc: Processes arguments provided to script.
     # Usage: processArgs "$@"
@@ -302,9 +341,30 @@ processArgs() {
             --include-dotfiles) # Include dotfiles
                 option_include_dotfiles="true";
                 vbm "DEBUG:Option enabled:include dotfiles";;
+            -j | --jobs) # Specify max_job_count
+                if [[ -n "$2" ]] && [[ "$2" =~ ^[0-9]+$ ]]; then
+                    max_job_count="$2";
+                    vbm "STATUS:Max job count set to:$max_job_count";
+                    shift;
+                else
+                    showUsage;
+                    die "FATAL:Invalid job count:$2";
+                fi;;
+            -n | --batch-size)
+                if [[ -n "$2" ]] && [[ "$2" =~ ^[0-9]+$ ]]; then
+                    max_batch_size="$2";
+                    vbm "STATUS:Max batch size set to:$max_batch_size";
+                    shift;
+                else
+                    showUsage;
+                    die "FATAL:Invalid xargs file batch size:$2";
+                fi;;
             -r | --recursive) # Specify recursive option
                 option_recursive="true";
-                vbm "DEBUG:option enabled:include files in dirs recursively";;
+                vbm "DEBUG:Option enabled:include files in dirs recursively";;
+            --verify)
+                option_verify="true";
+                vbm "DEBUG:Option enabled:verify OTS files";;
            --version) showVersion; exit 1;; # Show version
            -v | --verbose) opVerbose="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode. See [1].
             --) # End of all options. See [2].
@@ -336,7 +396,7 @@ get_parent_dirnames() {
     # Input: arg1  input  path
     # Output: stdout   newline-delimited list of parent dirs
     # Version: 0.0.1
-    # Depends: yell(), die(), try()
+    # Depends: yell(), die(), must()
     local path
 
     # Check input
@@ -354,14 +414,6 @@ get_parent_dirnames() {
         echo "$name_base";
     done;    
 }; # Output parent dirnames to stdout
-cmdwrap() {
-    # print command to stderr
-    echo "$@" 1>&2;
-
-    # execute command
-    "$@";
-}; # print and execute string together
-export -f cmdwrap; # export cmdwrap for use in other functions
 main() {
     # Desc: Creates `.ots` file:
     #     - for each file specified in arrayPosArgs array
@@ -377,7 +429,6 @@ main() {
     # Ref/Attrib: [1] How to create an array of unique elements from a string/array in bash https://unix.stackexchange.com/a/167194
     #             [2] How to find files containing newlines in their names https://stackoverflow.com/a/21727028
     #             [3] Get mtime of specific file using Bash? https://stackoverflow.com/a/4774377
-    #             [4] Search/Replace with string substitution instead of sed. https://www.shellcheck.net/wiki/SC2001
     local -a file_list file_list_pruned;
     local -a files_to_verify files_to_upgrade files_to_stamp
     local -a files_to_verify_pruned files_to_upgrade_pruned files_to_stamp_pruned
@@ -611,6 +662,7 @@ main() {
     ## Sort and prune file action arrays
     ### files to upgrade
     while read -r -d $'\0' line; do
+        if [[ -z $line ]]; then continue; fi; # Skip empty lines
         vbm "DEBUG:adding to files_to_upgrade_pruned:line:$line";
         files_to_upgrade_pruned+=("$line");
     done < <(printf "%s\0" "${files_to_upgrade[@]}" | sort -zu | shuf -z); # See [1]
@@ -621,6 +673,7 @@ main() {
 
     ### files to verify
     while read -r -d $'\0' line; do
+        if [[ -z $line ]]; then continue; fi; # Skip empty lines
         vbm "DEBUG:adding to files_to_verify_pruned:line:$line";
         files_to_verify_pruned+=("$line");
     done < <(printf "%s\0" "${files_to_verify[@]}" | sort -zu | shuf -z); # See [1]
@@ -631,6 +684,7 @@ main() {
 
     ### files to stamp
     while read -r -d $'\0' line; do
+        if [[ -z $line ]]; then continue; fi; # Skip empty lines
         vbm "DEBUG:adding to files_to_stamp_pruned:line:$line";
         files_to_stamp_pruned+=("$line");
     done < <(printf "%s\0" "${files_to_stamp[@]}" | sort -zu | shuf -z); # See [1]
@@ -638,87 +692,95 @@ main() {
         vbm "DEBUG:files_to_stamp_pruned:";
         printf "%s\n" "${files_to_stamp_pruned[@]}";
     fi;
-    
+
     # Act on files
-    ## Assemble upgrade file commands
-    for item in "${files_to_upgrade_pruned[@]}"; do
-        path_prf="$(cut -d $'\n' -f1 < <(echo "$item"))";
-        path_prf_sesc="${path_prf//\"/\\\"}"; # escape path double quotes. See [4].
-        if [[ -z "$path_prf" ]]; then
-            yell "ERROR:blank upgrade item encountered. Skipping:item:$item";
-            continue;
-        fi;
-        vbm "DEBUG:Attempting to upgrade proof file:path_prf:$path_prf";
-        if [[ ! $option_dry_run == "true" ]]; then
-            ### Try upgrade with known calendars in random order
-            while read -r url; do
-                vbm "DEBUG:Upgrading with calendar:url:$url";
-                if [[ "$opVerbose" = "true" ]]; then
-                    commands+=("cmdwrap ots -v -l $url --no-default-whitelist upgrade \"$path_prf_sesc\"") && break;
-                else
-                    commands+=("cmdwrap ots -l $url --no-default-whitelist upgrade \"$path_prf_sesc\"") && break;
-                fi;
-                #ots -l "$url" --no-default-whitelist upgrade "$path_prf" && break;
-            done < <(printf "%s\n" "${calendars[@]}" | shuf);
+    ## Assemble and execute upgrade file commands
+    if [[ ${#files_to_upgrade_pruned[@]} -gt 0 ]]; then
+        if [[ $option_dry_run == "true" ]]; then
+            yell "DEBUG:DRY RUN:Not running:\"ots upgrade\"";
         else
-            yell "DEBUG:DRY RUN:Not running:\"ots upgrade $path_prf\"";
+            ### Pick random calendar
+            function get_calurl() {
+                # Desc: Returns random calendar url
+                # Input:  string  OTS_CALS   newline-delimited list of calendars
+                # Output: stdout             single calendar url
+                printf "%s" "$OTS_CALS" | shuf -n1;
+            }; # print random calendar url to stdout
+            export -f get_calurl;
+
+            ### Perform upgrade command in xargs batches
+            printf '%s\0' "${files_to_upgrade_pruned[@]}" \
+                | shuf -z \
+                | xargs -0 -r -n "$max_batch_size" -P "$max_job_count" bash -c 'ots --no-default-whitelist -l "$(get_calurl)" upgrade "$@";' _ ; 
         fi;
-    done;
+    fi;
 
-    ## Assemble verify file commands
-    for item in "${files_to_verify_pruned[@]}"; do
-        path_src="$(cut -d $'\n' -f1 < <(echo "$item"))";
-        path_prf="$(cut -d $'\n' -f2 < <(echo "$item"))";
-        path_src_sesc="${path_src//\"/\\\"}"; # escape path double quotes. See [4].
-        path_prf_sesc="${path_prf//\"/\\\"}"; # escape path double quotes. See [4].
-        if [[ -z "$path_src" ]] || [[ -z "$path_prf" ]]; then
-            yell "ERROR:blank verify item encountered. Skipping:item:$item";
-            continue;
-        fi;
-        vbm "DEBUG:Attempting to verify source file:path_src:$path_src";
-        vbm "DEBUG:    against proof file:          path_prf:$path_prf";
-        if [[ ! $option_dry_run == "true" ]]; then
-            ### Try verify with known calendars in random order
-            while read -r url; do
-                vbm "DEBUG:Verifying with calendar:url:$url";
-                if [[ "$opVerbose" = "true" ]]; then
-                    commands+=("cmdwrap ots -v -l $url --no-default-whitelist verify -f \"$path_src_sesc\" \"$path_prf_sesc\"") && break;
+    # ## Assemble and execute verify file commands
+    if [[ $option_verify == "true" ]]; then
+        for item in "${files_to_verify_pruned[@]}"; do
+            wait_for_jobslot && {
+                path_src="$(cut -d $'\n' -f1 < <(echo "$item"))";
+                path_prf="$(cut -d $'\n' -f2 < <(echo "$item"))";
+                if [[ -z "$path_src" ]] || [[ -z "$path_prf" ]]; then
+                    yell "ERROR:blank verify item encountered. Skipping:item:$item";
+                    return 1; # would have been `continue` were it not in a subshell
+                fi;
+                vbm "DEBUG:Attempting to verify source file:path_src:$path_src";
+                vbm "DEBUG:    against proof file:          path_prf:$path_prf";
+                if [[ ! $option_dry_run == "true" ]]; then
+                    ### Try verify with known calendars in random order
+                    while read -r url; do
+                        vbm "DEBUG:Verifying with calendar:url:$url";
+
+                        #### assemble command
+                        local -a cmd_temp;
+                        cmd_temp=("ots");
+                        if [[ "$opVerbose" = "true" ]]; then cmd_temp+=("-v"); fi;
+                        cmd_temp+=("-l" "$url" "--no-default-whitelist");
+                        cmd_temp+=("verify" "-f" "$path_src" "$path_prf");
+                        if [[ "$opVerbose" = "true" ]]; then declare -p cmd_temp; fi;
+
+                        #### execute command
+                        "${cmd_temp[@]}";
+                        unset cmd_temp;
+                        break;
+                        #ots -l "$url" --no-default-whitelist verify -f "$path_src" "$path_prf" && break;
+                    done < <(printf "%s\n" "${calendars[@]}" | shuf);
                 else
-                    commands+=("cmdwrap ots -l $url --no-default-whitelist verify -f \"$path_src_sesc\" \"$path_prf_sesc\"") && break;
+                    yell "DEBUG:DRY RUN:Not running:\"ots verify -f $path_src $path_prf\"";
                 fi;
-                #ots -l "$url" --no-default-whitelist verify -f "$path_src" "$path_prf" && break;
-            done < <(printf "%s\n" "${calendars[@]}" | shuf);
-        else
-            yell "DEBUG:DRY RUN:Not running:\"ots verify -f $path_src $path_prf\"";
-        fi;
-    done;
+            } &
+        done;
+
+        ## Wait for jobs to finish.
+        wait;
+    else
+        vbm "DEBUG:Skipping operation:ots verify.";
+    fi;
     
-    ## Assemble stamp file commands
-    for item in "${files_to_stamp_pruned[@]}"; do
-        path_src="$(cut -d $'\n' -f1 < <(echo "$item"))";
-        path_src_sesc="${path_src//\"/\\\"}"; # escape path double quotes. See [4].
-        if [[ -z "$path_src" ]]; then
-            yell "ERROR:blank stamp item encountered. Skipping:item:$item";
-            continue;
-        fi;
-        vbm "DEBUG:Attempting to stamp source file:path_src:$path_src";
-        if [[ ! $option_dry_run == "true" ]]; then
-            if [[ "$opVerbose" = "true" ]]; then
-                commands+=("cmdwrap ots -v stamp \"$path_src_sesc\"");
-            else
-                commands+=("cmdwrap ots stamp \"$path_src_sesc\"");
-            fi;
-            #ots stamp "$path_src";
+    ## Assemble and execute stamp file commands
+    if [[ ${#files_to_stamp_pruned[@]} -gt 0 ]]; then
+        if [[ $option_dry_run == "true" ]]; then
+            yell "DEBUG:DRY RUN:Not running:\"ots stamp\"";
         else
-            yell "DEBUG:DRY RUN:Not running:\"ots stamp $path_src\"";
+            function get_calurl() {
+                # Desc: Returns random calendar url
+                # Input:  string  OTS_CALS   newline-delimited list of calendars
+                # Output: stdout             single calendar url
+                printf "%s" "$OTS_CALS" | shuf -n1;
+            }; # print random calendar url to stdout
+            export -f get_calurl;
+
+            ### Perform stamp command in xargs batches
+            printf '%s\0' "${files_to_stamp_pruned[@]}" \
+                | shuf -z \
+                | xargs -0 -r -n "$max_batch_size" -P "$max_job_count" bash -c 'ots stamp "$@";' _ ;
         fi;
-    done;
+    fi;
 
-    ## Run commands
-    yell "DEBUG:commands:$(printf "%s\n" "${commands[@]}")";
-    printf "%s\n" "${commands[@]}" | parallel --group;
 
 }; # main program
 
 # Run program
 main "$@";
+exit 0;