NBash

Форк
0
157 строк · 3.3 Кб
1
#!/bin/bash
2
#
3
# Copyright (C) 2013, 2023  Etersoft
4
# Copyright (C) 2013, 2023  Vitaly Lipatov <lav@etersoft.ru>
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19

20
have_patool()
21
{
22
	# TODO: optimize?
23
	[ -n "$use_7z" ] && return 1
24
	[ -n "$use_patool" ] || is_command patool
25
}
26

27
set_backend()
28
{
29
	HAVE_7Z='7z'
30

31
	[ "$ERC_BACKEND" = "patool" ] && use_patool=1
32
	[ "$ERC_BACKEND" = "7z" ] && use_7z=1
33

34
	[ -n "$use_patool" ] && [ -n "$use_7z" ] && fatal "Don't use --use-7z and --use-patool simulateously."
35

36
	if [ -n "$use_7z" ] || ! have_patool ; then
37
		# TODO: try install patool and p7zip
38
		is_command 7za && HAVE_7Z='7za'
39
		if ! is_command $HAVE_7Z ; then
40
			# 7-zip
41
			is_command 7zz && HAVE_7Z='7zz'
42
		fi
43
		if ! is_command $HAVE_7Z ; then
44
			# reduced p7-zip
45
			is_command 7zr && HAVE_7Z='7zr'
46
		fi
47
		is_command $HAVE_7Z || fatal "Could not find any patool or 7-zip backend. Please install p7zip package and retry."
48
	fi
49
	[ -z "$force" ] || HAVE_7Z="$HAVE_7Z -y"
50
}
51

52

53
list_subformats()
54
{
55
	local i
56
	for i in gz bz2 xz bzip2 lz4 lzma zst zstd ; do
57
		echo "tar.$i"
58
	done
59

60
	for i in gz bz2 xz lz4 ; do
61
		echo "cpio.$i"
62
	done
63
}
64

65
list_extraformats()
66
{
67
	cat <<EOF
68
bz2
69
gz
70
lz4
71
Z
72
tgz
73
pax
74
zst
75
zstd
76
exe
77
EOF
78
}
79

80
# it is extension list really
81
# list all supported formats: tar rar and so on
82
list_formats()
83
{
84
	list_subformats
85
	if have_patool ; then
86
		a='' patool formats | grep ".* files:" | sed "s/ .*//g"
87
	else
88
		echo "tar zip 7z tar.7z"
89
		list_subformats
90
	fi
91
	# TODO: do not use patool formats in such case?
92
	# See ArchiveCompressions in patool
93
	list_extraformats
94
}
95

96
# returns true if arg is XXX: from list_formats
97
is_target_format()
98
{
99
	[ "${1/:/}" = "$1" ] && return 1
100
	local arc="$(echo "$1" | sed -e 's|:.*||g')"
101
	list_formats | grep -q "^$arc$" && return 0
102
	return 1
103
}
104

105
# TODO: detect by name, without comparing with existing?
106
# 1.zip -> zip
107
get_archive_ext()
108
{
109
	local i
110
	for i in $(list_formats) ; do
111
		[ -z "${1/*.$i/}" ] && echo "$i" && return
112
	done
113
	return 1
114
}
115

116
# 1.zip -> 1
117
get_archive_name()
118
{
119
	local ext
120
	ext=$(get_archive_ext "$1")
121
	if [ -n "$ext" ] ; then
122
		echo $(dirname "$1")/$(basename "$1" .$ext)
123
	else
124
		echo "$1"
125
		return 1
126
	fi
127
}
128

129
# FIXME
130
is_plain_text()
131
{
132
	file --mime-type "$1" | grep -q "text/" && echo "plain" && return
133
	file "$1" | grep -q "empty" && echo "plain" && return
134
	return 1
135
}
136

137

138
# TODO: improve
139
# TODO: detect by extension as default
140
get_archive_type()
141
{
142
	local aext
143
	if [ -r "$1" ] ; then
144
		# TODO: rewrite with mime
145
		file "$1" | grep -q "Zip archive data" && echo "zip" && return
146
		file "$1" | grep -q "RAR archive data" && echo "rar" && return
147
	# used for unexists files too
148
	#else
149
	#	warning "Couldn't read $1, skipping mime checking"
150
	fi
151

152
	if aext=$(get_archive_ext "$1") ; then
153
		echo $aext
154
		return
155
	fi
156
	return 1
157
}
158

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.