summaryrefslogtreecommitdiff
path: root/etc/setup.d/05file
blob: 13cca7c203ca1d85de41352aff3cbd8087fe1ecc (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/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"

# Check file type
check_filetype()
{
    if echo "$CHROOT_FILE" | grep -q '\.tar$'; then
	filetype="tar"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.gz|\.tgz)$'; then
	filetype="tgz"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.bz2|\.tbz)$'; then
	filetype="tbz"
    else
	fatal "Unsupported filetype for $CHROOT_FILE"
    fi
}

# Unpack archive
unpack_file()
{
    if [ ! -f "$CHROOT_FILE" ]; then
	fatal "File '$CHROOT_FILE' does not exist"
    fi

    if [ "$filetype" = "tar" ]; then
	tar $TAR_VERBOSE -xf "$CHROOT_FILE"
    elif [ "$filetype" = "tgz" ]; then
	tar $TAR_VERBOSE -xzf "$CHROOT_FILE"
    elif [ "$filetype" = "tbz" ]; then
	tar $TAR_VERBOSE -xjf "$CHROOT_FILE"
    else
	fatal "Unsupported filetype for $CHROOT_FILE"
    fi
}

# Repack archive
repack_file()
{
    NEWFILE=$(mktemp "${CHROOT_FILE}.XXXXXX")

    trap "if [ -f \"$NEWFILE\" ]; then rm -f \"$NEWFILE\"; fi" 0

    if [ "$filetype" = "tar" ]; then
	tar $TAR_VERBOSE -cf "$NEWFILE" .
    elif [ "$filetype" = "tgz" ]; then
	tar $TAR_VERBOSE -czf "$NEWFILE" .
    elif [ "$filetype" = "tbz" ]; then
	tar $TAR_VERBOSE -cjf "$NEWFILE" .
    else
	fatal "Unsupported filetype for $CHROOT_FILE"
    fi

    if [ -f "$CHROOT_FILE" ]; then
	info "Setting ownership and permissions from old archive"
	chown --reference="$CHROOT_FILE" "$NEWFILE"
	chmod --reference="$CHROOT_FILE" "$NEWFILE"
    else
	warn "Old archive no longer exists"
	warn "Setting ownership and permissions to root:root 0600"
	chown root:root "$NEWFILE"
	chmod 0600 "$NEWFILE"
    fi
    mv "$NEWFILE" "$CHROOT_FILE"

    trap "" 0
}

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

if [ "$CHROOT_TYPE" = "file" ]; then

    check_filetype

    UNPACK_LOCATION="${CHROOT_FILE_UNPACK_DIR}/${SESSION_ID}"

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

	info "File unpack directory: $UNPACK_LOCATION"

        if [ ! -d "$UNPACK_LOCATION" ]; then
	    mkdir -p "$UNPACK_LOCATION"
	    info "Created file unpack directory: $UNPACK_LOCATION"
        fi
	cd "$UNPACK_LOCATION"
	info "Changed CWD to $UNPACK_LOCATION"

	unpack_file

    elif [ "$STAGE" = "setup-stop" ]; then

	if [ "$STATUS" = "ok" ] && [ "$CHROOT_FILE_REPACK" = "true" ]; then
	    if [ -d "$UNPACK_LOCATION" ]; then
		info "Repacking chroot archive file $CHROOT_FILE from $UNPACK_LOCATION"
		cd "$UNPACK_LOCATION" && repack_file
	    else
		warn "Not repacking chroot archive file: $UNPACK_LOCATION does not exist (it may have been removed previously)"
	    fi
	fi

	if [ "$CHROOT_SESSION_PURGE" = "true" ]; then
	    info "Purging $UNPACK_LOCATION"
	    if [ -d "$UNPACK_LOCATION" ]; then
		rm -rf "$UNPACK_LOCATION"
	    fi
	fi

    fi

fi