V1.pm 13 KB

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