Debian Codegraph @

etc/setup.d/20copyfiles

raw symbols ◀ ▶
Source reconstructed at patch state 5 of 6 — not the installed (fully patched) source.
6 Debian patches touch this file
1 #!/bin/sh
2 # Copyright © 2005-2007 Roger Leigh <[email protected]>
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 . "$SETUP_DATA_DIR/common-data"
23 . "$SETUP_DATA_DIR/common-functions"
24 . "$SETUP_DATA_DIR/common-config"
25
26 if [ "$VERBOSE" = "verbose" ]; then
27 CP_VERBOSE="--verbose"
28 fi
29
30 # Copy a file if the source and destination differ
31 # $1: source file
32 # $2: destination file
33 copy_file()
34 {
35 if [ -e "$1" ]; then
36
37 COPY="true"
38
39 if [ -e "$2" ]; then
40
41 # Device and inode
42 da=$(/usr/bin/stat --format="%d %i" "$1")
43 # This one can fail since it might not exist yet
44 db=$(/usr/bin/stat --format="%d %i" "$2" 2>/dev/null || :)
45
46 if [ "$da" = "$db" ]; then
47 COPY="false"
48 elif [ -L "$2" ]; then
49 # Copy if destination is a symlink
50 :
51 elif [ -f "$1" ] && [ -f "$2" ]; then
52 # Content
53 ca=$(/usr/bin/md5sum "$1" | sed -e 's/\(^[0-9a-f][0-9a-f]*\).*$/\1/')
54 cb=$(/usr/bin/md5sum "$2" 2>/dev/null || :)
55 cb=$(echo "$cb" | sed -e 's/\(^[0-9a-f][0-9a-f]*\).*$/\1/')
56 # Copy only if file contents differ
57 if [ "$ca" = "$cb" ]; then
58 COPY="false"
59 fi
60 fi
61 fi
62
63 # Copy only if files are different
64 if [ "$COPY" = "true" ]; then
65 mkdir -p $(dirname $2)
66 if [ -f "$1" ]; then
67 cp --remove-destination --preserve=all $CP_VERBOSE "$1" "$2"
68 else
69 # Copy non-regular file directly
70 cp --remove-destination -a $CP_VERBOSE "$1" "$2"
71 fi
72 fi
73
74 else
75 fatal "Not copying nonexistent file: $file"
76 fi
77 }
78
79 # Resolve a relative path inside the chroot to its absolute path on the host
80 # $1: base path of the chroot
81 # $2: relative path to resolve
82 resolve_path()
83 {
84 base_path="$(realpath "$1")"
85 relative_destination="${2#/}"
86 absolute_destination="$base_path"
87
88 while [ -n "$relative_destination" ]; do
89 first_component="${relative_destination%%/*}"
90 relative_destination="${relative_destination#$first_component}"
91 relative_destination="${relative_destination#/}"
92
93 # If the first component is a link
94 if link="$(readlink "$absolute_destination/$first_component")"; then
95 # If the first component is a relative link
96 if [ "${link#/}" = "$link" ]; then
97 relative_destination="$link/$relative_destination"
98 else
99 absolute_destination="$base_path"
100 relative_destination="${link#/}/$relative_destination"
101 fi
102 else
103 absolute_destination="$(realpath "$absolute_destination/$first_component")"
104
105 # If the absolute destination gets out of the chroot
106 if [ "${absolute_destination#$base_path}" = "$absolute_destination" ]; then
107 absolute_destination="$base_path"
108 fi
109 fi
110 done
111
112 echo "$absolute_destination"
113 }
114
115 if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then
116
117 if [ -n "$SETUP_COPYFILES" ]; then
118 if [ -f "$SETUP_COPYFILES" ]; then
119 while read src dst; do
120 : ${dst:=$src}
121 if echo "$src" | grep -Eq '^(#|$)' ; then
122 continue
123 fi
124 if echo "$src" | grep -q '^/' &&
125 echo "$dst" | grep -q '^/'; then
126 copy_file "$src" "$(resolve_path "${CHROOT_PATH}" "$dst")"
127 else
128 warn "Not copying file with relative path: $src -> $dst"
129 fi
130 done < "$SETUP_COPYFILES"
131 else
132 fatal "copyfiles file '$SETUP_COPYFILES' does not exist"
133 fi
134 fi
135
136 fi
137