FusionInventory-Agent

 view release on metacpan or  search on metacpan

lib/FusionInventory/Agent/Task/Deploy/File.pm  view on Meta::CPAN

        # try to download from peers
        foreach my $peer (@peers) {
            my $success = $self->_downloadPeer($peer, $sha512, $path);
            if ($success) {
                $lastPeer = $peer;
                next PART;
            }
            # Update filepath so retention is kept in the future on long search
            if ( time - $nextPathUpdate > 0 ) {
                $path = $self->normalizedPartFilePath($sha512);
                $nextPathUpdate = _getNextPathUpdateTime();
            }
        }

        # try to download from mirrors
        foreach my $mirror (@{$self->{mirrors}}) {
            my $success = $self->_download($mirror, $sha512, $path);
            next PART if $success;
            # Update filepath so retention is kept in the future on long search
            if ( time - $nextPathUpdate > 0 ) {
                $path = $self->normalizedPartFilePath($sha512);
                $nextPathUpdate = _getNextPathUpdateTime();
            }
        }
    }
}

sub _getNextPathUpdateTime {
    my $time = time;
    return $time + 60 - $time % 60;
}

sub _downloadPeer {
    my ($self, $peer, $sha512, $path) = @_;

    my $source = 'http://'.$peer.':62354/deploy/getFile/';

    return $self->_download($source, $sha512, $path, $peer);
}

sub _download {
    my ($self, $source, $sha512, $path, $peer) = @_;

    unless ($source =~ m|^https?://|i) {
        $self->{logger}->error("Source or mirror is not a valid URL: $source");
        return;
    }

    return unless $sha512 =~ /^(.)(.)/;
    my $sha512dir = $1.'/'.$1.$2.'/';

    # Check source url ends with a slash
    $source .= '/' unless $source =~ m|/$|;

    my $url = $source.$sha512dir.$sha512;

    $self->{logger}->debug("File part URL: $url");

    my $request = HTTP::Request->new(GET => $url);
    # We want to try direct download without proxy if peer if defined and then
    # we also prefer to use really short timeout to disqualify busy peers and
    # also avoid to block for not responding peers while using P2P
    my $timeout = $peer ? 1 : 180 ;
    my $response = $self->{client}->request($request, $path, $peer, $timeout);

    if ($response->code != 200) {
        if ($response->code != 404 || $response->status_line() =~ /Nothing found/) {
            $self->{logger}->debug2("Remote peer $peer is useless, we should forget it out for a while");
            $self->{p2pnet}->forgetPeer($peer) if $self->{p2pnet};
        }
        return;
    }
    return if ! -f $path;

    if ($self->_getSha512ByFile($path) ne $sha512) {
        $self->{logger}->debug("sha512 failure: $sha512");
        unlink($path);
        return;
    }

    return 1;
}

sub filePartsExists {
    my ($self) = @_;

    foreach my $sha512 (@{$self->{multiparts}}) {

        my $filePath  = $self->getPartFilePath($sha512);
        return 0 unless -f $filePath;

    }
    return 1;
}

sub _getSha512ByFile {
    my ($self, $filePath) = @_;

    my $sha = Digest::SHA->new('512');

    my $sha512;
    eval {
        $sha->addfile($filePath, 'b');
        $sha512 = $sha->hexdigest;
    };
    $self->{logger}->debug("SHA512 failure: $@") if $@;

    return $sha512;
}

sub validateFileByPath {
    my ($self, $filePath) = @_;


    if (-f $filePath) {
        if ($self->_getSha512ByFile($filePath) eq $self->{sha512}) {
            return 1;
        }
    }

    return 0;



( run in 2.322 seconds using v1.01-cache-2.11-cpan-98e64b0badf )