You need to sign in or sign up before continuing.
Commit adccb837 authored by Vitaly Lipatov's avatar Vitaly Lipatov

add erc and ercat functions via embedded erc and use it

parent 7d2f0117
......@@ -53,7 +53,6 @@ check_pkg_integrity()
true
;;
*)
assure_exists_erc
docmd erc test "$PKG" && return
;;
esac
......
......@@ -250,17 +250,15 @@ __prepare_source_package()
[ -x "$alpkg" ] || docmd chmod u+x $verbose "$alpkg"
./$alpkg --appimage-extract || fatal
alpkg=$PKGNAME-$VERSION.tar
assure_exists_erc
# make a tar for alien
a= erc a $alpkg squashfs-root
erc a $alpkg squashfs-root
return
fi
__set_version_pkgname $alpkg
if [ -n "$VERSION" ] ; then
# TODO: don't use erc for detect type? then we potentially can skip install it
assure_exists_erc
pkgtype="$(a= erc type $alpkg)"
pkgtype="$(erc type $alpkg)"
local newalpkg
newalpkg=$PKGNAME-$VERSION.$pkgtype
#[ -n "$PKGNAME" ] || PKGNAME=$(basename $alpkg .$pkgtype)
......@@ -273,8 +271,7 @@ __prepare_source_package()
# converts directly unsupported formats
newalpkg=$PKGNAME-$VERSION.tar
#newalpkg=$(basename $alpkg .$pkgtype).tar
assure_exists_erc
a= erc repack $alpkg $newalpkg || fatal
erc repack $alpkg $newalpkg || fatal
fi
if [ "$alpkg" != "$newalpkg" ] ; then
rm -f $verbose $alpkg
......
......@@ -529,6 +529,42 @@ eget()
$EGET "$@"
}
# will replaced within disabled_erc in packaged version
erc()
{
local ERC
# use internal eget only if exists
if [ -s $SHAREDIR/tools_erc ] ; then
$SHAREDIR/tools_erc "$@"
return
fi
fatal "Internal error: missed tools_erc"
# FIXME: we need disable output here, ercat can be used for get output
assure_exists_erc >/dev/null
# run external command, not the function
ERC=$(which erc) || fatal "Missed command erc from installed package erc"
$ERC "$@"
}
# will replaced within disabled_ercat in packaged version
ercat()
{
local ERCAT
# use internal eget only if exists
if [ -s $SHAREDIR/tools_ercat ] ; then
$SHAREDIR/tools_ercat "$@"
return
fi
fatal "Internal error: missed tools_ercat"
# FIXME: we need disable output here, ercat can be used for get output
assure_exists_erc >/dev/null
# run external command, not the function
ERCAT=$(which ercat) || fatal "Missed command ercat from installed package erc"
$ERCAT "$@"
}
estrlist()
{
if [ -s $SHAREDIR/tools_estrlist ] ; then
......
#!/bin/bash
#
# Copyright (C) 2013 Etersoft
# Copyright (C) 2013 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/>.
#
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
a='' patool formats | grep ".* files:" | sed "s/ .*//g"
# 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
}
epm-sh-functions
\ No newline at end of file
#!/bin/bash
#
# Copyright (C) 2013-2015, 2017 Etersoft
# Copyright (C) 2013-2015, 2017 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/>.
#
PROGDIR=$(dirname $0)
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)
# will replaced to /usr/share/erc during install
SHAREDIR=$(dirname $0)
load_helper()
{
local CMD="$SHAREDIR/$1"
[ -r "$CMD" ] || fatal "Have no $CMD helper file"
. $CMD
}
load_helper erc-sh-functions
load_helper erc-sh-archive
check_tty
# 1.zip tar: -> 1.tar
build_target_name()
{
is_target_format $2 && echo $(get_archive_name "$1").${2/:/} && return
echo "$1"
return 1
}
phelp()
{
echo "$Descr
$Usage
Commands:
$(get_help HELPCMD)
Options:
$(get_help HELPOPT)
Examples:
# erc dir - pack dir to dirname.zip
# erc a archive.zip file(s)... - pack files to archive.zip
# erc [x] archive.zip - unpack
# unerc archive.zip - unpack
# erc [repack] archive1.zip... archive2.rar 7z: - repack all to 7z
# erc -f [repack] archive.zip archive.7z - force repack zip to 7z (override target in anyway)
# erc file/dir zip: - pack file to zip
"
}
print_version()
{
echo "Etersoft archive manager version @VERSION@"
echo "Copyright (c) Etersoft 2013-2020"
echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
}
progname="${0##*/}"
Usage="Usage: $progname [options] [<command>] [params]..."
Descr="erc - universal archive manager"
progname="${0##*/}"
force=
target=
verbose=--verbose
case "$1" in
-h|--help|help) # HELPOPT: this help
phelp
exit
;;
-V|--version) # HELPOPT: print version
print_version
exit
;;
-q|--quiet) # HELPOPT: be silent
verbose=
shift
;;
-f|--force) # HELPOPT: override target
force=-f
shift
;;
esac
cmd="$1"
eval lastarg=\${$#}
# if the first arg is some archive, suggest extract
if get_archive_type "$cmd" 2>/dev/null >/dev/null ; then
if is_target_format $lastarg ; then
cmd=repack
else
cmd=extract
fi
# erc dir (pack to zip by default)
elif [ -d "$cmd" ] && [ -z "$2" ] ; then
cmd=pack
target=$(basename "$1").zip
# erc dir zip:
elif test -r "$cmd" && is_target_format "$2" ; then
cmd=pack
elif [ "$progname" = "unerc" ] ; then
cmd=extract
else
shift
fi
# Just printout help if run without args
if [ -z "$cmd" ] ; then
print_version
echo
fatal "Run $ $progname --help for get help"
fi
# TODO: Если программа-архиватор не установлена, предлагать установку с помощью epm
case $cmd in
a|-a|create|pack|add) # HELPCMD: create archive / add file(s) to archive
# TODO: realize archive addition if already exist (and separate adding?)
if [ -z "$target" ] && is_target_format $lastarg ; then
[ $# = 2 ] || fatal "Need two args"
target="$(build_target_name "$1" "$2")"
fi
if [ -n "$target" ] ; then
[ -e "$target" ] && [ -n "$force" ] && docmd rm -f "$target"
docmd patool $verbose create "$target" "$1"
exit
fi
# erc archive.zip file(s)
[ -e "$1" ] && [ -n "$force" ] && docmd rm -f "$1"
docmd patool $verbose create "$@"
;;
e|x|-e|-x|u|-u|extract|unpack) # HELPCMD: extract files from archive
# TODO: move to patool
if [ "$(get_archive_type "$1" 2>/dev/null)" = "exe" ] ; then
docmd 7z x "$1"
exit
fi
docmd patool $verbose extract "$@"
;;
# TODO: implement deletion
# d|delete) # HELPCMD: delete file(s) from archive
# docmd patool delete "$@"
# ;;
l|-l|list) # HELPCMD: list archive contents
# TODO: move to patool
if [ "$(get_archive_type "$1" 2>/dev/null)" = "exe" ] ; then
docmd 7z l "$1"
exit
fi
docmd patool $verbose list "$@"
;;
t|-t|test|check) # HELPCMD: test for archive integrity
# TODO: move to patool
if [ "$(get_archive_type "$1" 2>/dev/null)" = "exe" ] ; then
docmd 7z t "$1"
exit
fi
docmd patool $verbose test "$@"
;;
type) # HELPCMD: print type of archive
get_archive_type "$1" || fatal "Can't recognize $1 as archive"
;;
diff) # HELPCMD: compare two archive
# check 2 arg
docmd patool $verbose diff "$@"
;;
b|-b|bench|benchmark) # HELPCMD: do CPU benchmark
#assure_cmd 7z
# TODO: can be 7za?
docmd 7z b
;;
search|grep) # HELPCMD: search in files from archive
docmd patool $verbose search "$@"
;;
repack|conv) # HELPCMD: convert source archive to target
# TODO: need repack remove source file?
# TODO: check for 2 arg
if ! is_target_format $lastarg ; then
[ $# = 2 ] || fatal "Need two args"
[ "$(realpath "$1")" = "$(realpath "$2")" ] && warning "Output file is the same as input" && exit
[ -e "$2" ] && [ -n "$force" ] && docmd rm -f "$2"
docmd patool $verbose repack "$1" "$2"
exit
fi
# add support for target zip:
for i in "$@" ; do
[ "$i" = "$lastarg" ] && continue
target="$(build_target_name "$i" "$lastarg")"
[ "$(realpath "$1")" = "$(realpath "$target")" ] && warning "Output file is the same as input" && exit
[ -e "$target" ] && [ -n "$force" ] && docmd rm -f "$target"
docmd patool $verbose repack "$i" "$target" || exit
done
;;
formats) # HELPCMD: lists supported archive formats
# TODO: print allowed with current programs separately
if [ -n "$verbose" ] ; then
docmd patool formats "$@"
echo "Also we supports:"
( list_subformats ; list_extraformats ) | sed -e "s|^| |"
else
list_formats
fi
;;
*)
# TODO: If we have archive in parameter, just unpack it
fatal "Unknown command $1"
;;
esac
#!/bin/bash
#
# Copyright (C) 2013, 2020 Etersoft
# Copyright (C) 2013, 2020 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/>.
#
PROGDIR=$(dirname $0)
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)
# will replaced to /usr/share/erc during install
SHAREDIR=$(dirname $0)
load_helper()
{
local CMD="$SHAREDIR/$1"
[ -r "$CMD" ] || fatal "Have no $CMD helper file"
. $CMD
}
load_helper erc-sh-functions
load_helper erc-sh-archive
check_tty
phelp()
{
echo "$Descr
$Usage
Commands:
$(get_help HELPCMD)
Options:
$(get_help HELPOPT)
"
}
print_version()
{
echo "Etersoft uncompressor version @VERSION@"
echo "Copyright (c) Etersoft 2013, 2020"
echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
}
regular_unpack()
{
local file="$1"
local prg="$2"
local pkg="$3"
local opt="$4"
# instead of epm assure
if ! which "$prg" >/dev/null 2>/dev/null ; then
epm assure $prg $pkg || fatal "Try install $pkg package for $prg unpack command."
fi
docmd $prg $opt $file || fatal
}
progname="${0##*/}"
Usage="Usage: $progname [options] file(s)..."
Descr="ercat - universal uncompressor"
cmd=$1
# Just printout help if run without args
if [ -z "$cmd" ] ; then
print_version
echo
fatal "Run $ $progname --help for get help"
fi
case $cmd in
-h|--help|help) # HELPOPT: this help
phelp
exit
;;
-v|--version) # HELPOPT: print version
print_version
exit
;;
esac
# TODO: check ext
# TODO: check file existence
# TODO: check by content
for f in $@ ; do
TYPE=$(get_archive_ext $f) || TYPE=$(is_plain_text $f) || { warning "Skipping unrecognized $f" ; continue ; }
case $TYPE in
gz)
regular_unpack "$f" gunzip gzip -c
;;
bz2)
regular_unpack "$f" bzcat bzip2
;;
xz)
regular_unpack "$f" xzcat xz
;;
Z|compress)
regular_unpack "$f" zcat gzip
;;
lzma)
regular_unpack "$f" lzcat xz
;;
zst|zstd)
regular_unpack "$f" zstdcat zstd
;;
lz4)
regular_unpack "$f" lz4cat lz4
;;
plain)
docmd cat "$f" || fatal
;;
*)
fatal "Unsupported compression format $TYPE"
;;
esac
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment