V1.pm 13 KB

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