V1.pm 14 KB

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