erc-sh-archive 3.49 KB
Newer Older
1 2
#!/bin/bash
#
3 4
# Copyright (C) 2013, 2023  Etersoft
# Copyright (C) 2013, 2023  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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/>.
#

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

27 28 29
set_backend()
{
	HAVE_7Z='7z'
30 31 32 33 34

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

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

	if [ -n "$use_7z" ] || ! have_patool ; then
		# TODO: try install patool and p7zip
		is_command 7za && HAVE_7Z='7za'
39 40 41 42 43 44 45 46
		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
47
		is_command $HAVE_7Z || fatal "Could not find any patool or 7-zip backend. Please install p7zip package and retry."
48 49 50
	fi
	[ -z "$force" ] || HAVE_7Z="$HAVE_7Z -y"
}
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
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
77 78
AppImage
appimage
79 80 81 82 83 84 85 86
EOF
}

# it is extension list really
# list all supported formats: tar rar and so on
list_formats()
{
	list_subformats
87 88 89 90 91 92
	if have_patool ; then
		a='' patool formats | grep ".* files:" | sed "s/ .*//g"
	else
		echo "tar zip 7z tar.7z"
		list_subformats
	fi
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
	# 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
149 150 151
	# used for unexists files too
	#else
	#	warning "Couldn't read $1, skipping mime checking"
152 153 154 155 156 157 158 159
	fi

	if aext=$(get_archive_ext "$1") ; then
		echo $aext
		return
	fi
	return 1
}
160 161 162 163 164 165 166 167 168 169 170

extract_command() {
    local cmd="$1"
    local archive="$2"

    if is_command tar; then
        docmd $cmd "$archive"
    else
        docmd $HAVE_7Z x -so "$archive" | docmd $HAVE_7Z x -y -si -ttar
    fi
}