update-xref.pl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/perl
  2. # Run this from cron to update the identifier database that lxr uses
  3. # to turn function names into clickable links.
  4. # Created 12-Jun-98 by jwz.
  5. # Updated 27-Feb-99 by endico. Added multiple tree support.
  6. use Cwd;
  7. use File::Basename;
  8. use lib 'lib';
  9. use LXR::Common;
  10. use LXR::Config;
  11. use LXR::Shell;
  12. # we use:
  13. =notes
  14. my used_apps = qw(
  15. mv
  16. time
  17. date
  18. uptime
  19. );
  20. =cut
  21. my $DEBUGGER = '';
  22. my @paths=qw(
  23. /usr/local/bin
  24. /opt/local/bin
  25. /usr/ucb
  26. );
  27. my $STDERRTOSTDOUT = '2>&1';
  28. my $STDERRTODEVNUL = '2>/dev/null';
  29. my $ERROR_OUTPUT = $STDERRTOSTDOUT;
  30. my $TREE;
  31. my %defaults = qw(
  32. TIME time
  33. UPTIME uptime
  34. DATE date
  35. );
  36. my $by_unit = 0;
  37. sub do_mkdir {
  38. my $dir = shift;
  39. return if -d $dir;
  40. die "dangling symlink $dir" if -l $dir;
  41. mkdir $dir || die "can't create $dir";
  42. }
  43. sub process_args {
  44. my $was_arg;
  45. do {
  46. $was_arg = 0;
  47. $TREE = shift;
  48. if ($TREE) {
  49. if ($TREE eq '-cron') {
  50. # run from a cron script, silence error output
  51. $was_arg = 1;
  52. $defaults{TIME} = $defaults{UPTIME} = '';
  53. $ERROR_OUTPUT = $STDERRTODEVNUL;
  54. } elsif ($TREE eq '-g') {
  55. $was_arg = 1;
  56. $DEBUGGER = 'perl -d';
  57. } elsif ($TREE eq '--by-unit') {
  58. # index each top level directory individually and then merge
  59. $was_arg = 1;
  60. $by_unit = 1;
  61. } else {
  62. $TREE =~ s{/$}{};
  63. }
  64. }
  65. } while ($TREE && $was_arg);
  66. }
  67. process_args(@ARGV);
  68. check_defaults(\%defaults);
  69. my $DATE = $defaults{DATE};
  70. my $TIME = $defaults{TIME};
  71. my $UPTIME = $defaults{UPTIME};
  72. my $lxr_dir = '.';
  73. die "can't find $lxr_dir" unless -d $lxr_dir;
  74. my $lxr_conf = "$lxr_dir/lxr.conf";
  75. unless (-f $lxr_conf) {
  76. die "could not find $lxr_conf";
  77. }
  78. unless (defined $TREE) {
  79. # need to sniff lxr.conf
  80. open LXRCONF, "< $lxr_conf" || die "Could not open $lxr_conf";
  81. while ($line = <LXRCONF>) {
  82. #since no tree is defined, assume sourceroot is defined the old way
  83. #grab sourceroot from config file indexing only a single tree where
  84. #format is "sourceroot: dirname"
  85. next unless $line =~ /^sourceroot:\s*(\S+)(\s+\S+|)$/;
  86. if ($2 ne '') {
  87. $TREE = $1;
  88. } else {
  89. $src_dir = $1;
  90. }
  91. last;
  92. }
  93. close LXRCONF;
  94. }
  95. open HTACCESS, '<', "$lxr_dir/.htaccess";
  96. while ($line = <HTACCESS>) {
  97. next unless $line =~ /^SetEnv\s+(\S+)\s+(.*)[\r\n]*$/;
  98. my ($envvar, $value) = ($1, $2);
  99. #SetEnv LD_LIBRARY_PATH /zfsroot/.zfs/snapshot/solex_snv41_eol/usr/sfw/lib:/usr/sfw/lib:/zfsroot/.zfs/snapshot/solex_snv41_eol/usr/local/BerkeleyDB.4.4/lib:/usr/local/BerkeleyDB.4.4/lib
  100. if ($envvar =~ /PATH/) {
  101. $value = $ENV{$envvar}.':'.$value;
  102. $value =~ s/::+/:/g;
  103. }
  104. $ENV{$envvar} = $value;
  105. }
  106. close HTACCESS;
  107. # let LXR:: handle lxr.conf
  108. $ENV{'SCRIPT_NAME'} = "/$TREE/" . basename($0);
  109. my ($Conf, $HTTP, $Path, $head) = &init($0);
  110. {
  111. my @trees = @{$Conf->{'trees'}};
  112. die "Could not find tree $TREE" if scalar @trees > 1 && !(grep /^\Q$TREE\E$/, @trees);
  113. }
  114. die "dbdir not set" unless defined $Conf->dbdir;
  115. $db_dir = $Conf->dbdir;
  116. $src_dir = $Conf->sourceroot;
  117. unless (defined $src_dir) {
  118. die 'Could not find matching sourceroot:'.($TREE ?" for $TREE":'');
  119. }
  120. do_mkdir $db_dir;
  121. my $pid_lock = get_lock($db_dir, 'xref');
  122. $log = "$db_dir/genxref.log";
  123. #exec > $log 2>&1
  124. #XXX what does |set -x| mean?
  125. #system ("set -x > $log");
  126. system ("$DATE >> $log");
  127. $lxr_dir=getcwd;
  128. my $db_tmp_dir="$db_dir/tmp";
  129. unless (-d $db_tmp_dir) {
  130. do_mkdir $db_tmp_dir;
  131. } else {
  132. unless (-w $db_tmp_dir) {
  133. die "can't write to $db_tmp_dir";
  134. }
  135. for my $name (qw(xref fileidx)) {
  136. my $file = "$db_tmp_dir/$name";
  137. if (-f $file && ! -w $file) {
  138. die "$file isn't writable.";
  139. }
  140. }
  141. }
  142. chdir $db_tmp_dir || die "can't change to $db_tmp_dir";
  143. #XXX what does |set -e| mean?
  144. #system ("set -e >> $log");
  145. my $success = 0;
  146. if ($by_unit) {
  147. chdir $src_dir;
  148. my @dirs = sort <*>;
  149. chdir $db_tmp_dir;
  150. my ($othertree, $otherpath, $skipdb) = ('', '', '');
  151. if ($TREE =~ /^(.*)-(?:.*?)$/) {
  152. $othertree = $1;
  153. $otherpath = $Conf->{'treehash'}{$othertree};
  154. for my $tree (keys %{$Conf->{'treehash'}}) {
  155. my $path = $Conf->{'treehash'}{$tree};
  156. if ($otherpath eq $path) {
  157. $skipdb = "$db_dir/../$tree/tmp";
  158. last if -d $skipdb;
  159. }
  160. $skipdb = '';
  161. }
  162. unless ($otherpath && -d $otherpath && -d $skipdb) {
  163. ($othertree, $otherpath, $skipdb) = ('', '', '');
  164. }
  165. }
  166. foreach my $dir (@dirs) {
  167. my $skip = 0;
  168. if ($otherpath) {
  169. $skip = 1 if system("$lxr_dir/compare-dir-trees.pl", "$src_dir/$dir", "$otherpath/$dir") == 0;
  170. }
  171. if ($skip) {
  172. foreach my $file ("$skipdb/fileidx.$dir", "$skipdb/xref.$dir") {
  173. if (-f $file) {
  174. system('cp', '-lf', $file, '.');
  175. }
  176. }
  177. } else {
  178. $success = system("$TIME $DEBUGGER $lxr_dir/genxref $src_dir/$dir default $dir >> $log $ERROR_OUTPUT") == 0;
  179. }
  180. }
  181. $success = system("$TIME $DEBUGGER $lxr_dir/genxref $src_dir merge ".join(' ',@dirs)." >> $log $ERROR_OUTPUT") == 0;
  182. } else {
  183. $success = system("$TIME $DEBUGGER $lxr_dir/genxref $src_dir >> $log $ERROR_OUTPUT") == 0;
  184. }
  185. if ($success) {
  186. if (system("chmod", "-R", "a+r", $db_tmp_dir)) {
  187. die "chmod failed";
  188. }
  189. if (system("mv", "$db_tmp_dir/xref", "$db_tmp_dir/fileidx", $db_dir)) {
  190. die "move failed";
  191. }
  192. } else {
  193. open LOG, '>>', $log;
  194. print LOG 'Error executing genxref
  195. ';
  196. close LOG;
  197. }
  198. chdir "../..";
  199. system ("$DATE >> $log");
  200. system ("$UPTIME >> $log") if $UPTIME =~ /\w/;
  201. unlink $pid_lock;
  202. exit 0;