| 1 |
#!/bin/sh |
| 2 |
set -e |
| 3 |
|
| 4 |
# Keep the sort order the same at all times. |
| 5 |
LC_COLLATE=C |
| 6 |
export LC_COLLATE |
| 7 |
unset LC_ALL |
| 8 |
|
| 9 |
filter_ignore() { |
| 10 |
case "$VCS" in |
| 11 |
darcs) ignorefile=.darcsignore ;; |
| 12 |
git) ignorefile=.gitignore ;; |
| 13 |
esac |
| 14 |
|
| 15 |
if [ -n "$ignorefile" ] && [ -e "$ignorefile" ]; then |
| 16 |
if command -v mktemp >/dev/null; then |
| 17 |
tempfile="mktemp -t etckeeper-$VCS.XXXXXXXXXX" |
| 18 |
elif command -v tempfile >/dev/null; then |
| 19 |
tempfile="tempfile" |
| 20 |
else |
| 21 |
echo "etckeeper warning: can't find tempfile or mktemp" >&2 |
| 22 |
exit 1 |
| 23 |
fi |
| 24 |
listfile="$($tempfile)" |
| 25 |
case "$VCS" in |
| 26 |
darcs) |
| 27 |
LC_CTYPE=C grep -v '^[[:space:]]*\(#\|$\)' "$ignorefile" > "$listfile" || true |
| 28 |
LC_CTYPE=C grep -Evf "$listfile" |
| 29 |
;; |
| 30 |
git) |
| 31 |
(git ls-files -oi --exclude-standard; git ls-files -oi --exclude-standard --directory) | sort | uniq > "$listfile" || true |
| 32 |
if [ -s "$listfile" ]; then |
| 33 |
sed 's/^\.\///' | LC_CTYPE=C grep -xFvf "$listfile" |
| 34 |
else |
| 35 |
cat - |
| 36 |
fi |
| 37 |
;; |
| 38 |
esac |
| 39 |
rm -f "$listfile" |
| 40 |
unset listfile |
| 41 |
else |
| 42 |
cat - |
| 43 |
fi |
| 44 |
} |
| 45 |
|
| 46 |
shellquote() { |
| 47 |
# Single quotes text, escaping existing single quotes. |
| 48 |
sed -e "s/'/'\"'\"'/g" -e "s/^/'/" -e "s/$/'/" |
| 49 |
} |
| 50 |
|
| 51 |
generate_metadata() { |
| 52 |
# This function generates the script commands to fix any file |
| 53 |
# ownerships that aren't owner=root, group=root, as well as to |
| 54 |
# store the permissions of files. |
| 55 |
# The script is produced on stdout. Errors go to stderr. |
| 56 |
# |
| 57 |
# The script can use a 'maybe' function, which only runs a command |
| 58 |
# if the file in its last argument exists. |
| 59 |
|
| 60 |
# We want files in the directory containing VCS data |
| 61 |
# but we want find to ignore the VCS files themselves. |
| 62 |
# |
| 63 |
# (Note that when using this, the find expression must end with |
| 64 |
# -print or -exec, else the excluded directories will actually be |
| 65 |
# printed!) |
| 66 |
NOVCS='. -path ./.git -prune -o -path ./.bzr -prune -o -path ./.hg -prune -o -path ./_darcs -prune -o' |
| 67 |
|
| 68 |
if [ "$VCS" = git ] || [ "$VCS" = hg ]; then |
| 69 |
# These version control systems do not track directories, |
| 70 |
# so empty directories must be stored specially. |
| 71 |
find $NOVCS -type d -empty -print | |
| 72 |
sort | shellquote | sed -e "s/^/mkdir -p /" |
| 73 |
fi |
| 74 |
|
| 75 |
if [ "$VCS" = darcs ]; then |
| 76 |
# This version control system does not track symlinks, |
| 77 |
# so they must be stored specially. |
| 78 |
find $NOVCS -type l -print | sort | filter_ignore | while read link; do |
| 79 |
dest=$( readlink "$link" ) |
| 80 |
printf "ln -sf '%s' '%s'\n" "$(echo "$dest" | shellquote)" "$(echo "$link" | shellquote)" |
| 81 |
done |
| 82 |
fi |
| 83 |
|
| 84 |
# Store things that don't have the default user or group. |
| 85 |
# Store all file modes, in case the user has an unusual umask. |
| 86 |
find $NOVCS \( -type f -or -type d \) -print | filter_ignore | sort | maybe_chmod_chown |
| 87 |
|
| 88 |
# We don't handle xattrs. |
| 89 |
# Maybe check for getfattr/setfattr and use them if they're available? |
| 90 |
} |
| 91 |
|
| 92 |
maybe_chmod_chown() { |
| 93 |
if command -v perl >/dev/null; then |
| 94 |
perl -ne ' |
| 95 |
BEGIN { $q=chr(39) } |
| 96 |
sub uidname { |
| 97 |
my $want=shift; |
| 98 |
if (exists $uidcache{$want}) { |
| 99 |
return $uidcache{$want}; |
| 100 |
} |
| 101 |
my $name=scalar getpwuid($want); |
| 102 |
return $uidcache{$want}=defined $name ? $name : $want; |
| 103 |
} |
| 104 |
sub gidname { |
| 105 |
my $want=shift; |
| 106 |
if (exists $gidcache{$want}) { |
| 107 |
return $gidcache{$want}; |
| 108 |
} |
| 109 |
my $name=scalar getgrgid($want); |
| 110 |
return $gidcache{$want}=defined $name ? $name : $want; |
| 111 |
} |
| 112 |
chomp; |
| 113 |
my @stat=stat($_); |
| 114 |
my $mode = $stat[2]; |
| 115 |
my $uid = $stat[4]; |
| 116 |
my $gid = $stat[5]; |
| 117 |
s/$q/$q"$q"$q/g; # escape single quotes |
| 118 |
s/^/$q/; |
| 119 |
s/$/$q/; |
| 120 |
if ($uid != $>) { |
| 121 |
printf "maybe chown $q%s$q %s\n", uidname($uid), $_; |
| 122 |
} |
| 123 |
if ($gid != $)) { |
| 124 |
printf "maybe chgrp $q%s$q %s\n", gidname($gid), $_; |
| 125 |
} |
| 126 |
printf "maybe chmod %04o %s\n", $mode & 07777, $_; |
| 127 |
' |
| 128 |
return $? |
| 129 |
else |
| 130 |
# fallback if perl isn't present |
| 131 |
euid=$(id -u) |
| 132 |
egid=$(id -g) |
| 133 |
q="'" |
| 134 |
while read x; do |
| 135 |
stat=$(stat -c "%f:%u:%g:%a:%U:%G" "$x") |
| 136 |
IFS=":" read mode uid gid perm uname gname <<EOF |
| 137 |
$stat |
| 138 |
EOF |
| 139 |
x=$q$(echo $x | sed "s/$q/$q\"$q\"$q/")$q |
| 140 |
if [ $uid -ne $euid ]; then |
| 141 |
echo maybe chown "'$uname'" $x |
| 142 |
fi |
| 143 |
if [ $gid -ne $egid ]; then |
| 144 |
echo maybe chgrp "'$gname'" $x |
| 145 |
fi |
| 146 |
echo maybe chmod 0$perm $x |
| 147 |
done |
| 148 |
fi |
| 149 |
} |
| 150 |
|
| 151 |
if [ "$VCS" = git ] || [ "$VCS" = hg ] || [ "$VCS" = bzr ] || [ "$VCS" = darcs ]; then |
| 152 |
if [ -f .metadata ]; then |
| 153 |
# remove obsolete .metadata file |
| 154 |
# git allows fully deleting it at this point, other VCS |
| 155 |
# may not (the repo is locked for hg). |
| 156 |
if [ "$VCS" = git ]; then |
| 157 |
$VCS rm .metadata |
| 158 |
else |
| 159 |
rm -f .metadata |
| 160 |
fi |
| 161 |
fi |
| 162 |
|
| 163 |
echo "# Generated by etckeeper. Do not edit." > .etckeeper |
| 164 |
echo >> .etckeeper |
| 165 |
|
| 166 |
# Make sure the file is not readable by others, since it can leak |
| 167 |
# information about contents of non-readable directories in /etc. |
| 168 |
chmod 700 .etckeeper |
| 169 |
|
| 170 |
generate_metadata >> .etckeeper |
| 171 |
|
| 172 |
# stage the file as part of the current commit |
| 173 |
if [ "$VCS" = git ]; then |
| 174 |
# this will do nothing if the metadata file is unchanged. |
| 175 |
git add .etckeeper |
| 176 |
fi |
| 177 |
# hg, bzr and darcs add not done, they will automatically |
| 178 |
# include the file in the current commit |
| 179 |
fi |