epm-reposave 2.61 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2020, 2021  Etersoft
# Copyright (C) 2020, 2021  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# 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/>.
#

load_helper epm-sh-altlinux

22

23
# under root only
24
SAVELISTDIR=/tmp/eepm-etc-save
25 26
__save_alt_repo_lists()
{
27
	assure_root
28 29 30 31 32 33 34 35 36 37 38
	info "Creating copy of all sources lists to $SAVELISTDIR ..."
	local i
	rm -rf $verbose $SAVELISTDIR 2>/dev/null
	mkdir -p $SAVELISTDIR/apt/ $SAVELISTDIR/apt/sources.list.d/
	for i in /etc/apt/sources.list /etc/apt/sources.list.d/*.list ; do
		[ -s "$i" ] || continue
		local DD="$(echo "$i" | sed -e "s|/etc|$SAVELISTDIR|")"
		cp -af $verbose "$i" "$DD" || fatal "Can't save apt source list files to $SAVELISTDIR"
	done
}

39
# under root only
40 41
__restore_alt_repo_lists()
{
42
	assure_root
43 44 45 46 47 48 49 50
	info "Restoring copy of all sources lists from $SAVELISTDIR ..."
	local i
	[ -d "$SAVELISTDIR/apt" ] || return 0
	mkdir -p $SAVELISTDIR/apt/ $SAVELISTDIR/apt/sources.list.d/
	for i in /etc/apt/sources.list /etc/apt/sources.list.d/*.list ; do
		[ -s "$i" ] || continue
		local DD="$(echo "$i" | sed -e "s|/etc|$SAVELISTDIR|")"
		# restore only if there are differences
51 52 53 54 55
		if diff -q "$DD" "$i" >/dev/null ; then
			rm -f $verbose "$DD"
		else
			mv $verbose "$DD" "$i" || warning "Can't restore $i file"
		fi
56 57 58
	done
}

59 60 61 62 63 64 65
# save and restore repo lists
__on_error_restore_alt_repo_lists()
{
	warning "An error occurred..."
	epm repo restore
}

66 67
try_change_alt_repo()
{
68 69
	epm repo save
	trap __on_error_restore_alt_repo_lists EXIT
70 71 72 73 74 75
}

end_change_alt_repo()
{
	trap - EXIT
}
76 77


78

79 80 81
epm_reposave()
{
case $PMTYPE in
82
	apt-*)
83 84 85 86
		if ! is_root ; then
			sudoepm repo save
			return
		fi
87
		__save_alt_repo_lists
88 89 90 91 92 93 94 95 96 97 98
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

epm_reporestore()
{
case $PMTYPE in
99
	apt-*)
100 101 102 103
		if ! is_root ; then
			sudoepm repo restore
			return
		fi
104
		__restore_alt_repo_lists
105 106 107 108 109 110 111
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}
112 113 114 115 116 117 118 119 120 121 122 123 124

epm_reporeset()
{
case $PMTYPE in
	winget)
		sudocmd winget source reset
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}