diff options
author | Perl Tidy <perltidy@bugzilla.org> | 2018-12-05 21:38:52 +0100 |
---|---|---|
committer | Dylan William Hardison <dylan@hardison.net> | 2018-12-05 23:49:08 +0100 |
commit | 8ec8da0491ad89604700b3e29a227966f6d84ba1 (patch) | |
tree | 9d270f173330ca19700e0ba9f2ee931300646de1 /Bugzilla/Attachment | |
parent | a7bb5a65b71644d9efce5fed783ed545b9336548 (diff) | |
download | bugzilla-8ec8da0491ad89604700b3e29a227966f6d84ba1.tar.gz bugzilla-8ec8da0491ad89604700b3e29a227966f6d84ba1.tar.xz |
no bug - reformat all the code using the new perltidy rules
Diffstat (limited to 'Bugzilla/Attachment')
-rw-r--r-- | Bugzilla/Attachment/Archive.pm | 154 | ||||
-rw-r--r-- | Bugzilla/Attachment/Database.pm | 52 | ||||
-rw-r--r-- | Bugzilla/Attachment/FileSystem.pm | 52 | ||||
-rw-r--r-- | Bugzilla/Attachment/PatchReader.pm | 536 | ||||
-rw-r--r-- | Bugzilla/Attachment/S3.pm | 56 |
5 files changed, 435 insertions, 415 deletions
diff --git a/Bugzilla/Attachment/Archive.pm b/Bugzilla/Attachment/Archive.pm index ccedf1da4..1b22fc50e 100644 --- a/Bugzilla/Attachment/Archive.pm +++ b/Bugzilla/Attachment/Archive.pm @@ -16,108 +16,110 @@ use IO::File; use constant HEADER_SIZE => 45; use constant HEADER_FORMAT => 'ANNNH64'; -has 'file' => ( is => 'ro', required => 1 ); -has 'input_fh' => ( is => 'lazy', predicate => 'has_input_fh' ); -has 'output_fh' => ( is => 'lazy', predicate => 'has_output_fh' ); -has 'checksum' => ( is => 'lazy', clearer => 'reset_checksum' ); +has 'file' => (is => 'ro', required => 1); +has 'input_fh' => (is => 'lazy', predicate => 'has_input_fh'); +has 'output_fh' => (is => 'lazy', predicate => 'has_output_fh'); +has 'checksum' => (is => 'lazy', clearer => 'reset_checksum'); sub read_member { - my ($self) = @_; - my $header = $self->_read_header(); - my ($type, $bug_id, $attach_id, $data_len, $hash) = unpack HEADER_FORMAT, $header; - if ( $type eq 'D' ) { - $self->checksum->add($header); - my $data = $self->_read_data( $data_len, $hash ); - return { - bug_id => $bug_id, - attach_id => $attach_id, - data_len => $data_len, - hash => $hash, - data => $data, - }; - } - elsif ($type eq 'C') { - die "bad overall checksum\n" unless $hash eq $self->checksum->hexdigest; - $self->reset_checksum; - return undef; - } - else { - die "unknown member type: $type\n"; - } + my ($self) = @_; + my $header = $self->_read_header(); + my ($type, $bug_id, $attach_id, $data_len, $hash) = unpack HEADER_FORMAT, + $header; + if ($type eq 'D') { + $self->checksum->add($header); + my $data = $self->_read_data($data_len, $hash); + return { + bug_id => $bug_id, + attach_id => $attach_id, + data_len => $data_len, + hash => $hash, + data => $data, + }; + } + elsif ($type eq 'C') { + die "bad overall checksum\n" unless $hash eq $self->checksum->hexdigest; + $self->reset_checksum; + return undef; + } + else { + die "unknown member type: $type\n"; + } } sub write_attachment { - my ( $self, $attachment ) = @_; - my $data = $attachment->data; - my $bug_id = $attachment->bug_id; - my $attach_id = $attachment->id; - - if (defined $data && length($data) == $attachment->datasize) { - my $header = pack HEADER_FORMAT, 'D', $bug_id, $attach_id, length($data), sha256_hex($data); - $self->checksum->add($header); - $self->output_fh->print($header, $data); - } + my ($self, $attachment) = @_; + my $data = $attachment->data; + my $bug_id = $attachment->bug_id; + my $attach_id = $attachment->id; + + if (defined $data && length($data) == $attachment->datasize) { + my $header = pack HEADER_FORMAT, 'D', $bug_id, $attach_id, length($data), + sha256_hex($data); + $self->checksum->add($header); + $self->output_fh->print($header, $data); + } } sub write_checksum { - my ($self) = @_; - my $header = pack HEADER_FORMAT, 'C', 0, 0, 0, $self->checksum->hexdigest; - $self->output_fh->print($header); - $self->reset_checksum; - $self->output_fh->flush; + my ($self) = @_; + my $header = pack HEADER_FORMAT, 'C', 0, 0, 0, $self->checksum->hexdigest; + $self->output_fh->print($header); + $self->reset_checksum; + $self->output_fh->flush; } sub _build_checksum { - my ($self) = @_; - return Digest::SHA->new(256); + my ($self) = @_; + return Digest::SHA->new(256); } sub _build_input_fh { - my ($self) = @_; - if ($self->has_output_fh) { - croak "I will not read and write a file at the same time"; - } - my $file = $self->file; - return IO::File->new( $self->file, '<:bytes' ) or die "cannot read $file: $!"; + my ($self) = @_; + if ($self->has_output_fh) { + croak "I will not read and write a file at the same time"; + } + my $file = $self->file; + return IO::File->new($self->file, '<:bytes') or die "cannot read $file: $!"; } sub _build_output_fh { - my ($self) = @_; - if ($self->has_input_fh) { - croak "I will not read and write a file at the same time"; - } - my $file = $self->file; - if (-e $file) { - croak "I will not overwrite a file (file $file already exists)"; - } - return IO::File->new( $file, '>:bytes' ) or die "cannot write $file: $!"; + my ($self) = @_; + if ($self->has_input_fh) { + croak "I will not read and write a file at the same time"; + } + my $file = $self->file; + if (-e $file) { + croak "I will not overwrite a file (file $file already exists)"; + } + return IO::File->new($file, '>:bytes') or die "cannot write $file: $!"; } sub _read_header { - my ($self) = @_; - my $header = '' x HEADER_SIZE; - my $header_len = $self->input_fh->read($header, HEADER_SIZE); - if ( !$header_len || $header_len != HEADER_SIZE ) { - die "bad header\n"; - } - return $header; + my ($self) = @_; + my $header = '' x HEADER_SIZE; + my $header_len = $self->input_fh->read($header, HEADER_SIZE); + if (!$header_len || $header_len != HEADER_SIZE) { + die "bad header\n"; + } + return $header; } sub _read_data { - my ($self, $data_len, $hash) = @_; + my ($self, $data_len, $hash) = @_; - my $data = '' x $data_len; - my $read_data_len = $self->input_fh->read($data, $data_len); + my $data = '' x $data_len; + my $read_data_len = $self->input_fh->read($data, $data_len); - unless ( $read_data_len == $data_len ) { - die "bad data\n"; - } + unless ($read_data_len == $data_len) { + die "bad data\n"; + } - unless ( $hash eq sha256_hex($data) ) { - die "bad checksum:\n\t$hash\n\t" . sha226_hex($data) . "\n"; - } + unless ($hash eq sha256_hex($data)) { + die "bad checksum:\n\t$hash\n\t" . sha226_hex($data) . "\n"; + } - return $data; + return $data; } -1;
\ No newline at end of file +1; diff --git a/Bugzilla/Attachment/Database.pm b/Bugzilla/Attachment/Database.pm index 42cdd2d6f..661ac9131 100644 --- a/Bugzilla/Attachment/Database.pm +++ b/Bugzilla/Attachment/Database.pm @@ -14,48 +14,40 @@ use warnings; use Bugzilla::Util qw(trick_taint); sub new { - return bless({}, shift); + return bless({}, shift); } sub store { - my ($self, $attach_id, $data) = @_; - my $dbh = Bugzilla->dbh; - my $sth = $dbh->prepare("INSERT INTO attach_data (id, thedata) VALUES ($attach_id, ?)"); - trick_taint($data); - $sth->bind_param(1, $data, $dbh->BLOB_TYPE); - $sth->execute(); + my ($self, $attach_id, $data) = @_; + my $dbh = Bugzilla->dbh; + my $sth = $dbh->prepare( + "INSERT INTO attach_data (id, thedata) VALUES ($attach_id, ?)"); + trick_taint($data); + $sth->bind_param(1, $data, $dbh->BLOB_TYPE); + $sth->execute(); } sub retrieve { - my ($self, $attach_id) = @_; - my $dbh = Bugzilla->dbh; - my ($data) = $dbh->selectrow_array( - "SELECT thedata FROM attach_data WHERE id = ?", - undef, - $attach_id - ); - return $data; + my ($self, $attach_id) = @_; + my $dbh = Bugzilla->dbh; + my ($data) + = $dbh->selectrow_array("SELECT thedata FROM attach_data WHERE id = ?", + undef, $attach_id); + return $data; } sub remove { - my ($self, $attach_id) = @_; - my $dbh = Bugzilla->dbh; - $dbh->do( - "DELETE FROM attach_data WHERE id = ?", - undef, - $attach_id - ); + my ($self, $attach_id) = @_; + my $dbh = Bugzilla->dbh; + $dbh->do("DELETE FROM attach_data WHERE id = ?", undef, $attach_id); } sub exists { - my ($self, $attach_id) = @_; - my $dbh = Bugzilla->dbh; - my ($exists) = $dbh->selectrow_array( - "SELECT 1 FROM attach_data WHERE id = ?", - undef, - $attach_id - ); - return !!$exists; + my ($self, $attach_id) = @_; + my $dbh = Bugzilla->dbh; + my ($exists) = $dbh->selectrow_array("SELECT 1 FROM attach_data WHERE id = ?", + undef, $attach_id); + return !!$exists; } 1; diff --git a/Bugzilla/Attachment/FileSystem.pm b/Bugzilla/Attachment/FileSystem.pm index a9fdf83b6..f10a994fd 100644 --- a/Bugzilla/Attachment/FileSystem.pm +++ b/Bugzilla/Attachment/FileSystem.pm @@ -14,50 +14,50 @@ use warnings; use Bugzilla::Constants qw(bz_locations); sub new { - return bless({}, shift); + return bless({}, shift); } sub store { - my ($self, $attach_id, $data) = @_; - my $path = _local_path($attach_id); - mkdir($path, 0770) unless -d $path; - open(my $fh, '>', _local_file($attach_id)); - binmode($fh); - print $fh $data; - close($fh); + my ($self, $attach_id, $data) = @_; + my $path = _local_path($attach_id); + mkdir($path, 0770) unless -d $path; + open(my $fh, '>', _local_file($attach_id)); + binmode($fh); + print $fh $data; + close($fh); } sub retrieve { - my ($self, $attach_id) = @_; - if (open(my $fh, '<', _local_file($attach_id))) { - local $/; - binmode($fh); - my $data = <$fh>; - close($fh); - return $data; - } - return undef; + my ($self, $attach_id) = @_; + if (open(my $fh, '<', _local_file($attach_id))) { + local $/; + binmode($fh); + my $data = <$fh>; + close($fh); + return $data; + } + return undef; } sub remove { - my ($self, $attach_id) = @_; - unlink(_local_file($attach_id)); + my ($self, $attach_id) = @_; + unlink(_local_file($attach_id)); } sub exists { - my ($self, $attach_id) = @_; - return -e _local_file($attach_id); + my ($self, $attach_id) = @_; + return -e _local_file($attach_id); } sub _local_path { - my ($attach_id) = @_; - my $hash = sprintf('group.%03d', $attach_id % 1000); - return bz_locations()->{attachdir} . '/' . $hash; + my ($attach_id) = @_; + my $hash = sprintf('group.%03d', $attach_id % 1000); + return bz_locations()->{attachdir} . '/' . $hash; } sub _local_file { - my ($attach_id) = @_; - return _local_path($attach_id) . '/attachment.' . $attach_id; + my ($attach_id) = @_; + return _local_path($attach_id) . '/attachment.' . $attach_id; } 1; diff --git a/Bugzilla/Attachment/PatchReader.pm b/Bugzilla/Attachment/PatchReader.pm index 8025f5b82..0ae70d3f4 100644 --- a/Bugzilla/Attachment/PatchReader.pm +++ b/Bugzilla/Attachment/PatchReader.pm @@ -20,148 +20,160 @@ use Bugzilla::Attachment; use Bugzilla::Util; sub process_diff { - my ($attachment, $format, $context) = @_; - my $dbh = Bugzilla->dbh; - my $cgi = Bugzilla->cgi; - my $lc = Bugzilla->localconfig; - my $vars = {}; - - my ($reader, $last_reader) = setup_patch_readers(undef, $context); - - if ($format eq 'raw') { - require Bugzilla::PatchReader::DiffPrinter::raw; - $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::raw()); - - Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get") - if Bugzilla->user->id; - # Actually print out the patch. - print $cgi->header(-type => 'text/plain', - -expires => '+3M'); - disable_utf8(); - $reader->iterate_string('Attachment ' . $attachment->id, $attachment->data); - } - else { - my @other_patches = (); - if ($lc->{interdiffbin} && $lc->{diffpath}) { - # Get the list of attachments that the user can view in this bug. - my @attachments = - @{Bugzilla::Attachment->get_attachments_by_bug($attachment->bug)}; - # Extract patches only. - @attachments = grep {$_->ispatch == 1} @attachments; - # We want them sorted from newer to older. - @attachments = sort { $b->id <=> $a->id } @attachments; - - # Ignore the current patch, but select the one right before it - # chronologically. - my $select_next_patch = 0; - foreach my $attach (@attachments) { - if ($attach->id == $attachment->id) { - $select_next_patch = 1; - } - else { - push(@other_patches, { 'id' => $attach->id, - 'desc' => $attach->description, - 'selected' => $select_next_patch }); - $select_next_patch = 0; - } - } - } + my ($attachment, $format, $context) = @_; + my $dbh = Bugzilla->dbh; + my $cgi = Bugzilla->cgi; + my $lc = Bugzilla->localconfig; + my $vars = {}; - $vars->{'bugid'} = $attachment->bug_id; - $vars->{'attachid'} = $attachment->id; - $vars->{'description'} = $attachment->description; - $vars->{'other_patches'} = \@other_patches; - - setup_template_patch_reader($last_reader, $format, $context, $vars); - # The patch is going to be displayed in a HTML page and if the utf8 - # param is enabled, we have to encode attachment data as utf8. - if (Bugzilla->params->{'utf8'}) { - $attachment->data; # Populate ->{data} - utf8::decode($attachment->{data}); - } - $reader->iterate_string('Attachment ' . $attachment->id, $attachment->data); - } -} + my ($reader, $last_reader) = setup_patch_readers(undef, $context); -sub process_interdiff { - my ($old_attachment, $new_attachment, $format, $context) = @_; - my $cgi = Bugzilla->cgi; - my $lc = Bugzilla->localconfig; - my $vars = {}; - - if (Bugzilla->user->id) { - foreach my $attachment ($old_attachment, $new_attachment) { - Bugzilla->log_user_request($attachment->bug_id, $attachment->id, "attachment-get"); + if ($format eq 'raw') { + require Bugzilla::PatchReader::DiffPrinter::raw; + $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::raw()); + + Bugzilla->log_user_request($attachment->bug_id, $attachment->id, + "attachment-get") + if Bugzilla->user->id; + + # Actually print out the patch. + print $cgi->header(-type => 'text/plain', -expires => '+3M'); + disable_utf8(); + $reader->iterate_string('Attachment ' . $attachment->id, $attachment->data); + } + else { + my @other_patches = (); + if ($lc->{interdiffbin} && $lc->{diffpath}) { + + # Get the list of attachments that the user can view in this bug. + my @attachments + = @{Bugzilla::Attachment->get_attachments_by_bug($attachment->bug)}; + + # Extract patches only. + @attachments = grep { $_->ispatch == 1 } @attachments; + + # We want them sorted from newer to older. + @attachments = sort { $b->id <=> $a->id } @attachments; + + # Ignore the current patch, but select the one right before it + # chronologically. + my $select_next_patch = 0; + foreach my $attach (@attachments) { + if ($attach->id == $attachment->id) { + $select_next_patch = 1; } + else { + push( + @other_patches, + { + 'id' => $attach->id, + 'desc' => $attach->description, + 'selected' => $select_next_patch + } + ); + $select_next_patch = 0; + } + } } - # Encode attachment data as utf8 if it's going to be displayed in a HTML - # page using the UTF-8 encoding. - if ($format ne 'raw' && Bugzilla->params->{'utf8'}) { - $old_attachment->data; # Populate ->{data} - utf8::decode($old_attachment->{data}); - $new_attachment->data; # Populate ->{data} - utf8::decode($new_attachment->{data}); - } - - # Get old patch data. - my ($old_filename, $old_file_list) = get_unified_diff($old_attachment, $format); - # Get new patch data. - my ($new_filename, $new_file_list) = get_unified_diff($new_attachment, $format); + $vars->{'bugid'} = $attachment->bug_id; + $vars->{'attachid'} = $attachment->id; + $vars->{'description'} = $attachment->description; + $vars->{'other_patches'} = \@other_patches; - my $warning = warn_if_interdiff_might_fail($old_file_list, $new_file_list); + setup_template_patch_reader($last_reader, $format, $context, $vars); - # Send through interdiff, send output directly to template. - # Must hack path so that interdiff will work. - $ENV{'PATH'} = $lc->{diffpath}; - - my ($pid, $interdiff_stdout, $interdiff_stderr); - $interdiff_stderr = gensym; - $pid = open3(gensym, $interdiff_stdout, $interdiff_stderr, - $lc->{interdiffbin}, $old_filename, $new_filename); - binmode $interdiff_stdout; - - # Check for errors - { - local $/ = undef; - my $error = <$interdiff_stderr>; - if ($error) { - warn($error); - $warning = 'interdiff3'; - } + # The patch is going to be displayed in a HTML page and if the utf8 + # param is enabled, we have to encode attachment data as utf8. + if (Bugzilla->params->{'utf8'}) { + $attachment->data; # Populate ->{data} + utf8::decode($attachment->{data}); } + $reader->iterate_string('Attachment ' . $attachment->id, $attachment->data); + } +} - my ($reader, $last_reader) = setup_patch_readers("", $context); - - if ($format eq 'raw') { - require Bugzilla::PatchReader::DiffPrinter::raw; - $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::raw()); - # Actually print out the patch. - print $cgi->header(-type => 'text/plain', - -expires => '+3M'); - disable_utf8(); +sub process_interdiff { + my ($old_attachment, $new_attachment, $format, $context) = @_; + my $cgi = Bugzilla->cgi; + my $lc = Bugzilla->localconfig; + my $vars = {}; + + if (Bugzilla->user->id) { + foreach my $attachment ($old_attachment, $new_attachment) { + Bugzilla->log_user_request($attachment->bug_id, $attachment->id, + "attachment-get"); } - else { - # In case the HTML page is displayed with the UTF-8 encoding. - binmode $interdiff_stdout, ':utf8' if Bugzilla->params->{'utf8'}; - - $vars->{'warning'} = $warning if $warning; - $vars->{'bugid'} = $new_attachment->bug_id; - $vars->{'oldid'} = $old_attachment->id; - $vars->{'old_desc'} = $old_attachment->description; - $vars->{'newid'} = $new_attachment->id; - $vars->{'new_desc'} = $new_attachment->description; - - setup_template_patch_reader($last_reader, $format, $context, $vars); + } + + # Encode attachment data as utf8 if it's going to be displayed in a HTML + # page using the UTF-8 encoding. + if ($format ne 'raw' && Bugzilla->params->{'utf8'}) { + $old_attachment->data; # Populate ->{data} + utf8::decode($old_attachment->{data}); + $new_attachment->data; # Populate ->{data} + utf8::decode($new_attachment->{data}); + } + + # Get old patch data. + my ($old_filename, $old_file_list) = get_unified_diff($old_attachment, $format); + + # Get new patch data. + my ($new_filename, $new_file_list) = get_unified_diff($new_attachment, $format); + + my $warning = warn_if_interdiff_might_fail($old_file_list, $new_file_list); + + # Send through interdiff, send output directly to template. + # Must hack path so that interdiff will work. + $ENV{'PATH'} = $lc->{diffpath}; + + my ($pid, $interdiff_stdout, $interdiff_stderr); + $interdiff_stderr = gensym; + $pid = open3(gensym, $interdiff_stdout, $interdiff_stderr, $lc->{interdiffbin}, + $old_filename, $new_filename); + binmode $interdiff_stdout; + + # Check for errors + { + local $/ = undef; + my $error = <$interdiff_stderr>; + if ($error) { + warn($error); + $warning = 'interdiff3'; } - $reader->iterate_fh($interdiff_stdout, 'interdiff #' . $old_attachment->id . - ' #' . $new_attachment->id); - waitpid($pid, 0) if $pid; - $ENV{'PATH'} = ''; - - # Delete temporary files. - unlink($old_filename) or warn "Could not unlink $old_filename: $!"; - unlink($new_filename) or warn "Could not unlink $new_filename: $!"; + } + + my ($reader, $last_reader) = setup_patch_readers("", $context); + + if ($format eq 'raw') { + require Bugzilla::PatchReader::DiffPrinter::raw; + $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::raw()); + + # Actually print out the patch. + print $cgi->header(-type => 'text/plain', -expires => '+3M'); + disable_utf8(); + } + else { + # In case the HTML page is displayed with the UTF-8 encoding. + binmode $interdiff_stdout, ':utf8' if Bugzilla->params->{'utf8'}; + + $vars->{'warning'} = $warning if $warning; + $vars->{'bugid'} = $new_attachment->bug_id; + $vars->{'oldid'} = $old_attachment->id; + $vars->{'old_desc'} = $old_attachment->description; + $vars->{'newid'} = $new_attachment->id; + $vars->{'new_desc'} = $new_attachment->description; + + setup_template_patch_reader($last_reader, $format, $context, $vars); + } + $reader->iterate_fh($interdiff_stdout, + 'interdiff #' . $old_attachment->id . ' #' . $new_attachment->id); + waitpid($pid, 0) if $pid; + $ENV{'PATH'} = ''; + + # Delete temporary files. + unlink($old_filename) or warn "Could not unlink $old_filename: $!"; + unlink($new_filename) or warn "Could not unlink $new_filename: $!"; } ###################### @@ -169,144 +181,154 @@ sub process_interdiff { ###################### sub get_unified_diff { - my ($attachment, $format) = @_; - - # Bring in the modules we need. - require Bugzilla::PatchReader::Raw; - require Bugzilla::PatchReader::FixPatchRoot; - require Bugzilla::PatchReader::DiffPrinter::raw; - require Bugzilla::PatchReader::PatchInfoGrabber; - require File::Temp; - - $attachment->ispatch - || ThrowCodeError('must_be_patch', { 'attach_id' => $attachment->id }); - - # Reads in the patch, converting to unified diff in a temp file. - my $reader = new Bugzilla::PatchReader::Raw; - my $last_reader = $reader; - - # Fixes patch root (makes canonical if possible). - if (Bugzilla->params->{'cvsroot'}) { - my $fix_patch_root = - new Bugzilla::PatchReader::FixPatchRoot(Bugzilla->params->{'cvsroot'}); - $last_reader->sends_data_to($fix_patch_root); - $last_reader = $fix_patch_root; - } - - # Grabs the patch file info. - my $patch_info_grabber = new Bugzilla::PatchReader::PatchInfoGrabber(); - $last_reader->sends_data_to($patch_info_grabber); - $last_reader = $patch_info_grabber; - - # Prints out to temporary file. - my ($fh, $filename) = File::Temp::tempfile(); - if ($format ne 'raw' && Bugzilla->params->{'utf8'}) { - # The HTML page will be displayed with the UTF-8 encoding. - binmode $fh, ':utf8'; - } - my $raw_printer = new Bugzilla::PatchReader::DiffPrinter::raw($fh); - $last_reader->sends_data_to($raw_printer); - $last_reader = $raw_printer; - - # Iterate! - $reader->iterate_string($attachment->id, $attachment->data); - - return ($filename, $patch_info_grabber->patch_info()->{files}); + my ($attachment, $format) = @_; + + # Bring in the modules we need. + require Bugzilla::PatchReader::Raw; + require Bugzilla::PatchReader::FixPatchRoot; + require Bugzilla::PatchReader::DiffPrinter::raw; + require Bugzilla::PatchReader::PatchInfoGrabber; + require File::Temp; + + $attachment->ispatch + || ThrowCodeError('must_be_patch', {'attach_id' => $attachment->id}); + + # Reads in the patch, converting to unified diff in a temp file. + my $reader = new Bugzilla::PatchReader::Raw; + my $last_reader = $reader; + + # Fixes patch root (makes canonical if possible). + if (Bugzilla->params->{'cvsroot'}) { + my $fix_patch_root + = new Bugzilla::PatchReader::FixPatchRoot(Bugzilla->params->{'cvsroot'}); + $last_reader->sends_data_to($fix_patch_root); + $last_reader = $fix_patch_root; + } + + # Grabs the patch file info. + my $patch_info_grabber = new Bugzilla::PatchReader::PatchInfoGrabber(); + $last_reader->sends_data_to($patch_info_grabber); + $last_reader = $patch_info_grabber; + + # Prints out to temporary file. + my ($fh, $filename) = File::Temp::tempfile(); + if ($format ne 'raw' && Bugzilla->params->{'utf8'}) { + + # The HTML page will be displayed with the UTF-8 encoding. + binmode $fh, ':utf8'; + } + my $raw_printer = new Bugzilla::PatchReader::DiffPrinter::raw($fh); + $last_reader->sends_data_to($raw_printer); + $last_reader = $raw_printer; + + # Iterate! + $reader->iterate_string($attachment->id, $attachment->data); + + return ($filename, $patch_info_grabber->patch_info()->{files}); } sub warn_if_interdiff_might_fail { - my ($old_file_list, $new_file_list) = @_; - - # Verify that the list of files diffed is the same. - my @old_files = sort keys %{$old_file_list}; - my @new_files = sort keys %{$new_file_list}; - if (@old_files != @new_files - || join(' ', @old_files) ne join(' ', @new_files)) + my ($old_file_list, $new_file_list) = @_; + + # Verify that the list of files diffed is the same. + my @old_files = sort keys %{$old_file_list}; + my @new_files = sort keys %{$new_file_list}; + if (@old_files != @new_files || join(' ', @old_files) ne join(' ', @new_files)) + { + return 'interdiff1'; + } + + # Verify that the revisions in the files are the same. + foreach my $file (keys %{$old_file_list}) { + if ( + $old_file_list->{$file}{old_revision} ne $new_file_list->{$file}{old_revision}) { - return 'interdiff1'; - } - - # Verify that the revisions in the files are the same. - foreach my $file (keys %{$old_file_list}) { - if ($old_file_list->{$file}{old_revision} ne - $new_file_list->{$file}{old_revision}) - { - return 'interdiff2'; - } + return 'interdiff2'; } - return undef; + } + return undef; } sub setup_patch_readers { - my ($diff_root, $context) = @_; - - # Parameters: - # format=raw|html - # context=patch|file|0-n - # collapsed=0|1 - # headers=0|1 - - # Define the patch readers. - # The reader that reads the patch in (whatever its format). - require Bugzilla::PatchReader::Raw; - my $reader = new Bugzilla::PatchReader::Raw; - my $last_reader = $reader; - # Fix the patch root if we have a cvs root. - if (Bugzilla->params->{'cvsroot'}) { - require Bugzilla::PatchReader::FixPatchRoot; - $last_reader->sends_data_to(new Bugzilla::PatchReader::FixPatchRoot(Bugzilla->params->{'cvsroot'})); - $last_reader->sends_data_to->diff_root($diff_root) if defined($diff_root); - $last_reader = $last_reader->sends_data_to; - } - - # Add in cvs context if we have the necessary info to do it - if ($context ne 'patch' && Bugzilla->localconfig->{cvsbin} - && Bugzilla->params->{'cvsroot_get'}) - { - require Bugzilla::PatchReader::AddCVSContext; - # We need to set $cvsbin as global, because PatchReader::CVSClient - # needs it in order to find 'cvs'. - $main::cvsbin = Bugzilla->localconfig->{cvsbin}; - $last_reader->sends_data_to( - new Bugzilla::PatchReader::AddCVSContext($context, Bugzilla->params->{'cvsroot_get'})); - $last_reader = $last_reader->sends_data_to; - } - - return ($reader, $last_reader); + my ($diff_root, $context) = @_; + + # Parameters: + # format=raw|html + # context=patch|file|0-n + # collapsed=0|1 + # headers=0|1 + + # Define the patch readers. + # The reader that reads the patch in (whatever its format). + require Bugzilla::PatchReader::Raw; + my $reader = new Bugzilla::PatchReader::Raw; + my $last_reader = $reader; + + # Fix the patch root if we have a cvs root. + if (Bugzilla->params->{'cvsroot'}) { + require Bugzilla::PatchReader::FixPatchRoot; + $last_reader->sends_data_to(new Bugzilla::PatchReader::FixPatchRoot( + Bugzilla->params->{'cvsroot'})); + $last_reader->sends_data_to->diff_root($diff_root) if defined($diff_root); + $last_reader = $last_reader->sends_data_to; + } + + # Add in cvs context if we have the necessary info to do it + if ( $context ne 'patch' + && Bugzilla->localconfig->{cvsbin} + && Bugzilla->params->{'cvsroot_get'}) + { + require Bugzilla::PatchReader::AddCVSContext; + + # We need to set $cvsbin as global, because PatchReader::CVSClient + # needs it in order to find 'cvs'. + $main::cvsbin = Bugzilla->localconfig->{cvsbin}; + $last_reader->sends_data_to(new Bugzilla::PatchReader::AddCVSContext( + $context, Bugzilla->params->{'cvsroot_get'} + )); + $last_reader = $last_reader->sends_data_to; + } + + return ($reader, $last_reader); } sub setup_template_patch_reader { - my ($last_reader, $format, $context, $vars) = @_; - my $cgi = Bugzilla->cgi; - my $template = Bugzilla->template; - - require Bugzilla::PatchReader::DiffPrinter::template; - - # Define the vars for templates. - if (defined $cgi->param('headers')) { - $vars->{'headers'} = $cgi->param('headers'); - } - else { - $vars->{'headers'} = 1; + my ($last_reader, $format, $context, $vars) = @_; + my $cgi = Bugzilla->cgi; + my $template = Bugzilla->template; + + require Bugzilla::PatchReader::DiffPrinter::template; + + # Define the vars for templates. + if (defined $cgi->param('headers')) { + $vars->{'headers'} = $cgi->param('headers'); + } + else { + $vars->{'headers'} = 1; + } + + $vars->{'collapsed'} = $cgi->param('collapsed'); + $vars->{'context'} = $context; + $vars->{'do_context'} + = Bugzilla->localconfig->{cvsbin} + && Bugzilla->params->{'cvsroot_get'} + && !$vars->{'newid'}; + + # Print everything out. + print $cgi->header(-type => 'text/html'); + + $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::template( + $template, + "attachment/diff-header.$format.tmpl", + "attachment/diff-file.$format.tmpl", + "attachment/diff-footer.$format.tmpl", + { + %{$vars}, + bonsai_url => Bugzilla->params->{'bonsai_url'}, + lxr_url => Bugzilla->params->{'lxr_url'}, + lxr_root => Bugzilla->params->{'lxr_root'}, } - - $vars->{'collapsed'} = $cgi->param('collapsed'); - $vars->{'context'} = $context; - $vars->{'do_context'} = Bugzilla->localconfig->{cvsbin} - && Bugzilla->params->{'cvsroot_get'} && !$vars->{'newid'}; - - # Print everything out. - print $cgi->header(-type => 'text/html'); - - $last_reader->sends_data_to(new Bugzilla::PatchReader::DiffPrinter::template($template, - "attachment/diff-header.$format.tmpl", - "attachment/diff-file.$format.tmpl", - "attachment/diff-footer.$format.tmpl", - { %{$vars}, - bonsai_url => Bugzilla->params->{'bonsai_url'}, - lxr_url => Bugzilla->params->{'lxr_url'}, - lxr_root => Bugzilla->params->{'lxr_root'}, - })); + )); } 1; diff --git a/Bugzilla/Attachment/S3.pm b/Bugzilla/Attachment/S3.pm index e1c7269a5..7f4755720 100644 --- a/Bugzilla/Attachment/S3.pm +++ b/Bugzilla/Attachment/S3.pm @@ -15,44 +15,48 @@ use Bugzilla::Error; use Bugzilla::S3; sub new { - my $s3 = Bugzilla::S3->new({ - aws_access_key_id => Bugzilla->params->{aws_access_key_id}, - aws_secret_access_key => Bugzilla->params->{aws_secret_access_key}, - secure => 1, - }); - return bless({ - s3 => $s3, - bucket => $s3->bucket(Bugzilla->params->{s3_bucket}), - }, shift); + my $s3 = Bugzilla::S3->new({ + aws_access_key_id => Bugzilla->params->{aws_access_key_id}, + aws_secret_access_key => Bugzilla->params->{aws_secret_access_key}, + secure => 1, + }); + return + bless({s3 => $s3, bucket => $s3->bucket(Bugzilla->params->{s3_bucket}),}, + shift); } sub store { - my ($self, $attach_id, $data) = @_; - unless ($self->{bucket}->add_key($attach_id, $data)) { - warn "Failed to add attachment $attach_id to S3: " . $self->{bucket}->errstr . "\n"; - ThrowCodeError('s3_add_failed', { attach_id => $attach_id, reason => $self->{bucket}->errstr }); - } + my ($self, $attach_id, $data) = @_; + unless ($self->{bucket}->add_key($attach_id, $data)) { + warn "Failed to add attachment $attach_id to S3: " + . $self->{bucket}->errstr . "\n"; + ThrowCodeError('s3_add_failed', + {attach_id => $attach_id, reason => $self->{bucket}->errstr}); + } } sub retrieve { - my ($self, $attach_id) = @_; - my $response = $self->{bucket}->get_key($attach_id); - if (!$response) { - warn "Failed to retrieve attachment $attach_id from S3: " . $self->{bucket}->errstr . "\n"; - ThrowCodeError('s3_get_failed', { attach_id => $attach_id, reason => $self->{bucket}->errstr }); - } - return $response->{value}; + my ($self, $attach_id) = @_; + my $response = $self->{bucket}->get_key($attach_id); + if (!$response) { + warn "Failed to retrieve attachment $attach_id from S3: " + . $self->{bucket}->errstr . "\n"; + ThrowCodeError('s3_get_failed', + {attach_id => $attach_id, reason => $self->{bucket}->errstr}); + } + return $response->{value}; } sub remove { - my ($self, $attach_id) = @_; - $self->{bucket}->delete_key($attach_id) - or warn "Failed to remove attachment $attach_id from S3: " . $self->{bucket}->errstr . "\n"; + my ($self, $attach_id) = @_; + $self->{bucket}->delete_key($attach_id) + or warn "Failed to remove attachment $attach_id from S3: " + . $self->{bucket}->errstr . "\n"; } sub exists { - my ($self, $attach_id) = @_; - return !!$self->{bucket}->head_key($attach_id); + my ($self, $attach_id) = @_; + return !!$self->{bucket}->head_key($attach_id); } 1; |