Package.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # Copyright 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Package;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Gettext;
  17. use Dpkg::ErrorHandling qw(error syserr warning internerr subprocerr);
  18. use Dpkg::Fields;
  19. use Dpkg::Cdata;
  20. use Dpkg::Checksums;
  21. use Dpkg::Version qw(parseversion);
  22. use Dpkg::Deps qw(@src_dep_fields);
  23. use Dpkg::Compression;
  24. use Dpkg::Exit;
  25. use Dpkg::Path qw(check_files_are_the_same);
  26. use POSIX;
  27. use File::Basename;
  28. # Public variables
  29. our $diff_ignore_default_regexp = '
  30. # Ignore general backup files
  31. (?:^|/).*~$|
  32. # Ignore emacs recovery files
  33. (?:^|/)\.#.*$|
  34. # Ignore vi swap files
  35. (?:^|/)\..*\.swp$|
  36. # Ignore baz-style junk files or directories
  37. (?:^|/),,.*(?:$|/.*$)|
  38. # File-names that should be ignored (never directories)
  39. (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore)$|
  40. # File or directory names that should be ignored
  41. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|
  42. \.shelf|_MTN|\.be|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  43. ';
  44. # Take out comments and newlines
  45. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  46. $diff_ignore_default_regexp =~ s/\n//sg;
  47. no warnings 'qw';
  48. our @tar_ignore_default_pattern = qw(
  49. *.a
  50. *.la
  51. *.o
  52. *.so
  53. *.swp
  54. *~
  55. ,,*
  56. .[#~]*
  57. .arch-ids
  58. .arch-inventory
  59. .be
  60. .bzr
  61. .bzr.backup
  62. .bzr.tags
  63. .bzrignore
  64. .cvsignore
  65. .deps
  66. .git
  67. .gitignore
  68. .hg
  69. .shelf
  70. .svn
  71. CVS
  72. DEADJOE
  73. RCS
  74. _MTN
  75. _darcs
  76. {arch}
  77. );
  78. # Private stuff
  79. my @dsc_fields = (qw(Format Source Binary Architecture Version Origin
  80. Maintainer Uploaders Dm-Upload-Allowed Homepage
  81. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  82. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  83. @src_dep_fields,
  84. qw(Checksums-Md5 Checksums-Sha1 Checksums-Sha256 Files));
  85. # Object methods
  86. sub new {
  87. my ($this, %args) = @_;
  88. my $class = ref($this) || $this;
  89. my $self = {
  90. 'fields' => Dpkg::Fields::Object->new(),
  91. 'options' => {},
  92. };
  93. bless $self, $class;
  94. if (exists $args{"filename"}) {
  95. $self->initialize($args{"filename"});
  96. }
  97. if (exists $args{"options"}) {
  98. $self->{'options'} = $args{'options'};
  99. }
  100. $self->init_options();
  101. return $self;
  102. }
  103. sub init_options {
  104. my ($self) = @_;
  105. # Use full ignore list by default
  106. $self->{'options'}{'diff_ignore_regexp'} ||= $diff_ignore_default_regexp;
  107. $self->{'options'}{'tar_ignore'} = [ @tar_ignore_default_pattern ]
  108. unless @{$self->{'options'}{'tar_ignore'}};
  109. }
  110. sub initialize {
  111. my ($self, $filename) = @_;
  112. my ($fn, $dir) = fileparse($filename);
  113. error(_g("%s is not the name of a file"), $filename) unless $fn;
  114. $self->{'basedir'} = $dir || "./";
  115. $self->{'filename'} = $fn;
  116. # Check if it contains a signature
  117. open(DSC, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  118. $self->{'is_signed'} = 0;
  119. while (<DSC>) {
  120. next if /^\s*$/o;
  121. $self->{'is_signed'} = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  122. last;
  123. }
  124. close(DSC);
  125. # Read the fields
  126. open(CDATA, "<", $filename) || syserr(_g("cannot open %s"), $filename);
  127. my $fields = parsecdata(\*CDATA,
  128. sprintf(_g("source control file %s"), $filename),
  129. allow_pgp => 1);
  130. close(CDATA);
  131. $self->{'fields'} = $fields;
  132. foreach my $f (qw(Source Format Version Files)) {
  133. unless (defined($fields->{$f})) {
  134. error(_g("missing critical source control field %s"), $f);
  135. }
  136. }
  137. $self->parse_files();
  138. $self->upgrade_object_type(0);
  139. }
  140. sub upgrade_object_type {
  141. my ($self, $update_format) = @_;
  142. $update_format = 1 unless defined $update_format;
  143. my $format = $self->{'fields'}{'Format'};
  144. if ($format =~ /^([\d\.]+)(?:\s+\((.*)\))?$/) {
  145. my ($version, $variant, $major, $minor) = ($1, $2, $1, undef);
  146. $major =~ s/\.[\d\.]+$//;
  147. my $module = "Dpkg::Source::Package::V$major";
  148. $module .= "::$variant" if defined $variant;
  149. eval "require $module; \$minor = \$${module}::CURRENT_MINOR_VERSION;";
  150. $minor = 0 unless defined $minor;
  151. if ($update_format) {
  152. $self->{'fields'}{'Format'} = "$major.$minor";
  153. $self->{'fields'}{'Format'} .= " ($variant)" if defined $variant;
  154. }
  155. if ($@) {
  156. error(_g("source package format `%s' is not supported (Perl module %s is required)"), $format, $module);
  157. }
  158. bless $self, $module;
  159. } else {
  160. error(_g("invalid Format field `%s'"), $format);
  161. }
  162. }
  163. sub get_filename {
  164. my ($self) = @_;
  165. return $self->{'basedir'} . $self->{'filename'};
  166. }
  167. sub get_files {
  168. my ($self) = @_;
  169. return keys %{$self->{'files'}};
  170. }
  171. sub parse_files {
  172. my ($self) = @_;
  173. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  174. my $files = $self->{'fields'}{'Files'};
  175. foreach my $file (split(/\n /, $files)) {
  176. next if $file eq '';
  177. $file =~ m/^($check_regex{md5}) # checksum
  178. [ \t]+(\d+) # size
  179. [ \t]+($rx_fname) # filename
  180. $/x
  181. || error(_g("Files field contains bad line `%s'"), $file);
  182. if (exists $self->{'files'}{$3}) {
  183. error(_g("file `%s' listed twice in Files field"), $3);
  184. } else {
  185. $self->{'files'}{$3} = $2;
  186. }
  187. }
  188. }
  189. sub check_checksums {
  190. my ($self) = @_;
  191. my ($fields, %checksum, %size) = $self->{'fields'};
  192. my $has_md5 = 1;
  193. if (not exists $fields->{'Checksums-Md5'}) {
  194. $fields->{'Checksums-Md5'} = $fields->{'Files'};
  195. $has_md5 = 0;
  196. }
  197. # extract the checksums from the fields in two hashes
  198. readallchecksums($self->{'fields'}, \%checksum, \%size);
  199. delete $fields->{'Checksums-Md5'} unless $has_md5;
  200. # getchecksums verify the checksums if they are pre-filled
  201. foreach my $file ($self->get_files()) {
  202. getchecksums($self->{'basedir'} . $file, $checksum{$file},
  203. \$size{$file});
  204. }
  205. }
  206. sub get_basename {
  207. my ($self, $with_revision) = @_;
  208. my $f = $self->{'fields'};
  209. unless (exists $f->{'Source'} and exists $f->{'Version'}) {
  210. error(_g("source and version are required to compute the source basename"));
  211. }
  212. my %v = parseversion($f->{'Version'});
  213. my $basename = $f->{'Source'} . "_" . $v{"version"};
  214. if ($with_revision and $f->{'Version'} =~ /-/) {
  215. $basename .= "-" . $v{'revision'};
  216. }
  217. return $basename;
  218. }
  219. sub find_original_tarballs {
  220. my ($self, $ext) = @_;
  221. $ext ||= $comp_regex;
  222. my $basename = $self->get_basename();
  223. my @tar;
  224. foreach my $dir (".", $self->{'basedir'}, $self->{'options'}{'origtardir'}) {
  225. next unless defined($dir) and -d $dir;
  226. opendir(DIR, $dir) || syserr(_g("cannot opendir %s"), $dir);
  227. push @tar, map { "$dir/$_" }
  228. grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$ext$/ }
  229. readdir(DIR);
  230. closedir(DIR);
  231. }
  232. return @tar;
  233. }
  234. sub is_signed {
  235. my $self = shift;
  236. return $self->{'is_signed'};
  237. }
  238. sub check_signature {
  239. my ($self) = @_;
  240. my $dsc = $self->get_filename();
  241. if (-x '/usr/bin/gpg') {
  242. my $gpg_command = 'gpg -q --verify ';
  243. if (-r '/usr/share/keyrings/debian-keyring.gpg') {
  244. $gpg_command = $gpg_command.'--keyring /usr/share/keyrings/debian-keyring.gpg ';
  245. }
  246. $gpg_command = $gpg_command.quotemeta($dsc).' 2>&1';
  247. #TODO: cleanup here
  248. my @gpg_output = `$gpg_command`;
  249. my $gpg_status = $? >> 8;
  250. if ($gpg_status) {
  251. print STDERR join("",@gpg_output);
  252. error(_g("failed to verify signature on %s"), $dsc)
  253. if ($gpg_status == 1);
  254. }
  255. } else {
  256. warning(_g("could not verify signature on %s since gpg isn't installed"),
  257. $dsc);
  258. }
  259. }
  260. sub parse_cmdline_options {
  261. my ($self, @opts) = @_;
  262. foreach (@opts) {
  263. if (not $self->parse_cmdline_option($_)) {
  264. warning(_g("%s is not a valid option for %s"), $_, ref($self));
  265. }
  266. }
  267. }
  268. sub parse_cmdline_option {
  269. return 0;
  270. }
  271. sub extract {
  272. my $self = shift;
  273. my $newdirectory = $_[0];
  274. # Copy orig tarballs
  275. if ($self->{'options'}{'copy_orig_tarballs'}) {
  276. my $basename = $self->get_basename();
  277. my ($dirname, $destdir) = fileparse($newdirectory);
  278. $destdir ||= "./";
  279. foreach my $orig (grep { /^\Q$basename\E\.orig(-\w+)?\.tar\.$comp_regex$/ }
  280. $self->get_files())
  281. {
  282. my $src = File::Spec->catfile($self->{'basedir'}, $orig);
  283. my $dst = File::Spec->catfile($destdir, $orig);
  284. if (not check_files_are_the_same($src, $dst, 1)) {
  285. system('cp', '--', $src, $dst);
  286. subprocerr("cp $src to $dst") if $?;
  287. }
  288. }
  289. }
  290. # Try extract
  291. eval { $self->do_extract(@_) };
  292. if ($@) {
  293. &$_() foreach reverse @Dpkg::Exit::handlers;
  294. die $@;
  295. }
  296. # Make sure debian/rules is executable
  297. my $rules = File::Spec->catfile($newdirectory, "debian", "rules");
  298. my @s = lstat($rules);
  299. if (not scalar(@s)) {
  300. unless ($! == ENOENT) {
  301. syserr(_g("cannot stat %s"), $rules);
  302. }
  303. warning(_g("%s does not exist"), $rules);
  304. } elsif (-f _) {
  305. chmod($s[2] | 0111, $rules) ||
  306. syserr(_g("cannot make %s executable"), $rules);
  307. } else {
  308. warning(_g("%s is not a plain file"), $rules);
  309. }
  310. }
  311. sub do_extract {
  312. error("Dpkg::Source::Package doesn't know how to unpack a source package. Use one of the subclasses.");
  313. }
  314. # Function used specifically during creation of a source package
  315. sub build {
  316. my $self = shift;
  317. eval { $self->do_build(@_) };
  318. if ($@) {
  319. &$_() foreach reverse @Dpkg::Exit::handlers;
  320. die $@;
  321. }
  322. }
  323. sub do_build {
  324. error("Dpkg::Source::Package doesn't know how to build a source package. Use one of the subclasses.");
  325. }
  326. sub can_build {
  327. my ($self, $dir) = @_;
  328. return (0, "can_build() has not been overriden");
  329. }
  330. sub add_file {
  331. my ($self, $filename) = @_;
  332. my ($fn, $dir) = fileparse($filename);
  333. if (exists $self->{'files'}{$fn}) {
  334. internerr(_g("tried to add file `%s' twice"), $fn);
  335. }
  336. my (%sums, $size);
  337. getchecksums($filename, \%sums, \$size);
  338. $self->{'files'}{$fn} = $size;
  339. foreach my $alg (sort keys %sums) {
  340. $self->{'fields'}{"Checksums-$alg"} .= "\n $sums{$alg} $size $fn";
  341. }
  342. $self->{'fields'}{'Files'}.= "\n $sums{md5} $size $fn";
  343. }
  344. sub write_dsc {
  345. my ($self, %opts) = @_;
  346. my $fields = $self->{'fields'};
  347. foreach my $f (keys %{$opts{'override'}}) {
  348. $fields->{$f} = $opts{'override'}{$f};
  349. }
  350. unless($opts{'nocheck'}) {
  351. foreach my $f (qw(Source Version)) {
  352. unless (defined($fields->{$f})) {
  353. error(_g("missing information for critical output field %s"), $f);
  354. }
  355. }
  356. foreach my $f (qw(Maintainer Architecture Standards-Version)) {
  357. unless (defined($fields->{$f})) {
  358. warning(_g("missing information for output field %s"), $f);
  359. }
  360. }
  361. }
  362. foreach my $f (keys %{$opts{'remove'}}) {
  363. delete $fields->{$f};
  364. }
  365. my $filename = $opts{'filename'};
  366. unless (defined $filename) {
  367. $filename = $self->get_basename(1) . ".dsc";
  368. }
  369. open(DSC, ">", $filename) || syserr(_g("cannot write %s"), $filename);
  370. delete $fields->{'Checksums-Md5'}; # identical with Files field
  371. tied(%{$fields})->set_field_importance(@dsc_fields);
  372. tied(%{$fields})->output(\*DSC, $opts{'substvars'});
  373. close(DSC);
  374. }
  375. # vim: set et sw=4 ts=8
  376. 1;