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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#
# Copyright (C) 2013, 2023 Etersoft
# Copyright (C) 2013, 2023 Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
have_patool()
{
# TODO: optimize?
[ -n "$use_7z" ] && return 1
[ -n "$use_patool" ] || is_command patool
}
set_backend()
{
HAVE_7Z='7z'
[ -n "$use_patool" ] && [ -n "$use_7z" ] && fatal "Don't use --use-7z and --use-tool simulateously."
if [ -n "$use_7z" ] || ! have_patool ; then
# TODO: try install patool and p7zip
is_command 7za && HAVE_7Z='7za'
if ! is_command $HAVE_7Z ; then
# 7-zip
is_command 7zz && HAVE_7Z='7zz'
fi
if ! is_command $HAVE_7Z ; then
# reduced p7-zip
is_command 7zr && HAVE_7Z='7zr'
fi
is_command $HAVE_7Z || fatal "Could not find any patool or 7-zip backend. Please install p7zip package and retry."
fi
[ -z "$force" ] || HAVE_7Z="$HAVE_7Z -y"
}
list_subformats()
{
local i
for i in gz bz2 xz bzip2 lz4 lzma zst zstd ; do
echo "tar.$i"
done
for i in gz bz2 xz lz4 ; do
echo "cpio.$i"
done
}
list_extraformats()
{
cat <<EOF
bz2
gz
lz4
Z
tgz
pax
zst
zstd
exe
EOF
}
# it is extension list really
# list all supported formats: tar rar and so on
list_formats()
{
list_subformats
if have_patool ; then
a='' patool formats | grep ".* files:" | sed "s/ .*//g"
else
echo "tar zip 7z tar.7z"
list_subformats
fi
# TODO: do not use patool formats in such case?
# See ArchiveCompressions in patool
list_extraformats
}
# returns true if arg is XXX: from list_formats
is_target_format()
{
[ "${1/:/}" = "$1" ] && return 1
local arc="$(echo "$1" | sed -e 's|:.*||g')"
list_formats | grep -q "^$arc$" && return 0
return 1
}
# TODO: detect by name, without comparing with existing?
# 1.zip -> zip
get_archive_ext()
{
local i
for i in $(list_formats) ; do
[ -z "${1/*.$i/}" ] && echo "$i" && return
done
return 1
}
# 1.zip -> 1
get_archive_name()
{
local ext
ext=$(get_archive_ext "$1")
if [ -n "$ext" ] ; then
echo $(dirname "$1")/$(basename "$1" .$ext)
else
echo "$1"
return 1
fi
}
# FIXME
is_plain_text()
{
file --mime-type "$1" | grep -q "text/" && echo "plain" && return
file "$1" | grep -q "empty" && echo "plain" && return
return 1
}
# TODO: improve
# TODO: detect by extension as default
get_archive_type()
{
local aext
if [ -r "$1" ] ; then
# TODO: rewrite with mime
file "$1" | grep -q "Zip archive data" && echo "zip" && return
file "$1" | grep -q "RAR archive data" && echo "rar" && return
else
warning "Couldn't read $1, skipping mime checking"
fi
if aext=$(get_archive_ext "$1") ; then
echo $aext
return
fi
return 1
}