Debian Codegraph @

bin/ftar

raw symbols SCIP
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # ftar -- extract tar files using FAI classes
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # Copyright (C) 2001-2021 Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
21 #
22 # A copy of the GNU General Public License is available as
23 # '/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
24 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
25 # can also obtain it by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #*********************************************************************
28
29 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30 die() {
31
32 local e=$1 # first parameter is the exit code
33 shift
34
35 echo "ftar: $*" >&2 # print error message
36 exit $e
37 }
38 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39 extract() {
40
41 local file=$1
42 local catname=$2
43
44 if [ $deletefiles -eq 1 ] ; then
45 cd $target/$dir && rm -f -- .* * 2>/dev/null
46 deletefiles=0
47 fi
48
49 if [ $removedir -eq 1 ]; then
50 cd $target/$dir || die 4 "ERROR: cd to $target/$dir failed. Aborting."
51 [ $PWD = "/" ] && die 3 "WARNING: Will not do recursive removal of directory /"
52 rm -rf -- .* * 2>/dev/null
53 removedir=0
54 fi
55
56 echo "ftar: extracting $file to $target/$dir" | tr -s '/'
57 $catname $file | tar $xattrs --numeric-owner -C $target/$dir $vflag -xf -
58 tardone=1
59 # if option -1 is set, only one class will be used
60 [ $single -eq 1 ] && exit 0
61 }
62 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63 usage() {
64
65 cat <<EOF
66 ftar, extract tar files using classes.
67
68 Copyright (C) 2001-2021 by Thomas Lange
69
70 Usage: ftar [OPTION] ... SOURCE
71
72 -1 Use only first tar file matching class name.
73 -c class[class] Define classes (space separated).
74 -d Delete all files in target before extracting.
75 -D Create debug output.
76 -h Show summary of options.
77 -r Recursively remove files in target before extracting.
78 -s source_dir Look for source files relative to source_dir.
79 -t target_dir Extract files relativ to target_dir.
80 -v Be verbose. Not yet used.
81
82 EOF
83 exit 0
84 }
85 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86
87 source=$FAI/files
88 target=$FAI_ROOT
89 deletefiles=0
90 removedir=0
91 tardone=0
92 single=0
93 ignore=0
94
95 if [ -n "$classes" ]; then
96 [ -f $LOGDIR/FAI_CLASSES ] && classes=$(< $LOGDIR/FAI_CLASSES)
97 fi
98
99 while getopts 1hDdrvs:t:c:i opt ; do
100 case "$opt" in
101 d) deletefiles=1 ;;
102 D) debug=1 ;;
103 i) ignore=1 ;;
104 r) removedir=1 ;;
105 # v) verbose=1 ;;
106 1) single=1 ;;
107 s) source=$OPTARG ;;
108 t) target=$OPTARG ;;
109 c) classes=$OPTARG ;;
110 h) usage ;;
111 esac
112 done
113 shift $((OPTIND - 1))
114
115 [ "$1" ] || usage
116
117 # detect target fstype
118 targetfstype=$(awk -vt="$target" '{if($2==t){print $3}}' /proc/mounts)
119 if [ "$targetfstype" != "nfs" ]; then
120 xattrs="--xattrs --xattrs-include=*.* --selinux --acl"
121 else
122 xattrs=
123 fi
124
125 # last class has highest priority
126 # reverse order of classes
127 for class in $classes; do
128 revclasses="$class $revclasses"
129 done
130
131 [ "$debug" ] && vflag="-v"
132 [ "$debug" ] && echo "ftar: classes : $revclasses"
133 [ -z "$source" ] && die 1 "Source directory undefined."
134 [ -z "$target" ] && die 1 "Target directory undefined."
135
136 # currently only one directory is used
137 dir=$1
138 fpath=$source/$dir
139 [ -d $fpath ] || die 1 "No directory $fpath"
140
141 for c in $revclasses ; do
142 # what if a directory exists which is equal to the hostname or a classname?
143 [ -f $fpath/$c.tgz ] && extract $fpath/$c.tgz zcat
144 [ -f $fpath/$c.tar ] && extract $fpath/$c.tar cat
145 [ -f $fpath/$c.tar.gz ] && extract $fpath/$c.tar.gz zcat
146 [ -f $fpath/$c.tar.bz2 ] && extract $fpath/$c.tar.bz2 bzcat
147 [ -f $fpath/$c.tar.xz ] && extract $fpath/$c.tar.xz xzcat
148 [ -f $fpath/$c.txz ] && extract $fpath/$c.txz xzcat
149 [ -f $fpath/$c.tar.zst ] && extract $fpath/$c.tar.zst zstdcat
150 [ -f $fpath/$c.tzst ] && extract $fpath/$c.tzst zstdcat
151 done
152
153 if [ $tardone -eq 0 ]; then
154 if [ $ignore -eq 1 ]; then
155 echo "ftar: No matching class found in $fpath"
156 exit 0
157 else
158 die 1 "No matching class found in $fpath"
159 fi
160 fi
161
162 exit 0
163