Skip to content

Instantly share code, notes, and snippets.

@emlun
Last active March 31, 2016 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emlun/6c9fbcf38f2ccfe14a4b944cbbd91f9b to your computer and use it in GitHub Desktop.
Save emlun/6c9fbcf38f2ccfe14a4b944cbbd91f9b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Clean out Syncthing *.sync-conflict-* files from Password Store
#
# Usage: Run the script from anywhere with no parameters.
#
# The script will remove all conflict files in `~/.password-store` for which
# the plaintext contents of the original file and the conflict file have the
# same SHA256 checksum. I.e. if `foo.sync-conflict-20151115-205258.gpg` and
# `foo.gpg` have the same checksums after decrypting, then the former will be
# removed.
#
# Author: Emil Lundberg <lundberg.emil@gmail.com>
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
TMPDIR=$(mktemp -d)
echo "Working with TMPDIR=$TMPDIR"
for conflict_fname in $(find ~/.password-store -type f -name '*.sync-conflict-*.gpg'); do
fname="${conflict_fname%.sync-conflict-*.gpg}.gpg"
bname=$(basename "$fname")
decrypted_file="$TMPDIR/$bname"
checksum_file="$TMPDIR/$bname.sha256sum"
gpg -q -d "$conflict_fname" | sha256sum > "$checksum_file"
sed -i "s#-\$#${decrypted_file}#" "$checksum_file"
gpg -q -d "$fname" > "$decrypted_file"
if sha256sum -c "$checksum_file" &>/dev/null; then
echo "Removing $conflict_fname"
rm "$conflict_fname"
else
echo "File ${conflict_fname} differs from ${fname}"
fi
done
shred -u "$TMPDIR"/*
rmdir "$TMPDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment