V1.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. # Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2008, 2012-2015 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Source::Package::V1;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '0.01';
  20. use POSIX qw(:errno_h);
  21. use Cwd;
  22. use File::Basename;
  23. use File::Temp qw(tempfile);
  24. use File::Spec;
  25. use Dpkg ();
  26. use Dpkg::Gettext;
  27. use Dpkg::ErrorHandling;
  28. use Dpkg::Compression;
  29. use Dpkg::Source::Archive;
  30. use Dpkg::Source::Patch;
  31. use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
  32. use Dpkg::Source::Functions qw(erasedir);
  33. use Dpkg::Source::Package::V3::Native;
  34. use parent qw(Dpkg::Source::Package);
  35. our $CURRENT_MINOR_VERSION = '0';
  36. sub init_options {
  37. my $self = shift;
  38. # Don't call $self->SUPER::init_options() on purpose, V1.0 has no
  39. # ignore by default
  40. if ($self->{options}{diff_ignore_regex}) {
  41. $self->{options}{diff_ignore_regex} .= '|(?:^|/)debian/source/local-.*$';
  42. } else {
  43. $self->{options}{diff_ignore_regex} = '(?:^|/)debian/source/local-.*$';
  44. }
  45. push @{$self->{options}{tar_ignore}}, 'debian/source/local-options',
  46. 'debian/source/local-patch-header';
  47. $self->{options}{sourcestyle} //= 'X';
  48. $self->{options}{skip_debianization} //= 0;
  49. $self->{options}{ignore_bad_version} //= 0;
  50. $self->{options}{abort_on_upstream_changes} //= 0;
  51. # V1.0 only supports gzip compression.
  52. $self->{options}{compression} //= 'gzip';
  53. $self->{options}{comp_level} //= compression_get_property('gzip', 'default_level');
  54. $self->{options}{comp_ext} //= compression_get_property('gzip', 'file_ext');
  55. }
  56. my @module_cmdline = (
  57. {
  58. name => '-sa',
  59. help => N_('auto select original source'),
  60. when => 'build',
  61. }, {
  62. name => '-sk',
  63. help => N_('use packed original source (unpack and keep)'),
  64. when => 'build',
  65. }, {
  66. name => '-sp',
  67. help => N_('use packed original source (unpack and remove)'),
  68. when => 'build',
  69. }, {
  70. name => '-su',
  71. help => N_('use unpacked original source (pack and keep)'),
  72. when => 'build',
  73. }, {
  74. name => '-sr',
  75. help => N_('use unpacked original source (pack and remove)'),
  76. when => 'build',
  77. }, {
  78. name => '-ss',
  79. help => N_('trust packed and unpacked original sources are same'),
  80. when => 'build',
  81. }, {
  82. name => '-sn',
  83. help => N_('there is no diff, do main tarfile only'),
  84. when => 'build',
  85. }, {
  86. name => '-sA, -sK, -sP, -sU, -sR',
  87. help => N_('like -sa, -sk, -sp, -su, -sr but may overwrite'),
  88. when => 'build',
  89. }, {
  90. name => '--abort-on-upstream-changes',
  91. help => N_('abort if generated diff has upstream files changes'),
  92. when => 'build',
  93. }, {
  94. name => '-sp',
  95. help => N_('leave original source packed in current directory'),
  96. when => 'extract',
  97. }, {
  98. name => '-su',
  99. help => N_('do not copy original source to current directory'),
  100. when => 'extract',
  101. }, {
  102. name => '-sn',
  103. help => N_('unpack original source tree too'),
  104. when => 'extract',
  105. }, {
  106. name => '--skip-debianization',
  107. help => N_('do not apply debian diff to upstream sources'),
  108. when => 'extract',
  109. },
  110. );
  111. sub describe_cmdline_options {
  112. return @module_cmdline;
  113. }
  114. sub parse_cmdline_option {
  115. my ($self, $opt) = @_;
  116. my $o = $self->{options};
  117. if ($opt =~ m/^-s([akpursnAKPUR])$/) {
  118. warning(g_('-s%s option overrides earlier -s%s option'), $1,
  119. $o->{sourcestyle}) if $o->{sourcestyle} ne 'X';
  120. $o->{sourcestyle} = $1;
  121. $o->{copy_orig_tarballs} = 0 if $1 eq 'n'; # Extract option -sn
  122. return 1;
  123. } elsif ($opt eq '--skip-debianization') {
  124. $o->{skip_debianization} = 1;
  125. return 1;
  126. } elsif ($opt eq '--ignore-bad-version') {
  127. $o->{ignore_bad_version} = 1;
  128. return 1;
  129. } elsif ($opt eq '--abort-on-upstream-changes') {
  130. $o->{abort_on_upstream_changes} = 1;
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. sub do_extract {
  136. my ($self, $newdirectory) = @_;
  137. my $sourcestyle = $self->{options}{sourcestyle};
  138. my $fields = $self->{fields};
  139. $sourcestyle =~ y/X/p/;
  140. unless ($sourcestyle =~ m/[pun]/) {
  141. usageerr(g_('source handling style -s%s not allowed with -x'),
  142. $sourcestyle);
  143. }
  144. my $dscdir = $self->{basedir};
  145. my $basename = $self->get_basename();
  146. my $basenamerev = $self->get_basename(1);
  147. # V1.0 only supports gzip compression
  148. my ($tarfile, $difffile);
  149. my $tarsign;
  150. foreach my $file ($self->get_files()) {
  151. if ($file =~ /^(?:\Q$basename\E\.orig|\Q$basenamerev\E)\.tar\.gz$/) {
  152. error(g_('multiple tarfiles in v1.0 source package')) if $tarfile;
  153. $tarfile = $file;
  154. } elsif ($file =~ /^\Q$basename\E\.orig\.tar\.gz\.asc$/) {
  155. $tarsign = $file;
  156. } elsif ($file =~ /^\Q$basenamerev\E\.diff\.gz$/) {
  157. $difffile = $file;
  158. } else {
  159. error(g_('unrecognized file for a %s source package: %s'),
  160. 'v1.0', $file);
  161. }
  162. }
  163. error(g_('no tarfile in Files field')) unless $tarfile;
  164. my $native = $difffile ? 0 : 1;
  165. if ($native and ($tarfile =~ /\.orig\.tar\.gz$/)) {
  166. warning(g_('native package with .orig.tar'));
  167. $native = 0; # V3::Native doesn't handle orig.tar
  168. }
  169. if ($native) {
  170. Dpkg::Source::Package::V3::Native::do_extract($self, $newdirectory);
  171. } else {
  172. my $expectprefix = $newdirectory;
  173. $expectprefix .= '.orig';
  174. if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
  175. error(g_('unpack target exists: %s'), $newdirectory);
  176. } else {
  177. erasedir($newdirectory);
  178. }
  179. if (-e $expectprefix) {
  180. rename($expectprefix, "$newdirectory.tmp-keep")
  181. or syserr(g_("unable to rename '%s' to '%s'"), $expectprefix,
  182. "$newdirectory.tmp-keep");
  183. }
  184. info(g_('unpacking %s'), $tarfile);
  185. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  186. $tar->extract($expectprefix);
  187. if ($sourcestyle =~ /u/) {
  188. # -su: keep .orig directory unpacked
  189. if (-e "$newdirectory.tmp-keep") {
  190. error(g_('unable to keep orig directory (already exists)'));
  191. }
  192. system('cp', '-ar', '--', $expectprefix, "$newdirectory.tmp-keep");
  193. subprocerr("cp $expectprefix to $newdirectory.tmp-keep") if $?;
  194. }
  195. rename($expectprefix, $newdirectory)
  196. or syserr(g_('failed to rename newly-extracted %s to %s'),
  197. $expectprefix, $newdirectory);
  198. # rename the copied .orig directory
  199. if (-e "$newdirectory.tmp-keep") {
  200. rename("$newdirectory.tmp-keep", $expectprefix)
  201. or syserr(g_('failed to rename saved %s to %s'),
  202. "$newdirectory.tmp-keep", $expectprefix);
  203. }
  204. }
  205. if ($difffile and not $self->{options}{skip_debianization}) {
  206. my $patch = "$dscdir$difffile";
  207. info(g_('applying %s'), $difffile);
  208. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  209. my $analysis = $patch_obj->apply($newdirectory, force_timestamp => 1);
  210. my @files = grep { ! m{^\Q$newdirectory\E/debian/} }
  211. sort keys %{$analysis->{filepatched}};
  212. info(g_('upstream files that have been modified: %s'),
  213. "\n " . join("\n ", @files)) if scalar @files;
  214. }
  215. }
  216. sub can_build {
  217. my ($self, $dir) = @_;
  218. # As long as we can use gzip, we can do it as we have
  219. # native packages as fallback
  220. return (0, g_('only supports gzip compression'))
  221. unless $self->{options}{compression} eq 'gzip';
  222. return 1;
  223. }
  224. sub do_build {
  225. my ($self, $dir) = @_;
  226. my $sourcestyle = $self->{options}{sourcestyle};
  227. my @argv = @{$self->{options}{ARGV}};
  228. my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
  229. my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
  230. if (scalar(@argv) > 1) {
  231. usageerr(g_('-b takes at most a directory and an orig source ' .
  232. 'argument (with v1.0 source package)'));
  233. }
  234. $sourcestyle =~ y/X/A/;
  235. unless ($sourcestyle =~ m/[akpursnAKPUR]/) {
  236. usageerr(g_('source handling style -s%s not allowed with -b'),
  237. $sourcestyle);
  238. }
  239. my $sourcepackage = $self->{fields}{'Source'};
  240. my $basenamerev = $self->get_basename(1);
  241. my $basename = $self->get_basename();
  242. my $basedirname = $basename;
  243. $basedirname =~ s/_/-/;
  244. # Try to find a .orig tarball for the package
  245. my $origdir = "$dir.orig";
  246. my $origtargz = $self->get_basename() . '.orig.tar.gz';
  247. if (-e $origtargz) {
  248. unless (-f $origtargz) {
  249. error(g_("packed orig '%s' exists but is not a plain file"), $origtargz);
  250. }
  251. } else {
  252. $origtargz = undef;
  253. }
  254. if (@argv) {
  255. # We have a second-argument <orig-dir> or <orig-targz>, check what it
  256. # is to decide the mode to use
  257. my $origarg = shift(@argv);
  258. if (length($origarg)) {
  259. stat($origarg)
  260. or syserr(g_('cannot stat orig argument %s'), $origarg);
  261. if (-d _) {
  262. $origdir = File::Spec->catdir($origarg);
  263. $sourcestyle =~ y/aA/rR/;
  264. unless ($sourcestyle =~ m/[ursURS]/) {
  265. error(g_('orig argument is unpacked but source handling ' .
  266. 'style -s%s calls for packed (.orig.tar.<ext>)'),
  267. $sourcestyle);
  268. }
  269. } elsif (-f _) {
  270. $origtargz = $origarg;
  271. $sourcestyle =~ y/aA/pP/;
  272. unless ($sourcestyle =~ m/[kpsKPS]/) {
  273. error(g_('orig argument is packed but source handling ' .
  274. 'style -s%s calls for unpacked (.orig/)'),
  275. $sourcestyle);
  276. }
  277. } else {
  278. error(g_('orig argument %s is not a plain file or directory'),
  279. $origarg);
  280. }
  281. } else {
  282. $sourcestyle =~ y/aA/nn/;
  283. unless ($sourcestyle =~ m/n/) {
  284. error(g_('orig argument is empty (means no orig, no diff) ' .
  285. 'but source handling style -s%s wants something'),
  286. $sourcestyle);
  287. }
  288. }
  289. } elsif ($sourcestyle =~ m/[aA]/) {
  290. # We have no explicit <orig-dir> or <orig-targz>, try to use
  291. # a .orig tarball first, then a .orig directory and fall back to
  292. # creating a native .tar.gz
  293. if ($origtargz) {
  294. $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
  295. } else {
  296. if (stat($origdir)) {
  297. unless (-d _) {
  298. error(g_("unpacked orig '%s' exists but is not a directory"),
  299. $origdir);
  300. }
  301. $sourcestyle =~ y/aA/rR/; # .orig directory
  302. } elsif ($! != ENOENT) {
  303. syserr(g_("unable to stat putative unpacked orig '%s'"), $origdir);
  304. } else {
  305. $sourcestyle =~ y/aA/nn/; # Native tar.gz
  306. }
  307. }
  308. }
  309. my ($dirname, $dirbase) = fileparse($dir);
  310. if ($dirname ne $basedirname) {
  311. warning(g_("source directory '%s' is not <sourcepackage>" .
  312. "-<upstreamversion> '%s'"), $dir, $basedirname);
  313. }
  314. my ($tarname, $tardirname, $tardirbase);
  315. my $tarsign;
  316. if ($sourcestyle ne 'n') {
  317. my ($origdirname, $origdirbase) = fileparse($origdir);
  318. if ($origdirname ne "$basedirname.orig") {
  319. warning(g_('.orig directory name %s is not <package>' .
  320. '-<upstreamversion> (wanted %s)'),
  321. $origdirname, "$basedirname.orig");
  322. }
  323. $tardirbase = $origdirbase;
  324. $tardirname = $origdirname;
  325. $tarname = $origtargz || "$basename.orig.tar.gz";
  326. $tarsign = "$tarname.asc";
  327. unless ($tarname =~ /\Q$basename\E\.orig\.tar\.gz/) {
  328. warning(g_('.orig.tar name %s is not <package>_<upstreamversion>' .
  329. '.orig.tar (wanted %s)'),
  330. $tarname, "$basename.orig.tar.gz");
  331. }
  332. }
  333. if ($sourcestyle eq 'n') {
  334. $self->{options}{ARGV} = []; # ensure we have no error
  335. Dpkg::Source::Package::V3::Native::do_build($self, $dir);
  336. } elsif ($sourcestyle =~ m/[urUR]/) {
  337. if (stat($tarname)) {
  338. unless ($sourcestyle =~ m/[UR]/) {
  339. error(g_("tarfile '%s' already exists, not overwriting, " .
  340. 'giving up; use -sU or -sR to override'), $tarname);
  341. }
  342. } elsif ($! != ENOENT) {
  343. syserr(g_("unable to check for existence of '%s'"), $tarname);
  344. }
  345. info(g_('building %s in %s'),
  346. $sourcepackage, $tarname);
  347. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  348. DIR => getcwd(), UNLINK => 0);
  349. my $tar = Dpkg::Source::Archive->new(filename => $newtar,
  350. compression => compression_guess_from_filename($tarname),
  351. compression_level => $self->{options}{comp_level});
  352. $tar->create(options => \@tar_ignore, chdir => $tardirbase);
  353. $tar->add_directory($tardirname);
  354. $tar->finish();
  355. rename($newtar, $tarname)
  356. or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
  357. $newtar, $tarname);
  358. chmod(0666 &~ umask(), $tarname)
  359. or syserr(g_("unable to change permission of '%s'"), $tarname);
  360. } else {
  361. info(g_('building %s using existing %s'),
  362. $sourcepackage, $tarname);
  363. }
  364. $self->add_file($tarname) if $tarname;
  365. # XXX: Re-enable once a stable dpkg supports extracting upstream signatures
  366. # for source 1.0 format, either in 1.17.x or 1.18.x.
  367. #$self->add_file($tarsign) if $tarsign and -e $tarsign;
  368. if ($sourcestyle =~ m/[kpKP]/) {
  369. if (stat($origdir)) {
  370. unless ($sourcestyle =~ m/[KP]/) {
  371. error(g_("orig directory '%s' already exists, not overwriting, ".
  372. 'giving up; use -sA, -sK or -sP to override'),
  373. $origdir);
  374. }
  375. push_exit_handler(sub { erasedir($origdir) });
  376. erasedir($origdir);
  377. pop_exit_handler();
  378. } elsif ($! != ENOENT) {
  379. syserr(g_("unable to check for existence of orig directory '%s'"),
  380. $origdir);
  381. }
  382. my $tar = Dpkg::Source::Archive->new(filename => $origtargz);
  383. $tar->extract($origdir);
  384. }
  385. my $ur; # Unrepresentable changes
  386. if ($sourcestyle =~ m/[kpursKPUR]/) {
  387. my $diffname = "$basenamerev.diff.gz";
  388. info(g_('building %s in %s'),
  389. $sourcepackage, $diffname);
  390. my ($ndfh, $newdiffgz) = tempfile("$diffname.new.XXXXXX",
  391. DIR => getcwd(), UNLINK => 0);
  392. push_exit_handler(sub { unlink($newdiffgz) });
  393. my $diff = Dpkg::Source::Patch->new(filename => $newdiffgz,
  394. compression => 'gzip',
  395. compression_level => $self->{options}{comp_level});
  396. $diff->create();
  397. $diff->add_diff_directory($origdir, $dir,
  398. basedirname => $basedirname,
  399. diff_ignore_regex => $diff_ignore_regex,
  400. options => []); # Force empty set of options to drop the
  401. # default -p option
  402. $diff->finish() || $ur++;
  403. pop_exit_handler();
  404. my $analysis = $diff->analyze($origdir);
  405. my @files = grep { ! m{^debian/} }
  406. map { s{^[^/]+/+}{}r }
  407. sort keys %{$analysis->{filepatched}};
  408. if (scalar @files) {
  409. warning(g_('the diff modifies the following upstream files: %s'),
  410. "\n " . join("\n ", @files));
  411. info(g_("use the '3.0 (quilt)' format to have separate and " .
  412. 'documented changes to upstream files, see dpkg-source(1)'));
  413. error(g_('aborting due to --abort-on-upstream-changes'))
  414. if $self->{options}{abort_on_upstream_changes};
  415. }
  416. rename($newdiffgz, $diffname)
  417. or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
  418. $newdiffgz, $diffname);
  419. chmod(0666 &~ umask(), $diffname)
  420. or syserr(g_("unable to change permission of '%s'"), $diffname);
  421. $self->add_file($diffname);
  422. }
  423. if ($sourcestyle =~ m/[prPR]/) {
  424. erasedir($origdir);
  425. }
  426. if ($ur) {
  427. errormsg(g_('unrepresentable changes to source'));
  428. exit(1);
  429. }
  430. }
  431. 1;