V1.pm 12 KB

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