dpkg-source.pl 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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::IPC;
  24. my @filesinarchive;
  25. my %dirincluded;
  26. my %notfileobject;
  27. my $fn;
  28. my $ur;
  29. my $varlistfile;
  30. my $controlfile;
  31. my $changelogfile;
  32. my $changelogformat;
  33. our $diff_ignore_regexp = '';
  34. my $diff_ignore_default_regexp = '
  35. # Ignore general backup files
  36. (?:^|/).*~$|
  37. # Ignore emacs recovery files
  38. (?:^|/)\.#.*$|
  39. # Ignore vi swap files
  40. (?:^|/)\..*\.swp$|
  41. # Ignore baz-style junk files or directories
  42. (?:^|/),,.*(?:$|/.*$)|
  43. # File-names that should be ignored (never directories)
  44. (?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore)$|
  45. # File or directory names that should be ignored
  46. (?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|
  47. \.shelf|_MTN|\.bzr(?:\.backup|tags)?)(?:$|/.*$)
  48. ';
  49. # Take out comments and newlines
  50. $diff_ignore_default_regexp =~ s/^#.*$//mg;
  51. $diff_ignore_default_regexp =~ s/\n//sg;
  52. no warnings 'qw';
  53. my @tar_ignore_default_pattern = qw(
  54. *.a
  55. *.la
  56. *.o
  57. *.so
  58. *.swp
  59. *~
  60. ,,*
  61. .[#~]*
  62. .arch-ids
  63. .arch-inventory
  64. .bzr
  65. .bzr.backup
  66. .bzr.tags
  67. .bzrignore
  68. .cvsignore
  69. .deps
  70. .git
  71. .gitignore
  72. .hg
  73. .shelf
  74. .svn
  75. CVS
  76. DEADJOE
  77. RCS
  78. _MTN
  79. _darcs
  80. {arch}
  81. );
  82. my $sourcestyle = 'X';
  83. my $min_dscformat = 1;
  84. my $max_dscformat = 3;
  85. my $def_dscformat = "1.0"; # default format for -b
  86. my $expectprefix;
  87. # Compression
  88. my $compression = 'gzip';
  89. my $comp_level = '9';
  90. my $comp_ext = $comp_ext{$compression};
  91. # Packages
  92. my %remove;
  93. my %override;
  94. # Files
  95. my %checksum;
  96. my %size;
  97. my %type; # used by checktype
  98. my %filepatched; # used by checkdiff
  99. my %dirtocreate; # used by checkdiff
  100. my @tar_ignore;
  101. my $substvars = Dpkg::Substvars->new();
  102. use POSIX;
  103. use Fcntl qw (:mode);
  104. use English;
  105. use File::Temp qw (tempfile);
  106. textdomain("dpkg-dev");
  107. my @dsc_fields = (qw(Format Source Binary Architecture Version Origin
  108. Maintainer Uploaders Dm-Upload-Allowed Homepage
  109. Standards-Version Vcs-Browser Vcs-Arch Vcs-Bzr
  110. Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn Vcs-Svn),
  111. @src_dep_fields,
  112. qw(Files));
  113. # Make sure patch doesn't get any funny ideas
  114. delete $ENV{'POSIXLY_CORRECT'};
  115. my @exit_handlers = ();
  116. sub exit_handler {
  117. &$_ foreach ( reverse @exit_handlers );
  118. exit(127);
  119. }
  120. $SIG{'INT'} = \&exit_handler;
  121. $SIG{'HUP'} = \&exit_handler;
  122. $SIG{'QUIT'} = \&exit_handler;
  123. sub version {
  124. printf _g("Debian %s version %s.\n"), $progname, $version;
  125. print _g("
  126. Copyright (C) 1996 Ian Jackson and Klee Dienes.");
  127. print _g("
  128. This is free software; see the GNU General Public Licence version 2 or
  129. later for copying conditions. There is NO warranty.
  130. ");
  131. }
  132. sub usage {
  133. printf _g(
  134. "Usage: %s [<option> ...] <command>
  135. Commands:
  136. -x <filename>.dsc [<output-dir>]
  137. extract source package.
  138. -b <dir> [<orig-dir>|<orig-targz>|\'\']
  139. build source package.
  140. Build options:
  141. -c<controlfile> get control info from this file.
  142. -l<changelogfile> get per-version info from this file.
  143. -F<changelogformat> force change log format.
  144. -V<name>=<value> set a substitution variable.
  145. -T<varlistfile> read variables here, not debian/substvars.
  146. -D<field>=<value> override or add a .dsc field and value.
  147. -U<field> remove a field.
  148. -E turn certain warnings into errors.
  149. -W when -E is enabled, -W disables it.
  150. -q quiet operation, do not print warnings.
  151. -i[<regexp>] filter out files to ignore diffs of
  152. (defaults to: '%s').
  153. -I[<pattern>] filter out files when building tarballs
  154. (defaults to: %s).
  155. -sa auto select orig source (-sA is default).
  156. -sk use packed orig source (unpack & keep).
  157. -sp use packed orig source (unpack & remove).
  158. -su use unpacked orig source (pack & keep).
  159. -sr use unpacked orig source (pack & remove).
  160. -ss trust packed & unpacked orig src are same.
  161. -sn there is no diff, do main tarfile only.
  162. -sA,-sK,-sP,-sU,-sR like -sa,-sk,-sp,-su,-sr but may overwrite.
  163. -Z<compression> select compression to use (defaults to 'gzip',
  164. supported are: %s).
  165. -z<level> compression level to use (defaults to '9',
  166. supported are: '1'-'9', 'best', 'fast')
  167. Extract options:
  168. -sp (default) leave orig source packed in current dir.
  169. -sn do not copy original source to current dir.
  170. -su unpack original source tree too.
  171. General options:
  172. -h, --help show this help message.
  173. --version show the version.
  174. "), $progname,
  175. $diff_ignore_default_regexp,
  176. join('', map { " -I$_" } @tar_ignore_default_pattern),
  177. "@comp_supported";
  178. }
  179. sub handleformat {
  180. my $fmt = shift;
  181. return unless $fmt =~ /^(\d+)/; # only check major version
  182. return $1 >= $min_dscformat && $1 <= $max_dscformat;
  183. }
  184. sub loadvcs {
  185. my $vcs = shift;
  186. my $mod = "Dpkg::Source::VCS::$vcs";
  187. eval qq{require $mod};
  188. return if $@;
  189. return import $mod;
  190. }
  191. my $opmode;
  192. my $tar_ignore_default_pattern_done;
  193. while (@ARGV && $ARGV[0] =~ m/^-/) {
  194. $_=shift(@ARGV);
  195. if (m/^-b$/) {
  196. &setopmode('build');
  197. } elsif (m/^-x$/) {
  198. &setopmode('extract');
  199. } elsif (m/^-Z/) {
  200. $compression = $POSTMATCH;
  201. $comp_ext = $comp_ext{$compression};
  202. usageerr(_g("%s is not a supported compression"), $compression)
  203. unless $comp_supported{$compression};
  204. Dpkg::Source::Compressor->set_default_compression($compression);
  205. } elsif (m/^-z/) {
  206. $comp_level = $POSTMATCH;
  207. usageerr(_g("%s is not a compression level"), $comp_level)
  208. unless $comp_level =~ /^([1-9]|fast|best)$/;
  209. Dpkg::Source::Compressor->set_default_compression_level($comp_level);
  210. } elsif (m/^-s([akpursnAKPUR])$/) {
  211. warning(_g("-s%s option overrides earlier -s%s option"), $1, $sourcestyle)
  212. if $sourcestyle ne 'X';
  213. $sourcestyle= $1;
  214. } elsif (m/^-c/) {
  215. $controlfile= $POSTMATCH;
  216. } elsif (m/^-l/) {
  217. $changelogfile= $POSTMATCH;
  218. } elsif (m/^-F([0-9a-z]+)$/) {
  219. $changelogformat=$1;
  220. } elsif (m/^-D([^\=:]+)[=:]/) {
  221. $override{$1}= $POSTMATCH;
  222. } elsif (m/^-U([^\=:]+)$/) {
  223. $remove{$1}= 1;
  224. } elsif (m/^-i(.*)$/) {
  225. $diff_ignore_regexp = $1 ? $1 : $diff_ignore_default_regexp;
  226. } elsif (m/^-I(.+)$/) {
  227. push @tar_ignore, "--exclude=$1";
  228. } elsif (m/^-I$/) {
  229. unless ($tar_ignore_default_pattern_done) {
  230. push @tar_ignore,
  231. map { "--exclude=$_" } @tar_ignore_default_pattern;
  232. # Prevent adding multiple times
  233. $tar_ignore_default_pattern_done = 1;
  234. }
  235. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  236. $substvars->set($1, $POSTMATCH);
  237. } elsif (m/^-T/) {
  238. $varlistfile = $POSTMATCH;
  239. warning(_g("substvars support is deprecated (see README.feature-removal-schedule)"));
  240. } elsif (m/^-(h|-help)$/) {
  241. &usage; exit(0);
  242. } elsif (m/^--version$/) {
  243. &version; exit(0);
  244. } elsif (m/^-W$/) {
  245. $warnable_error= 1;
  246. } elsif (m/^-E$/) {
  247. $warnable_error= 0;
  248. } elsif (m/^-q$/) {
  249. $quiet_warnings = 1;
  250. } elsif (m/^--$/) {
  251. last;
  252. } else {
  253. usageerr(_g("unknown option \`%s'"), $_);
  254. }
  255. }
  256. defined($opmode) || &usageerr(_g("need -x or -b"));
  257. if ($opmode eq 'build') {
  258. @ARGV || &usageerr(_g("-b needs a directory"));
  259. @ARGV<=2 || &usageerr(_g("-b takes at most a directory and an orig source argument"));
  260. my $dir = shift(@ARGV);
  261. $dir= "./$dir" unless $dir =~ m:^/:; $dir =~ s,/*$,,;
  262. stat($dir) || error(_g("cannot stat directory %s: %s"), $dir, $!);
  263. -d $dir || error(_g("directory argument %s is not a directory"), $dir);
  264. $changelogfile= "$dir/debian/changelog" unless defined($changelogfile);
  265. $controlfile= "$dir/debian/control" unless defined($controlfile);
  266. my %options = (file => $changelogfile);
  267. $options{"changelogformat"} = $changelogformat if $changelogformat;
  268. my $changelog = parse_changelog(%options);
  269. my $control = Dpkg::Control->new($controlfile);
  270. my $fields = Dpkg::Fields::Object->new();
  271. $fields->{"Format"} = $compression eq 'gzip' ? $def_dscformat : '2.0';
  272. my @sourcearch;
  273. my %archadded;
  274. my $archspecific = 0; # XXX: Not used?!
  275. my @binarypackages;
  276. # Scan control info of source package
  277. my $src_fields = $control->get_source();
  278. foreach $_ (keys %{$src_fields}) {
  279. my $v = $src_fields->{$_};
  280. if (m/^Source$/i) {
  281. set_source_package($v);
  282. } elsif (m/^(Format|Standards-Version|Origin|Maintainer|Homepage)$/i ||
  283. m/^Dm-Upload-Allowed$/i ||
  284. m/^Vcs-(Browser|Arch|Bzr|Cvs|Darcs|Git|Hg|Mtn|Svn)$/i) {
  285. $fields->{$_} = $v;
  286. } elsif (m/^Uploaders$/i) {
  287. ($fields->{$_} = $v) =~ s/[\r\n]//g; # Merge in a single-line
  288. } elsif (m/^Build-(Depends|Conflicts)(-Indep)?$/i) {
  289. my $dep;
  290. my $type = $dep_field_type{capit($_)};
  291. $dep = Dpkg::Deps::parse($v, union => $type eq 'union');
  292. error(_g("error occurred while parsing %s"), $_) unless defined $dep;
  293. my $facts = Dpkg::Deps::KnownFacts->new();
  294. $dep->simplify_deps($facts);
  295. $dep->sort();
  296. $fields->{$_} = $dep->dump();
  297. } elsif (s/^X[BC]*S[BC]*-//i) { # Include XS-* fields
  298. $fields->{$_} = $v;
  299. } elsif (m/^$control_src_field_regex$/i || m/^X[BC]+-/i) {
  300. # Silently ignore valid fields
  301. } else {
  302. unknown(_g('general section of control info file'));
  303. }
  304. }
  305. # Scan control info of binary packages
  306. foreach my $pkg ($control->get_packages()) {
  307. my $p = $pkg->{'Package'};
  308. push(@binarypackages,$p);
  309. foreach $_ (keys %{$pkg}) {
  310. my $v = $pkg->{$_};
  311. if (m/^Architecture$/) {
  312. if (debarch_eq($v, 'any')) {
  313. @sourcearch= ('any');
  314. } elsif (debarch_eq($v, 'all')) {
  315. if (!@sourcearch || $sourcearch[0] eq 'all') {
  316. @sourcearch= ('all');
  317. } else {
  318. @sourcearch= ('any');
  319. }
  320. } else {
  321. if (@sourcearch && grep($sourcearch[0] eq $_, 'any', 'all')) {
  322. @sourcearch= ('any');
  323. } else {
  324. for my $a (split(/\s+/, $v)) {
  325. error(_g("`%s' is not a legal architecture string"),
  326. $a)
  327. unless $a =~ /^[\w-]+$/;
  328. error(_g("architecture %s only allowed on its " .
  329. "own (list for package %s is `%s')"),
  330. $a, $p, $a)
  331. if grep($a eq $_, 'any','all');
  332. push(@sourcearch,$a) unless $archadded{$a}++;
  333. }
  334. }
  335. }
  336. $fields->{'Architecture'}= join(' ',@sourcearch);
  337. } elsif (s/^X[BC]*S[BC]*-//i) { # Include XS-* fields
  338. $fields->{$_} = $v;
  339. } elsif (m/^$control_pkg_field_regex$/ ||
  340. m/^X[BC]+-/i) { # Silently ignore valid fields
  341. } else {
  342. unknown(_g("package's section of control info file"));
  343. }
  344. }
  345. }
  346. # Scan fields of dpkg-parsechangelog
  347. foreach $_ (keys %{$changelog}) {
  348. my $v = $changelog->{$_};
  349. if (m/^Source$/) {
  350. set_source_package($v);
  351. } elsif (m/^Version$/) {
  352. check_version($v);
  353. $fields->{$_} = $v;
  354. } elsif (s/^X[BS]*C[BS]*-//i) {
  355. $fields->{$_} = $v;
  356. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/i ||
  357. m/^X[BS]+-/i) {
  358. } else {
  359. unknown(_g("parsed version of changelog"));
  360. }
  361. }
  362. my $vcs;
  363. if ($fields->{Format} =~ /^\s*(\d+\.\d+)\s*$/) {
  364. if ($1 >= 3.0) {
  365. error(_g("don't know how to generate %s format source package (missing vcs specifier in Format field?)"),
  366. $1);
  367. }
  368. if ($1 > 1.0) {
  369. error(_g("don't know how to generate %s format source package"),
  370. $1);
  371. }
  372. }
  373. elsif ($fields->{Format} =~ /^\s*(\d+(?:\.\d+)?)\s+\((\w+)\)\s*$/) {
  374. $fields->{Format} = $1;
  375. if ($1 < 3.0) {
  376. error(_g("control info file 'Format' field for version %s does not support vcs specifier \"%s\""),
  377. $1, $2);
  378. }
  379. if ($1 >= 4) {
  380. error(_g("unsupported control info file 'Format' value \"%s\""),
  381. $1);
  382. }
  383. $vcs = $2;
  384. loadvcs($2)
  385. || error(_g("unsupported vcs \"%s\" in control info file 'Format' field"), $2);
  386. if ($sourcestyle =~ /[akpursKPUR]/) {
  387. warning(_g("source handling style -s%s not supported when generating %s format source package"),
  388. $sourcestyle, $vcs);
  389. }
  390. $sourcestyle = 'v';
  391. }
  392. $sourcestyle =~ y/X/A/;
  393. $sourcestyle =~ m/[akpursnAKPURv]/ ||
  394. usageerr(_g("source handling style -s%s not allowed with -b"),
  395. $sourcestyle);
  396. $fields->{'Binary'}= join(', ', @binarypackages);
  397. foreach my $f (keys %override) {
  398. $fields->{$f} = $override{$f};
  399. }
  400. for my $f (qw(Version)) {
  401. defined($fields->{$f}) ||
  402. error(_g("missing information for critical output field %s"), $f);
  403. }
  404. for my $f (qw(Maintainer Architecture Standards-Version)) {
  405. defined($fields->{$f}) ||
  406. warning(_g("missing information for output field %s"), $f);
  407. }
  408. defined($sourcepackage) || &error(_g("unable to determine source package name !"));
  409. $fields->{'Source'} = $sourcepackage;
  410. for my $f (keys %remove) {
  411. delete $fields->{$f};
  412. }
  413. my $version = $fields->{'Version'};
  414. $version =~ s/^\d+://;
  415. my $upstreamversion = $version;
  416. $upstreamversion =~ s/-[^-]*$//;
  417. my $basenamerev = $sourcepackage.'_'.$version;
  418. my $basename = $sourcepackage.'_'.$upstreamversion;
  419. my $basedirname = $basename;
  420. $basedirname =~ s/_/-/;
  421. my $origdir = "$dir.orig";
  422. my $origtargz;
  423. # Try to find a .orig tarball for the package
  424. my @origtargz = map { "$basename.orig.tar.$comp_ext{$_}" } ($compression, @comp_supported);
  425. foreach my $origtar (@origtargz) {
  426. if (stat($origtar)) {
  427. -f _ || error(_g("packed orig `%s' exists but is not a plain file"),
  428. $origtar);
  429. $origtargz = $origtar;
  430. last;
  431. } elsif ($! != ENOENT) {
  432. syserr(_g("unable to stat putative packed orig `%s'"), $origtar);
  433. }
  434. }
  435. if (@ARGV) {
  436. # We have a second-argument <orig-dir> or <orig-targz>, check what it
  437. # is to decide the mode to use
  438. my $origarg = shift(@ARGV);
  439. if (length($origarg)) {
  440. stat($origarg) ||
  441. error(_g("cannot stat orig argument %s: %s"), $origarg, $!);
  442. if (-d _) {
  443. $origdir= $origarg;
  444. $origdir= "./$origdir" unless $origdir =~ m,^/,; $origdir =~ s,/*$,,;
  445. $sourcestyle =~ y/aA/rR/;
  446. $sourcestyle =~ m/[ursURS]/ ||
  447. error(_g("orig argument is unpacked but source handling " .
  448. "style -s%s calls for packed (.orig.tar.<ext>)"),
  449. $sourcestyle);
  450. } elsif (-f _) {
  451. $origtargz= $origarg;
  452. $sourcestyle =~ y/aA/pP/;
  453. $sourcestyle =~ m/[kpsKPS]/ ||
  454. error(_g("orig argument is packed but source handling " .
  455. "style -s%s calls for unpacked (.orig/)"),
  456. $sourcestyle);
  457. } else {
  458. &error("orig argument $origarg is not a plain file or directory");
  459. }
  460. } else {
  461. $sourcestyle =~ y/aA/nn/;
  462. $sourcestyle =~ m/n/ ||
  463. error(_g("orig argument is empty (means no orig, no diff) " .
  464. "but source handling style -s%s wants something"),
  465. $sourcestyle);
  466. }
  467. } elsif ($sourcestyle =~ m/[aA]/) {
  468. # We have no explicit <orig-dir> or <orig-targz>, try to use
  469. # a .orig tarball first, then a .orig directory and fall back to
  470. # creating a native .tar.gz
  471. if ($origtargz) {
  472. $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
  473. } else {
  474. if (stat($origdir)) {
  475. -d _ || error(_g("unpacked orig `%s' exists but is not a directory"),
  476. $origdir);
  477. $sourcestyle =~ y/aA/rR/; # .orig directory
  478. } elsif ($! != ENOENT) {
  479. syserr(_g("unable to stat putative unpacked orig `%s'"), $origdir);
  480. } else {
  481. $sourcestyle =~ y/aA/nn/; # Native tar.gz
  482. }
  483. }
  484. }
  485. my $dirbase = $dir;
  486. $dirbase =~ s,/?$,,;
  487. $dirbase =~ s,[^/]+$,,;
  488. my $dirname = $&;
  489. $dirname eq $basedirname ||
  490. warning(_g("source directory '%s' is not <sourcepackage>" .
  491. "-<upstreamversion> '%s'"), $dir, $basedirname);
  492. my $tarname;
  493. my $tardirname;
  494. my $tardirbase;
  495. my $origdirname;
  496. if ($sourcestyle eq 'v') {
  497. $tarname="$basenamerev.$vcs.tar.gz";
  498. $tardirbase= $dirbase; $tardirname= "$dirbase/$tarname.tmp";
  499. eval qq{Dpkg::Source::VCS::${vcs}::prep_tar(\$dir, \$tardirname)};
  500. if ($@) {
  501. failure($@);
  502. }
  503. push @exit_handlers, sub { erasedir($tardirname) };
  504. }
  505. elsif ($sourcestyle ne 'n') {
  506. my $origdirbase = $origdir;
  507. $origdirbase =~ s,/?$,,;
  508. $origdirbase =~ s,[^/]+$,,; $origdirname= $&;
  509. $origdirname eq "$basedirname.orig" ||
  510. warning(_g(".orig directory name %s is not <package>" .
  511. "-<upstreamversion> (wanted %s)"),
  512. $origdirname, "$basedirname.orig");
  513. $tardirbase= $origdirbase; $tardirname= $origdirname;
  514. $tarname= $origtargz || "$basename.orig.tar.$comp_ext";
  515. if ($tarname =~ /\Q$basename\E\.orig\.tar\.($comp_regex)/) {
  516. if (($1 ne 'gz') && ($fields->{'Format'} < 2)) { $fields->{'Format'} = '2.0' };
  517. } else {
  518. warning(_g(".orig.tar name %s is not <package>_<upstreamversion>" .
  519. ".orig.tar (wanted %s)"),
  520. $tarname, "$basename.orig.tar.$comp_regex");
  521. }
  522. } else {
  523. $tardirbase= $dirbase; $tardirname= $dirname;
  524. $tarname= "$basenamerev.tar.$comp_ext";
  525. }
  526. if ($sourcestyle =~ m/[nurURv]/) {
  527. if (stat($tarname)) {
  528. $sourcestyle =~ m/[nURv]/ ||
  529. error(_g("tarfile `%s' already exists, not overwriting, " .
  530. "giving up; use -sU or -sR to override"), $tarname);
  531. } elsif ($! != ENOENT) {
  532. syserr(_g("unable to check for existence of `%s'"), $tarname);
  533. }
  534. printf(_g("%s: building %s in %s")."\n",
  535. $progname, $sourcepackage, $tarname);
  536. my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
  537. DIR => getcwd(), UNLINK => 0);
  538. my $tar = Dpkg::Source::Archiver->new(filename => $newtar,
  539. compression => get_compression_from_filename($tarname),
  540. compression_level => $comp_level);
  541. $tar->create(options => \@tar_ignore);
  542. $tar->add_directory($tardirname);
  543. $tar->close();
  544. rename($newtar, $tarname) ||
  545. syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  546. $newtar, $tarname);
  547. chmod(0666 &~ umask(), $tarname) ||
  548. syserr(_g("unable to change permission of `%s'"), $tarname);
  549. } else {
  550. printf(_g("%s: building %s using existing %s")."\n",
  551. $progname, $sourcepackage, $tarname);
  552. }
  553. addfile($fields, "$tarname");
  554. if ($sourcestyle =~ m/[kpKP]/) {
  555. if (stat($origdir)) {
  556. $sourcestyle =~ m/[KP]/ ||
  557. error(_g("orig dir `%s' already exists, not overwriting, ".
  558. "giving up; use -sA, -sK or -sP to override"),
  559. $origdir);
  560. push @exit_handlers, sub { erasedir($origdir) };
  561. erasedir($origdir);
  562. pop @exit_handlers;
  563. } elsif ($! != ENOENT) {
  564. syserr(_g("unable to check for existence of orig dir `%s'"),
  565. $origdir);
  566. }
  567. $expectprefix= $origdir; $expectprefix =~ s,^\./,,;
  568. my $tar = Dpkg::Source::Archiver->new(filename => $origtargz);
  569. $tar->extract($expectprefix);
  570. }
  571. if ($sourcestyle eq 'v') {
  572. erasedir($tardirname)
  573. }
  574. if ($sourcestyle =~ m/[kpursKPUR]/) {
  575. my $diffname = "$basenamerev.diff.$comp_ext";
  576. printf(_g("%s: building %s in %s")."\n",
  577. $progname, $sourcepackage, $diffname)
  578. || &syserr(_g("write building diff message"));
  579. my ($ndfh, $newdiffgz) = tempfile( "$diffname.new.XXXXXX",
  580. DIR => &getcwd, UNLINK => 0 );
  581. my $compressor = Dpkg::Source::Compressor->new();
  582. my $diff_handle;
  583. $compressor->compress(from_pipe => \$diff_handle, to_file => $newdiffgz);
  584. my $find_handle;
  585. my $pid = fork_and_exec(
  586. 'exec' => [ 'find', '.', '-print0' ],
  587. 'chdir' => $dir,
  588. 'to_pipe' => \$find_handle,
  589. );
  590. $/ = "\0";
  591. file:
  592. while (defined($fn= <$find_handle>)) {
  593. $fn =~ s/\0$//;
  594. next file if $fn =~ m/$diff_ignore_regexp/o;
  595. $fn =~ s,^\./,,;
  596. lstat("$dir/$fn") || syserr(_g("cannot stat file %s"), "$dir/$fn");
  597. my $mode = S_IMODE((lstat(_))[2]);
  598. my $size = (lstat(_))[7];
  599. if (-l _) {
  600. $type{$fn}= 'symlink';
  601. checktype($origdir, $fn, '-l') || next;
  602. defined(my $n = readlink("$dir/$fn")) ||
  603. syserr(_g("cannot read link %s"), "$dir/$fn");
  604. defined(my $n2 = readlink("$origdir/$fn")) ||
  605. syserr(_g("cannot read orig link %s"), "$origdir/$fn");
  606. $n eq $n2 || &unrepdiff2(sprintf(_g("symlink to %s"), $n2),
  607. sprintf(_g("symlink to %s"), $n));
  608. } elsif (-f _) {
  609. my $ofnread;
  610. $type{$fn}= 'plain file';
  611. if (!lstat("$origdir/$fn")) {
  612. $! == ENOENT ||
  613. syserr(_g("cannot stat orig file %s"), "$origdir/$fn");
  614. $ofnread= '/dev/null';
  615. if( !$size ) {
  616. warning(_g("newly created empty file '%s' will not " .
  617. "be represented in diff"), $fn);
  618. } else {
  619. if( $mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) ) {
  620. warning(_g("executable mode %04o of '%s' will " .
  621. "not be represented in diff"), $mode, $fn)
  622. unless $fn eq 'debian/rules';
  623. }
  624. if( $mode & ( S_ISUID | S_ISGID | S_ISVTX ) ) {
  625. warning(_g("special mode %04o of '%s' will not " .
  626. "be represented in diff"), $mode, $fn);
  627. }
  628. }
  629. } elsif (-f _) {
  630. $ofnread= "$origdir/$fn";
  631. } else {
  632. &unrepdiff2(_g("something else"),
  633. _g("plain file"));
  634. next;
  635. }
  636. my $tab = ("$basedirname/$fn" =~ / /) ? "\t" : '';
  637. my $diffgen;
  638. my $diff_pid = fork_and_exec(
  639. 'exec' => [ 'diff', '-u', '-p',
  640. '-L', "$basedirname.orig/$fn$tab",
  641. '-L', "$basedirname/$fn$tab",
  642. '--', "$ofnread", "$dir/$fn" ],
  643. 'env' => { LC_ALL => 'C', LANG => 'C', TZ => 'UTC0' },
  644. 'to_pipe' => \$diffgen
  645. );
  646. my $difflinefound = 0;
  647. $/ = "\n";
  648. while (<$diffgen>) {
  649. if (m/^binary/i) {
  650. close($diffgen);
  651. $/ = "\0";
  652. &unrepdiff(_g("binary file contents changed"));
  653. next file;
  654. } elsif (m/^[-+\@ ]/) {
  655. $difflinefound=1;
  656. } elsif (m/^\\ No newline at end of file$/) {
  657. warning(_g("file %s has no final newline (either " .
  658. "original or modified version)"), $fn);
  659. } else {
  660. s/\n$//;
  661. internerr(_g("unknown line from diff -u on %s: `%s'"),
  662. $fn, $_);
  663. }
  664. print($diff_handle $_) || syserr(_g("failed to write to compression pipe"));
  665. }
  666. close($diffgen);
  667. wait_child($diff_pid, nocheck => 1, cmdline => 'diff');
  668. $/ = "\0";
  669. my $es;
  670. if (WIFEXITED($?) && (($es=WEXITSTATUS($?))==0 || $es==1)) {
  671. if ($es==1 && !$difflinefound) {
  672. &unrepdiff(_g("diff gave 1 but no diff lines found"));
  673. }
  674. } else {
  675. subprocerr(_g("diff on %s"), "$dir/$fn");
  676. }
  677. } elsif (-p _) {
  678. $type{$fn}= 'pipe';
  679. checktype($origdir, $fn, '-p');
  680. } elsif (-b _ || -c _ || -S _) {
  681. &unrepdiff(_g("device or socket is not allowed"));
  682. } elsif (-d _) {
  683. $type{$fn}= 'directory';
  684. if (!lstat("$origdir/$fn")) {
  685. $! == ENOENT ||
  686. syserr(_g("cannot stat orig file %s"), "$origdir/$fn");
  687. } elsif (! -d _) {
  688. &unrepdiff2(_g('not a directory'),
  689. _g('directory'));
  690. }
  691. } else {
  692. &unrepdiff(sprintf(_g("unknown file type (%s)"), $!));
  693. }
  694. }
  695. close($find_handle);
  696. wait_child($pid);
  697. close($diff_handle) || syserr(_g("finish write to compression pipe"));
  698. $compressor->wait_end_process();
  699. rename($newdiffgz, $diffname) ||
  700. syserr(_g("unable to rename `%s' (newly created) to `%s'"),
  701. $newdiffgz, $diffname);
  702. chmod(0666 &~ umask(), $diffname) ||
  703. syserr(_g("unable to change permission of `%s'"), $diffname);
  704. $find_handle = undef;
  705. $pid = fork_and_exec(
  706. 'exec' => [ 'find', '.', '-print0' ],
  707. 'chdir' => $origdir,
  708. 'to_pipe' => \$find_handle,
  709. );
  710. $/ = "\0";
  711. while (defined($fn= <$find_handle>)) {
  712. $fn =~ s/\0$//;
  713. next if $fn =~ m/$diff_ignore_regexp/o;
  714. $fn =~ s,^\./,,;
  715. next if defined($type{$fn});
  716. lstat("$origdir/$fn") ||
  717. syserr(_g("cannot check orig file %s"), "$origdir/$fn");
  718. if (-f _) {
  719. warning(_g("ignoring deletion of file %s"), $fn);
  720. } elsif (-d _) {
  721. warning(_g("ignoring deletion of directory %s"), $fn);
  722. } elsif (-l _) {
  723. warning(_g("ignoring deletion of symlink %s"), $fn);
  724. } else {
  725. &unrepdiff2(_g('not a file, directory or link'),
  726. _g('nonexistent'));
  727. }
  728. }
  729. close($find_handle);
  730. wait_child($pid);
  731. addfile($fields, $diffname);
  732. }
  733. if ($sourcestyle =~ m/[prPR]/) {
  734. erasedir($origdir);
  735. }
  736. printf(_g("%s: building %s in %s")."\n",
  737. $progname, $sourcepackage, "$basenamerev.dsc")
  738. || &syserr(_g("write building message"));
  739. open(DSC, ">", "$basenamerev.dsc") ||
  740. syserr(_g("create %s"), "$basenamerev.dsc");
  741. delete $fields->{'Checksums-Md5'}; # identical with Files field
  742. $substvars->parse($varlistfile) if $varlistfile && -e $varlistfile;
  743. tied(%{$fields})->set_field_importance(@dsc_fields);
  744. tied(%{$fields})->output(\*DSC, $substvars);
  745. close(DSC);
  746. if ($ur) {
  747. printf(STDERR _g("%s: unrepresentable changes to source")."\n",
  748. $progname) || syserr(_g("write error msg: %s"), $!);
  749. exit(1);
  750. }
  751. exit(0);
  752. } else { # -> opmode ne 'build'
  753. $sourcestyle =~ y/X/p/;
  754. $sourcestyle =~ m/[pun]/ ||
  755. usageerr(_g("source handling style -s%s not allowed with -x"),
  756. $sourcestyle);
  757. @ARGV>=1 || &usageerr(_g("-x needs at least one argument, the .dsc"));
  758. @ARGV<=2 || &usageerr(_g("-x takes no more than two arguments"));
  759. my $dsc = shift(@ARGV);
  760. $dsc= "./$dsc" unless $dsc =~ m:^/:;
  761. ! -d $dsc
  762. || &usageerr(_g("-x needs the .dsc file as first argument, not a directory"));
  763. my $dscdir = $dsc;
  764. $dscdir = "./$dscdir" unless $dsc =~ m,^/|^\./,;
  765. $dscdir =~ s,/[^/]+$,,;
  766. my $newdirectory;
  767. if (@ARGV) {
  768. $newdirectory= shift(@ARGV);
  769. ! -e $newdirectory || error(_g("unpack target exists: %s"), $newdirectory);
  770. }
  771. my $is_signed = 0;
  772. open(DSC, "<", $dsc) || error(_g("cannot open .dsc file %s: %s"), $dsc, $!);
  773. while (<DSC>) {
  774. next if /^\s*$/o;
  775. $is_signed = 1 if /^-----BEGIN PGP SIGNED MESSAGE-----$/o;
  776. last;
  777. }
  778. close(DSC);
  779. if ($is_signed) {
  780. if (-x '/usr/bin/gpg') {
  781. my $gpg_command = 'gpg -q --verify ';
  782. if (-r '/usr/share/keyrings/debian-keyring.gpg') {
  783. $gpg_command = $gpg_command.'--keyring /usr/share/keyrings/debian-keyring.gpg ';
  784. }
  785. $gpg_command = $gpg_command.quotemeta($dsc).' 2>&1';
  786. my @gpg_output = `$gpg_command`;
  787. my $gpg_status = $? >> 8;
  788. if ($gpg_status) {
  789. print STDERR join("",@gpg_output);
  790. error(_g("failed to verify signature on %s"), $dsc)
  791. if ($gpg_status == 1);
  792. }
  793. } else {
  794. warning(_g("could not verify signature on %s since gpg isn't installed"),
  795. $dsc);
  796. }
  797. } else {
  798. warning(_g("extracting unsigned source package (%s)"), $dsc);
  799. }
  800. open(CDATA, "<", $dsc) || error(_g("cannot open .dsc file %s: %s"), $dsc, $!);
  801. my $fields = parsecdata(\*CDATA, sprintf(_g("source control file %s"), $dsc),
  802. allow_pgp => 1);
  803. close(CDATA);
  804. for my $f (qw(Source Version Files)) {
  805. defined($fields->{$f}) ||
  806. error(_g("missing critical source control field %s"), $f);
  807. }
  808. my $dscformat = $def_dscformat;
  809. if (defined $fields->{'Format'}) {
  810. if (not handleformat($fields->{'Format'})) {
  811. error(_g("Unsupported format of .dsc file (%s)"), $fields->{'Format'});
  812. }
  813. $dscformat=$fields->{'Format'};
  814. }
  815. set_source_package($fields->{'Source'});
  816. my $version = $fields->{'Version'};
  817. my $baseversion;
  818. my $revision;
  819. check_version($version);
  820. $version =~ s/^\d+://;
  821. if ($version =~ m/-([^-]+)$/) {
  822. $baseversion= $`; $revision= $1;
  823. } else {
  824. $baseversion= $version; $revision= '';
  825. }
  826. readallchecksums($fields, \%checksum, \%size);
  827. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  828. my $files = $fields->{'Files'};
  829. my @tarfiles;
  830. my $difffile;
  831. my $debianfile;
  832. my %vcsfiles;
  833. my %seen;
  834. for my $file (split(/\n /, $files)) {
  835. next if $file eq '';
  836. $file =~ m/^($check_regex{md5}) # checksum
  837. [ \t]+(\d+) # size
  838. [ \t]+($rx_fname) # filename
  839. $/x
  840. || error(_g("Files field contains bad line `%s'"), $file);
  841. (my $md5sum,$size{$3},$file) = ($1,$2,$3);
  842. if (exists($checksum{$file}{md5})
  843. and $checksum{$file}{md5} ne $md5sum) {
  844. error(_g("Conflicting checksums \`%s\' and \`%s' for file \`%s'"),
  845. $checksum{$file}{md5}, $md5sum, $file);
  846. }
  847. $checksum{$file}{md5} = $md5sum;
  848. local $_ = $file;
  849. error(_g("Files field contains invalid filename `%s'"), $file)
  850. unless s/^\Q$sourcepackage\E_\Q$baseversion\E(?=[.-])// and
  851. s/\.$comp_regex$//;
  852. s/^-\Q$revision\E(?=\.)// if length $revision;
  853. error(_g("repeated file type - files `%s' and `%s'"), $seen{$_}, $file)
  854. if $seen{$_};
  855. $seen{$_} = $file;
  856. checkstats($dscdir, $file);
  857. if (/^\.(?:orig(-\w+)?\.)?tar$/) {
  858. if ($1) { push @tarfiles, $file; } # push orig-foo.tar.gz to the end
  859. else { unshift @tarfiles, $file; }
  860. } elsif (/^\.debian\.tar$/) {
  861. $debianfile = $file;
  862. } elsif (/^\.(\w+)\.tar$/) {
  863. my $vcs=$1;
  864. # TODO try to load vcs module
  865. push @tarfiles, $file;
  866. $vcsfiles{$file}=$vcs;
  867. } elsif (/^\.diff$/) {
  868. $difffile = $file;
  869. } else {
  870. error(_g("unrecognised file type - `%s'"), $file);
  871. }
  872. }
  873. &error(_g("no tarfile in Files field")) unless @tarfiles;
  874. my $native = !($difffile || $debianfile);
  875. if ($native) {
  876. warning(_g("multiple tarfiles in native package")) if @tarfiles > 1;
  877. warning(_g("native package with .orig.tar"))
  878. unless $seen{'.tar'} or $seen{"-$revision.tar"} or %vcsfiles;
  879. } else {
  880. warning(_g("no upstream tarfile in Files field"))
  881. unless $seen{'.orig.tar'} or %vcsfiles;
  882. if ($dscformat =~ /^1\./) {
  883. warning(_g("multiple upstream tarballs in %s format dsc"), $dscformat)
  884. if @tarfiles > 1;
  885. warning(_g("debian.tar in %s format dsc"), $dscformat)
  886. if $debianfile;
  887. }
  888. }
  889. if (%vcsfiles && $dscformat !~ /^3\./) {
  890. warning(sprintf(_g("<rc>.tar file in %s format dsc"), $dscformat));
  891. }
  892. $newdirectory = $sourcepackage.'-'.$baseversion unless defined($newdirectory);
  893. $expectprefix = $newdirectory;
  894. $expectprefix .= '.orig' if $difffile || $debianfile;
  895. checkdiff("$dscdir/$difffile") if $difffile;
  896. printf(_g("%s: extracting %s in %s")."\n",
  897. $progname, $sourcepackage, $newdirectory)
  898. || &syserr(_g("write extracting message"));
  899. &erasedir($newdirectory);
  900. ! -e "$expectprefix"
  901. || rename("$expectprefix","$newdirectory.tmp-keep")
  902. || syserr(_g("unable to rename `%s' to `%s'"), $expectprefix, "$newdirectory.tmp-keep");
  903. push @tarfiles, $debianfile if $debianfile;
  904. for my $tarfile (@tarfiles)
  905. {
  906. my $target;
  907. if ($tarfile =~ /\.orig-(\w+)\.tar/) {
  908. my $sub = $1;
  909. $sub =~ s/\d+$// if $sub =~ /\D/;
  910. $target = "$expectprefix/$sub";
  911. } elsif ($tarfile =~ /\.debian\.tar/) {
  912. $target = "$expectprefix/debian";
  913. } else {
  914. $target = $expectprefix;
  915. }
  916. printf(_g("%s: unpacking %s")."\n", $progname, $tarfile);
  917. my $tar = Dpkg::Source::Archiver->new(filename => "$dscdir/$tarfile");
  918. $tar->extract($target);
  919. # for the first tar file:
  920. if ($tarfile eq $tarfiles[0] and !$native)
  921. {
  922. # -sp: copy the .orig.tar.gz if required
  923. if ($sourcestyle =~ /p/) {
  924. stat("$dscdir/$tarfile") ||
  925. syserr(_g("failed to stat `%s' to see if need to copy"),
  926. "$dscdir/$tarfile");
  927. my ($dsctardev, $dsctarino) = stat _;
  928. my $copy_required;
  929. if (stat($tarfile)) {
  930. my ($dumptardev, $dumptarino) = stat _;
  931. $copy_required = ($dumptardev != $dsctardev ||
  932. $dumptarino != $dsctarino);
  933. } else {
  934. $! == ENOENT ||
  935. syserr(_g("failed to check destination `%s' " .
  936. "to see if need to copy"), $tarfile);
  937. $copy_required = 1;
  938. }
  939. if ($copy_required) {
  940. system('cp','--',"$dscdir/$tarfile", $tarfile);
  941. $? && subprocerr("cp $dscdir/$tarfile to $tarfile");
  942. }
  943. }
  944. # -su: keep .orig directory unpacked
  945. elsif ($sourcestyle =~ /u/ and $expectprefix ne $newdirectory) {
  946. ! -e "$newdirectory.tmp-keep"
  947. || &error(_g("unable to keep orig directory (already exists)"));
  948. system('cp','-ar','--',$expectprefix,"$newdirectory.tmp-keep");
  949. $? && subprocerr("cp $expectprefix to $newdirectory.tmp-keep");
  950. }
  951. }
  952. if (exists $vcsfiles{$tarfile}) {
  953. printf(_g("%s: extracting source from %s repository")."\n", $progname, $vcsfiles{$tarfile});
  954. loadvcs($vcsfiles{$tarfile})
  955. || error(sprintf(_g("unsupported vcs \"%s\""), $vcsfiles{$tarfile}));
  956. eval qq{Dpkg::Source::VCS::$vcsfiles{$tarfile}::post_unpack_tar(\$target)};
  957. if ($@) {
  958. &syserr($@);
  959. }
  960. }
  961. }
  962. my @patches;
  963. push @patches, "$dscdir/$difffile" if $difffile;
  964. if ($debianfile and -d (my $pd = "$expectprefix/debian/patches"))
  965. {
  966. my @p;
  967. opendir D, $pd;
  968. while (defined ($_ = readdir D))
  969. {
  970. # patches match same rules as run-parts
  971. next unless /^[\w-]+$/ and -f "$pd/$_";
  972. my $p = $_;
  973. checkdiff("$pd/$p");
  974. push @p, $p;
  975. }
  976. closedir D;
  977. push @patches, map "$newdirectory/debian/patches/$_", sort @p;
  978. }
  979. for my $dircreate (keys %dirtocreate) {
  980. my $dircreatem = "";
  981. for my $dircreatep (split("/", $dircreate)) {
  982. $dircreatem .= $dircreatep . "/";
  983. if (!lstat($dircreatem)) {
  984. $! == ENOENT || syserr(_g("cannot stat %s"), $dircreatem);
  985. mkdir($dircreatem,0777)
  986. || syserr(_g("failed to create %s subdirectory"), $dircreatem);
  987. }
  988. else {
  989. -d _ || error(_g("diff patches file in directory `%s', " .
  990. "but %s isn't a directory !"),
  991. $dircreate, $dircreatem);
  992. }
  993. }
  994. }
  995. if ($newdirectory ne $expectprefix)
  996. {
  997. rename($expectprefix,$newdirectory) ||
  998. syserr(_g("failed to rename newly-extracted %s to %s"),
  999. $expectprefix, $newdirectory);
  1000. # rename the copied .orig directory
  1001. ! -e "$newdirectory.tmp-keep"
  1002. || rename("$newdirectory.tmp-keep",$expectprefix)
  1003. || syserr(_g("failed to rename saved %s to %s"),
  1004. "$newdirectory.tmp-keep", $expectprefix);
  1005. }
  1006. for my $patch (@patches) {
  1007. printf(_g("%s: applying %s")."\n", $progname, $patch);
  1008. my ($diff_handle, $compressor);
  1009. if ($patch =~ /\.$comp_regex$/) {
  1010. $compressor = Dpkg::Source::Compressor->new();
  1011. $compressor->uncompress(from_file => $patch, to_pipe => \$diff_handle);
  1012. } else {
  1013. open $diff_handle, $patch or error(_g("can't open diff `%s'"), $patch);
  1014. }
  1015. fork_and_exec(
  1016. 'exec' => [ 'patch', '-s', '-t', '-F', '0', '-N', '-p1', '-u',
  1017. '-V', 'never', '-g0', '-b', '-z', '.dpkg-orig' ],
  1018. 'chdir' => $newdirectory,
  1019. env => { LC_ALL => 'C', LANG => 'C' },
  1020. wait_child => 1,
  1021. from_handle => $diff_handle
  1022. );
  1023. $compressor->wait_end_process() if $patch =~ /\.$comp_regex$/;
  1024. }
  1025. my $now = time;
  1026. for $fn (keys %filepatched) {
  1027. my $ftr = "$newdirectory/" . substr($fn, length($expectprefix) + 1);
  1028. utime($now, $now, $ftr) ||
  1029. syserr(_g("cannot change timestamp for %s"), $ftr);
  1030. $ftr.= ".dpkg-orig";
  1031. unlink($ftr) || syserr(_g("remove patch backup file %s"), $ftr);
  1032. }
  1033. if (!(my @s = lstat("$newdirectory/debian/rules"))) {
  1034. $! == ENOENT || syserr(_g("cannot stat %s"), "$newdirectory/debian/rules");
  1035. warning(_g("%s does not exist"), "$newdirectory/debian/rules");
  1036. } elsif (-f _) {
  1037. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  1038. syserr(_g("cannot make %s executable"), "$newdirectory/debian/rules");
  1039. } else {
  1040. warning(_g("%s is not a plain file"), "$newdirectory/debian/rules");
  1041. }
  1042. exit(0);
  1043. }
  1044. sub checkstats {
  1045. my ($dscdir, $f) = @_;
  1046. getchecksums("$dscdir/$f", $checksum{$f}, \$size{$f});
  1047. }
  1048. sub erasedir {
  1049. my ($dir) = @_;
  1050. if (!lstat($dir)) {
  1051. $! == ENOENT && return;
  1052. syserr(_g("cannot stat directory %s (before removal)"), $dir);
  1053. }
  1054. system 'rm','-rf','--',$dir;
  1055. $? && subprocerr("rm -rf $dir");
  1056. if (!stat($dir)) {
  1057. $! == ENOENT && return;
  1058. syserr(_g("unable to check for removal of dir `%s'"), $dir);
  1059. }
  1060. failure(_g("rm -rf failed to remove `%s'"), $dir);
  1061. }
  1062. # check diff for sanity, find directories to create as a side effect
  1063. sub checkdiff
  1064. {
  1065. my $diff = shift;
  1066. my ($diff_handle, $compressor);
  1067. if ($diff =~ /\.$comp_regex$/) {
  1068. $compressor = Dpkg::Source::Compressor->new();
  1069. $compressor->uncompress(from_file => $diff, to_pipe => \$diff_handle);
  1070. } else {
  1071. open $diff_handle, $diff or error(_g("can't open diff `%s'"), $diff);
  1072. }
  1073. $/ = "\n";
  1074. $_ = <$diff_handle>;
  1075. HUNK:
  1076. while (defined($_) || !eof($diff_handle)) {
  1077. # skip cruft leading up to patch (if any)
  1078. until (/^--- /) {
  1079. last HUNK unless defined ($_ = <$diff_handle>);
  1080. }
  1081. # read file header (---/+++ pair)
  1082. s/\n$// or error(_g("diff `%s' is missing trailing newline"), $diff);
  1083. s/^--- // or
  1084. error(_g("expected ^--- in line %d of diff `%s'"), $., $diff);
  1085. s/\t.*//;
  1086. $_ eq '/dev/null' or s!^(\./)?[^/]+/!$expectprefix/! or
  1087. error(_g("diff `%s' patches file with no subdirectory"), $diff);
  1088. /\.dpkg-orig$/ and
  1089. error(_g("diff `%s' patches file with name ending .dpkg-orig"),
  1090. $diff);
  1091. $fn = $_;
  1092. (defined($_= <$diff_handle>) and s/\n$//) or
  1093. error(_g("diff `%s' finishes in middle of ---/+++ (line %d)"),
  1094. $diff, $.);
  1095. s/\t.*//;
  1096. (s/^\+\+\+ // and s!^(\./)?[^/]+/!!) or
  1097. error(_g("line after --- isn't as expected in diff `%s' (line %d)"),
  1098. $diff, $.);
  1099. if ($fn eq '/dev/null') {
  1100. $fn = "$expectprefix/$_";
  1101. } else {
  1102. $_ eq substr($fn, length($expectprefix) + 1) or
  1103. error(_g("line after --- isn't as expected in diff `%s' (line %d)"),
  1104. $diff, $.);
  1105. }
  1106. my $dirname = $fn;
  1107. if ($dirname =~ s,/[^/]+$,, && !defined($dirincluded{$dirname})) {
  1108. $dirtocreate{$dirname} = 1;
  1109. }
  1110. defined($notfileobject{$fn}) &&
  1111. error(_g("diff `%s' patches something which is not a plain file"),
  1112. $diff);
  1113. defined($filepatched{$fn}) &&
  1114. $filepatched{$fn} eq $diff &&
  1115. error(_g("diff patches file %s twice"), $fn);
  1116. $filepatched{$fn} = $diff;
  1117. # read hunks
  1118. my $hunk = 0;
  1119. while (defined($_ = <$diff_handle>) && !(/^--- / or /^Index:/)) {
  1120. # read hunk header (@@)
  1121. s/\n$// or error(_g("diff `%s' is missing trailing newline"), $diff);
  1122. next if /^\\ No newline/;
  1123. /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@( .*)?$/ or
  1124. error(_g("Expected ^\@\@ in line %d of diff `%s'"), $., $diff);
  1125. my ($olines, $nlines) = ($1 ? $2 : 1, $3 ? $4 : 1);
  1126. ++$hunk;
  1127. # read hunk
  1128. while ($olines || $nlines) {
  1129. defined($_ = <$diff_handle>) or
  1130. error(_g("unexpected end of diff `%s'"), $diff);
  1131. s/\n$// or
  1132. error(_g("diff `%s' is missing trailing newline"), $diff);
  1133. next if /^\\ No newline/;
  1134. if (/^ /) { --$olines; --$nlines; }
  1135. elsif (/^-/) { --$olines; }
  1136. elsif (/^\+/) { --$nlines; }
  1137. else {
  1138. error(_g("expected [ +-] at start of line %d of diff `%s'"),
  1139. $., $diff);
  1140. }
  1141. }
  1142. }
  1143. $hunk or error(_g("expected ^\@\@ at line %d of diff `%s'"), $., $diff);
  1144. }
  1145. close($diff_handle);
  1146. $compressor->wait_end_process() if $diff =~ /\.$comp_regex$/;
  1147. }
  1148. sub checktype {
  1149. my ($dir, $fn, $type) = @_;
  1150. if (!lstat("$dir/$fn")) {
  1151. &unrepdiff2(_g("nonexistent"),$type{$fn});
  1152. } else {
  1153. my $v = eval("$type _ ? 2 : 1");
  1154. $v || internerr(_g("checktype %s (%s)"), "$@", $type);
  1155. return 1 if $v == 2;
  1156. &unrepdiff2(_g("something else"),$type{$fn});
  1157. }
  1158. return 0;
  1159. }
  1160. sub setopmode {
  1161. defined($opmode) && &usageerr(_g("only one of -x or -b allowed, and only once"));
  1162. $opmode= $_[0];
  1163. }
  1164. sub unrepdiff {
  1165. printf(STDERR _g("%s: cannot represent change to %s: %s")."\n",
  1166. $progname, $fn, $_[0])
  1167. || &syserr(_g("write syserr unrep"));
  1168. $ur++;
  1169. }
  1170. sub unrepdiff2 {
  1171. printf(STDERR _g("%s: cannot represent change to %s:\n".
  1172. "%s: new version is %s\n".
  1173. "%s: old version is %s\n"),
  1174. $progname, $fn, $progname, $_[1], $progname, $_[0])
  1175. || &syserr(_g("write syserr unrep"));
  1176. $ur++;
  1177. }
  1178. my %added_files;
  1179. sub addfile {
  1180. my ($fields, $filename)= @_;
  1181. $added_files{$filename}++ &&
  1182. internerr(_g("tried to add file `%s' twice"), $filename);
  1183. my (%sums, $size);
  1184. getchecksums($filename, \%sums, \$size);
  1185. foreach my $alg (sort keys %sums) {
  1186. $fields->{"Checksums-$alg"} .= "\n $sums{$alg} $size $filename";
  1187. }
  1188. $fields->{'Files'}.= "\n $sums{md5} $size $filename";
  1189. }