summaryrefslogtreecommitdiffstats
path: root/mgaapplet_urpm.pm
blob: 2496eb15a8bd54dc5ce741723686cf5dfbc65140 (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
package mgaapplet_urpm;

# taken from urpmi:

use MDK::Common;
use urpm::download;

sub userdir {
    my ($urpm) = @_;
    $< or return;

    my $dir = ($urpm->{urpmi_root} || '') . urpm::userdir_prefix($urpm) . $<;
    mkdir $dir, 0755; # try to create it

    -d $dir && ! -l $dir or $urpm->{fatal}(1, sprintf("fail to create directory %s", $dir));
    -o $dir && -w $dir or $urpm->{fatal}(1, sprintf("invalid owner for directory %s", $dir));

    mkdir "$dir/partial";
    mkdir "$dir/rpms";

    $dir;
}
sub ensure_valid_cachedir {
    my ($urpm) = @_;
    if (my $dir = userdir($urpm)) {
	$urpm->{cachedir} = $dir;
    }
    -w "$urpm->{cachedir}/partial" or $urpm->{fatal}(1, "Can not download packages into %s", "$urpm->{cachedir}/partial");
}
sub valid_cachedir {
    my ($urpm) = @_;
    userdir($urpm) || $urpm->{cachedir};
}

sub get_content {
    my ($urpm, $url) = @_;

    my $download_dir = valid_cachedir($urpm) . '/partial/';
    my $file = $download_dir . basename($url);

    unlink $file; # prevent "partial file" errors
    eval { urpm::download::sync_rel($urpm, $url, quiet => 1, dir => $download_dir) };
    #sync_url($urpm, $url, dir => $download_dir, quiet => 1) or return;

    my @l = cat_($file);
    unlink $file;

    wantarray() ? @l : join('', @l);
}
    
1;