dpkg-source.pl 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. #! /usr/bin/perl
  2. # vim: set et sw=4 ts=8
  3. use strict;
  4. use warnings;
  5. use Dpkg;
  6. use Dpkg::Gettext;
  7. use Dpkg::Checksums;
  8. use Dpkg::ErrorHandling qw(warning warnerror error failure unknown
  9. internerr syserr subprocerr usageerr
  10. $warnable_error $quiet_warnings);
  11. use Dpkg::Arch qw(debarch_eq);
  12. use Dpkg::Deps qw(@src_dep_fields %dep_field_type);
  13. use Dpkg::Fields qw(:list capit);
  14. use Dpkg::Compression;
  15. use Dpkg::Cdata;
  16. use Dpkg::Control;
  17. use Dpkg::Substvars;
  18. use Dpkg::Version qw(check_version);
  19. use Dpkg::Vars;
  20. use Dpkg::Changelog qw(parse_changelog);
  21. use Dpkg::Source::Compressor;
  22. use Dpkg::Source::Archiver;
  23. use Dpkg::Source::Patch;
  24. use Dpkg::IPC;
  25. my @filesinarchive;
  26. my %dirincluded;
  27. my %notfileobject;
  28. my $fn;
  29. my $ur;
  30. my $varlistfile;
  31. my $controlfile;
  32. my $changelogfile;
  33. my $changelogformat;
  34. our $diff_ignore_regexp = '';
  35. my $diff_ignore_default_regexp = '
  36. # Ignore general backup files
  37. (?:^|/).*~$|
  38. # Ignore emacs recovery files
  39. (?:^|/)\.#.*$|
  40. # Ignore vi swap files
  41. (?:^|/)\..*\.swp$|
  42. # Ignore baz-style junk files or directories
  43. (?:^|/),,.*(?:$|/.*$)|
  44. # File-names that should be ignored (never directories)
  45. (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore)$|
  46. # File or directory names that should be ignored
  47. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|
  48. \.shelf|_MTN|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  49. ';
  50. # Take out comments and newlines
  51. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  52. $diff_ignore_default_regexp =~ s/\n//sg;
  53. no warnings 'qw';
  54. my @tar_ignore_default_pattern = qw(
  55. *.a
  56. *.la
  57. *.o
  58. *.so
  59. *.swp
  60. *~
  61. ,,*
  62. .[#~]*
  63. .arch-ids
  64. .arch-inventory
  65. .bzr
  66. .bzr.backup
  67. .bzr.tags
  68. .bzrignore
  69. .cvsignore
  70. .deps
  71. .git
  72. .gitignore
  73. .hg
  74. .shelf
  75. .svn
  76. CVS
  77. DEADJOE
  78. RCS
  79. _MTN
  80. _darcs
  81. {arch}
  82. );
  83. my $sourcestyle = 'X';
  84. my $min_dscformat = 1;
  85. my $max_dscformat = 3;
  86. my $def_dscformat = "1.0"; # default format for -b
  87. my $expectprefix;
  88. # Compression
  89. my $compression = 'gzip';
  90. my $comp_level = '9';
  91. my $comp_ext = $comp_ext{$compression};
  92. # Packages
  93. my %remove;
  94. my %override;
  95. # Files
  96. my %checksum;
  97. my %size;
  98. my @tar_ignore;
  99. my $substvars = Dpkg::Substvars->new();
  100. use POSIX;
  101. use Fcntl qw(:mode);
  102. use English;
  103. use File::Temp qw(tempfile);
  104. textdomain("dpkg-dev");
  105. my @dsc_fields = (qw(Format Source Binary Architecture Version Origin
  106. Maintainer Uploaders Dm-Upload-Allowed Homepage
  107. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  108. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  109. @src_dep_fields,
  110. qw(Files));
  111. # Make sure patch doesn't get any funny ideas
  112. delete $ENV{'POSIXLY_CORRECT'};
  113. my @exit_handlers = ();
  114. sub exit_handler {
  115. &$_ foreach ( reverse @exit_handlers );
  116. exit(127);
  117. }
  118. $SIG{'INT'} = \&exit_handler;
  119. $SIG{'HUP'} = \&exit_handler;
  120. $SIG{'QUIT'} = \&exit_handler;
  121. sub version {
  122. printf _g("Debian %s version %s.\n"), $progname, $version;
  123. print _g("
  124. Copyright (C) 1996 Ian Jackson and Klee Dienes.");
  125. print _g("
  126. This is free software; see the GNU General Public Licence version 2 or
  127. later for copying conditions. There is NO warranty.
  128. ");
  129. }
  130. sub usage {
  131. printf _g(
  132. "Usage: %s [<option> ...] <command>
  133. Commands:
  134. -x <filename>.dsc [<output-dir>]
  135. extract source package.
  136. -b <dir> [<orig-dir>|<orig-targz>|\'\']
  137. build source package.
  138. Build options:
  139. -c<controlfile> get control info from this file.
  140. -l<changelogfile> get per-version info from this file.
  141. -F<changelogformat> force change log format.
  142. -V<name>=<value> set a substitution variable.
  143. -T<varlistfile> read variables here, not debian/substvars.
  144. -D<field>=<value> override or add a .dsc field and value.
  145. -U<field> remove a field.
  146. -E turn certain warnings into errors.
  147. -W when -E is enabled, -W disables it.
  148. -q quiet operation, do not print warnings.
  149. -i[<regexp>] filter out files to ignore diffs of
  150. (defaults to: '%s').
  151. -I[<pattern>] filter out files when building tarballs
  152. (defaults to: %s).
  153. -sa auto select orig source (-sA is default).
  154. -sk use packed orig source (unpack & keep).
  155. -sp use packed orig source (unpack & remove).
  156. -su use unpacked orig source (pack & keep).
  157. -sr use unpacked orig source (pack & remove).
  158. -ss trust packed & unpacked orig src are same.
  159. -sn there is no diff, do main tarfile only.
  160. -sA,-sK,-sP,-sU,-sR like -sa,-sk,-sp,-su,-sr but may overwrite.
  161. -Z<compression> select compression to use (defaults to 'gzip',
  162. supported are: %s).
  163. -z<level> compression level to use (defaults to '9',
  164. supported are: '1'-'9', 'best', 'fast')
  165. Extract options:
  166. -sp (default) leave orig source packed in current dir.
  167. -sn do not copy original source to current dir.
  168. -su unpack original source tree too.
  169. General options:
  170. -h, --help show this help message.
  171. --version show the version.
  172. "), $progname,
  173. $diff_ignore_default_regexp,
  174. join('', map { " -I$_" } @tar_ignore_default_pattern),
  175. "@comp_supported";
  176. }
  177. sub handleformat {
  178. my $fmt = shift;
  179. return unless $fmt =~ /^(\d+)/; # only check major version
  180. return $1 >= $min_dscformat && $1 <= $max_dscformat;
  181. }
  182. sub loadvcs {
  183. my $vcs = shift;
  184. my $mod = "Dpkg::Source::VCS::$vcs";
  185. eval qq{require $mod};
  186. return if $@;
  187. return import $mod;
  188. }
  189. my $opmode;
  190. my $tar_ignore_default_pattern_done;
  191. while (@ARGV && $ARGV[0] =~ m/^-/) {
  192. $_=shift(@ARGV);
  193. if (m/^-b$/) {
  194. &setopmode('build');
  195. } elsif (m/^-x$/) {
  196. &setopmode('extract');
  197. } elsif (m/^-Z/) {
  198. $compression = $POSTMATCH;
  199. $comp_ext = $comp_ext{$compression};
  200. usageerr(_g("%s is not a supported compression"), $compression)
  201. unless $comp_supported{$compression};
  202. Dpkg::Source::Compressor->set_default_compression($compression);
  203. } elsif (m/^-z/) {
  204. $comp_level = $POSTMATCH;
  205. usageerr(_g("%s is not a compression level"), $comp_level)
  206. unless $comp_level =~ /^([1-9]|fast|best)$/;
  207. Dpkg::Source::Compressor->set_default_compression_level($comp_level);
  208. } elsif (m/^-s([akpursnAKPUR])$/) {
  209. warning(_g("-s%s option overrides earlier -s%s option"), $1, $sourcestyle)
  210. if $sourcestyle ne 'X';
  211. $sourcestyle= $1;
  212. } elsif (m/^-c/) {
  213. $controlfile= $POSTMATCH;
  214. } elsif (m/^-l/) {
  215. $changelogfile= $POSTMATCH;
  216. } elsif (m/^-F([0-9a-z]+)$/) {
  217. $changelogformat=$1;
  218. } elsif (m/^-D([^\=:]+)[=:]/) {
  219. $override{$1}= $POSTMATCH;
  220. } elsif (m/^-U([^\=:]+)$/) {
  221. $remove{$1}= 1;
  222. } elsif (m/^-i(.*)$/) {
  223. $diff_ignore_regexp = $1 ? $1 : $diff_ignore_default_regexp;
  224. } elsif (m/^-I(.+)$/) {
  225. push @tar_ignore, "--exclude=$1";
  226. } elsif (m/^-I$/) {
  227. unless ($tar_ignore_default_pattern_done) {
  228. push @tar_ignore,
  229. map { "--exclude=$_" } @tar_ignore_default_pattern;
  230. # Prevent adding multiple times
  231. $tar_ignore_default_pattern_done = 1;
  232. }
  233. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  234. $substvars->set($1, $POSTMATCH);
  235. } elsif (m/^-T/) {
  236. $varlistfile = $POSTMATCH;
  237. warning(_g("substvars support is deprecated (see README.feature-removal-schedule)"));
  238. } elsif (m/^-(h|-help)$/) {
  239. &usage; exit(0);
  240. } elsif (m/^--version$/) {
  241. &version; exit(0);
  242. } elsif (m/^-W$/) {
  243. $warnable_error= 1;
  244. } elsif (m/^-E$/) {
  245. $warnable_error= 0;
  246. } elsif (m/^-q$/) {
  247. $quiet_warnings = 1;
  248. } elsif (m/^--$/) {
  249. last;
  250. } else {
  251. usageerr(_g("unknown option \`%s'"), $_);
  252. }
  253. }
  254. defined($opmode) || &usageerr(_g("need -x or -b"));
  255. if ($opmode eq 'build') {
  256. @ARGV || &usageerr(_g("-b needs a directory"));
  257. @ARGV<=2 || &usageerr(_g("-b takes at most a directory and an orig source argument"));
  258. my $dir = shift(@ARGV);
  259. $dir= "./$dir" unless $dir =~ m:^/:; $dir =~ s,/*$,,;
  260. stat($dir) || error(_g("cannot stat directory %s: %s"), $dir, $!);
  261. -d $dir || error(_g("directory argument %s is not a directory"), $dir);
  262. $changelogfile= "$dir/debian/changelog" unless defined($changelogfile);
  263. $controlfile= "$dir/debian/control" unless defined($controlfile);
  264. my %options = (file => $changelogfile);
  265. $options{"changelogformat"} = $changelogformat if $changelogformat;
  266. my $changelog = parse_changelog(%options);
  267. my $control = Dpkg::Control->new($controlfile);
  268. my $fields = Dpkg::Fields::Object->new();
  269. $fields->{"Format"} = $compression eq 'gzip' ? $def_dscformat : '2.0';
  270. my @sourcearch;
  271. my %archadded;
  272. my $archspecific = 0; # XXX: Not used?!
  273. my @binarypackages;
  274. # Scan control info of source package
  275. my $src_fields = $control->get_source();
  276. foreach $_ (keys %{$src_fields}) {
  277. my $v = $src_fields->{$_};
  278. if (m/^Source$/i) {
  279. set_source_package($v);
  280. } elsif (m/^(Format|Standards-Version|Origin|Maintainer|Homepage)$/i ||
  281. m/^Dm-Upload-Allowed$/i ||
  282. m/^Vcs-(Browser|Arch|Bzr|Cvs|Darcs|Git|Hg|Mtn|Svn)$/i) {
  283. $fields->{$_} = $v;
  284. } elsif (m/^Uploaders$/i) {
  285. ($fields->{$_} = $v) =~ s/[\r\n]//g; # Merge in a single-line
  286. } elsif (m/^Build-(Depends|Conflicts)(-Indep)?$/i) {
  287. my $dep;
  288. my $type = $dep_field_type{capit($_)};
  289. $dep = Dpkg::Deps::parse($v, union => $type eq 'union');
  290. error(_g("error occurred while parsing %s"), $_) unless defined $dep;
  291. my $facts = Dpkg::Deps::KnownFacts->new();
  292. $dep->simplify_deps($facts);
  293. $dep->sort();
  294. $fields->{$_} = $dep->dump();
  295. } elsif (s/^X[BC]*S[BC]*-//i) { # Include XS-* fields
  296. $fields->{$_} = $v;
  297. } elsif (m/^$control_src_field_regex$/i || m/^X[BC]+-/i) {
  298. # Silently ignore valid fields
  299. } else {
  300. unknown(_g('general section of control info file'));
  301. }
  302. }
  303. # Scan control info of binary packages
  304. foreach my $pkg ($control->get_packages()) {
  305. my $p = $pkg->{'Package'};
  306. push(@binarypackages,$p);
  307. foreach $_ (keys %{$pkg}) {
  308. my $v = $pkg->{$_};
  309. if (m/^Architecture$/) {
  310. if (debarch_eq($v, 'any')) {
  311. @sourcearch= ('any');
  312. } elsif (debarch_eq($v, 'all')) {
  313. if (!@sourcearch || $sourcearch[0] eq 'all') {
  314. @sourcearch= ('all');
  315. } else {
  316. @sourcearch= ('any');
  317. }
  318. } else {
  319. if (@sourcearch && grep($sourcearch[0] eq $_, 'any', 'all')) {
  320. @sourcearch= ('any');
  321. } else {
  322. for my $a (split(/\s+/, $v)) {
  323. error(_g("`%s' is not a legal architecture string"),
  324. $a)
  325. unless $a =~ /^[\w-]+$/;
  326. error(_g("architecture %s only allowed on its " .
  327. "own (list for package %s is `%s')"),
  328. $a, $p, $a)
  329. if grep($a eq $_, 'any','all');
  330. push(@sourcearch,$a) unless $archadded{$a}++;
  331. }
  332. }
  333. }
  334. $fields->{'Architecture'}= join(' ',@sourcearch);
  335. } elsif (s/^X[BC]*S[BC]*-//i) { # Include XS-* fields
  336. $fields->{$_} = $v;
  337. } elsif (m/^$control_pkg_field_regex$/ ||
  338. m/^X[BC]+-/i) { # Silently ignore valid fields
  339. } else {
  340. unknown(_g("package's section of control info file"));
  341. }
  342. }
  343. }
  344. # Scan fields of dpkg-parsechangelog
  345. foreach $_ (keys %{$changelog}) {
  346. my $v = $changelog->{$_};
  347. if (m/^Source$/) {
  348. set_source_package($v);
  349. } elsif (m/^Version$/) {
  350. check_version($v);
  351. $fields->{$_} = $v;
  352. } elsif (s/^X[BS]*C[BS]*-//i) {
  353. $fields->{$_} = $v;
  354. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/i ||
  355. m/^X[BS]+-/i) {
  356. } else {
  357. unknown(_g("parsed version of changelog"));
  358. }
  359. }
  360. my $vcs;
  361. if ($fields->{Format} =~ /^\s*(\d+\.\d+)\s*$/) {
  362. if ($1 >= 3.0) {
  363. error(_g("don't know how to generate %s format source package (missing vcs specifier in Format field?)"),
  364. $1);
  365. }
  366. if ($1 > 1.0) {
  367. error(_g("don't know how to generate %s format source package"),
  368. $1);
  369. }
  370. }
  371. elsif ($fields->{Format} =~ /^\s*(\d+(?:\.\d+)?)\s+\((\w+)\)\s*$/) {
  372. $fields->{Format} = $1;
  373. if ($1 < 3.0) {
  374. error(_g("control info file 'Format' field for version %s does not support vcs specifier \"%s\""),
  375. $1, $2);
  376. }
  377. if ($1 >= 4) {
  378. error(_g("unsupported control info file 'Format' value \"%s\""),
  379. $1);
  380. }
  381. $vcs = $2;
  382. loadvcs($2)
  383. || error(_g("unsupported vcs \"%s\" in control info file 'Format' field"), $2);
  384. if ($sourcestyle =~ /[akpursKPUR]/) {
  385. warning(_g("source handling style -s%s not supported when generating %s format source package"),
  386. $sourcestyle, $vcs);
  387. }
  388. $sourcestyle = 'v';
  389. }
  390. $sourcestyle =~ y/X/A/;
  391. $sourcestyle =~ m/[akpursnAKPURv]/ ||
  392. usageerr(_g("source handling style -s%s not allowed with -b"),
  393. $sourcestyle);
  394. $fields->{'Binary'}= join(', ', @binarypackages);
  395. foreach my $f (keys %override) {
  396. $fields->{$f} = $override{$f};
  397. }
  398. for my $f (qw(Version)) {
  399. defined($fields->{$f}) ||
  400. error(_g("missing information for critical output field %s"), $f);
  401. }
  402. for my $f (qw(Maintainer Architecture Standards-Version)) {
  403. defined($fields->{$f}) ||
  404. warning(_g("missing information for output field %s"), $f);
  405. }
  406. defined($sourcepackage) || &error(_g("unable to determine source package name !"));
  407. $fields->{'Source'} = $sourcepackage;
  408. for my $f (keys %remove) {
  409. delete $fields->{$f};
  410. }
  411. my $version = $fields->{'Version'};
  412. $version =~ s/^\d+://;
  413. my $upstreamversion = $version;
  414. $upstreamversion =~ s/-[^-]*$//;
  415. my $basenamerev = $sourcepackage.'_'.$version;
  416. my $basename = $sourcepackage.'_'.$upstreamversion;
  417. my $basedirname = $basename;
  418. $basedirname =~ s/_/-/;
  419. my $origdir = "$dir.orig";
  420. my $origtargz;
  421. # Try to find a .orig tarball for the package
  422. my @origtargz = map { "$basename.orig.tar.$comp_ext{$_}" } ($compression, @comp_supported);
  423. foreach my $origtar (@origtargz) {
  424. if (stat($origtar)) {
  425. -f _ || error(_g("packed orig `%s' exists but is not a plain file"),
  426. $origtar);
  427. $origtargz = $origtar;
  428. last;
  429. } elsif ($! != ENOENT) {
  430. syserr(_g("unable to stat putative packed orig `%s'"), $origtar);
  431. }
  432. }
  433. if (@ARGV) {
  434. # We have a second-argument <orig-dir> or <orig-targz>, check what it
  435. # is to decide the mode to use
  436. my $origarg = shift(@ARGV);
  437. if (length($origarg)) {
  438. stat($origarg) ||
  439. error(_g("cannot stat orig argument %s: %s"), $origarg, $!);
  440. if (-d _) {
  441. $origdir= $origarg;
  442. $origdir= "./$origdir" unless $origdir =~ m,^/,; $origdir =~ s,/*$,,;
  443. $sourcestyle =~ y/aA/rR/;
  444. $sourcestyle =~ m/[ursURS]/ ||
  445. error(_g("orig argument is unpacked but source handling " .
  446. "style -s%s calls for packed (.orig.tar.<ext>)"),
  447. $sourcestyle);
  448. } elsif (-f _) {
  449. $origtargz= $origarg;
  450. $sourcestyle =~ y/aA/pP/;
  451. $sourcestyle =~ m/[kpsKPS]/ ||
  452. error(_g("orig argument is packed but source handling " .
  453. "style -s%s calls for unpacked (.orig/)"),
  454. $sourcestyle);
  455. } else {
  456. &error("orig argument $origarg is not a plain file or directory");
  457. }
  458. } else {
  459. $sourcestyle =~ y/aA/nn/;
  460. $sourcestyle =~ m/n/ ||
  461. error(_g("orig argument is empty (means no orig, no diff) " .
  462. "but source handling style -s%s wants something"),
  463. $sourcestyle);
  464. }
  465. } elsif ($sourcestyle =~ m/[aA]/) {
  466. # We have no explicit <orig-dir> or <orig-targz>, try to use
  467. # a .orig tarball first, then a .orig directory and fall back to
  468. # creating a native .tar.gz
  469. if ($origtargz) {
  470. $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
  471. } else {
  472. if (stat($origdir)) {
  473. -d _ || error(_g("unpacked orig `%s' exists but is not a directory"),
  474. $origdir);
  475. $sourcestyle =~ y/aA/rR/; # .orig directory
  476. } elsif ($! != ENOENT) {
  477. syserr(_g("unable to stat putative unpacked orig `%s'"), $origdir);
  478. } else {
  479. $sourcestyle =~ y/aA/nn/; # Native tar.gz
  480. }
  481. }
  482. }
  483. my $dirbase = $dir;
  484. $dirbase =~ s,/?$,,;
  485. $dirbase =~ s,[^/]+$,,;
  486. my $dirname = $&;
  487. $dirname eq $basedirname ||
  488. warning(_g("source directory '%s' is not <sourcepackage>" .
  489. "-<upstreamversion> '%s'"), $dir, $basedirname);
  490. my $tarname;
  491. my $tardirname;
  492. my $tardirbase;
  493. my $origdirname;
  494. if ($sourcestyle eq 'v') {
  495. $tarname="$basenamerev.$vcs.tar.gz";
  496. $tardirbase= $dirbase; $tardirname= "$dirbase/$tarname.tmp";
  497. eval qq{Dpkg::Source::VCS::${vcs}::prep_tar(\$dir, \$tardirname)};
  498. if ($@) {
  499. failure($@);
  500. }
  501. push @exit_handlers, sub { erasedir($tardirname) };
  502. }
  503. elsif ($sourcestyle ne 'n') {
  504. my $origdirbase = $origdir;
  505. $origdirbase =~ s,/?$,,;
  506. $origdirbase =~ s,[^/]+$,,; $origdirname= $&;
  507. $origdirname eq "$basedirname.orig" ||
  508. warning(_g(".orig directory name %s is not <package>" .
  509. "-<upstreamversion> (wanted %s)"),
  510. $origdirname, "$basedirname.orig");
  511. $tardirbase= $origdirbase; $tardirname= $origdirname;
  512. $tarname= $origtargz || "$basename.orig.tar.$comp_ext";
  513. if ($tarname =~ /\Q$basename\E\.orig\.tar\.($comp_regex)/) {
  514. if (($1 ne 'gz') && ($fields->{'Format'} < 2)) { $fields->{'Format'} = '2.0' };
  515. } else {
  516. warning(_g(".orig.tar name %s is not <package>_<upstreamversion>" .
  517. ".orig.tar (wanted %s)"),
  518. $tarname, "$basename.orig.tar.$comp_regex");
  519. }
  520. } else {
  521. $tardirbase= $dirbase; $tardirname= $dirname;
  522. $tarname= "$basenamerev.tar.$comp_ext";
  523. }
  524. if ($sourcestyle =~ m/[nurURv]/) {
  525. if (stat($tarname)) {
  526. $sourcestyle =~ m/[nURv]/ ||
  527. error(_g("tarfile `%s' already exists, not overwriting, " .
  528. "giving up; use -sU or -sR to override"), $tarname);
  529. } elsif ($! != ENOENT) {
  530. syserr(_g("unable to check for existence of `%s'"), $tarname);
  531. }
  532. printf(_g("%s: building %s in %s")."\n",
  533. $progname, $sourcepackage, $tarname);
  534. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  535. DIR => getcwd(), UNLINK => 0);
  536. my $tar = Dpkg::Source::Archiver->new(filename => $newtar,
  537. compression => get_compression_from_filename($tarname),
  538. compression_level => $comp_level);
  539. $tar->create(options => \@tar_ignore);
  540. $tar->add_directory($tardirname);
  541. $tar->finish();
  542. rename($newtar, $tarname) ||
  543. syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  544. $newtar, $tarname);
  545. chmod(0666 &~ umask(), $tarname) ||
  546. syserr(_g("unable to change permission of `%s'"), $tarname);
  547. } else {
  548. printf(_g("%s: building %s using existing %s")."\n",
  549. $progname, $sourcepackage, $tarname);
  550. }
  551. addfile($fields, "$tarname");
  552. if ($sourcestyle =~ m/[kpKP]/) {
  553. if (stat($origdir)) {
  554. $sourcestyle =~ m/[KP]/ ||
  555. error(_g("orig dir `%s' already exists, not overwriting, ".
  556. "giving up; use -sA, -sK or -sP to override"),
  557. $origdir);
  558. push @exit_handlers, sub { erasedir($origdir) };
  559. erasedir($origdir);
  560. pop @exit_handlers;
  561. } elsif ($! != ENOENT) {
  562. syserr(_g("unable to check for existence of orig dir `%s'"),
  563. $origdir);
  564. }
  565. $expectprefix= $origdir; $expectprefix =~ s,^\./,,;
  566. my $tar = Dpkg::Source::Archiver->new(filename => $origtargz);
  567. $tar->extract($expectprefix);
  568. }
  569. if ($sourcestyle eq 'v') {
  570. erasedir($tardirname)
  571. }
  572. if ($sourcestyle =~ m/[kpursKPUR]/) {
  573. my $diffname = "$basenamerev.diff.$comp_ext";
  574. printf(_g("%s: building %s in %s")."\n",
  575. $progname, $sourcepackage, $diffname)
  576. || &syserr(_g("write building diff message"));
  577. my ($ndfh, $newdiffgz) = tempfile( "$diffname.new.XXXXXX",
  578. DIR => &getcwd, UNLINK => 0 );
  579. my $diff = Dpkg::Source::Patch->new(filename => $newdiffgz,
  580. compression => get_compression_from_filename($diffname));
  581. $diff->create();
  582. $diff->add_diff_directory($origdir, $dir,
  583. basedirname => $basedirname,
  584. diff_ignore_regexp => $diff_ignore_regexp);
  585. $diff->finish() || $ur++;
  586. rename($newdiffgz, $diffname) ||
  587. syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  588. $newdiffgz, $diffname);
  589. chmod(0666 &~ umask(), $diffname) ||
  590. syserr(_g("unable to change permission of `%s'"), $diffname);
  591. addfile($fields, $diffname);
  592. }
  593. if ($sourcestyle =~ m/[prPR]/) {
  594. erasedir($origdir);
  595. }
  596. printf(_g("%s: building %s in %s")."\n",
  597. $progname, $sourcepackage, "$basenamerev.dsc")
  598. || &syserr(_g("write building message"));
  599. open(DSC, ">", "$basenamerev.dsc") ||
  600. syserr(_g("create %s"), "$basenamerev.dsc");
  601. delete $fields->{'Checksums-Md5'}; # identical with Files field
  602. $substvars->parse($varlistfile) if $varlistfile && -e $varlistfile;
  603. tied(%{$fields})->set_field_importance(@dsc_fields);
  604. tied(%{$fields})->output(\*DSC, $substvars);
  605. close(DSC);
  606. if ($ur) {
  607. printf(STDERR _g("%s: unrepresentable changes to source")."\n",
  608. $progname) || syserr(_g("write error msg: %s"), $!);
  609. exit(1);
  610. }
  611. exit(0);
  612. } else { # -> opmode ne 'build'
  613. $sourcestyle =~ y/X/p/;
  614. $sourcestyle =~ m/[pun]/ ||
  615. usageerr(_g("source handling style -s%s not allowed with -x"),
  616. $sourcestyle);
  617. @ARGV>=1 || &usageerr(_g("-x needs at least one argument, the .dsc"));
  618. @ARGV<=2 || &usageerr(_g("-x takes no more than two arguments"));
  619. my $dsc = shift(@ARGV);
  620. $dsc= "./$dsc" unless $dsc =~ m:^/:;
  621. ! -d $dsc
  622. || &usageerr(_g("-x needs the .dsc file as first argument, not a directory"));
  623. my $dscdir = $dsc;
  624. $dscdir = "./$dscdir" unless $dsc =~ m,^/|^\./,;
  625. $dscdir =~ s,/[^/]+$,,;
  626. my $newdirectory;
  627. if (@ARGV) {
  628. $newdirectory= shift(@ARGV);
  629. ! -e $newdirectory || error(_g("unpack target exists: %s"), $newdirectory);
  630. }
  631. my $is_signed = 0;
  632. open(DSC, "<", $dsc) || error(_g("cannot open .dsc file %s: %s"), $dsc, $!);
  633. while (<DSC>) {
  634. next if /^\s*$/o;
  635. $is_signed = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  636. last;
  637. }
  638. close(DSC);
  639. if ($is_signed) {
  640. if (-x '/usr/bin/gpg') {
  641. my $gpg_command = 'gpg -q --verify ';
  642. if (-r '/usr/share/keyrings/debian-keyring.gpg') {
  643. $gpg_command = $gpg_command.'--keyring /usr/share/keyrings/debian-keyring.gpg ';
  644. }
  645. $gpg_command = $gpg_command.quotemeta($dsc).' 2>&1';
  646. my @gpg_output = `$gpg_command`;
  647. my $gpg_status = $? >> 8;
  648. if ($gpg_status) {
  649. print STDERR join("",@gpg_output);
  650. error(_g("failed to verify signature on %s"), $dsc)
  651. if ($gpg_status == 1);
  652. }
  653. } else {
  654. warning(_g("could not verify signature on %s since gpg isn't installed"),
  655. $dsc);
  656. }
  657. } else {
  658. warning(_g("extracting unsigned source package (%s)"), $dsc);
  659. }
  660. open(CDATA, "<", $dsc) || error(_g("cannot open .dsc file %s: %s"), $dsc, $!);
  661. my $fields = parsecdata(\*CDATA, sprintf(_g("source control file %s"), $dsc),
  662. allow_pgp => 1);
  663. close(CDATA);
  664. for my $f (qw(Source Version Files)) {
  665. defined($fields->{$f}) ||
  666. error(_g("missing critical source control field %s"), $f);
  667. }
  668. my $dscformat = $def_dscformat;
  669. if (defined $fields->{'Format'}) {
  670. if (not handleformat($fields->{'Format'})) {
  671. error(_g("Unsupported format of .dsc file (%s)"), $fields->{'Format'});
  672. }
  673. $dscformat=$fields->{'Format'};
  674. }
  675. set_source_package($fields->{'Source'});
  676. my $version = $fields->{'Version'};
  677. my $baseversion;
  678. my $revision;
  679. check_version($version);
  680. $version =~ s/^\d+://;
  681. if ($version =~ m/-([^-]+)$/) {
  682. $baseversion= $`; $revision= $1;
  683. } else {
  684. $baseversion= $version; $revision= '';
  685. }
  686. readallchecksums($fields, \%checksum, \%size);
  687. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  688. my $files = $fields->{'Files'};
  689. my @tarfiles;
  690. my $difffile;
  691. my $debianfile;
  692. my %vcsfiles;
  693. my %seen;
  694. for my $file (split(/\n /, $files)) {
  695. next if $file eq '';
  696. $file =~ m/^($check_regex{md5}) # checksum
  697. [ \t]+(\d+) # size
  698. [ \t]+($rx_fname) # filename
  699. $/x
  700. || error(_g("Files field contains bad line `%s'"), $file);
  701. (my $md5sum,$size{$3},$file) = ($1,$2,$3);
  702. if (exists($checksum{$file}{md5})
  703. and $checksum{$file}{md5} ne $md5sum) {
  704. error(_g("Conflicting checksums \`%s\' and \`%s' for file \`%s'"),
  705. $checksum{$file}{md5}, $md5sum, $file);
  706. }
  707. $checksum{$file}{md5} = $md5sum;
  708. local $_ = $file;
  709. error(_g("Files field contains invalid filename `%s'"), $file)
  710. unless s/^\Q$sourcepackage\E_\Q$baseversion\E(?=[.-])// and
  711. s/\.$comp_regex$//;
  712. s/^-\Q$revision\E(?=\.)// if length $revision;
  713. error(_g("repeated file type - files `%s' and `%s'"), $seen{$_}, $file)
  714. if $seen{$_};
  715. $seen{$_} = $file;
  716. checkstats($dscdir, $file);
  717. if (/^\.(?:orig(-\w+)?\.)?tar$/) {
  718. if ($1) { push @tarfiles, $file; } # push orig-foo.tar.gz to the end
  719. else { unshift @tarfiles, $file; }
  720. } elsif (/^\.debian\.tar$/) {
  721. $debianfile = $file;
  722. } elsif (/^\.(\w+)\.tar$/) {
  723. my $vcs=$1;
  724. # TODO try to load vcs module
  725. push @tarfiles, $file;
  726. $vcsfiles{$file}=$vcs;
  727. } elsif (/^\.diff$/) {
  728. $difffile = $file;
  729. } else {
  730. error(_g("unrecognised file type - `%s'"), $file);
  731. }
  732. }
  733. &error(_g("no tarfile in Files field")) unless @tarfiles;
  734. my $native = !($difffile || $debianfile);
  735. if ($native) {
  736. warning(_g("multiple tarfiles in native package")) if @tarfiles > 1;
  737. warning(_g("native package with .orig.tar"))
  738. unless $seen{'.tar'} or $seen{"-$revision.tar"} or %vcsfiles;
  739. } else {
  740. warning(_g("no upstream tarfile in Files field"))
  741. unless $seen{'.orig.tar'} or %vcsfiles;
  742. if ($dscformat =~ /^1\./) {
  743. warning(_g("multiple upstream tarballs in %s format dsc"), $dscformat)
  744. if @tarfiles > 1;
  745. warning(_g("debian.tar in %s format dsc"), $dscformat)
  746. if $debianfile;
  747. }
  748. }
  749. if (%vcsfiles && $dscformat !~ /^3\./) {
  750. warning(sprintf(_g("<rc>.tar file in %s format dsc"), $dscformat));
  751. }
  752. $newdirectory = $sourcepackage.'-'.$baseversion unless defined($newdirectory);
  753. $expectprefix = $newdirectory;
  754. $expectprefix .= '.orig' if $difffile || $debianfile;
  755. printf(_g("%s: extracting %s in %s")."\n",
  756. $progname, $sourcepackage, $newdirectory)
  757. || &syserr(_g("write extracting message"));
  758. &erasedir($newdirectory);
  759. ! -e "$expectprefix"
  760. || rename("$expectprefix","$newdirectory.tmp-keep")
  761. || syserr(_g("unable to rename `%s' to `%s'"), $expectprefix, "$newdirectory.tmp-keep");
  762. push @tarfiles, $debianfile if $debianfile;
  763. for my $tarfile (@tarfiles)
  764. {
  765. my $target;
  766. if ($tarfile =~ /\.orig-(\w+)\.tar/) {
  767. my $sub = $1;
  768. $sub =~ s/\d+$// if $sub =~ /\D/;
  769. $target = "$expectprefix/$sub";
  770. } elsif ($tarfile =~ /\.debian\.tar/) {
  771. $target = "$expectprefix/debian";
  772. } else {
  773. $target = $expectprefix;
  774. }
  775. printf(_g("%s: unpacking %s")."\n", $progname, $tarfile);
  776. my $tar = Dpkg::Source::Archiver->new(filename => "$dscdir/$tarfile");
  777. $tar->extract($target);
  778. # for the first tar file:
  779. if ($tarfile eq $tarfiles[0] and !$native)
  780. {
  781. # -sp: copy the .orig.tar.gz if required
  782. if ($sourcestyle =~ /p/) {
  783. stat("$dscdir/$tarfile") ||
  784. syserr(_g("failed to stat `%s' to see if need to copy"),
  785. "$dscdir/$tarfile");
  786. my ($dsctardev, $dsctarino) = stat _;
  787. my $copy_required;
  788. if (stat($tarfile)) {
  789. my ($dumptardev, $dumptarino) = stat _;
  790. $copy_required = ($dumptardev != $dsctardev ||
  791. $dumptarino != $dsctarino);
  792. } else {
  793. $! == ENOENT ||
  794. syserr(_g("failed to check destination `%s' " .
  795. "to see if need to copy"), $tarfile);
  796. $copy_required = 1;
  797. }
  798. if ($copy_required) {
  799. system('cp','--',"$dscdir/$tarfile", $tarfile);
  800. $? && subprocerr("cp $dscdir/$tarfile to $tarfile");
  801. }
  802. }
  803. # -su: keep .orig directory unpacked
  804. elsif ($sourcestyle =~ /u/ and $expectprefix ne $newdirectory) {
  805. ! -e "$newdirectory.tmp-keep"
  806. || &error(_g("unable to keep orig directory (already exists)"));
  807. system('cp','-ar','--',$expectprefix,"$newdirectory.tmp-keep");
  808. $? && subprocerr("cp $expectprefix to $newdirectory.tmp-keep");
  809. }
  810. }
  811. if (exists $vcsfiles{$tarfile}) {
  812. printf(_g("%s: extracting source from %s repository")."\n", $progname, $vcsfiles{$tarfile});
  813. loadvcs($vcsfiles{$tarfile})
  814. || error(sprintf(_g("unsupported vcs \"%s\""), $vcsfiles{$tarfile}));
  815. eval qq{Dpkg::Source::VCS::$vcsfiles{$tarfile}::post_unpack_tar(\$target)};
  816. if ($@) {
  817. &syserr($@);
  818. }
  819. }
  820. }
  821. my @patches;
  822. push @patches, "$dscdir/$difffile" if $difffile;
  823. if ($debianfile and -d (my $pd = "$expectprefix/debian/patches"))
  824. {
  825. my @p;
  826. opendir D, $pd;
  827. while (defined ($_ = readdir D))
  828. {
  829. # patches match same rules as run-parts
  830. next unless /^[\w-]+$/ and -f "$pd/$_";
  831. push @p, $_;
  832. }
  833. closedir D;
  834. push @patches, map "$newdirectory/debian/patches/$_", sort @p;
  835. }
  836. if ($newdirectory ne $expectprefix)
  837. {
  838. rename($expectprefix,$newdirectory) ||
  839. syserr(_g("failed to rename newly-extracted %s to %s"),
  840. $expectprefix, $newdirectory);
  841. # rename the copied .orig directory
  842. ! -e "$newdirectory.tmp-keep"
  843. || rename("$newdirectory.tmp-keep",$expectprefix)
  844. || syserr(_g("failed to rename saved %s to %s"),
  845. "$newdirectory.tmp-keep", $expectprefix);
  846. }
  847. my $now = time;
  848. for my $patch (@patches) {
  849. printf(_g("%s: applying %s")."\n", $progname, $patch);
  850. my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
  851. $patch_obj->apply($newdirectory, timestamp => $now);
  852. }
  853. if (!(my @s = lstat("$newdirectory/debian/rules"))) {
  854. $! == ENOENT || syserr(_g("cannot stat %s"), "$newdirectory/debian/rules");
  855. warning(_g("%s does not exist"), "$newdirectory/debian/rules");
  856. } elsif (-f _) {
  857. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  858. syserr(_g("cannot make %s executable"), "$newdirectory/debian/rules");
  859. } else {
  860. warning(_g("%s is not a plain file"), "$newdirectory/debian/rules");
  861. }
  862. exit(0);
  863. }
  864. sub checkstats {
  865. my ($dscdir, $f) = @_;
  866. getchecksums("$dscdir/$f", $checksum{$f}, \$size{$f});
  867. }
  868. sub erasedir {
  869. my ($dir) = @_;
  870. if (!lstat($dir)) {
  871. $! == ENOENT && return;
  872. syserr(_g("cannot stat directory %s (before removal)"), $dir);
  873. }
  874. system 'rm','-rf','--',$dir;
  875. $? && subprocerr("rm -rf $dir");
  876. if (!stat($dir)) {
  877. $! == ENOENT && return;
  878. syserr(_g("unable to check for removal of dir `%s'"), $dir);
  879. }
  880. failure(_g("rm -rf failed to remove `%s'"), $dir);
  881. }
  882. sub setopmode {
  883. defined($opmode) && &usageerr(_g("only one of -x or -b allowed, and only once"));
  884. $opmode= $_[0];
  885. }
  886. my %added_files;
  887. sub addfile {
  888. my ($fields, $filename)= @_;
  889. $added_files{$filename}++ &&
  890. internerr(_g("tried to add file `%s' twice"), $filename);
  891. my (%sums, $size);
  892. getchecksums($filename, \%sums, \$size);
  893. foreach my $alg (sort keys %sums) {
  894. $fields->{"Checksums-$alg"} .= "\n $sums{$alg} $size $filename";
  895. }
  896. $fields->{'Files'}.= "\n $sums{md5} $size $filename";
  897. }