dpkg-source.pl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. #! /usr/bin/perl
  2. my $dpkglibdir = ".";
  3. my $version = "1.3.0"; # This line modified by Makefile
  4. my @filesinarchive;
  5. my %dirincluded;
  6. my %notfileobject;
  7. my $fn;
  8. $sourcestyle = 'X';
  9. use POSIX;
  10. use POSIX qw (:errno_h :signal_h);
  11. use strict 'refs';
  12. push (@INC, $dpkglibdir);
  13. require 'controllib.pl';
  14. sub usageversion {
  15. print STDERR
  16. "Debian GNU/Linux dpkg-source $version. Copyright (C) 1996
  17. Ian Jackson and Klee Dienes. This is free software; see the GNU
  18. General Public Licence version 2 or later for copying conditions.
  19. There is NO warranty.
  20. Usage: dpkg-source -x <filename>.dsc
  21. dpkg-source -b <directory> [<orig-directory>|<orig-targz>|\'\']
  22. Build options: -c<controlfile> get control info from this file
  23. -l<changelogfile> get per-version info from this file
  24. -F<changelogformat> force change log format
  25. -V<name>=<value> set a substitution variable
  26. -T<varlistfile> read variables here, not debian/substvars
  27. -D<field>=<value> override or add a .dsc field and value
  28. -U<field> remove a field
  29. -sa auto select orig source (-sA is default)
  30. -sk use packed orig source (unpack & keep)
  31. -sp use packed orig source (unpack & remove)
  32. -su use unpacked orig source (pack & keep)
  33. -sr use unpacked orig source (pack & remove)
  34. -ss trust packed & unpacked orig src are same
  35. -sn there is no diff, do main tarfile only
  36. -sA,-sK,-sP,-sU,-sR like -sa,-sp,-sk,-su,-sr but may overwrite
  37. Extract options: -sp (default) leave orig source packed in current dir
  38. -sn do not copy original source to current dir
  39. -su unpack original source tree too
  40. General options: -h print this message
  41. ";
  42. }
  43. $i = 100;
  44. grep ($fieldimps {$_} = $i--,
  45. qw (Source Version Binary Maintainer Architecture Standards-Version));
  46. while (@ARGV && $ARGV[0] =~ m/^-/) {
  47. $_=shift(@ARGV);
  48. if (m/^-b$/) {
  49. &setopmode('build');
  50. } elsif (m/^-x$/) {
  51. &setopmode('extract');
  52. } elsif (m/^-s([akpursnAKPUR])$/) {
  53. $sourcestyle= $1;
  54. } elsif (m/^-c/) {
  55. $controlfile= $';
  56. } elsif (m/^-l/) {
  57. $changelogfile= $';
  58. } elsif (m/^-F([0-9a-z]+)$/) {
  59. $changelogformat=$1;
  60. } elsif (m/^-D([^\=:]+)[=:]/) {
  61. $override{$1}= $';
  62. } elsif (m/^-U([^\=:]+)$/) {
  63. $remove{$1}= 1;
  64. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/) {
  65. $substvar{$1}= $';
  66. } elsif (m/^-T/) {
  67. $varlistfile= $';
  68. } elsif (m/^-h$/) {
  69. &usageversion; exit(0);
  70. } elsif (m/^--$/) {
  71. last;
  72. } else {
  73. &usageerr("unknown option $_");
  74. }
  75. }
  76. defined($opmode) || &usageerr("need -x or -b");
  77. if ($opmode eq 'build') {
  78. $sourcestyle =~ y/X/A/;
  79. $sourcestyle =~ m/[akpursnAKPUR]/ ||
  80. &usageerr("source handling style -s$sourcestyle not allowed with -b");
  81. @ARGV || &usageerr("-b needs a directory");
  82. @ARGV<=2 || &usageerr("-b takes at most a directory and an orig source argument");
  83. $dir= shift(@ARGV);
  84. $dir= "./$dir" unless $dir =~ m:^/:; $dir =~ s,/*$,,;
  85. stat($dir) || &error("cannot stat directory $dir: $!");
  86. -d $dir || &error("directory argument $dir is not a directory");
  87. $changelogfile= "$dir/debian/changelog" unless defined($changelogfile);
  88. $controlfile= "$dir/debian/control" unless defined($controlfile);
  89. &parsechangelog;
  90. &parsecontrolfile;
  91. $archspecific=0;
  92. for $_ (keys %fi) {
  93. $v= $fi{$_};
  94. if (s/^C //) {
  95. #print STDERR "G key >$_< value >$v<\n";
  96. if (m/^Source$/) { &setsourcepackage; }
  97. elsif (m/^Standards-Version$|^Maintainer$/) { $f{$_}= $v; }
  98. elsif (s/^X[BC]*S[BC]*-//i) { $f{$_}= $v; }
  99. elsif (m/^(Section|Priority|Files)$/ || m/^X[BC]+-/i) { }
  100. else { &unknown('general section of control info file'); }
  101. } elsif (s/^C(\d+) //) {
  102. #print STDERR "P key >$_< value >$v<\n";
  103. $i=$1; $p=$fi{"C$i Package"};
  104. push(@binarypackages,$p) unless $packageadded{$p}++;
  105. if (m/^Architecture$/) {
  106. #print STDERR "$p >$v< >".join(' ',@sourcearch)."<\n";
  107. if ($v eq 'any') {
  108. @sourcearch= ('any');
  109. } elsif ($v eq 'all') {
  110. if (!@sourcearch || $sourcearch[0] eq 'all') {
  111. @sourcearch= ('all');
  112. } else {
  113. @sourcearch= ('any');
  114. }
  115. } else {
  116. if (grep($sourcearch[0] eq $_, 'any','all')) {
  117. @sourcearch= ('any');
  118. } else {
  119. for $a (split(/\s+/,$v)) {
  120. &error("architecture $a only allowed on its own".
  121. " (list for package $p is \`$a')")
  122. if grep($a eq $_, 'any','all');
  123. push(@sourcearch,$a) unless $archadded{$a}++;
  124. }
  125. }
  126. }
  127. $f{'Architecture'}= join(' ',@sourcearch);
  128. } elsif (s/^X[BC]*S[BC]*-//i) {
  129. $f{$_}= $v;
  130. } elsif (m/^(Package|Essential|Pre-Depends|Depends|Provides)$/ ||
  131. m/^(Recommends|Suggests|Optional|Conflicts|Replaces)$/ ||
  132. m/^(Description|Section|Priority)$/ ||
  133. m/^X[CS]+-/i) {
  134. } else {
  135. &unknown("package's section of control info file");
  136. }
  137. } elsif (s/^L //) {
  138. #print STDERR "L key >$_< value >$v<\n";
  139. if (m/^Source$/) {
  140. &setsourcepackage;
  141. } elsif (m/^Version$/) {
  142. $f{$_}= $v;
  143. } elsif (s/^X[BS]*C[BS]*-//i) {
  144. $f{$_}= $v;
  145. } elsif (m/^(Maintainer|Changes|Urgency|Distribution|Date|Closes)$/ ||
  146. m/^X[BS]+-/i) {
  147. } else {
  148. &unknown("parsed version of changelog");
  149. }
  150. } else {
  151. &internerr("value from nowhere, with key >$_< and value >$v<");
  152. }
  153. }
  154. $f{'Binary'}= join(', ',@binarypackages);
  155. for $f (keys %override) { $f{&capit($f)}= $override{$f}; }
  156. for $f (qw(Version)) {
  157. defined($f{$f}) || &error("missing information for critical output field $f");
  158. }
  159. for $f (qw(Maintainer Architecture Standards-Version)) {
  160. defined($f{$f}) || &warn("missing information for output field $f");
  161. }
  162. defined($sourcepackage) || &error("unable to determine source package name !");
  163. $f{'Source'}= $sourcepackage;
  164. for $f (keys %remove) { delete $f{&capit($f)}; }
  165. $version= $f{'Version'};
  166. $version =~ s/^\d+://; $upstreamversion= $version; $upstreamversion =~ s/-[^-]*$//;
  167. $basenamerev= $sourcepackage.'_'.$version;
  168. $basename= $sourcepackage.'_'.$upstreamversion;
  169. $basedirname= $basename;
  170. #print STDERR ">$basedirname<\n";
  171. $basedirname =~ s/_/-/;
  172. #print STDERR ">$basedirname<\n";
  173. $origdir= "$dir.orig";
  174. $origtargz= "$basename.orig.tar.gz";
  175. if (@ARGV) {
  176. $origarg= shift(@ARGV);
  177. if (length($origarg)) {
  178. stat($origarg) || &error("cannot stat orig argument $origarg: $!");
  179. if (-d _) {
  180. $origdir= $origarg;
  181. $origdir= "./$origdir" unless $origdir =~ m,^/,; $origdir =~ s,/*$,,;
  182. $sourcestyle =~ y/aA/rR/;
  183. $sourcestyle =~ m/[ursURS]/ ||
  184. &error("orig argument is unpacked but source handling style".
  185. " -s$sourcestyle calls for packed (.orig.tar.gz)");
  186. } elsif (-f _) {
  187. $origtargz= $origarg;
  188. $sourcestyle =~ y/aA/pP/;
  189. $sourcestyle =~ m/[kpsKPS]/ ||
  190. &error("orig argument is packed but source handling style".
  191. " -s$sourcestyle calls for unpacked (.orig/)");
  192. } else {
  193. &error("orig argument $origarg is not a plain file or directory");
  194. }
  195. } else {
  196. $sourcestyle =~ y/aA/nn/;
  197. $sourcestyle =~ m/n/ ||
  198. &error("orig argument is empty (means no orig, no diff)".
  199. " but source handling style -s$sourcestyle wants something");
  200. }
  201. }
  202. if ($sourcestyle =~ m/[aA]/) {
  203. if (stat("$origtargz")) {
  204. -f _ || &error("packed orig \`$origtargz' exists but is not a plain file");
  205. $sourcestyle =~ y/aA/pP/;
  206. } elsif ($! != ENOENT) {
  207. &syserr("unable to stat putative packed orig \`$origtargz'");
  208. } elsif (stat("$origdir")) {
  209. -d _ || &error("unpacked orig \`$origdir' exists but is not a directory");
  210. $sourcestyle =~ y/aA/rR/;
  211. } elsif ($! != ENOENT) {
  212. &syserr("unable to stat putative unpacked orig \`$origdir'");
  213. } else {
  214. $sourcestyle =~ y/aA/nn/;
  215. }
  216. }
  217. $dirbase= $dir; $dirbase =~ s,/?$,,; $dirbase =~ s,[^/]+$,,; $dirname= $&;
  218. $dirname eq $basedirname || &warn("source directory \`$dir' is not <sourcepackage>".
  219. "-<upstreamversion> \`$basedirname'");
  220. if ($sourcestyle ne 'n') {
  221. $origdirbase= $origdir; $origdirbase =~ s,/?$,,;
  222. $origdirbase =~ s,[^/]+$,,; $origdirname= $&;
  223. $origdirname eq "$basedirname.orig" ||
  224. &warn(".orig directory name $origdirname is not <package>".
  225. "-<upstreamversion> (wanted $basedirname.orig)");
  226. $tardirbase= $origdirbase; $tardirname= $origdirname;
  227. $tarname= $origtargz;
  228. $tarname eq "$basename.orig.tar.gz" ||
  229. &warn(".orig.tar.gz name $tarname is not <package>_<upstreamversion>".
  230. ".orig.tar.gz (wanted $basename.orig.tar.gz)");
  231. } else {
  232. $tardirbase= $dirbase; $tardirname= $dirname;
  233. $tarname= "$basenamerev.tar.gz";
  234. }
  235. #print STDERR ">$dir|$origdir|$origtargz|$sourcestyle<\n";
  236. if ($sourcestyle =~ m/[nurUR]/) {
  237. if (stat($tarname)) {
  238. $sourcestyle =~ m/[nUR]/ ||
  239. &error("tarfile \`$tarname' already exists, not overwriting,".
  240. " giving up; use -sU or -sR to override");
  241. } elsif ($! != ENOENT) {
  242. &syserr("unable to check for existence of \`$tarname'");
  243. }
  244. #print STDERR ">$tarname|$tardirbase|$tardirname<\n";
  245. print("$progname: building $sourcepackage in $tarname\n")
  246. || &syserr("write building tar message");
  247. &forkgzipwrite("$tarname.new");
  248. defined($c2= fork) || &syserr("fork for tar");
  249. if (!$c2) {
  250. chdir($tardirbase) || &syserr("chdir to above (orig) source $tardirbase");
  251. #system('pwd && ls');
  252. open(STDOUT,">&GZIP") || &syserr("reopen gzip for tar");
  253. # FIXME: put `--' argument back when tar is fixed
  254. exec('tar','-cf','-',$tardirname); &syserr("exec tar");
  255. }
  256. close(GZIP);
  257. &reapgzip;
  258. $c2 == waitpid($c2,0) || &syserr("wait for tar");
  259. $? && !(WIFSIGNALED($c2) && WTERMSIG($c2) == SIGPIPE) && subprocerr("tar");
  260. rename("$tarname.new",$tarname) ||
  261. &syserr("unable to rename \`$tarname.new' (newly created) to \`$tarname'");
  262. } else {
  263. print("$progname: building $sourcepackage using existing $tarname\n")
  264. || &syserr("write using existing tar message");
  265. }
  266. addfile("$tarname");
  267. if ($sourcestyle =~ m/[kpKP]/) {
  268. if (stat($origdir)) {
  269. $sourcestyle =~ m/[KP]/ ||
  270. &error("orig dir \`$origdir' already exists, not overwriting,".
  271. " giving up; use -sA, -sK or -sP to override");
  272. erasedir($origdir);
  273. } elsif ($! != ENOENT) {
  274. &syserr("unable to check for existence of orig dir \`$origdir'");
  275. }
  276. $expectprefix= $origdir; $expectprefix =~ s,^\./,,;
  277. checktarsane($origtargz,$expectprefix);
  278. mkdir("$origtargz.tmp-nest",0755) ||
  279. &syserr("unable to create \`$origtargz.tmp-nest'");
  280. extracttar($origtargz,"$origtargz.tmp-nest",$expectprefix);
  281. rename("$origtargz.tmp-nest/$expectprefix",$expectprefix) ||
  282. &syserr("unable to rename \`$origtargz.tmp-nest/$expectprefix' to ".
  283. "\`$expectprefix'");
  284. rmdir("$origtargz.tmp-nest") ||
  285. &syserr("unable to remove \`$origtargz.tmp-nest'");
  286. }
  287. if ($sourcestyle =~ m/[kpursKPUR]/) {
  288. print("$progname: building $sourcepackage in $basenamerev.diff.gz\n")
  289. || &syserr("write building diff message");
  290. &forkgzipwrite("$basenamerev.diff.gz");
  291. defined($c2= open(FIND,"-|")) || &syserr("fork for find");
  292. if (!$c2) {
  293. chdir($dir) || &syserr("chdir to $dir for find");
  294. exec('find','.','-print0'); &syserr("exec find");
  295. }
  296. $/= "\0";
  297. file:
  298. while (defined($fn= <FIND>)) {
  299. $fn =~ s/\0$//; $fn =~ s,^\./,,;
  300. lstat("$dir/$fn") || &syserr("cannot stat file $dir/$fn");
  301. if (-l _) {
  302. $type{$fn}= 'symlink';
  303. &checktype('-l') || next;
  304. defined($n= readlink("$dir/$fn")) ||
  305. &syserr("cannot read link $dir/$fn");
  306. defined($n2= readlink("$origdir/$fn")) ||
  307. &syserr("cannot read orig link $origdir/$fn");
  308. $n eq $n2 || &unrepdiff2("symlink to $n2","symlink to $n");
  309. } elsif (-f _) {
  310. $type{$fn}= 'plain file';
  311. if (!lstat("$origdir/$fn")) {
  312. $! == ENOENT || &syserr("cannot stat orig file $origdir/$fn");
  313. $ofnread= '/dev/null';
  314. } elsif (-f _) {
  315. $ofnread= "$origdir/$fn";
  316. } else {
  317. &unrepdiff2("something else","plain file");
  318. next;
  319. }
  320. defined($c3= open(DIFFGEN,"-|")) || &syserr("fork for diff");
  321. if (!$c3) {
  322. exec('diff','-u',
  323. '-L',"$basedirname.orig/$fn",
  324. '-L',"$basedirname/$fn",
  325. '--',"$ofnread","$dir/$fn"); &syserr("exec diff");
  326. }
  327. $difflinefound= 0;
  328. $/= "\n";
  329. while (<DIFFGEN>) {
  330. if (m/^binary/i) {
  331. close(DIFFGEN); $/= "\0";
  332. &unrepdiff("binary file contents changed");
  333. next file;
  334. } elsif (m/^[-+\@ ]/) {
  335. $difflinefound=1;
  336. } elsif (m/^\\ No newline at end of file$/) {
  337. &warn("file $fn has no final newline ".
  338. "(either original or modified version)");
  339. } else {
  340. s/\n$//;
  341. &internerr("unknown line from diff -u on $fn: \`$_'");
  342. }
  343. print(GZIP $_) || &syserr("failed to write to gzip");
  344. }
  345. close(DIFFGEN); $/= "\0";
  346. if (WIFEXITED($?) && (($es=WEXITSTATUS($?))==0 || $es==1)) {
  347. if ($es==1 && !$difflinefound) {
  348. &unrepdiff("diff gave 1 but no diff lines found");
  349. }
  350. } else {
  351. subprocerr("diff on $dir/$fn");
  352. }
  353. } elsif (-p _) {
  354. $type{$fn}= 'pipe';
  355. &checktype('-p');
  356. } elsif (-b _ || -c _ || -S _) {
  357. &unrepdiff("device or socket is not allowed");
  358. } elsif (-d _) {
  359. $type{$fn}= 'directory';
  360. } else {
  361. &unrepdiff("unknown file type ($!)");
  362. }
  363. }
  364. close(FIND); $? && subprocerr("find on $dir");
  365. close(GZIP) || &syserr("finish write to gzip pipe");
  366. &reapgzip;
  367. defined($c2= open(FIND,"-|")) || &syserr("fork for 2nd find");
  368. if (!$c2) {
  369. chdir($origdir) || &syserr("chdir to $origdir for 2nd find");
  370. exec('find','.','-print0'); &syserr("exec 2nd find");
  371. }
  372. $/= "\0";
  373. while (defined($fn= <FIND>)) {
  374. $fn =~ s/\0$//; $fn =~ s,^\./,,;
  375. next if defined($type{$fn});
  376. lstat("$origdir/$fn") || &syserr("cannot check orig file $origdir/$fn");
  377. if (-f _) {
  378. &warn("ignoring deletion of file $fn");
  379. } elsif (-d _) {
  380. &warn("ignoring deletion of directory $fn");
  381. } elsif (-l _) {
  382. &warn("ignoring deletion of symlink $fn");
  383. } else {
  384. &unrepdiff2('not a file, directory or link','nonexistent');
  385. }
  386. }
  387. close(FIND); $? && subprocerr("find on $dirname");
  388. &addfile("$basenamerev.diff.gz");
  389. }
  390. if ($sourcestyle =~ m/[prPR]/) {
  391. erasedir($origdir);
  392. }
  393. print("$progname: building $sourcepackage in $basenamerev.dsc\n")
  394. || &syserr("write building message");
  395. open(STDOUT,"> $basenamerev.dsc") || &syserr("create $basenamerev.dsc");
  396. &outputclose(1);
  397. if ($ur) {
  398. print(STDERR "$progname: unrepresentable changes to source\n")
  399. || &syserr("write error msg: $!");
  400. exit(1);
  401. }
  402. exit(0);
  403. } else {
  404. $sourcestyle =~ y/X/p/;
  405. $sourcestyle =~ m/[pun]/ ||
  406. &usageerr("source handling style -s$sourcestyle not allowed with -x");
  407. @ARGV==1 || &usageerr("-x needs exactly one argument, the .dsc");
  408. $dsc= shift(@ARGV);
  409. $dsc= "./$dsc" unless $dsc =~ m:^/:;
  410. $dscdir= $dsc; $dscdir= "./$dscdir" unless $dsc =~ m,^/|^\./,;
  411. $dscdir =~ s,/[^/]+$,,;
  412. open(CDATA,"< $dsc") || &error("cannot open .dsc file $dsc: $!");
  413. &parsecdata('S',-1,"source control file $dsc");
  414. close(CDATA);
  415. for $f (qw(Source Version Files)) {
  416. defined($fi{"S $f"}) ||
  417. &error("missing critical source control field $f");
  418. }
  419. $sourcepackage= $fi{'S Source'};
  420. $sourcepackage =~ m/[^-+.0-9a-z]/ &&
  421. &error("source package name contains illegal character \`$&'");
  422. $sourcepackage =~ m/^[0-9a-z]./ ||
  423. &error("source package name is too short or starts with non-alphanum");
  424. $version= $fi{'S Version'};
  425. $version =~ m/[^-+:.0-9a-zA-Z]/ &&
  426. &error("version number contains illegal character \`$&'");
  427. $version =~ s/^\d+://;
  428. if ($version =~ m/-([^-]+)$/) {
  429. $baseversion= $`; $revision= $1;
  430. } else {
  431. $baseversion= $version; $revision= '';
  432. }
  433. $files= $fi{'S Files'};
  434. for $file (split(/\n /,$files)) {
  435. next if $file eq '';
  436. $file =~ m/^([0-9a-f]{32})[ \t]+(\d+)[ \t]+([0-9a-zA-Z][-+:.,=0-9a-zA-Z_]+)$/
  437. || &error("Files field contains bad line \`$file'");
  438. ($md5sum{$3},$size{$3},$file) = ($1,$2,$3);
  439. &setfile(\$tarfile) if $file =~ m/\.tar\.gz$/;
  440. &setfile(\$difffile) if $file =~ m/\.diff\.gz$/;
  441. }
  442. $newdirectory= $sourcepackage.'-'.$baseversion;
  443. $expectprefix= $newdirectory; $expectprefix.= '.orig' if length($difffile);
  444. length($tarfile) || &error("no tarfile in Files field");
  445. checkstats($tarfile);
  446. checkstats($difffile) if length($difffile);
  447. checktarsane("$dscdir/$tarfile",$expectprefix);
  448. if (length($difffile)) {
  449. &forkgzipread("$dscdir/$difffile");
  450. $/="\n";
  451. $_ = <GZIP>;
  452. do {
  453. # read file header (---/+++ pair)
  454. s/\n$// or &error("diff is missing trailing newline");
  455. /^--- / or &error("expected ^--- in line $. of diff");
  456. $fn= $';
  457. substr($fn,0,length($expectprefix)+1) eq "$expectprefix/" ||
  458. &error("diff patches file ($fn) not in expected subdirectory");
  459. $fn =~ m/\.dpkg-orig$/ &&
  460. &error("diff patches file with name ending .dpkg-orig");
  461. $dirname= $fn;
  462. if ($dirname =~ s,/[^/]+$,, && !defined($dirincluded{$dirname})) {
  463. $dirtocreate{$dirname} = 1;
  464. }
  465. defined($notfileobject{$fn}) &&
  466. &error("diff patches something which is not a plain file");
  467. $_= <GZIP>; s/\n$// ||
  468. &error("diff finishes in middle of ---/+++ (line $.)");
  469. $_ eq '+++ '.$newdirectory.substr($fn,length($expectprefix)) ||
  470. &error("line after --- for file $fn isn't as expected");
  471. $filepatched{$fn}++ && &error("diff patches file $fn twice");
  472. # read hunks
  473. my $hunk = 0;
  474. while (($_ = <GZIP>) && !/^--- /) {
  475. # read hunk header (@@)
  476. s/\n$// or &error("diff is missing trailing newline");
  477. /^@@ -\d+(,(\d+))? \+\d+(,(\d+))? @\@$/ or
  478. &error("Expected ^@@ in line $. of diff");
  479. my ($olines, $nlines) = ($1 ? $2 : 1, $3 ? $4 : 1);
  480. ++$hunk;
  481. # read hunk
  482. while ($olines || $nlines) {
  483. $_ = <GZIP> or &error("unexpected end of diff");
  484. s/\n$// or &error("diff is missing trailing newline");
  485. if (/^ /) { --$olines; --$nlines; }
  486. elsif (/^-/) { --$olines; }
  487. elsif (/^\+/) { --$nlines; }
  488. else { &error("expected [ +-] at start of line $. of diff"); }
  489. }
  490. }
  491. $hunk or &error("expected ^\@\@ at line $. of diff");
  492. } while ($_ || !eof(GZIP));
  493. close(GZIP);
  494. &reapgzip;
  495. }
  496. print("$progname: extracting $sourcepackage in $newdirectory\n")
  497. || &syserr("write extracting message");
  498. &erasedir($newdirectory);
  499. &erasedir("$newdirectory.orig");
  500. mkdir("$expectprefix.tmp-nest",0755)
  501. || &syserr("unable to create \`$expectprefix.tmp-nest'");
  502. extracttar("$dscdir/$tarfile","$expectprefix.tmp-nest","$expectprefix");
  503. rename("$expectprefix.tmp-nest/$expectprefix","$expectprefix")
  504. || &syserr("unable to rename \`$expectprefix.tmp-nest/$expectprefix' "
  505. ."to \`$expectprefix'");
  506. rmdir("$expectprefix.tmp-nest")
  507. || &syserr("unable to remove \`$expectprefix.tmp-nest'");
  508. for $dircreate (keys %dirtocreate) {
  509. $dircreatem= "";
  510. for $dircreatep (split("/",$dirc)) {
  511. $dircreatem.= $dircreatep;
  512. if (!lstat($dircreatem)) {
  513. $! == ENOENT || &syserr("cannot stat $dircreatem");
  514. mkdir($dircreatem,0777)
  515. || &syserr("failed to create $dircreatem subdirectory");
  516. }
  517. else {
  518. -d _ || &error("diff patches file in directory \`$dircreate',"
  519. ." but $dircreatem isn't a directory !");
  520. }
  521. }
  522. }
  523. if (length($difffile)) {
  524. rename($expectprefix,$newdirectory) ||
  525. &syserr("failed to rename newly-extracted $expectprefix to $newdirectory");
  526. if ($sourcestyle =~ m/u/) {
  527. mkdir("$expectprefix.tmp-nest",0755)
  528. || &syserr("unable to create \`$expectprefix.tmp-nest'");
  529. extracttar("$dscdir/$tarfile","$expectprefix.tmp-nest",
  530. "$expectprefix");
  531. rename("$expectprefix.tmp-nest/$expectprefix","$expectprefix")
  532. || &syserr("unable to rename \`$expectprefix.tmp-nest/"
  533. ."$expectprefix' to \`$expectprefix'");
  534. rmdir("$expectprefix.tmp-nest")
  535. || &syserr("unable to remove \`$expectprefix.tmp-nest'");
  536. } elsif ($sourcestyle =~ m/p/) {
  537. stat("$dscdir/$tarfile") ||
  538. &syserr("failed to stat \`$dscdir/$tarfile' to see if need to copy");
  539. ($dsctardev,$dsctarino) = stat _;
  540. $dumptar= $sourcepackage.'_'.$baseversion.'.orig.tar.gz';
  541. if (!stat($dumptar)) {
  542. $! == ENOENT || &syserr("failed to check destination \`$dumptar'".
  543. " to see if need to copy");
  544. } else {
  545. ($dumptardev,$dumptarino) = stat _;
  546. if ($dumptardev == $dsctardev && $dumptarino == $dsctarino) {
  547. $dumptar= '';
  548. }
  549. }
  550. if (length($dumptar)) {
  551. system('cp','--',"$dscdir/$tarfile","$dumptar");
  552. $? && subprocerr("cp $dscdir/$tarfile to $dumptar");
  553. }
  554. }
  555. &forkgzipread("$dscdir/$difffile");
  556. defined($c2= fork) || &syserr("fork for patch");
  557. if (!$c2) {
  558. open(STDIN,"<&GZIP") || &syserr("reopen gzip for patch");
  559. chdir($newdirectory) || &syserr("chdir to $newdirectory for patch");
  560. exec('patch','-s','-t','-F','0','-N','-p1','-u',
  561. '-V','never','-g0','-b','-z','.dpkg-orig');
  562. &syserr("exec patch");
  563. }
  564. close(GZIP);
  565. $c2 == waitpid($c2,0) || &syserr("wait for patch");
  566. $? && subprocerr("patch");
  567. &reapgzip;
  568. for $fn (keys %filepatched) {
  569. $ftr= "$newdirectory/".substr($fn,length($expectprefix)+1).".dpkg-orig";
  570. unlink($ftr) || &syserr("remove patch backup file $ftr");
  571. }
  572. if (!(@s= lstat("$newdirectory/debian/rules"))) {
  573. $! == ENOENT || &syserr("cannot stat $newdirectory/debian/rules");
  574. &warn("$newdirectory/debian/rules does not exist");
  575. } elsif (-f _) {
  576. chmod($s[2] | 0111, "$newdirectory/debian/rules") ||
  577. &syserr("cannot make $newdirectory/debian/rules executable");
  578. } else {
  579. &warn("$newdirectory/debian/rules is not a plain file");
  580. }
  581. }
  582. $execmode= 0777 & ~umask;
  583. (@s= stat('.')) || &syserr("cannot stat \`.'");
  584. $dirmode= $execmode | ($s[2] & 02000);
  585. $plainmode= $execmode & ~0111;
  586. $fifomode= ($plainmode & 0222) | (($plainmode & 0222) << 1);
  587. for $fn (@filesinarchive) {
  588. $fn= substr($fn,length($expectprefix)+1);
  589. $fn= "$newdirectory/$fn";
  590. (@s= lstat($fn)) || &syserr("cannot stat extracted object \`$fn'");
  591. $mode= $s[2];
  592. if (-d _) {
  593. $newmode= $dirmode;
  594. } elsif (-f _) {
  595. $newmode= ($mode & 0111) ? $execmode : $plainmode;
  596. } elsif (-p _) {
  597. $newmode= $fifomode;
  598. } elsif (!-l _) {
  599. &internerr("unknown object \`$fn' after extract (mode ".
  600. sprintf("0%o",$mode).")");
  601. } else { next; }
  602. next if ($mode & 07777) == $newmode;
  603. chmod($newmode,$fn) ||
  604. &syserr(sprintf("cannot change mode of \`%s' to 0%o from 0%o",
  605. $fn,$newmode,$mode));
  606. }
  607. exit(0);
  608. }
  609. sub checkstats {
  610. my ($f) = @_;
  611. my @s;
  612. my $m;
  613. open(STDIN,"< $dscdir/$f") || &syserr("cannot read $dscdir/$f");
  614. (@s= stat(STDIN)) || &syserr("cannot fstat $dscdir/$f");
  615. $s[7] == $size{$f} || &error("file $f has size $s[7] instead of expected $size{$f}");
  616. $m= `md5sum`; $? && subprocerr("md5sum $f"); $m =~ s/\n$//;
  617. $m =~ m/^[0-9a-f]{32}$/ || &failure("md5sum of $f gave bad output \`$m'");
  618. $m eq $md5sum{$f} || &error("file $f has md5sum $m instead of expected $md5sum{$f}");
  619. open(STDIN,"</dev/null") || &syserr("reopen stdin from /dev/null");
  620. }
  621. sub erasedir {
  622. my ($dir) = @_;
  623. if (!lstat($dir)) {
  624. $! == ENOENT && return;
  625. &syserr("cannot stat directory $dir (before removal)");
  626. }
  627. system 'rm','-rf','--',$dir;
  628. $? && subprocerr("rm -rf $dir");
  629. if (!stat($dir)) {
  630. $! == ENOENT && return;
  631. &syserr("unable to check for removal of dir \`$dir'");
  632. }
  633. &failure("rm -rf failed to remove \`$dir'");
  634. }
  635. use strict 'vars';
  636. sub checktarcpio {
  637. my ($tarfileread, $wpfx) = @_;
  638. my ($tarprefix, $c2);
  639. @filesinarchive = ();
  640. # make <CPIO> read from the uncompressed archive file
  641. &forkgzipread ("$tarfileread");
  642. if (! defined ($c2 = open (CPIO,"-|"))) { &syserr ("fork for cpio"); }
  643. if (!$c2) {
  644. open (STDIN,"<&GZIP") || &syserr ("reopen gzip for cpio");
  645. &cpiostderr;
  646. exec ('cpio','-0t');
  647. &syserr ("exec cpio");
  648. }
  649. close (GZIP);
  650. $/ = "\0";
  651. open (CPIO, "<cpiolog");
  652. while (defined ($fn = <CPIO>)) {
  653. $fn =~ s/\0$//;
  654. # store printable name of file for error messages
  655. my $pname = $fn;
  656. $pname =~ y/ -~/?/c;
  657. if (! $tarprefix) {
  658. if ($fn =~ m/\n/) {
  659. &error("first output from cpio -0t (from \`$tarfileread') ".
  660. "contains newline - you probably have an out of ".
  661. "date version of cpio. GNU cpio 2.4.2-2 is known to work");
  662. }
  663. $tarprefix = ($fn =~ m,([^/]*)[/],)[0];
  664. # need to check for multiple dots on some operating systems
  665. # empty tarprefix (due to regex failer) will match emptry string
  666. if ($tarprefix =~ /^[.]*$/) {
  667. &error("tarfile \`$tarfileread' does not extract into a ".
  668. "directory off the current directory ($tarprefix from $pname)");
  669. }
  670. }
  671. if ($fn =~ m/\n/) {
  672. &error ("tarfile \`$tarfileread' contains object with".
  673. " newline in its name ($pname)");
  674. }
  675. next if ($fn eq '././@LongLink');
  676. my $fprefix = substr ($fn, 0, length ($tarprefix));
  677. my $slash = substr ($fn, length ($tarprefix), 1);
  678. if ((($slash ne '/') && ($slash ne '')) || ($fprefix ne $tarprefix)) {
  679. &error ("tarfile \`$tarfileread' contains object ($pname) ".
  680. "not in expected directory ($tarprefix)");
  681. }
  682. # need to check for multiple dots on some operating systems
  683. if ($fn =~ m/[.]{2,}/) {
  684. &error ("tarfile \`$tarfileread' contains object with".
  685. " /../ in its name ($pname)");
  686. }
  687. push (@filesinarchive, $fn);
  688. }
  689. close (CPIO);
  690. $? && subprocerr ("cpio");
  691. &reapgzip;
  692. $/= "\n";
  693. my $tarsubst = quotemeta ($tarprefix);
  694. @filesinarchive = map { s/^$tarsubst/$wpfx/; $_ } @filesinarchive;
  695. return $tarprefix;
  696. }
  697. sub checktarsane {
  698. my ($tarfileread, $wpfx) = @_;
  699. my ($c2);
  700. %dirincluded = ();
  701. %notfileobject = ();
  702. my $tarprefix = &checktarcpio ($tarfileread, $wpfx);
  703. # make <TAR> read from the uncompressed archive file
  704. &forkgzipread ("$tarfileread");
  705. if (! defined ($c2 = open (TAR,"-|"))) { &syserr ("fork for tar -t"); }
  706. if (! $c2) {
  707. $ENV{'LANG'}= 'C';
  708. open (STDIN, "<&GZIP") || &syserr ("reopen gzip for tar -t");
  709. exec ('tar', '-vvtf', '-'); &syserr ("exec tar -vvtf -");
  710. }
  711. close (GZIP);
  712. my $efix= 0;
  713. open (TAR, "<tarlog");
  714. while (<TAR>) {
  715. chomp;
  716. if (! m,^(\S{10})\s,) {
  717. &error("tarfile \`$tarfileread' contains unknown object ".
  718. "listed by tar as \`$_'");
  719. }
  720. my $mode = $1;
  721. $mode =~ s/^([-dpsl])// ||
  722. &error("tarfile \`$tarfileread' contains object \`$fn' with ".
  723. "unknown or forbidden type \`".substr($_,0,1)."'");
  724. my $type = $&;
  725. if ($mode =~ /^l/) { $_ =~ s/ -\> .*//; }
  726. s/ link to .+//;
  727. if (length ($_) <= 48) {
  728. &error ("tarfile \`$tarfileread' contains incomplete entry \`$_'\n");
  729. }
  730. my $tarfn = substr ($_, 48, length ($_) - 48);
  731. $tarfn = deoctify ($tarfn);
  732. # store printable name of file for error messages
  733. my $pname = $tarfn;
  734. $pname =~ y/ -~/?/c;
  735. # fetch name of file as given by cpio
  736. $fn = $filesinarchive[$efix++];
  737. if ($tarfn ne $fn) {
  738. if ((length ($fn) == 99) && (length ($tarfn) >= 99)
  739. && (substr ($fn, 0, 99) eq substr ($tarfn, 0, 99))) {
  740. # this file doesn't match because cpio truncated the name
  741. # to the first 100 characters. let it slide for now.
  742. &warn ("filename \`$pname' was truncated by cpio;" .
  743. " unable to check full pathname");
  744. } else {
  745. &error ("tarfile \`$tarfileread' contains unexpected object".
  746. " listed by tar as \`$_'; expected \`$pname'");
  747. }
  748. }
  749. # if cpio truncated the name above,
  750. # we still can't allow files to expand into /../
  751. # need to check for multiple dots on some operating systems
  752. if ($tarfn =~ m/[.]{2,}/) {
  753. &error ("tarfile \`$tarfileread' contains object with".
  754. "/../ in its name ($pname)");
  755. }
  756. if ($tarfn =~ /\.dpkg-orig$/) {
  757. &error ("tarfile \`$tarfileread' contains file with name ending in .dpkg-orig");
  758. }
  759. if ($mode =~ /[sStT]/ && $type ne 'd') {
  760. &error ("tarfile \`$tarfileread' contains setuid, setgid".
  761. " or sticky object \`$pname'");
  762. }
  763. if ($tarfn eq "$tarprefix/debian" && $type ne 'd') {
  764. &error ("tarfile \`$tarfileread' contains object \`debian'".
  765. " that isn't a directory");
  766. }
  767. if ($type eq 'd') { $tarfn =~ s,/$,,; }
  768. my $dirname = $tarfn;
  769. if (($dirname =~ s,/[^/]+$,,) && (! defined ($dirincluded{$dirname}))) {
  770. &error ("tarfile \`$tarfileread' contains object \`$pname' but its containing ".
  771. "directory \`$dirname' does not precede it");
  772. }
  773. if ($type eq 'd') { $dirincluded{$tarfn} = 1; }
  774. if ($type ne '-') { $notfileobject{$tarfn} = 1; }
  775. }
  776. close (TAR);
  777. #$? && subprocerr ("tar -vvtf");
  778. &reapgzip;
  779. my $tarsubst = quotemeta ($tarprefix);
  780. %dirincluded = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %dirincluded);
  781. %notfileobject = map { s/^$tarsubst/$wpfx/; $_=>1 } (keys %notfileobject);
  782. }
  783. no strict 'vars';
  784. sub extracttar {
  785. my ($tarfileread,$dirchdir,$newtopdir) = @_;
  786. &forkgzipread("$tarfileread");
  787. defined($c2= fork) || &syserr("fork for tar -xkf -");
  788. if (!$c2) {
  789. chdir("$dirchdir") || &syserr("cannot chdir to \`$dirchdir' for tar extract");
  790. open(STDIN,"<&GZIP") || &syserr("reopen gzip for cpio -i");
  791. &cpiostderr;
  792. exec('tar','-xkf','-'); &syserr("exec tar -xkf -");
  793. }
  794. close(GZIP);
  795. $c2 == waitpid($c2,0) || &syserr("wait for tar -xkf -");
  796. $? && subprocerr("tar -xkf -");
  797. &reapgzip;
  798. opendir(D,"$dirchdir") || &syserr("Unable to open dir $dirchdir");
  799. @dirchdirfiles = grep($_ ne "." && $_ ne "..",readdir(D));
  800. closedir(D) || &syserr("Unable to close dir $dirchdir");
  801. (@dirchdirfiles==1 && -d "$dirchdir/$dirchdirfiles[0]") ||
  802. &error("$tarfileread extracted into >1 directory");
  803. rename("$dirchdir/$dirchdirfiles[0]", "$dirchdir/$newtopdir") ||
  804. &syserr("Unable to rename $dirchdir/$dirchdirfiles[0] to ".
  805. "$dirchdir/$newtopdir");
  806. }
  807. sub cpiostderr {
  808. open(STDERR,"| egrep -v '^[0-9]+ blocks\$' >&2") ||
  809. &syserr("reopen stderr for cpio to grep out blocks message");
  810. }
  811. sub setfile {
  812. my ($varref) = @_;
  813. if (defined ($$varref)) {
  814. &error ("repeated file type - files " . $$varref . " and $file");
  815. }
  816. $$varref = $file;
  817. }
  818. sub checktype {
  819. if (!lstat("$origdir/$fn")) {
  820. &unrepdiff2("nonexistent",$type{$fn});
  821. } else {
  822. $v= eval("$_[0] _ ? 2 : 1"); $v || &internerr("checktype $@ ($_[0])");
  823. return 1 if $v == 2;
  824. &unrepdiff2("something else",$type{$fn});
  825. }
  826. return 0;
  827. }
  828. sub setopmode {
  829. defined($opmode) && &usageerr("only one of -x or -b allowed, and only once");
  830. $opmode= $_[0];
  831. }
  832. sub unrepdiff {
  833. print(STDERR "$progname: cannot represent change to $fn: $_[0]\n")
  834. || &syserr("write syserr unrep");
  835. $ur++;
  836. }
  837. sub unrepdiff2 {
  838. print(STDERR "$progname: cannot represent change to $fn:\n".
  839. "$progname: new version is $_[1]\n".
  840. "$progname: old version is $_[0]\n")
  841. || &syserr("write syserr unrep");
  842. $ur++;
  843. }
  844. sub forkgzipwrite {
  845. #print STDERR "forkgzipwrite $_[0]\n";
  846. open(GZIPFILE,"> $_[0]") || &syserr("create file $_[0]");
  847. pipe(GZIPREAD,GZIP) || &syserr("pipe for gzip");
  848. defined($cgz= fork) || &syserr("fork for gzip");
  849. if (!$cgz) {
  850. open(STDIN,"<&GZIPREAD") || &syserr("reopen gzip pipe"); close(GZIPREAD);
  851. close(GZIP); open(STDOUT,">&GZIPFILE") || &syserr("reopen tar.gz");
  852. exec('gzip','-9'); &syserr("exec gzip");
  853. }
  854. close(GZIPREAD);
  855. $gzipsigpipeok= 0;
  856. }
  857. sub forkgzipread {
  858. #print STDERR "forkgzipread $_[0]\n";
  859. open(GZIPFILE,"< $_[0]") || &syserr("read file $_[0]");
  860. pipe(GZIP,GZIPWRITE) || &syserr("pipe for gunzip");
  861. defined($cgz= fork) || &syserr("fork for gunzip");
  862. if (!$cgz) {
  863. open(STDOUT,">&GZIPWRITE") || &syserr("reopen gunzip pipe"); close(GZIPWRITE);
  864. close(GZIP); open(STDIN,"<&GZIPFILE") || &syserr("reopen input file");
  865. exec('gunzip'); &syserr("exec gunzip");
  866. }
  867. close(GZIPWRITE);
  868. $gzipsigpipeok= 1;
  869. }
  870. sub reapgzip {
  871. #print STDERR "reapgzip $_[0]\n";
  872. $cgz == waitpid($cgz,0) || &syserr("wait for gzip");
  873. !$? || ($gzipsigpipeok && WIFSIGNALED($?) && WTERMSIG($?)==SIGPIPE) ||
  874. subprocerr("gzip");
  875. close(GZIPFILE);
  876. }
  877. sub addfile {
  878. my ($filename)= @_;
  879. stat($filename) || &syserr("could not stat output file \`$filename'");
  880. $size= (stat _)[7];
  881. my $md5sum= `md5sum <$filename`;
  882. $? && &subprocerr("md5sum $filename");
  883. $md5sum =~ s/^([0-9a-f]{32})\n$/$1/ || &failure("md5sum gave bogus output \`$_'");
  884. $f{'Files'}.= "\n $md5sum $size $filename";
  885. }
  886. # replace \ddd with their corresponding character, refuse \ddd > \377
  887. # modifies $_ (hs)
  888. {
  889. my $backslash;
  890. sub deoctify {
  891. my $fn= $_[0];
  892. $backslash= sprintf("\\%03o", unpack("C", "\\")) if !$backslash;
  893. s/\\{2}/$backslash/g;
  894. @_= split(/\\/, $fn);
  895. foreach (@_) {
  896. /^(\d{3})/ or next;
  897. &failure("bogus character `\\$1' in `$fn'\n") if oct($1) > 255;
  898. $_= pack("c", oct($1)) . $';
  899. }
  900. return join("", @_);
  901. } }