Package.pm 12 KB

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