epm-reposave 2.88 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 28 29 30 31 32 33 34 35 36
    assure_root
    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
37 38
}

39
# under root only
40 41
__restore_alt_repo_lists()
{
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    assure_root
    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
        if diff -q "$DD" "$i" >/dev/null ; then
            rm -f $verbose "$DD"
        else
            mv $verbose "$DD" "$i" || warning "Can't restore $i file"
        fi
    done
57 58
}

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

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

end_change_alt_repo()
{
74
    trap - EXIT
75
}
76 77


78

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

}

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

}
112 113 114 115

epm_reporeset()
{
case $PMTYPE in
116 117 118 119 120 121
    winget)
        sudocmd winget source reset
        ;;
    *)
        fatal "Have no suitable command for $PMTYPE"
        ;;
122 123 124
esac

}