V1.pm 14 KB

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