Package.pm 13 KB

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