Skip to content

Instantly share code, notes, and snippets.

@larryv
Last active January 16, 2023 03:52
Show Gist options
  • Save larryv/8008a93d6fabf0568cc4adaabc0d1bfb to your computer and use it in GitHub Desktop.
Save larryv/8008a93d6fabf0568cc4adaabc0d1bfb to your computer and use it in GitHub Desktop.
A macOS shell script for syncing the kib.kiev.ua/x86docs documentation archive
#!/bin/sh
# x86docssync - Syncs documents from kib.kiev.ua/x86docs
#
# Written in 2020, 2022 by Lawrence Velázquez <vq@larryv.me>.
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# For a copy of the CC0 Public Domain Dedication, see
# <https://creativecommons.org/publicdomain/zero/1.0/>.
if test "$#" -eq 0; then
echo 'usage: x86docssync target_dir' >&2
exit 1
fi
dst=$1
# ---- STEP 1: SYNC -----
rsyncsrc=rsync://anon@kib.kiev.ua/x86docs
httpssrc=https://kib.kiev.ua/x86docs
RSYNC_PASSWORD= rsync --chmod=D755,F644 --recursive --times \
--human-readable --itemize-changes --stats --verbose \
-- "$rsyncsrc" "$dst"
rc=$?
# ----- STEP 2: ADD EXTRANEOUS EXTENDED ATTRIBUTE -----
cd -- "$dst" || exit
# Export these so I don't have to repeatedly pass them to the script
# through the positional parameters.
export rsyncsrc httpssrc
# NOTE: Remember to embed single quotes properly.
writexattr='
for f
do
# Percent-encode quotation marks to avoid code-injecting the
# property list. Fully encoding the URL is not worth the effort.
pencf=$(printf '\''%s.\n'\'' "${f#.}" | sed '\''s/"/%22/g'\'')
rsyncurl=${rsyncsrc}${pencf%.}
httpsurl=${httpssrc}${pencf%.}
wherefromval=$(printf '\''["%s","%s"]'\'' "$rsyncurl" "$httpsurl" \
| plutil -convert binary1 -o - - \
| od -A n -t x1 -v)
test -n "$wherefromval" || continue
xattr -wx com.apple.metadata:kMDItemWhereFroms "$wherefromval" "$f"
done
'
# Add "Where from" xattr to files that don't have it.
echo 'Updating xattr com.apple.metadata:kMDItemWhereFroms...' >&2
find . -type f ! -xattrname com.apple.metadata:kMDItemWhereFroms \
! -name '.*' -exec sh -c "$writexattr" sh {} +
# ---- STEP 3: PROPAGATE RSYNC'S EXIT STATUS -----
exit "$rc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment