dpkg-source.pl 38 KB

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