| 1 | package Build::PPK::Deptool::HTTP; | ||
| 2 | |||
| 3 | # Copyright (c) 2018, cPanel, L.L.C. | ||
| 4 | # All rights reserved. | ||
| 5 | # http://cpanel.net/ | ||
| 6 | # | ||
| 7 | # This is free software; you can redistribute it and/or modify it under the same | ||
| 8 | # terms as Perl itself. See the LICENSE file for further details. | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use POSIX (); | ||
| 14 | use Fcntl (); | ||
| 15 | |||
| 16 | use Cwd (); | ||
| 17 | use File::Basename (); | ||
| 18 | |||
| 19 | use Build::PPK::Pipeline (); | ||
| 20 | |||
| 21 | use Carp (); | ||
| 22 | |||
| 23 | sub fetch_dist { | ||
| 24 | my ( $class, %args ) = @_; | ||
| 25 | |||
| 26 | die('No URL specified') unless $args{'url'}; | ||
| 27 | die('No output path specified') unless $args{'path'}; | ||
| 28 | |||
| 29 | return if -e $args{'path'}; | ||
| 30 | |||
| 31 | my $tmpfile; | ||
| 32 | |||
| 33 | my @filters = ( | ||
| 34 | sub { | ||
| 35 | exec qw(wget -O -), $args{'url'} or Carp::confess("Unable to spawn wget: $!"); | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | if ( $args{'path'} =~ /\.tar/ ) { | ||
| 40 | $tmpfile = "$args{'path'}.tmp"; | ||
| 41 | |||
| 42 | push @filters, sub { | ||
| 43 | open( my $fh, '>', $tmpfile ) or Carp::confess("Cannot open $tmpfile for writing: $!"); | ||
| 44 | |||
| 45 | while ( my $len = read( STDIN, my $buf, 4096 ) ) { | ||
| 46 | print $fh $buf; | ||
| 47 | } | ||
| 48 | |||
| 49 | close $fh; | ||
| 50 | |||
| 51 | exit 0; | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | unless ( -d $args{'path'} ) { | ||
| 56 | mkdir( $args{'path'} ) or Carp::confess("Unable to create distribution directory $args{'path'} : $!"); | ||
| 57 | } | ||
| 58 | |||
| 59 | push @filters, sub { | ||
| 60 | chdir( $args{'path'} ) or Carp::confess("Unable to chdir() to $args{'path'}: $!"); | ||
| 61 | exec qw(tar pzxf -) or Carp::confess("Unable to spawn tar: $!"); | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | |||
| 65 | my $pipeline = Build::PPK::Pipeline->open(@filters); | ||
| 66 | |||
| 67 | close $pipeline->{'in'}; | ||
| 68 | |||
| 69 | sysopen( my $null_fh, '/dev/null', &Fcntl::O_RDONLY ) or Carp::confess("Unable to open /dev/null: $!"); | ||
| 70 | |||
| 71 | POSIX::dup2( fileno($null_fh), fileno( $pipeline->{'out'} ) ); | ||
| 72 | |||
| 73 | my $stderr; | ||
| 74 | |||
| 75 | while ( my $len = sysread( $pipeline->{'err'}, my $buf, 512 ) ) { | ||
| 76 | $stderr .= $buf; | ||
| 77 | } | ||
| 78 | |||
| 79 | my $statuses = $pipeline->close; | ||
| 80 | my $errors; | ||
| 81 | |||
| 82 | foreach my $pid ( keys %{$statuses} ) { | ||
| 83 | my $status = $statuses->{$pid}; | ||
| 84 | |||
| 85 | next unless $status != 0; | ||
| 86 | |||
| 87 | if ($stderr) { | ||
| 88 | chomp $stderr; | ||
| 89 | $errors = "Process $pid died with nonzero exit status $status: $stderr"; | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | $errors = "Process $pid died with nonzero exit status $status"; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | if ($errors) { | ||
| 97 | unlink($tmpfile) if $tmpfile; | ||
| 98 | die($errors); | ||
| 99 | } | ||
| 100 | |||
| 101 | if ($tmpfile) { | ||
| 102 | unless ( rename( $tmpfile => $args{'path'} ) ) { | ||
| 103 | die("Unable to rename temporary file $tmpfile to $args{'path'}: $!"); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; |