update-root.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/perl -w
  2. use Cwd;
  3. use File::Basename;
  4. use strict;
  5. use lib 'lib';
  6. use LXR::Common;
  7. use LXR::Config;
  8. my @paths=qw(
  9. /usr/local/bin
  10. );
  11. my $STDERRTOSTDOUT = '2>&1';
  12. my ($TREE, $new_src_dir, $force) = @ARGV;
  13. $force = 0 unless defined $force;
  14. if (!defined $TREE || $TREE eq '') {
  15. die "$0: [tree] new_source_directory [force]";
  16. }
  17. $TREE =~ s{/+$}{};
  18. if (!defined $new_src_dir && -d $TREE) {
  19. ($new_src_dir, $TREE) = ($TREE, '');
  20. } else {
  21. if (!defined $new_src_dir || $new_src_dir eq '') {
  22. die "you must specify a new source directory";
  23. }
  24. }
  25. unless (-d $new_src_dir) {
  26. die "new src dir $new_src_dir does not exist";
  27. }
  28. $ENV{'LANG'} = 'C';
  29. my $lxr_dir = '.';
  30. my $lxr_conf = "$lxr_dir/lxr.conf";
  31. unless (-f $lxr_conf) {
  32. die "could not find $lxr_conf";
  33. }
  34. # let LXR:: handle lxr.conf
  35. $ENV{'SCRIPT_NAME'} = "/$TREE/" . basename($0);
  36. my ($Conf, $HTTP, $Path, $head) = &init($0);
  37. my $db_dir = dirname $Conf->dbdir;
  38. my $src_dir = $Conf->sourceroot;
  39. unless (-d $db_dir) {
  40. die "dbdir: $db_dir does not exist, did you just move the whole lxr?";
  41. }
  42. $db_dir = $Conf->dbdir;
  43. open LXRCONF, "< $lxr_conf" || die "Could not open $lxr_conf";
  44. my $newconf = '';
  45. my $line;
  46. while ($line = <LXRCONF>) {
  47. warn "trailing whitespace on line $. {$line}" if $line =~ /^\w+:.*\w.*\s+\n$/;
  48. #grab sourceroot from config file indexing multiple trees where
  49. #format is "sourceroot: treename dirname"
  50. if ($TREE ne '') {
  51. if ($line =~ /^sourceroot:\s*\Q$TREE\E\s+(\S+)$/) {
  52. $src_dir = $1;
  53. $line = "sourceroot: $TREE $new_src_dir\n";
  54. }
  55. } else {
  56. if ($line =~ /^sourceroot:\s*(\S+)$/) {
  57. $src_dir = $1;
  58. $line = "sourceroot: $new_src_dir\n";
  59. }
  60. }
  61. $newconf .= $line;
  62. }
  63. close LXRCONF;
  64. if (defined $Conf->glimpsebin) {
  65. push @paths, $1 if ($Conf->glimpsebin =~ m{(.*)/([^/]*)$});
  66. }
  67. unless (defined $src_dir) {
  68. die "could not find sourceroot for tree $TREE";
  69. }
  70. open LXRCONF2, "> $lxr_conf.new";
  71. print LXRCONF2 $newconf;
  72. close LXRCONF2;
  73. my %pathmap=();
  74. for my $mapitem (@paths) {
  75. $pathmap{$mapitem} = 1;
  76. }
  77. for my $possible_path (keys %pathmap) {
  78. $ENV{'PATH'} = "$possible_path:$ENV{'PATH'}" if -d $possible_path;
  79. }
  80. unless (-d $db_dir) {
  81. die "could not find database for tree $TREE";
  82. } else {
  83. my $file_index = $db_dir . '/.glimpse_filenames';
  84. unless (-f $file_index) {
  85. unless ($force) {
  86. die "could not find file index for tree $TREE";
  87. }
  88. warn "tree $TREE did not have an index";
  89. } else {
  90. my $changed = 0;
  91. open FILELIST, "< $file_index";
  92. open NEWFILELIST, "> $file_index.new";
  93. while ($line = <FILELIST>) {
  94. $changed = 1 if $line =~ s/\Q$src_dir\E/$new_src_dir/;
  95. print NEWFILELIST $line;
  96. }
  97. close NEWFILELIST;
  98. close FILELIST;
  99. if ($changed) {
  100. rename "$file_index.new", $file_index;
  101. my $cmd = "(glimpseindex -R -H $db_dir $STDERRTOSTDOUT)";
  102. print "$cmd
  103. ";
  104. system($cmd);
  105. # build filename index
  106. # shared w/ update-search.pl
  107. my $db_dir_tmp = "$db_dir/tmp";
  108. mkdir $db_dir_tmp;
  109. my $mxr_dir_tmp = "$db_dir_tmp/.mxr";
  110. mkdir $mxr_dir_tmp;
  111. $cmd = "cp $db_dir/.glimpse_filenames $mxr_dir_tmp/files
  112. (glimpseindex -H $mxr_dir_tmp $mxr_dir_tmp $STDERRTOSTDOUT)
  113. perl -pi -e 's{tmp/\.mxr}{\.mxr}' $mxr_dir_tmp/.glimpse_filenames
  114. glimpseindex -H $mxr_dir_tmp -R
  115. ";
  116. if (-d "$db_dir/.mxr") {
  117. $cmd .= "mv $db_dir/.mxr $db_dir/.mxr-0
  118. mv $mxr_dir_tmp $db_dir/.mxr
  119. rmdir $db_dir_tmp
  120. rm -rf $db_dir/.mxr-0";
  121. } else {
  122. $cmd .= "mv $mxr_dir_tmp $db_dir/.mxr";
  123. }
  124. system($cmd);
  125. } else {
  126. unlink "$file_index.new";
  127. print "no changes needed\n";
  128. }
  129. }
  130. }
  131. rename "$lxr_conf.new", $lxr_conf;
  132. exit 0;