1 | #!/bin/sh |
---|
2 | # Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
3 | # |
---|
4 | # schroot is free software: you can redistribute it and/or modify it |
---|
5 | # under the terms of the GNU General Public License as published by |
---|
6 | # the Free Software Foundation, either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | # |
---|
9 | # schroot is distributed in the hope that it will be useful, but |
---|
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | # General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program. If not, see |
---|
16 | # <http://www.gnu.org/licenses/>. |
---|
17 | # |
---|
18 | ##################################################################### |
---|
19 | |
---|
20 | set -e |
---|
21 | |
---|
22 | if [ -f "$CHROOT_SCRIPT_CONFIG" ]; then |
---|
23 | . "$CHROOT_SCRIPT_CONFIG" |
---|
24 | elif [ "$2" = "ok" ]; then |
---|
25 | echo "script-config file '$CHROOT_SCRIPT_CONFIG' does not exist" |
---|
26 | exit 1 |
---|
27 | fi |
---|
28 | |
---|
29 | if [ "$AUTH_VERBOSITY" = "verbose" ]; then |
---|
30 | VERBOSE="--verbose" |
---|
31 | fi |
---|
32 | |
---|
33 | # Copy a file if the source and destination differ |
---|
34 | # $1: source file |
---|
35 | # $2: destination file |
---|
36 | copy_file() |
---|
37 | { |
---|
38 | if [ -e "$1" ]; then |
---|
39 | |
---|
40 | COPY="true" |
---|
41 | |
---|
42 | if [ -e "$2" ]; then |
---|
43 | |
---|
44 | # Device and inode |
---|
45 | da=$(/usr/bin/stat --format="%d %i" "$1") |
---|
46 | db=$(/usr/bin/stat --format="%d %i" "$2") |
---|
47 | |
---|
48 | if [ "$da" = "$db" ]; then |
---|
49 | COPY="false" |
---|
50 | elif [ -L "$2" ]; then |
---|
51 | # Copy if destination is a symlink |
---|
52 | : |
---|
53 | elif [ -f "$1" ] && [ -f "$2" ]; then |
---|
54 | # Content |
---|
55 | ca=$(/usr/bin/md5sum "$1" | sed -e 's/\(^[0-9a-f][0-9a-f]*\).*$/\1/') |
---|
56 | cb=$(/usr/bin/md5sum "$2" | sed -e 's/\(^[0-9a-f][0-9a-f]*\).*$/\1/') |
---|
57 | # Copy only if file contents differ |
---|
58 | if [ "$ca" = "$cb" ]; then |
---|
59 | COPY="false" |
---|
60 | fi |
---|
61 | fi |
---|
62 | fi |
---|
63 | |
---|
64 | # Copy only if files are different |
---|
65 | if [ "$COPY" = "true" ]; then |
---|
66 | if [ -f "$1" ]; then |
---|
67 | cp --preserve=all $VERBOSE "$1" "$2" |
---|
68 | else |
---|
69 | # Copy non-regular file directly |
---|
70 | cp -a $VERBOSE "$1" "$2" |
---|
71 | fi |
---|
72 | fi |
---|
73 | |
---|
74 | else |
---|
75 | echo "Not copying nonexistent file: $file" |
---|
76 | exit 1 |
---|
77 | fi |
---|
78 | } |
---|
79 | |
---|
80 | if [ $1 = "setup-start" ] || [ $1 = "setup-recover" ]; then |
---|
81 | |
---|
82 | if [ -n "$COPYFILES" ]; then |
---|
83 | if [ -f "$COPYFILES" ]; then |
---|
84 | while read file; do |
---|
85 | if echo "$file" | grep -q '^/'; then |
---|
86 | copy_file "$file" "${CHROOT_PATH}$file" |
---|
87 | else |
---|
88 | echo "Not copying file with relative path: $file" |
---|
89 | fi |
---|
90 | done < "$COPYFILES" |
---|
91 | else |
---|
92 | echo "copyfiles file '$COPYFILES' does not exist" |
---|
93 | exit 1 |
---|
94 | fi |
---|
95 | fi |
---|
96 | |
---|
97 | fi |
---|
98 | |
---|