summaryrefslogtreecommitdiff
path: root/etc/setup.d/20copyfiles
blob: bbeb2482e2483d9dc420addad7398c5b24376a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh
# Copyright © 2005-2007  Roger Leigh <rleigh@debian.org>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################

set -e

. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
. "$SETUP_DATA_DIR/common-config"

if [ "$VERBOSE" = "verbose" ]; then
  CP_VERBOSE="--verbose"
fi

# Copy a file if the source and destination differ
# $1: source file
# $2: destination file
copy_file()
{
    if [ -e "$1" ]; then

	COPY="true"

	if [ -e "$2" ]; then

            # Device and inode
	    da=$(/usr/bin/stat --format="%d %i" "$1")
	    # This one can fail since it might not exist yet
	    db=$(/usr/bin/stat --format="%d %i" "$2" 2>/dev/null || :)

	    if [ "$da" = "$db" ]; then
		COPY="false"
	    elif [ -L "$2" ]; then
		# Copy if destination is a symlink
		:
	    elif [ -f "$1" ] && [ -f "$2" ]; then
                # Content
		ca=$(/usr/bin/md5sum "$1" | sed -e 's/\(^[0-9a-f][0-9a-f]*\).*$/\1/')
		cb=$(/usr/bin/md5sum "$2" 2>/dev/null || :)
		cb=$(echo "$cb" | sed -e 's/\(^[0-9a-f][0-9a-f]*\).*$/\1/')
	        # Copy only if file contents differ
		if [ "$ca" = "$cb" ]; then
		    COPY="false"
		fi
	    fi
	fi

	# Copy only if files are different
	if [ "$COPY" = "true" ]; then
	    if [ -f "$1" ]; then
		cp --preserve=all $CP_VERBOSE "$1" "$2"
	    else
	        # Copy non-regular file directly
		cp -a $CP_VERBOSE "$1" "$2"
	    fi
	fi

    else
	fatal "Not copying nonexistent file: $file"
    fi
}

if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then

    if [ -n "$SETUP_COPYFILES" ]; then
	if [ -f "$SETUP_COPYFILES" ]; then
	    while read file; do
		if echo "$file" | egrep -q '^(#|$)' ; then
		    continue
		fi
		if echo "$file" | grep -q '^/'; then
		    copy_file "$file" "${CHROOT_PATH}$file"
		else
		    warn "Not copying file with relative path: $file"
		fi
	    done < "$SETUP_COPYFILES"
	else
	    fatal "copyfiles file '$SETUP_COPYFILES' does not exist"
	fi
    fi

fi