| 1 |
#! /usr/local/bin/bash |
| 2 |
|
| 3 |
# Shell script to copy files and directories, stripping things appropriately. |
| 4 |
# Executable programs are stripped completely, and library archives |
| 5 |
# have debugging symbols removed. |
| 6 |
|
| 7 |
usage() |
| 8 |
{ |
| 9 |
echo >&2 "Usage: $0 [-N NEWER-THAN-FILE] SOURCE-DIR TARGET-DIR" |
| 10 |
exit 1 |
| 11 |
} |
| 12 |
|
| 13 |
# If non-null, only copy files newer than this file when copying directories. |
| 14 |
newer_than='' |
| 15 |
|
| 16 |
while :; do |
| 17 |
case "$1" in |
| 18 |
-N) newer_than="$2"; shift 2 |
| 19 |
if test -r "$newer_than"; then |
| 20 |
case "$newer_than" in [!/]*) |
| 21 |
newer_than=`pwd`/"$newer_than" |
| 22 |
esac |
| 23 |
else |
| 24 |
# There is no such file, so don't try to use it |
| 25 |
newer_than='' |
| 26 |
fi;; |
| 27 |
-*) usage;; |
| 28 |
*) break;; |
| 29 |
esac |
| 30 |
done |
| 31 |
|
| 32 |
if [ $# -lt 2 ]; then |
| 33 |
usage |
| 34 |
fi |
| 35 |
|
| 36 |
OBJCOPY=${OBJCOPY:-i386-gnu-objcopy} |
| 37 |
|
| 38 |
check_inode() |
| 39 |
{ |
| 40 |
inode=`ls -dli "$1" | awk '{ print $1 }'` |
| 41 |
eval "old_inode=\${inode_${inode}}" |
| 42 |
eval "inode_${inode}=$2" |
| 43 |
} |
| 44 |
|
| 45 |
# Prints the files in DIR, only using those newer than $newer_than if |
| 46 |
# it's non-null |
| 47 |
dir_files() |
| 48 |
{ |
| 49 |
case "$newer_than" in |
| 50 |
"") test='';; |
| 51 |
*) test="-newer $newer_than";; |
| 52 |
esac |
| 53 |
|
| 54 |
find $1 -maxdepth 1 \( ! -type d $test -print \) \ |
| 55 |
| sed "s@^$1/@@" |
| 56 |
} |
| 57 |
|
| 58 |
copy() |
| 59 |
{ |
| 60 |
local from=$1 |
| 61 |
local to=$2 |
| 62 |
|
| 63 |
copyattrs() |
| 64 |
{ |
| 65 |
local ls="`ls -l $from`" |
| 66 |
local owner="`echo "$ls" | awk '{print $3 "." $4}'`" |
| 67 |
local mode="`echo "$ls" | sed -e 's/^.\(...\)\(...\)\(...\).*$/u=\1,g=\2,o=\3/' -e 's/-//g' -e 's/s/xs/g'`" |
| 68 |
chown "$owner" $to |
| 69 |
chmod "$mode" $to |
| 70 |
} |
| 71 |
|
| 72 |
plaincopy() |
| 73 |
{ |
| 74 |
rm -f $to |
| 75 |
echo "$from -> $to" |
| 76 |
if ! ln -f $from $to 2>/dev/null; then |
| 77 |
install -m 644 -c $from $to |
| 78 |
copyattrs |
| 79 |
fi |
| 80 |
} |
| 81 |
|
| 82 |
objcopy() |
| 83 |
{ |
| 84 |
if $OBJCOPY --strip-debug $from $to; then |
| 85 |
echo "strip-debug $from -> $to" |
| 86 |
copyattrs |
| 87 |
else |
| 88 |
plaincopy |
| 89 |
fi |
| 90 |
} |
| 91 |
|
| 92 |
copysymlink () |
| 93 |
{ |
| 94 |
linkto="`ls -ld $from | awk '{ print $NF }'`" |
| 95 |
if test -L $to; then |
| 96 |
oldlinkto="`ls -ld $to | awk '{ print $NF }'`" |
| 97 |
else |
| 98 |
oldlinkto=not-the-value-of-any-real-symlink |
| 99 |
fi |
| 100 |
|
| 101 |
if test $linkto != $oldlinkto; then |
| 102 |
rm -f $to |
| 103 |
echo "making symlink $to -> $linkto" |
| 104 |
ln -s $linkto $to |
| 105 |
fi |
| 106 |
} |
| 107 |
|
| 108 |
makelocalhardlink () |
| 109 |
{ |
| 110 |
if test -e $to; then |
| 111 |
fromino=`ls -dli "$from" | awk '{ print $1 }'` |
| 112 |
toino=`ls -dli "$to" | awk '{print $1 }'` |
| 113 |
if test $fromino != $toino; then |
| 114 |
echo "$to linked to $old_inode" |
| 115 |
ln -f $old_inode $to |
| 116 |
fi |
| 117 |
else |
| 118 |
echo "$to linked to $old_inode" |
| 119 |
ln -f $old_inode $to |
| 120 |
fi |
| 121 |
} |
| 122 |
|
| 123 |
check_inode $from $to |
| 124 |
|
| 125 |
# Not necessary; the other bits already do necessary deletion. |
| 126 |
# test -d $to || { test -e $to && rm -f $to } |
| 127 |
|
| 128 |
if test -L $from; then |
| 129 |
copysymlink |
| 130 |
elif test "$old_inode"; then |
| 131 |
makelocalhardlink |
| 132 |
elif test -d $from; then |
| 133 |
if test ! -d $to; then |
| 134 |
echo making $to |
| 135 |
mkdir -p $to |
| 136 |
fi |
| 137 |
for file in `dir_files $from` ..; do |
| 138 |
test "$file" = .. && continue |
| 139 |
copy $from/$file $to/$file |
| 140 |
done |
| 141 |
else |
| 142 |
case $from in |
| 143 |
*.a) |
| 144 |
read p l o g size rest <<foo |
| 145 |
`ls -l $from` |
| 146 |
foo |
| 147 |
# I've reenabled installing large libraries; please don't change |
| 148 |
# this without syncing with mib first. |
| 149 |
# if test $size -gt 200000; then |
| 150 |
# echo "skipping large library $from ($size)" |
| 151 |
# else |
| 152 |
objcopy |
| 153 |
# fi |
| 154 |
;; |
| 155 |
*.o | *.so | *.so.*) |
| 156 |
objcopy |
| 157 |
;; |
| 158 |
*) |
| 159 |
if test -x $from; then |
| 160 |
if $OBJCOPY -S $from $to 2>/dev/null; then |
| 161 |
echo "strip $from -> $to" |
| 162 |
copyattrs |
| 163 |
else |
| 164 |
plaincopy |
| 165 |
fi |
| 166 |
else |
| 167 |
plaincopy |
| 168 |
fi |
| 169 |
;; |
| 170 |
esac |
| 171 |
fi |
| 172 |
} |
| 173 |
|
| 174 |
if [ $# -gt 2 ]; then |
| 175 |
eval "todir=\${$#}" |
| 176 |
test -d $todir || { echo >&2 With multiple args, last must be a directory. |
| 177 |
exit 1; } |
| 178 |
while [ $# -gt 1 ]; do |
| 179 |
copy $1 $todir/`basename $1` |
| 180 |
shift |
| 181 |
done |
| 182 |
else |
| 183 |
copy $1 $2 |
| 184 |
fi |