V1.pm 13 KB

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