summaryrefslogtreecommitdiffstats
path: root/mgaapplet-update-checker
blob: 41ffb739c461861c4d56c908e3fb0a92fad587d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/perl
################################################################################
# Mandriva Online                                                              # 
#                                                                              #
# Copyright (C) 2003-2010 Mandriva                                             #
#                                                                              #
# Daouda Lo                                                                    #
# Thierry Vignaud <thierry.vignaud at gmail dot com>                           #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# 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 General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   #
################################################################################
use lib qw(/usr/lib/libDrakX /usr/lib/libDrakX/drakfirsttime);
use standalone; # for explanations
use MDK::Common;
use Rpmdrake::open_db;
use mgaapplet;
use urpm;
use urpm::lock;
use urpm::select;
use urpm::media;

# be nice with other processes:
setpriority(0, $$, 7);  # 0 is PRIO_PROCESS

my $root = $ARGV[0];

my $will_not_update_media;
# so that get_inactive_backport_media() doesn't vivify $urpm->{media}:
my $urpm = Rpmdrake::open_db::fast_open_urpmi_db();
{
    local $urpm->{fatal} = sub {
	print "Fatal: @_\n";
	$will_not_update_media = 1;
    };
    local $urpm->{error} = $urpm->{fatal};

    urpm::lock::urpmi_db($urpm, 'exclusive', 1);
}
checker_exit('locked') if $will_not_update_media;

my $is_it_a_devel_distro = is_it_a_devel_distro();

my $media = $is_it_a_devel_distro ? '-a' : '--update';
if (!run_program::run('urpmi.update', $media, if_($root, "--urpmi-root=$root"))) {
    checker_exit('error_updating') if $will_not_update_media;
}

update_backport_media($urpm);

# this eats 52Mb of RAM on 64bit:
# (hence we do it in the forked helper so that the applet doesn't eat too much RAM)
urpm::media::configure($urpm, if_(!$is_it_a_devel_distro, update => 1));

my @update_medias = get_update_medias($urpm);

if (!@update_medias) {
    checker_exit('no_update_medium');
} elsif (!any { ! $_->{ignore} } @update_medias) {
    checker_exit('no_enabled_medium');
}

if (my $_db = urpm::db_open_or_die($urpm)) {
    my $requested = {};
    my $state = {};
    my $need_restart = urpm::select::resolve_dependencies(
	$urpm, $state, $requested,
	callback_choices => sub { 0 },
	priority_upgrade => $urpm->{options}{'priority-upgrade'},
	auto_select => 1,
	);
    my @requested_strict = map { scalar $_->fullname } @{$urpm->{depslist}}[keys %{$state->{selected}}];

    if ($need_restart || @requested_strict) {
	# FIXME: log first found pkgs?
	warn ">> need_restart=$need_restart, updates=" . join(', ', @requested_strict) . "\n";
	checker_exit('updates');
    } else {
	checker_exit('uptodate');
    }
} else {
    checker_exit('db_not_open');
}
checker_exit('updates');

sub checker_exit {
    my ($state) = @_;
    POSIX::_exit($comm_codes{$state}{code});
}

sub update_backport_media {
    my ($urpm) = @_;
    # update inactive backport media:
    my @inactive_backport_media = Rpmdrake::open_db::get_inactive_backport_media($urpm);
    return if !@inactive_backport_media;
    log::explanations("updating inactive backport media " . join(', ', @inactive_backport_media));
    foreach (@inactive_backport_media) {
        run_program::run('urpmi.update', if_($root, "--urpmi-root=$root"), $_);
    }
}