controllib.pl 17 KB

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