epm-check_updated_repo 4.21 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2014, 2015, 2023  Etersoft
# Copyright (C) 2014, 2015, 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 23 24 25 26 27 28 29 30 31 32
__epm_apt_set_lists_pkg()
{
    # apt-dpkg
    pkg="Packages"

    LISTS='/var/lib/apt/lists'
    if [ "$BASEDISTRNAME" = "alt" ] ; then
        pkg="pkglist"
        # see update-kernel: Use Dir::State::lists for apt update freshness check (ALT bug 46987)
        eval "$(apt-config shell LISTS Dir::State::lists/f)"
    fi
}

33 34 35 36
# initially copied from update-kernel
# print number of days and error status if number of days more than 0
__epm_check_apt_db_days()
{
37
    local pkg
38
    local pkglists
39 40
    __epm_apt_set_lists_pkg
    pkglists=$(find $LISTS -name "*_$pkg*" -ctime +1 2>/dev/null)
41 42
    if [ -z "$pkglists" ] ; then
        # note: duplicate __is_repo_info_downloaded
43
        pkglists=$(find $LISTS -name "*_$pkg*" 2>/dev/null)
44
        [ -n "$pkglists" ] && return
45
        message "never downloaded"
46 47
        return 1
    fi
48 49 50 51 52

    local i t
    local ts=0
    # set ts to newest file ctime
    # shellcheck disable=SC2044
53
    for i in $(find $LISTS -name "*_$pkg*" 2>/dev/null); do
54 55 56 57 58 59 60 61 62
        t=$(stat -c%Z "$i")
        [ "$t" -gt "$ts" ] && ts=$t
    done

    if [ "$ts" -gt 0 ]; then
        # shellcheck disable=SC2017
        local now=$(date +%s)
        local days="$(( (now - ts) / (60 * 60 * 24) ))"
        [ "$days" = "0" ] && return 0
63 64
        [ "$days" = "1" ] && message "1 day old" && return 1
        message '$days days old'
65 66
    else
        # no any pkglist
67
        message "stalled"
68 69 70
    fi
    return 1
}
71 72 73

__epm_touch_apt_pkg()
{
74 75
    local pkg
    __epm_apt_set_lists_pkg
76
    # ordinal package file have date of latest upstream change, not latest update, so update fake file
77
    sudorun touch "$LISTS/eepm-fake_$pkg"
78 79 80 81 82 83 84 85 86 87 88
}

__epm_touch_pkg()
{
    case $PMTYPE in
        apt-*)
            __epm_touch_apt_pkg
            ;;
    esac
}

89
# check if we need initial update
90
__is_repo_info_downloaded()
91 92 93
{
    case $PMTYPE in
        apt-*)
94 95
            local pkg
            __epm_apt_set_lists_pkg
96
            local pkglists
97
            pkglists=$(find $LIST -name "*_$pkg*" 2>/dev/null)
98
            [ -n "$pkglists" ] || return 1
99 100 101 102 103 104 105 106 107 108
            ;;
        *)
            ;;
    esac
    return 0
}

__is_repo_info_uptodate()
{
    case $PMTYPE in
109
        apt-*)
110
            __epm_check_apt_db_days >/dev/null
111 112 113 114 115 116 117 118 119
            ;;
        *)
            ;;
    esac
    return 0
}

update_repo_if_needed()
{
120
    local days
121 122 123 124 125 126 127 128 129 130

    # for apt only
    case $PMTYPE in
        apt-*)
            ;;
        *)
            return
            ;;
    esac

131
    days="$(__epm_check_apt_db_days)" && return
132
    warning 'APT database is $days, please run epm update!'
133 134 135

    # TODO: enable __is_repo_info_downloaded

136
    return
137
    # check if we need skip update checking
138 139 140 141 142
    #if [ "$1" = "soft" ] && ! set_sudo nofail ; then
    #    # if sudo requires a password, skip autoupdate
    #    info "can't use sudo, so skip repo status checking"
    #    return 1
    #fi
143

Vitaly Lipatov's avatar
Vitaly Lipatov committed
144
    cd / || fatal
145
    if ! __is_repo_info_downloaded || ! __is_repo_info_uptodate ; then
146
        load_helper epm-update
147
        # FIXME: cleans!!!
148
        epm_update
149
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
150
    cd - >/dev/null || fatal
151 152

}
153 154 155 156

# save manually installed packages
save_installed_packages()
{
157 158
    [ -d $epm_vardir ] || return 0
    estrlist list "$@" | sudorun tee $epm_vardir/installed-via-epm >/dev/null
159 160 161 162
}

check_manually_installed()
{
163 164
    [ -r $epm_vardir/installed-via-epm ] || return 1
    grep -q -- "^$1\$" $epm_vardir/installed-via-epm
165 166 167 168
}

skip_manually_installed()
{
169 170 171 172 173
    local i
    for i in "$@" ; do
        check_manually_installed "$i" && continue
        echo "$i"
    done
174
}