controllib.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h);
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(warning error failure internerr syserr subprocerr);
  9. textdomain("dpkg-dev");
  10. our $sourcepackage; # - name of sourcepackage
  11. our %f; # - fields ???
  12. our %fi; # - map of fields values. keys are of the form "S# key"
  13. # where S is source (L is changelog, C is control)
  14. # and # is an index
  15. our %p2i; # - map from datafile+packagename to index in controlfile
  16. # (used if multiple packages can be listed). Key is
  17. # "S key" where S is the source and key is the packagename
  18. my $maxsubsts = 50;
  19. our %substvar; # - map with substitution variables
  20. my $parsechangelog = 'dpkg-parsechangelog';
  21. our @pkg_dep_fields = qw(Pre-Depends Depends Recommends Suggests Enhances
  22. Conflicts Breaks Replaces Provides);
  23. our @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  24. Build-Conflicts Build-Conflicts-Indep);
  25. sub getfowner
  26. {
  27. my $getlogin = getlogin();
  28. if (!defined($getlogin)) {
  29. open(SAVEIN, "<&STDIN");
  30. open(STDIN, "<&STDERR");
  31. $getlogin = getlogin();
  32. close(STDIN);
  33. open(STDIN, "<&SAVEIN");
  34. close(SAVEIN);
  35. }
  36. if (!defined($getlogin)) {
  37. open(SAVEIN, "<&STDIN");
  38. open(STDIN, "<&STDOUT");
  39. $getlogin = getlogin();
  40. close(STDIN);
  41. open(STDIN, "<&SAVEIN");
  42. close(SAVEIN);
  43. }
  44. my @fowner;
  45. if (defined($ENV{'LOGNAME'})) {
  46. @fowner = getpwnam($ENV{'LOGNAME'});
  47. if (!@fowner) {
  48. die(sprintf(_g('unable to get login information for username "%s"'), $ENV{'LOGNAME'}));
  49. }
  50. } elsif (defined($getlogin)) {
  51. @fowner = getpwnam($getlogin);
  52. if (!@fowner) {
  53. die(sprintf(_g('unable to get login information for username "%s"'), $getlogin));
  54. }
  55. } else {
  56. warning(sprintf(_g('no utmp entry available and LOGNAME not defined; using uid of process (%d)'), $<));
  57. @fowner = getpwuid($<);
  58. if (!@fowner) {
  59. die (sprintf(_g('unable to get login information for uid %d'), $<));
  60. }
  61. }
  62. @fowner = @fowner[2,3];
  63. return @fowner;
  64. }
  65. sub capit {
  66. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  67. return join '-', @pieces;
  68. }
  69. #
  70. # Architecture library
  71. #
  72. my (@cpu, @os);
  73. my (%cputable, %ostable);
  74. my (%cputable_re, %ostable_re);
  75. my %debtriplet_to_debarch;
  76. my %debarch_to_debtriplet;
  77. {
  78. my $host_arch;
  79. sub get_host_arch()
  80. {
  81. return $host_arch if defined $host_arch;
  82. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  83. $? && subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  84. chomp $host_arch;
  85. return $host_arch;
  86. }
  87. }
  88. sub get_valid_arches()
  89. {
  90. read_cputable() if (!@cpu);
  91. read_ostable() if (!@os);
  92. foreach my $os (@os) {
  93. foreach my $cpu (@cpu) {
  94. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  95. print $arch."\n" if defined($arch);
  96. }
  97. }
  98. }
  99. sub read_cputable
  100. {
  101. local $_;
  102. open CPUTABLE, "$pkgdatadir/cputable"
  103. or syserr(_g("unable to open cputable"));
  104. while (<CPUTABLE>) {
  105. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  106. $cputable{$1} = $2;
  107. $cputable_re{$1} = $3;
  108. push @cpu, $1;
  109. }
  110. }
  111. close CPUTABLE;
  112. }
  113. sub read_ostable
  114. {
  115. local $_;
  116. open OSTABLE, "$pkgdatadir/ostable"
  117. or syserr(_g("unable to open ostable"));
  118. while (<OSTABLE>) {
  119. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  120. $ostable{$1} = $2;
  121. $ostable_re{$1} = $3;
  122. push @os, $1;
  123. }
  124. }
  125. close OSTABLE;
  126. }
  127. sub read_triplettable()
  128. {
  129. read_cputable() if (!@cpu);
  130. local $_;
  131. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  132. or syserr(_g("unable to open triplettable"));
  133. while (<TRIPLETTABLE>) {
  134. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  135. my $debtriplet = $1;
  136. my $debarch = $2;
  137. if ($debtriplet =~ /<cpu>/) {
  138. foreach my $_cpu (@cpu) {
  139. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  140. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  141. $debarch_to_debtriplet{$da} = $dt;
  142. $debtriplet_to_debarch{$dt} = $da;
  143. }
  144. } else {
  145. $debarch_to_debtriplet{$2} = $1;
  146. $debtriplet_to_debarch{$1} = $2;
  147. }
  148. }
  149. }
  150. close TRIPLETTABLE;
  151. }
  152. sub debtriplet_to_gnutriplet(@)
  153. {
  154. read_cputable() if (!@cpu);
  155. read_ostable() if (!@os);
  156. my ($abi, $os, $cpu) = @_;
  157. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  158. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  159. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  160. }
  161. sub gnutriplet_to_debtriplet($)
  162. {
  163. my ($gnu) = @_;
  164. return undef unless defined($gnu);
  165. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  166. return undef unless defined($gnu_cpu) && defined($gnu_os);
  167. read_cputable() if (!@cpu);
  168. read_ostable() if (!@os);
  169. my ($os, $cpu);
  170. foreach my $_cpu (@cpu) {
  171. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  172. $cpu = $_cpu;
  173. last;
  174. }
  175. }
  176. foreach my $_os (@os) {
  177. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  178. $os = $_os;
  179. last;
  180. }
  181. }
  182. return undef if !defined($cpu) || !defined($os);
  183. return (split(/-/, $os, 2), $cpu);
  184. }
  185. sub debtriplet_to_debarch(@)
  186. {
  187. read_triplettable() if (!%debtriplet_to_debarch);
  188. my ($abi, $os, $cpu) = @_;
  189. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  190. return undef;
  191. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  192. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  193. } else {
  194. return undef;
  195. }
  196. }
  197. sub debarch_to_debtriplet($)
  198. {
  199. read_triplettable() if (!%debarch_to_debtriplet);
  200. local ($_) = @_;
  201. my $arch;
  202. if (/^linux-([^-]*)/) {
  203. # XXX: Might disappear in the future, not sure yet.
  204. $arch = $1;
  205. } else {
  206. $arch = $_;
  207. }
  208. my $triplet = $debarch_to_debtriplet{$arch};
  209. if (defined($triplet)) {
  210. return split('-', $triplet, 3);
  211. } else {
  212. return undef;
  213. }
  214. }
  215. sub debwildcard_to_debtriplet($)
  216. {
  217. local ($_) = @_;
  218. if (/any/) {
  219. if (/^([^-]*)-([^-]*)-(.*)/) {
  220. return ($1, $2, $3);
  221. } elsif (/^([^-]*)-([^-]*)$/) {
  222. return ('any', $1, $2);
  223. } else {
  224. return ($_, $_, $_);
  225. }
  226. } else {
  227. return debarch_to_debtriplet($_);
  228. }
  229. }
  230. sub debarch_eq($$)
  231. {
  232. my ($a, $b) = @_;
  233. return 1 if ($a eq $b);
  234. my @a = debarch_to_debtriplet($a);
  235. my @b = debarch_to_debtriplet($b);
  236. return 0 if grep(!defined, (@a, @b));
  237. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  238. }
  239. sub debarch_is($$)
  240. {
  241. my ($real, $alias) = @_;
  242. return 1 if ($alias eq $real or $alias eq 'any');
  243. my @real = debarch_to_debtriplet($real);
  244. my @alias = debwildcard_to_debtriplet($alias);
  245. return 0 if grep(!defined, (@real, @alias));
  246. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  247. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  248. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. sub substvars {
  254. my ($v) = @_;
  255. my $lhs;
  256. my $vn;
  257. my $rhs = '';
  258. my $count = 0;
  259. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  260. # If we have consumed more from the leftover data, then
  261. # reset the recursive counter.
  262. $count= 0 if (length($POSTMATCH) < length($rhs));
  263. $count < $maxsubsts ||
  264. &error(sprintf(_g("too many substitutions - recursive ? - in \`%s'"), $v));
  265. $lhs=$`; $vn=$1; $rhs=$';
  266. if (defined($substvar{$vn})) {
  267. $v= $lhs.$substvar{$vn}.$rhs;
  268. $count++;
  269. } else {
  270. warning(sprintf(_g("unknown substitution variable \${%s}"), $vn));
  271. $v= $lhs.$rhs;
  272. }
  273. }
  274. return $v;
  275. }
  276. my %fieldimps;
  277. sub set_field_importance(@)
  278. {
  279. my @fields = @_;
  280. my $i = 1;
  281. grep($fieldimps{$_} = $i++, @fields);
  282. }
  283. sub sort_field_by_importance($$)
  284. {
  285. my ($a, $b) = @_;
  286. if (defined $fieldimps{$a} && defined $fieldimps{$b}) {
  287. $fieldimps{$a} <=> $fieldimps{$b};
  288. } elsif (defined($fieldimps{$a})) {
  289. -1;
  290. } elsif (defined($fieldimps{$b})) {
  291. 1;
  292. } else {
  293. $a cmp $b;
  294. }
  295. }
  296. sub outputclose {
  297. my ($varlistfile) = @_;
  298. for my $f (keys %f) {
  299. $substvar{"F:$f"} = $f{$f};
  300. }
  301. &parsesubstvars($varlistfile) if (defined($varlistfile));
  302. for my $f (sort sort_field_by_importance keys %f) {
  303. my $v = $f{$f};
  304. if (defined($varlistfile)) {
  305. $v= &substvars($v);
  306. }
  307. $v =~ m/\S/ || next; # delete whitespace-only fields
  308. $v =~ m/\n\S/ && &internerr(sprintf(_g("field %s has newline then non whitespace >%s<"), $f, $v));
  309. $v =~ m/\n[ \t]*\n/ && &internerr(sprintf(_g("field %s has blank lines >%s<"), $f, $v));
  310. $v =~ m/\n$/ && &internerr(sprintf(_g("field %s has trailing newline >%s<"), $f, $v));
  311. if (defined($varlistfile)) {
  312. $v =~ s/,[\s,]*,/,/g;
  313. $v =~ s/^\s*,\s*//;
  314. $v =~ s/\s*,\s*$//;
  315. }
  316. $v =~ s/\$\{\}/\$/g;
  317. print("$f: $v\n") || &syserr(_g("write error on control data"));
  318. }
  319. close(STDOUT) || &syserr(_g("write error on close control data"));
  320. }
  321. sub parsecontrolfile {
  322. my $controlfile = shift;
  323. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  324. open(CDATA,"< $controlfile") || &error(sprintf(_g("cannot read control file %s: %s"), $controlfile, $!));
  325. binmode(CDATA);
  326. my $indices = parsecdata(\*CDATA, 'C', 1,
  327. sprintf(_g("control file %s"), $controlfile));
  328. $indices >= 2 || &error(_g("control file must have at least one binary package part"));
  329. for (my $i = 1; $i < $indices; $i++) {
  330. defined($fi{"C$i Package"}) ||
  331. &error(sprintf(_g("per-package paragraph %d in control ".
  332. "info file is missing Package line"),
  333. $i));
  334. }
  335. defined($fi{"C Source"}) ||
  336. &error(_g("source paragraph in control info file is ".
  337. "missing Source line"));
  338. }
  339. my $substvarsparsed = 0;
  340. sub parsesubstvars {
  341. my $varlistfile = shift;
  342. if (length($varlistfile) && !$substvarsparsed) {
  343. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  344. if (open(SV,"< $varlistfile")) {
  345. binmode(SV);
  346. while (<SV>) {
  347. next if m/^\#/ || !m/\S/;
  348. s/\s*\n$//;
  349. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  350. &error(sprintf(_g("bad line in substvars file %s at line %d"),
  351. $varlistfile, $.));
  352. $substvar{$1}= $';
  353. }
  354. close(SV);
  355. } elsif ($! != ENOENT ) {
  356. &error(sprintf(_g("unable to open substvars file %s: %s"),
  357. $varlistfile, $!));
  358. }
  359. $substvarsparsed = 1;
  360. }
  361. }
  362. sub parsedep {
  363. my ($dep_line, $use_arch, $reduce_arch) = @_;
  364. my @dep_list;
  365. my $host_arch = get_host_arch();
  366. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  367. my @or_list = ();
  368. ALTERNATE:
  369. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  370. my ($package, $relation, $version);
  371. $package = $1 if ($dep_or =~ s/^([a-zA-Z0-9][a-zA-Z0-9+._-]*)\s*//m);
  372. ($relation, $version) = ($1, $2)
  373. if ($dep_or =~ s/^\(\s*(=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  374. my @arches;
  375. @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  376. if ($reduce_arch && @arches) {
  377. my $seen_arch='';
  378. foreach my $arch (@arches) {
  379. $arch=lc($arch);
  380. if ($arch =~ /^!/) {
  381. my $not_arch;
  382. ($not_arch = $arch) =~ s/^!//;
  383. if (debarch_is($host_arch, $not_arch)) {
  384. next ALTERNATE;
  385. } else {
  386. # This is equivilant to
  387. # having seen the current arch,
  388. # unless the current arch
  389. # is also listed..
  390. $seen_arch=1;
  391. }
  392. } elsif (debarch_is($host_arch, $arch)) {
  393. $seen_arch=1;
  394. next;
  395. }
  396. }
  397. if (! $seen_arch) {
  398. next;
  399. }
  400. }
  401. if (length($dep_or)) {
  402. warning(sprintf(_g("can't parse dependency %s"), $dep_and));
  403. return undef;
  404. }
  405. push @or_list, [ $package, $relation, $version, \@arches ];
  406. }
  407. push @dep_list, \@or_list;
  408. }
  409. \@dep_list;
  410. }
  411. sub showdep {
  412. my ($dep_list, $show_arch) = @_;
  413. my @and_list;
  414. foreach my $dep_and (@$dep_list) {
  415. my @or_list = ();
  416. foreach my $dep_or (@$dep_and) {
  417. my ($package, $relation, $version, $arch_list) = @$dep_or;
  418. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  419. }
  420. push @and_list, join(' | ', @or_list);
  421. }
  422. join(', ', @and_list);
  423. }
  424. sub parsechangelog {
  425. my ($changelogfile, $changelogformat, $since) = @_;
  426. defined(my $c = open(CDATA, "-|")) || syserr(_g("fork for parse changelog"));
  427. if ($c) {
  428. binmode(CDATA);
  429. parsecdata(\*CDATA, 'L', 0, _g("parsed version of changelog"));
  430. close(CDATA);
  431. $? && subprocerr(_g("parse changelog"));
  432. } else {
  433. binmode(STDOUT);
  434. my @al = ($parsechangelog);
  435. push(@al,"-l$changelogfile");
  436. push(@al, "-F$changelogformat") if defined($changelogformat);
  437. push(@al, "-v$since") if defined($since);
  438. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  439. }
  440. }
  441. sub init_substvars
  442. {
  443. $substvar{'Format'} = 1.7;
  444. $substvar{'Newline'} = "\n";
  445. $substvar{'Space'} = " ";
  446. $substvar{'Tab'} = "\t";
  447. # XXX: Source-Version is now deprecated, remove in the future.
  448. $substvar{'Source-Version'}= $fi{"L Version"};
  449. $substvar{'binary:Version'} = $fi{"L Version"};
  450. $substvar{'source:Version'} = $fi{"L Version"};
  451. $substvar{'source:Version'} =~ s/\+b[0-9]+$//;
  452. $substvar{'source:Upstream-Version'} = $fi{"L Version"};
  453. $substvar{'source:Upstream-Version'} =~ s/-[^-]*$//;
  454. $substvar{"dpkg:Version"} = $version;
  455. $substvar{"dpkg:Upstream-Version"} = $version;
  456. $substvar{"dpkg:Upstream-Version"} =~ s/-[^-]+$//;
  457. }
  458. sub init_substvar_arch()
  459. {
  460. $substvar{'Arch'} = get_host_arch();
  461. }
  462. sub checkpackagename {
  463. my $name = shift || '';
  464. $name =~ m/[^-+.0-9a-z]/o &&
  465. &error(sprintf(_g("source package name `%s' contains illegal character `%s'"), $name, $&));
  466. $name =~ m/^[0-9a-z]/o ||
  467. &error(sprintf(_g("source package name `%s' starts with non-alphanum"), $name));
  468. }
  469. sub checkversion {
  470. my $version = shift || '';
  471. $version =~ m/[^-+:.0-9a-zA-Z~]/o &&
  472. &error(sprintf(_g("version number contains illegal character `%s'"), $&));
  473. }
  474. sub setsourcepackage {
  475. my $v = shift;
  476. checkpackagename( $v );
  477. if (defined($sourcepackage)) {
  478. $v eq $sourcepackage ||
  479. &error(sprintf(_g("source package has two conflicting values - %s and %s"), $sourcepackage, $v));
  480. } else {
  481. $sourcepackage= $v;
  482. }
  483. }
  484. sub readmd5sum {
  485. (my $md5sum = shift) or return;
  486. $md5sum =~ s/^([0-9a-f]{32})\s*\*?-?\s*\n?$/$1/o
  487. || &failure(sprintf(_g("md5sum gave bogus output `%s'"), $md5sum));
  488. return $md5sum;
  489. }
  490. # XXX: Should not be a global!!
  491. my $whatmsg;
  492. sub parsecdata {
  493. my ($cdata, $source, $many);
  494. ($cdata, $source, $many, $whatmsg) = @_;
  495. # many=0: ordinary control data like output from dpkg-parsechangelog
  496. # many=1: many paragraphs like in source control file
  497. # many=-1: single paragraph of control data optionally signed
  498. my $index = '';
  499. my $cf = '';
  500. my $paraborder = 1;
  501. while (<$cdata>) {
  502. s/\s*\n$//;
  503. next if (m/^$/ and $paraborder);
  504. next if (m/^#/);
  505. $paraborder=0;
  506. if (m/^(\S+)\s*:\s*(.*)$/) {
  507. $cf = $1;
  508. my $v = $2;
  509. $cf= &capit($cf);
  510. $fi{"$source$index $cf"}= $v;
  511. $fi{"o:$source$index $cf"}= $1;
  512. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  513. } elsif (m/^\s+\S/) {
  514. length($cf) || &syntax(_g("continued value line not in field"));
  515. $fi{"$source$index $cf"}.= "\n$_";
  516. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  517. $many == -2 && syntax(_g("expected blank line before PGP signature"));
  518. while (<$cdata>) {
  519. last if m/^$/;
  520. }
  521. $many= -2;
  522. } elsif (m/^$/) {
  523. $paraborder = 1;
  524. if ($many>0) {
  525. $index++; $cf='';
  526. } elsif ($many == -2) {
  527. $_ = <$cdata> while defined($_) && $_ =~ /^\s*$/;
  528. length($_) ||
  529. &syntax(_g("expected PGP signature, found EOF after blank line"));
  530. s/\n$//;
  531. m/^-----BEGIN PGP/ ||
  532. &syntax(sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  533. $many= -3; last;
  534. } else {
  535. while (<$cdata>) {
  536. /^\s*$/ ||
  537. &syntax(_g("found several \`paragraphs' where only one expected"));
  538. }
  539. }
  540. } else {
  541. &syntax(_g("line with unknown format (not field-colon-value)"));
  542. }
  543. }
  544. $many == -2 && &syntax(_g("found start of PGP body but no signature"));
  545. if (length($cf)) { $index++; }
  546. $index || &syntax(_g("empty file"));
  547. return $index;
  548. }
  549. sub syntax {
  550. &error(sprintf(_g("syntax error in %s at line %d: %s"), $whatmsg, $., $_[0]));
  551. }
  552. 1;