V1.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. foreach my $file ($self->get_files()) {
  150. if ($file =~ /^(?:\Q$basename\E\.orig|\Q$basenamerev\E)\.tar\.gz$/) {
  151. error(g_('multiple tarfiles in v1.0 source package')) if $tarfile;
  152. $tarfile = $file;
  153. } elsif ($file =~ /^\Q$basenamerev\E\.diff\.gz$/) {
  154. $difffile = $file;
  155. } else {
  156. error(g_('unrecognized file for a %s source package: %s'),
  157. 'v1.0', $file);
  158. }
  159. }
  160. error(g_('no tarfile in Files field')) unless $tarfile;
  161. my $native = $difffile ? 0 : 1;
  162. if ($native and ($tarfile =~ /\.orig\.tar\.gz$/)) {
  163. warning(g_('native package with .orig.tar'));
  164. $native = 0; # V3::Native doesn't handle orig.tar
  165. }
  166. if ($native) {
  167. Dpkg::Source::Package::V3::Native::do_extract($self, $newdirectory);
  168. } else {
  169. my $expectprefix = $newdirectory;
  170. $expectprefix .= '.orig';
  171. erasedir($newdirectory);
  172. if (-e $expectprefix) {
  173. rename($expectprefix, "$newdirectory.tmp-keep")
  174. or syserr(g_("unable to rename '%s' to '%s'"), $expectprefix,
  175. "$newdirectory.tmp-keep");
  176. }
  177. info(g_('unpacking %s'), $tarfile);
  178. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  179. $tar->extract($expectprefix);
  180. if ($sourcestyle =~ /u/) {
  181. # -su: keep .orig directory unpacked
  182. if (-e "$newdirectory.tmp-keep") {
  183. error(g_('unable to keep orig directory (already exists)'));
  184. }
  185. system('cp', '-ar', '--', $expectprefix, "$newdirectory.tmp-keep");
  186. subprocerr("cp $expectprefix to $newdirectory.tmp-keep") if $?;
  187. }
  188. rename($expectprefix, $newdirectory)
  189. or syserr(g_('failed to rename newly-extracted %s to %s'),
  190. $expectprefix, $newdirectory);
  191. # rename the copied .orig directory
  192. if (-e "$newdirectory.tmp-keep") {
  193. rename("$newdirectory.tmp-keep", $expectprefix)
  194. or syserr(g_('failed to rename saved %s to %s'),
  195. "$newdirectory.tmp-keep", $expectprefix);
  196. }
  197. }
  198. if ($difffile and not $self->{options}{skip_debianization}) {
  199. my $patch = "$dscdir$difffile";
  200. info(g_('applying %s'), $difffile);
  201. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  202. my $analysis = $patch_obj->apply($newdirectory, force_timestamp => 1);
  203. my @files = grep { ! m{^\Q$newdirectory\E/debian/} }
  204. sort keys %{$analysis->{filepatched}};
  205. info(g_('upstream files that have been modified: %s'),
  206. "\n " . join("\n ", @files)) if scalar @files;
  207. }
  208. }
  209. sub can_build {
  210. my ($self, $dir) = @_;
  211. # As long as we can use gzip, we can do it as we have
  212. # native packages as fallback
  213. return (0, g_('only supports gzip compression'))
  214. unless $self->{options}{compression} eq 'gzip';
  215. return 1;
  216. }
  217. sub do_build {
  218. my ($self, $dir) = @_;
  219. my $sourcestyle = $self->{options}{sourcestyle};
  220. my @argv = @{$self->{options}{ARGV}};
  221. my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
  222. my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
  223. if (scalar(@argv) > 1) {
  224. usageerr(g_('-b takes at most a directory and an orig source ' .
  225. 'argument (with v1.0 source package)'));
  226. }
  227. $sourcestyle =~ y/X/A/;
  228. unless ($sourcestyle =~ m/[akpursnAKPUR]/) {
  229. usageerr(g_('source handling style -s%s not allowed with -b'),
  230. $sourcestyle);
  231. }
  232. my $sourcepackage = $self->{fields}{'Source'};
  233. my $basenamerev = $self->get_basename(1);
  234. my $basename = $self->get_basename();
  235. my $basedirname = $basename;
  236. $basedirname =~ s/_/-/;
  237. # Try to find a .orig tarball for the package
  238. my $origdir = "$dir.orig";
  239. my $origtargz = $self->get_basename() . '.orig.tar.gz';
  240. if (-e $origtargz) {
  241. unless (-f $origtargz) {
  242. error(g_("packed orig '%s' exists but is not a plain file"), $origtargz);
  243. }
  244. } else {
  245. $origtargz = undef;
  246. }
  247. if (@argv) {
  248. # We have a second-argument <orig-dir> or <orig-targz>, check what it
  249. # is to decide the mode to use
  250. my $origarg = shift(@argv);
  251. if (length($origarg)) {
  252. stat($origarg)
  253. or syserr(g_('cannot stat orig argument %s'), $origarg);
  254. if (-d _) {
  255. $origdir = File::Spec->catdir($origarg);
  256. $sourcestyle =~ y/aA/rR/;
  257. unless ($sourcestyle =~ m/[ursURS]/) {
  258. error(g_('orig argument is unpacked but source handling ' .
  259. 'style -s%s calls for packed (.orig.tar.<ext>)'),
  260. $sourcestyle);
  261. }
  262. } elsif (-f _) {
  263. $origtargz = $origarg;
  264. $sourcestyle =~ y/aA/pP/;
  265. unless ($sourcestyle =~ m/[kpsKPS]/) {
  266. error(g_('orig argument is packed but source handling ' .
  267. 'style -s%s calls for unpacked (.orig/)'),
  268. $sourcestyle);
  269. }
  270. } else {
  271. error(g_('orig argument %s is not a plain file or directory'),
  272. $origarg);
  273. }
  274. } else {
  275. $sourcestyle =~ y/aA/nn/;
  276. unless ($sourcestyle =~ m/n/) {
  277. error(g_('orig argument is empty (means no orig, no diff) ' .
  278. 'but source handling style -s%s wants something'),
  279. $sourcestyle);
  280. }
  281. }
  282. } elsif ($sourcestyle =~ m/[aA]/) {
  283. # We have no explicit <orig-dir> or <orig-targz>, try to use
  284. # a .orig tarball first, then a .orig directory and fall back to
  285. # creating a native .tar.gz
  286. if ($origtargz) {
  287. $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
  288. } else {
  289. if (stat($origdir)) {
  290. unless (-d _) {
  291. error(g_("unpacked orig '%s' exists but is not a directory"),
  292. $origdir);
  293. }
  294. $sourcestyle =~ y/aA/rR/; # .orig directory
  295. } elsif ($! != ENOENT) {
  296. syserr(g_("unable to stat putative unpacked orig '%s'"), $origdir);
  297. } else {
  298. $sourcestyle =~ y/aA/nn/; # Native tar.gz
  299. }
  300. }
  301. }
  302. my ($dirname, $dirbase) = fileparse($dir);
  303. if ($dirname ne $basedirname) {
  304. warning(g_("source directory '%s' is not <sourcepackage>" .
  305. "-<upstreamversion> '%s'"), $dir, $basedirname);
  306. }
  307. my ($tarname, $tardirname, $tardirbase);
  308. if ($sourcestyle ne 'n') {
  309. my ($origdirname, $origdirbase) = fileparse($origdir);
  310. if ($origdirname ne "$basedirname.orig") {
  311. warning(g_('.orig directory name %s is not <package>' .
  312. '-<upstreamversion> (wanted %s)'),
  313. $origdirname, "$basedirname.orig");
  314. }
  315. $tardirbase = $origdirbase;
  316. $tardirname = $origdirname;
  317. $tarname = $origtargz || "$basename.orig.tar.gz";
  318. unless ($tarname =~ /\Q$basename\E\.orig\.tar\.gz/) {
  319. warning(g_('.orig.tar name %s is not <package>_<upstreamversion>' .
  320. '.orig.tar (wanted %s)'),
  321. $tarname, "$basename.orig.tar.gz");
  322. }
  323. }
  324. if ($sourcestyle eq 'n') {
  325. $self->{options}{ARGV} = []; # ensure we have no error
  326. Dpkg::Source::Package::V3::Native::do_build($self, $dir);
  327. } elsif ($sourcestyle =~ m/[nurUR]/) {
  328. if (stat($tarname)) {
  329. unless ($sourcestyle =~ m/[nUR]/) {
  330. error(g_("tarfile '%s' already exists, not overwriting, " .
  331. 'giving up; use -sU or -sR to override'), $tarname);
  332. }
  333. } elsif ($! != ENOENT) {
  334. syserr(g_("unable to check for existence of '%s'"), $tarname);
  335. }
  336. info(g_('building %s in %s'),
  337. $sourcepackage, $tarname);
  338. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  339. DIR => getcwd(), UNLINK => 0);
  340. my $tar = Dpkg::Source::Archive->new(filename => $newtar,
  341. compression => compression_guess_from_filename($tarname),
  342. compression_level => $self->{options}{comp_level});
  343. $tar->create(options => \@tar_ignore, chdir => $tardirbase);
  344. $tar->add_directory($tardirname);
  345. $tar->finish();
  346. rename($newtar, $tarname)
  347. or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
  348. $newtar, $tarname);
  349. chmod(0666 &~ umask(), $tarname)
  350. or syserr(g_("unable to change permission of '%s'"), $tarname);
  351. } else {
  352. info(g_('building %s using existing %s'),
  353. $sourcepackage, $tarname);
  354. }
  355. $self->add_file($tarname) if $tarname;
  356. if ($sourcestyle =~ m/[kpKP]/) {
  357. if (stat($origdir)) {
  358. unless ($sourcestyle =~ m/[KP]/) {
  359. error(g_("orig directory '%s' already exists, not overwriting, ".
  360. 'giving up; use -sA, -sK or -sP to override'),
  361. $origdir);
  362. }
  363. push_exit_handler(sub { erasedir($origdir) });
  364. erasedir($origdir);
  365. pop_exit_handler();
  366. } elsif ($! != ENOENT) {
  367. syserr(g_("unable to check for existence of orig directory '%s'"),
  368. $origdir);
  369. }
  370. my $tar = Dpkg::Source::Archive->new(filename => $origtargz);
  371. $tar->extract($origdir);
  372. }
  373. my $ur; # Unrepresentable changes
  374. if ($sourcestyle =~ m/[kpursKPUR]/) {
  375. my $diffname = "$basenamerev.diff.gz";
  376. info(g_('building %s in %s'),
  377. $sourcepackage, $diffname);
  378. my ($ndfh, $newdiffgz) = tempfile("$diffname.new.XXXXXX",
  379. DIR => getcwd(), UNLINK => 0);
  380. push_exit_handler(sub { unlink($newdiffgz) });
  381. my $diff = Dpkg::Source::Patch->new(filename => $newdiffgz,
  382. compression => 'gzip',
  383. compression_level => $self->{options}{comp_level});
  384. $diff->create();
  385. $diff->add_diff_directory($origdir, $dir,
  386. basedirname => $basedirname,
  387. diff_ignore_regex => $diff_ignore_regex,
  388. options => []); # Force empty set of options to drop the
  389. # default -p option
  390. $diff->finish() || $ur++;
  391. pop_exit_handler();
  392. my $analysis = $diff->analyze($origdir);
  393. my @files = grep { ! m{^debian/} }
  394. map { s{^[^/]+/+}{}r }
  395. sort keys %{$analysis->{filepatched}};
  396. if (scalar @files) {
  397. warning(g_('the diff modifies the following upstream files: %s'),
  398. "\n " . join("\n ", @files));
  399. info(g_("use the '3.0 (quilt)' format to have separate and " .
  400. 'documented changes to upstream files, see dpkg-source(1)'));
  401. error(g_('aborting due to --abort-on-upstream-changes'))
  402. if $self->{options}{abort_on_upstream_changes};
  403. }
  404. rename($newdiffgz, $diffname)
  405. or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
  406. $newdiffgz, $diffname);
  407. chmod(0666 &~ umask(), $diffname)
  408. or syserr(g_("unable to change permission of '%s'"), $diffname);
  409. $self->add_file($diffname);
  410. }
  411. if ($sourcestyle =~ m/[prPR]/) {
  412. erasedir($origdir);
  413. }
  414. if ($ur) {
  415. errormsg(g_('unrepresentable changes to source'));
  416. exit(1);
  417. }
  418. }
  419. 1;