# Keep confirmed pairs scoped to the current shared directories on both sides. sync_files() { [ -n "$REIMS_HOST_URL" ] && [ -n "$REIMS_TOKEN" ] || return 0 sync_guest_root=$(sync_guest_root_id) || return 0 sync_remote="$TMP_DIR/sync-remote.tsv" sync_local="$TMP_DIR/sync-local.tsv" sync_headers="$TMP_DIR/sync-headers" sync_plan="$TMP_DIR/sync-plan.tsv" sync_delta="$TMP_DIR/sync-delta.tsv" sync_state=/dev/null if ! sync_request -fsS -m 5 -D "$sync_headers" -o "$sync_remote" "$REIMS_HOST_URL/v1/files?sync=1" 2>/dev/null; then return 0 fi sync_scope=$(sync_header X-Reims-Sync-Scope) case "$sync_scope" in "") ;; *[!0-9a-f]*) return 0 ;; *) sync_state="$APP_DIR/file-sync-$sync_scope-$sync_guest_root.tsv" ;; esac sync_previous="$sync_state" [ -f "$sync_state" ] || sync_previous=/dev/null if ! find "$SHARE_SYNC"/. -name '.*' ! -name . -prune -o -type f -exec /bin/sh -c ' root=$1 tab=$2 shift 2 newline=$(printf "\nx") newline=${newline%x} cr=$(printf "\r") for file do rel=${file#"$root"/./} case "$rel" in *"$tab"*|*"$newline"*|*"$cr"*) continue ;; esac row=$(stat -f "%m${tab}%z${tab}%Fm:%Fc:%i:%z" "$file") || { # An unreadable file cannot confirm a deletion. printf "\t\t\t%s\n" "$rel" continue } printf "%s\t%s\n" "$row" "$rel" done ' sh "$SHARE_SYNC" "$TAB" {} + > "$sync_local" 2>/dev/null; then return 0 fi # A missing path is a deletion only after a confirmed pair has existed. # If the remaining copy changed, preserve it using the usual copy rules. awk -F '\t' -v extended="$sync_scope" ' BEGIN { OFS = FS } FILENAME == ARGV[1] { previousLocal[$4] = $1; previousRemote[$4] = $2; state[$4] = $3; paths[$4] = 1 } FILENAME == ARGV[2] { p = extended == "" ? $3 : $4 if (p != "") { remoteTime[p] = $1; remoteSize[p] = $2; remote[p] = extended == "" ? "-" : $3; paths[p] = 1 } } FILENAME == ARGV[3] && $4 != "" { if ($3 == "") { unreadable[$4] = 1; next } localTime[$4] = $1; localSize[$4] = $2; local[$4] = $3; paths[$4] = 1 } END { for (p in paths) { if (p in unreadable) continue l = p in local; r = p in remote paired = state[p] != "" && state[p] != "different" op = "" if (l && r) { compareByMeta = 0 if (extended != "" && paired) { if (remote[p] != previousRemote[p] && local[p] == previousLocal[p]) op = "download" else if (local[p] != previousLocal[p] && remote[p] == previousRemote[p]) { if (localSize[p] <= 268435456) op = "upload" } else if (local[p] != previousLocal[p] && remote[p] != previousRemote[p]) { compareByMeta = 1 } } else { compareByMeta = 1 } if (compareByMeta) { if (remoteTime[p] + 0 > localTime[p] + 2) op = "download" else if (localTime[p] + 0 > remoteTime[p] + 2 && localSize[p] <= 268435456) op = "upload" else if (localSize[p] != remoteSize[p]) { # Equal times do not show which copy is complete, so keep both. if (remoteTime[p] + 0 > localTime[p] + 0) op = "download" else if (localTime[p] + 0 > remoteTime[p] + 0 && localSize[p] <= 268435456) op = "upload" } else if (extended != "" && localTime[p] == remoteTime[p] && localSize[p] == remoteSize[p] && (local[p] != previousLocal[p] || remote[p] != previousRemote[p])) op = "verify" } } else if (r) { op = paired && remote[p] == previousRemote[p] ? "delete-host" : "download" } else if (l) { if (paired && local[p] == previousLocal[p]) op = "delete-guest" else if (localSize[p] <= 268435456) op = "upload" } else { op = "forget" } if (op != "") { localRevision = l ? local[p] : "-" remoteRevision = r ? remote[p] : "-" localMtime = l ? localTime[p] : "-" digest = state[p] != "" ? state[p] : "-" print op, localRevision, remoteRevision, localMtime, digest, p } } } ' "$sync_previous" "$sync_remote" "$sync_local" > "$sync_plan" || return 0 : > "$sync_delta" || return 0 sync_verify_left=8 while IFS="$TAB" read -r sync_op sync_guest_rev sync_host_rev sync_guest_time sync_digest sync_rel; do sync_apply_file done < "$sync_plan" if [ "$sync_state" != /dev/null ]; then awk -F '\t' ' $1 == "-" { delete rows[$4]; next } { rows[$4] = $0 } END { for (p in rows) print rows[p] } ' "$sync_previous" "$sync_delta" > "$TMP_DIR/sync-state.next" && mv -f "$TMP_DIR/sync-state.next" "$sync_state" fi } sync_request() { curl -H "Authorization: Bearer $REIMS_TOKEN" "$@" } sync_header() { awk -v name="$1:" 'tolower($1) == tolower(name) { sub(/\r$/, "", $2); print $2; exit }' "$sync_headers" } sync_guest_root_id() { [ -d "$SHARE_SYNC" ] && [ ! -L "$SHARE_SYNC" ] && stat -f '%d-%i-%B' "$SHARE_SYNC" 2>/dev/null } sync_guest_revision() { [ "$(sync_guest_root_id)" = "$sync_guest_root" ] || return 1 sync_check=$1 while [ "$sync_check" != "$SHARE_SYNC" ]; do [ ! -L "$sync_check" ] || return 1 sync_check=${sync_check%/*} done if [ -f "$1" ]; then stat -f '%Fm:%Fc:%i:%z' "$1" 2>/dev/null elif [ ! -e "$1" ]; then printf '%s\n' '-' fi } sync_record() { [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ] || return 0 printf '%s\t%s\t%s\t%s\n' "$1" "$2" "$3" "$sync_rel" >> "$sync_delta" } sync_apply_file() { case "/$sync_rel/" in //*|*/../*|*/./*|*//*) return ;; esac sync_target="$SHARE_SYNC/$sync_rel" [ "$(sync_guest_revision "$sync_target")" = "$sync_guest_rev" ] || return 0 sync_url="$REIMS_HOST_URL/v1/file?path=$(urlencode "$sync_rel")" if [ -n "$sync_scope" ]; then set -- -H "X-Reims-Sync-Scope: $sync_scope" if [ "$sync_host_rev" != - ]; then set -- "$@" -H "If-Match: $sync_host_rev" fi else set -- fi case "$sync_op" in delete-host) sync_status=$(sync_request -sS -m 60 "$@" -H "X-Reims-File-SHA256: $sync_digest" -X DELETE \ -o /dev/null -w '%{http_code}' "$sync_url" 2>/dev/null) || return 0 case "$sync_status" in 204) sync_record - - -; return ;; 412) sync_op=download ;; *) return 0 ;; esac ;; delete-guest|forget) # A partial host listing cannot confirm that a file was deleted. sync_status=$(sync_request -sS -m 10 "$@" -I -o /dev/null -w '%{http_code}' "$sync_url" 2>/dev/null) || return 0 [ "$sync_status" = 404 ] || return 0 if [ "$sync_op" = "forget" ]; then sync_record - - - return fi sync_actual=$(hash_file "$sync_target") [ -n "$sync_actual" ] && [ "$(sync_guest_revision "$sync_target")" = "$sync_guest_rev" ] || return 0 if [ "$sync_actual" = "$sync_digest" ]; then rm "$sync_target" && sync_record - - - return fi sync_op=upload ;; esac case "$sync_op" in download) sync_part="$TMP_DIR/sync-download.part" if sync_request -fsS --connect-timeout 10 --speed-time 30 --speed-limit 1024 "$@" -D "$sync_headers" -o "$sync_part" "$sync_url" 2>/dev/null; then sync_download_digest="" if [ -n "$sync_scope" ]; then sync_download_digest=$(hash_file "$sync_part") fi if sync_stamp=$(TZ=UTC date -r "$(sync_header X-File-Mtime)" '+%Y%m%d%H%M.%S' 2>/dev/null); then TZ=UTC touch -t "$sync_stamp" "$sync_part" 2>/dev/null || true fi if [ "$(sync_guest_revision "$sync_target")" = "$sync_guest_rev" ] && mkdir -p "$(dirname "$sync_target")" && mv -f "$sync_part" "$sync_target"; then sync_record "$(sync_guest_revision "$sync_target")" "$(sync_header ETag)" "$sync_download_digest" fi fi rm -f "$sync_part" ;; upload) if [ -n "$sync_scope" ] && [ "$sync_host_rev" = - ]; then set -- "$@" -H 'If-None-Match: *' fi if sync_request -fsS --connect-timeout 10 --speed-time 30 --speed-limit 1024 "$@" -X PUT --data-binary "@$sync_target" -D "$sync_headers" -o /dev/null \ "$sync_url&mtime=$sync_guest_time" 2>/dev/null && [ -n "$sync_scope" ] && sync_uploaded_rev=$(sync_guest_revision "$sync_target") && [ -n "$sync_uploaded_rev" ] && [ "$sync_uploaded_rev" != - ]; then # A file written during the upload differs from this revision, # so the next round uploads the finished copy. sync_record "$sync_guest_rev" "$(sync_header ETag)" "$(sync_header X-Reims-File-SHA256)" fi ;; verify) # Bound initial migration work when adopting pre-existing copies. [ "$sync_verify_left" -gt 0 ] || return 0 sync_verify_left=$((sync_verify_left - 1)) if sync_request -fsS -m 15 "$@" -I -D "$sync_headers" -o /dev/null "$sync_url&sha256=1" 2>/dev/null; then sync_host_hash=$(sync_header X-Reims-File-SHA256) sync_guest_hash=$(hash_file "$sync_target") if [ -n "$sync_host_hash" ] && [ -n "$sync_guest_hash" ] && [ "$(sync_guest_revision "$sync_target")" = "$sync_guest_rev" ]; then [ "$sync_host_hash" = "$sync_guest_hash" ] || sync_host_hash=different sync_record "$sync_guest_rev" "$sync_host_rev" "$sync_host_hash" fi fi ;; esac }