Debian Codegraph @

etc/setup.d/20copyfiles

raw symbols ◀ ▶
Source reconstructed at patch state 2 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 if [ -f "$1" ]; then
66 cp --remove-destination --preserve=all $CP_VERBOSE "$1" "$2"
67 else
68 # Copy non-regular file directly
69 cp --remove-destination -a $CP_VERBOSE "$1" "$2"
70 fi
71 fi
72
73 else
74 fatal "Not copying nonexistent file: $file"
75 fi
76 }
77
78 # Resolve a relative path inside the chroot to its absolute path on the host
79 # $1: base path of the chroot
80 # $2: relative path to resolve
81 resolve_path()
82 {
83 base_path="$(realpath "$1")"
84 relative_destination="${2#/}"
85 absolute_destination="$base_path"
86
87 while [ -n "$relative_destination" ]; do
88 first_component="${relative_destination%%/*}"
89 relative_destination="${relative_destination#$first_component}"
90 relative_destination="${relative_destination#/}"
91
92 # If the first component is a link
93 if link="$(readlink "$absolute_destination/$first_component")"; then
94 # If the first component is a relative link
95 if [ "${link#/}" = "$link" ]; then
96 relative_destination="$link/$relative_destination"
97 else
98 absolute_destination="$base_path"
99 relative_destination="${link#/}/$relative_destination"
100 fi
101 else
102 absolute_destination="$(realpath "$absolute_destination/$first_component")"
103
104 # If the absolute destination gets out of the chroot
105 if [ "${absolute_destination#$base_path}" = "$absolute_destination" ]; then
106 absolute_destination="$base_path"
107 fi
108 fi
109 done
110
111 echo "$absolute_destination"
112 }
113
114 if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then
115
116 if [ -n "$SETUP_COPYFILES" ]; then
117 if [ -f "$SETUP_COPYFILES" ]; then
118 while read file; do
119 if echo "$file" | egrep -q '^(#|$)' ; then
120 continue
121 fi
122 if echo "$file" | grep -q '^/'; then
123 copy_file "$file" "$(resolve_path "${CHROOT_PATH}" "$file")"
124 else
125 warn "Not copying file with relative path: $file"
126 fi
127 done < "$SETUP_COPYFILES"
128 else
129 fatal "copyfiles file '$SETUP_COPYFILES' does not exist"
130 fi
131 fi
132
133 fi
134