V1.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. sub parse_cmdline_option {
  57. my ($self, $opt) = @_;
  58. my $o = $self->{options};
  59. if ($opt =~ m/^-s([akpursnAKPUR])$/) {
  60. warning(g_('-s%s option overrides earlier -s%s option'), $1,
  61. $o->{sourcestyle}) if $o->{sourcestyle} ne 'X';
  62. $o->{sourcestyle} = $1;
  63. $o->{copy_orig_tarballs} = 0 if $1 eq 'n'; # Extract option -sn
  64. return 1;
  65. } elsif ($opt eq '--skip-debianization') {
  66. $o->{skip_debianization} = 1;
  67. return 1;
  68. } elsif ($opt eq '--ignore-bad-version') {
  69. $o->{ignore_bad_version} = 1;
  70. return 1;
  71. } elsif ($opt eq '--abort-on-upstream-changes') {
  72. $o->{abort_on_upstream_changes} = 1;
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. sub do_extract {
  78. my ($self, $newdirectory) = @_;
  79. my $sourcestyle = $self->{options}{sourcestyle};
  80. my $fields = $self->{fields};
  81. $sourcestyle =~ y/X/p/;
  82. unless ($sourcestyle =~ m/[pun]/) {
  83. usageerr(g_('source handling style -s%s not allowed with -x'),
  84. $sourcestyle);
  85. }
  86. my $dscdir = $self->{basedir};
  87. my $basename = $self->get_basename();
  88. my $basenamerev = $self->get_basename(1);
  89. # V1.0 only supports gzip compression
  90. my ($tarfile, $difffile);
  91. foreach my $file ($self->get_files()) {
  92. if ($file =~ /^(?:\Q$basename\E\.orig|\Q$basenamerev\E)\.tar\.gz$/) {
  93. error(g_('multiple tarfiles in v1.0 source package')) if $tarfile;
  94. $tarfile = $file;
  95. } elsif ($file =~ /^\Q$basenamerev\E\.diff\.gz$/) {
  96. $difffile = $file;
  97. } else {
  98. error(g_('unrecognized file for a %s source package: %s'),
  99. 'v1.0', $file);
  100. }
  101. }
  102. error(g_('no tarfile in Files field')) unless $tarfile;
  103. my $native = $difffile ? 0 : 1;
  104. if ($native and ($tarfile =~ /\.orig\.tar\.gz$/)) {
  105. warning(g_('native package with .orig.tar'));
  106. $native = 0; # V3::Native doesn't handle orig.tar
  107. }
  108. if ($native) {
  109. Dpkg::Source::Package::V3::Native::do_extract($self, $newdirectory);
  110. } else {
  111. my $expectprefix = $newdirectory;
  112. $expectprefix .= '.orig';
  113. erasedir($newdirectory);
  114. if (-e $expectprefix) {
  115. rename($expectprefix, "$newdirectory.tmp-keep")
  116. or syserr(g_("unable to rename '%s' to '%s'"), $expectprefix,
  117. "$newdirectory.tmp-keep");
  118. }
  119. info(g_('unpacking %s'), $tarfile);
  120. my $tar = Dpkg::Source::Archive->new(filename => "$dscdir$tarfile");
  121. $tar->extract($expectprefix);
  122. if ($sourcestyle =~ /u/) {
  123. # -su: keep .orig directory unpacked
  124. if (-e "$newdirectory.tmp-keep") {
  125. error(g_('unable to keep orig directory (already exists)'));
  126. }
  127. system('cp', '-ar', '--', $expectprefix, "$newdirectory.tmp-keep");
  128. subprocerr("cp $expectprefix to $newdirectory.tmp-keep") if $?;
  129. }
  130. rename($expectprefix, $newdirectory)
  131. or syserr(g_('failed to rename newly-extracted %s to %s'),
  132. $expectprefix, $newdirectory);
  133. # rename the copied .orig directory
  134. if (-e "$newdirectory.tmp-keep") {
  135. rename("$newdirectory.tmp-keep", $expectprefix)
  136. or syserr(g_('failed to rename saved %s to %s'),
  137. "$newdirectory.tmp-keep", $expectprefix);
  138. }
  139. }
  140. if ($difffile and not $self->{options}{skip_debianization}) {
  141. my $patch = "$dscdir$difffile";
  142. info(g_('applying %s'), $difffile);
  143. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  144. my $analysis = $patch_obj->apply($newdirectory, force_timestamp => 1);
  145. my @files = grep { ! m{^\Q$newdirectory\E/debian/} }
  146. sort keys %{$analysis->{filepatched}};
  147. info(g_('upstream files that have been modified: %s'),
  148. "\n " . join("\n ", @files)) if scalar @files;
  149. }
  150. }
  151. sub can_build {
  152. my ($self, $dir) = @_;
  153. # As long as we can use gzip, we can do it as we have
  154. # native packages as fallback
  155. return (0, g_('only supports gzip compression'))
  156. unless $self->{options}{compression} eq 'gzip';
  157. return 1;
  158. }
  159. sub do_build {
  160. my ($self, $dir) = @_;
  161. my $sourcestyle = $self->{options}{sourcestyle};
  162. my @argv = @{$self->{options}{ARGV}};
  163. my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
  164. my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
  165. if (scalar(@argv) > 1) {
  166. usageerr(g_('-b takes at most a directory and an orig source ' .
  167. 'argument (with v1.0 source package)'));
  168. }
  169. $sourcestyle =~ y/X/A/;
  170. unless ($sourcestyle =~ m/[akpursnAKPUR]/) {
  171. usageerr(g_('source handling style -s%s not allowed with -b'),
  172. $sourcestyle);
  173. }
  174. my $sourcepackage = $self->{fields}{'Source'};
  175. my $basenamerev = $self->get_basename(1);
  176. my $basename = $self->get_basename();
  177. my $basedirname = $basename;
  178. $basedirname =~ s/_/-/;
  179. # Try to find a .orig tarball for the package
  180. my $origdir = "$dir.orig";
  181. my $origtargz = $self->get_basename() . '.orig.tar.gz';
  182. if (-e $origtargz) {
  183. unless (-f $origtargz) {
  184. error(g_("packed orig '%s' exists but is not a plain file"), $origtargz);
  185. }
  186. } else {
  187. $origtargz = undef;
  188. }
  189. if (@argv) {
  190. # We have a second-argument <orig-dir> or <orig-targz>, check what it
  191. # is to decide the mode to use
  192. my $origarg = shift(@argv);
  193. if (length($origarg)) {
  194. stat($origarg)
  195. or syserr(g_('cannot stat orig argument %s'), $origarg);
  196. if (-d _) {
  197. $origdir = File::Spec->catdir($origarg);
  198. $sourcestyle =~ y/aA/rR/;
  199. unless ($sourcestyle =~ m/[ursURS]/) {
  200. error(g_('orig argument is unpacked but source handling ' .
  201. 'style -s%s calls for packed (.orig.tar.<ext>)'),
  202. $sourcestyle);
  203. }
  204. } elsif (-f _) {
  205. $origtargz = $origarg;
  206. $sourcestyle =~ y/aA/pP/;
  207. unless ($sourcestyle =~ m/[kpsKPS]/) {
  208. error(g_('orig argument is packed but source handling ' .
  209. 'style -s%s calls for unpacked (.orig/)'),
  210. $sourcestyle);
  211. }
  212. } else {
  213. error(g_('orig argument %s is not a plain file or directory'),
  214. $origarg);
  215. }
  216. } else {
  217. $sourcestyle =~ y/aA/nn/;
  218. unless ($sourcestyle =~ m/n/) {
  219. error(g_('orig argument is empty (means no orig, no diff) ' .
  220. 'but source handling style -s%s wants something'),
  221. $sourcestyle);
  222. }
  223. }
  224. } elsif ($sourcestyle =~ m/[aA]/) {
  225. # We have no explicit <orig-dir> or <orig-targz>, try to use
  226. # a .orig tarball first, then a .orig directory and fall back to
  227. # creating a native .tar.gz
  228. if ($origtargz) {
  229. $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
  230. } else {
  231. if (stat($origdir)) {
  232. unless (-d _) {
  233. error(g_("unpacked orig '%s' exists but is not a directory"),
  234. $origdir);
  235. }
  236. $sourcestyle =~ y/aA/rR/; # .orig directory
  237. } elsif ($! != ENOENT) {
  238. syserr(g_("unable to stat putative unpacked orig '%s'"), $origdir);
  239. } else {
  240. $sourcestyle =~ y/aA/nn/; # Native tar.gz
  241. }
  242. }
  243. }
  244. my ($dirname, $dirbase) = fileparse($dir);
  245. if ($dirname ne $basedirname) {
  246. warning(g_("source directory '%s' is not <sourcepackage>" .
  247. "-<upstreamversion> '%s'"), $dir, $basedirname);
  248. }
  249. my ($tarname, $tardirname, $tardirbase);
  250. if ($sourcestyle ne 'n') {
  251. my ($origdirname, $origdirbase) = fileparse($origdir);
  252. if ($origdirname ne "$basedirname.orig") {
  253. warning(g_('.orig directory name %s is not <package>' .
  254. '-<upstreamversion> (wanted %s)'),
  255. $origdirname, "$basedirname.orig");
  256. }
  257. $tardirbase = $origdirbase;
  258. $tardirname = $origdirname;
  259. $tarname = $origtargz || "$basename.orig.tar.gz";
  260. unless ($tarname =~ /\Q$basename\E\.orig\.tar\.gz/) {
  261. warning(g_('.orig.tar name %s is not <package>_<upstreamversion>' .
  262. '.orig.tar (wanted %s)'),
  263. $tarname, "$basename.orig.tar.gz");
  264. }
  265. }
  266. if ($sourcestyle eq 'n') {
  267. $self->{options}{ARGV} = []; # ensure we have no error
  268. Dpkg::Source::Package::V3::Native::do_build($self, $dir);
  269. } elsif ($sourcestyle =~ m/[nurUR]/) {
  270. if (stat($tarname)) {
  271. unless ($sourcestyle =~ m/[nUR]/) {
  272. error(g_("tarfile '%s' already exists, not overwriting, " .
  273. 'giving up; use -sU or -sR to override'), $tarname);
  274. }
  275. } elsif ($! != ENOENT) {
  276. syserr(g_("unable to check for existence of '%s'"), $tarname);
  277. }
  278. info(g_('building %s in %s'),
  279. $sourcepackage, $tarname);
  280. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  281. DIR => getcwd(), UNLINK => 0);
  282. my $tar = Dpkg::Source::Archive->new(filename => $newtar,
  283. compression => compression_guess_from_filename($tarname),
  284. compression_level => $self->{options}{comp_level});
  285. $tar->create(options => \@tar_ignore, chdir => $tardirbase);
  286. $tar->add_directory($tardirname);
  287. $tar->finish();
  288. rename($newtar, $tarname)
  289. or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
  290. $newtar, $tarname);
  291. chmod(0666 &~ umask(), $tarname)
  292. or syserr(g_("unable to change permission of '%s'"), $tarname);
  293. } else {
  294. info(g_('building %s using existing %s'),
  295. $sourcepackage, $tarname);
  296. }
  297. $self->add_file($tarname) if $tarname;
  298. if ($sourcestyle =~ m/[kpKP]/) {
  299. if (stat($origdir)) {
  300. unless ($sourcestyle =~ m/[KP]/) {
  301. error(g_("orig directory '%s' already exists, not overwriting, ".
  302. 'giving up; use -sA, -sK or -sP to override'),
  303. $origdir);
  304. }
  305. push_exit_handler(sub { erasedir($origdir) });
  306. erasedir($origdir);
  307. pop_exit_handler();
  308. } elsif ($! != ENOENT) {
  309. syserr(g_("unable to check for existence of orig directory '%s'"),
  310. $origdir);
  311. }
  312. my $tar = Dpkg::Source::Archive->new(filename => $origtargz);
  313. $tar->extract($origdir);
  314. }
  315. my $ur; # Unrepresentable changes
  316. if ($sourcestyle =~ m/[kpursKPUR]/) {
  317. my $diffname = "$basenamerev.diff.gz";
  318. info(g_('building %s in %s'),
  319. $sourcepackage, $diffname);
  320. my ($ndfh, $newdiffgz) = tempfile("$diffname.new.XXXXXX",
  321. DIR => getcwd(), UNLINK => 0);
  322. push_exit_handler(sub { unlink($newdiffgz) });
  323. my $diff = Dpkg::Source::Patch->new(filename => $newdiffgz,
  324. compression => 'gzip',
  325. compression_level => $self->{options}{comp_level});
  326. $diff->create();
  327. $diff->add_diff_directory($origdir, $dir,
  328. basedirname => $basedirname,
  329. diff_ignore_regex => $diff_ignore_regex,
  330. options => []); # Force empty set of options to drop the
  331. # default -p option
  332. $diff->finish() || $ur++;
  333. pop_exit_handler();
  334. my $analysis = $diff->analyze($origdir);
  335. my @files = grep { ! m{^debian/} }
  336. map { my $file = $_; $file =~ s{^[^/]+/+}{}; $file }
  337. sort keys %{$analysis->{filepatched}};
  338. if (scalar @files) {
  339. warning(g_('the diff modifies the following upstream files: %s'),
  340. "\n " . join("\n ", @files));
  341. info(g_("use the '3.0 (quilt)' format to have separate and " .
  342. 'documented changes to upstream files, see dpkg-source(1)'));
  343. error(g_('aborting due to --abort-on-upstream-changes'))
  344. if $self->{options}{abort_on_upstream_changes};
  345. }
  346. rename($newdiffgz, $diffname)
  347. or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
  348. $newdiffgz, $diffname);
  349. chmod(0666 &~ umask(), $diffname)
  350. or syserr(g_("unable to change permission of '%s'"), $diffname);
  351. $self->add_file($diffname);
  352. }
  353. if ($sourcestyle =~ m/[prPR]/) {
  354. erasedir($origdir);
  355. }
  356. if ($ur) {
  357. printf { *STDERR } g_('%s: unrepresentable changes to source') . "\n",
  358. $Dpkg::PROGNAME;
  359. exit(1);
  360. }
  361. }
  362. 1;