home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / bin / updatedb < prev    next >
Text File  |  1994-12-22  |  4KB  |  145 lines

  1. #!/bin/sh
  2. # updatedb -- build a locate pathname database
  3. # Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9.  
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14.  
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. # csh original by James Woods; sh conversion by David MacKenzie.
  20.  
  21. usage="\
  22. Usage: updatedb [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
  23.        [--prunepaths='dir1 dir2...'] [--output=dbfile] [--netuser=user]
  24.        [--old-format] [--version] [--help]"
  25.  
  26. old=no
  27. for arg
  28. do
  29.   opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
  30.   val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
  31.   case "$opt" in
  32.     --localpaths) SEARCHPATHS="$val" ;;
  33.     --netpaths) NETPATHS="$val" ;;
  34.     --prunepaths) PRUNEPATHS="$val" ;;
  35.     --output) LOCATE_DB="$val" ;;
  36.     --netuser) NETUSER="$val" ;;
  37.     --old-format) old=yes ;;
  38.     --version) echo "GNU updatedb version 4.1"; exit 0 ;;
  39.     --help) echo "$usage"; exit 0 ;;
  40.     *) echo "updatedb: invalid option $opt
  41. $usage" >&2
  42.        exit 1 ;;
  43.   esac
  44. done
  45.  
  46. # You can set these in the environment, or use command-line options,
  47. # to override their defaults:
  48.  
  49. # Non-network directories to put in the database.
  50. : ${SEARCHPATHS="/"}
  51.  
  52. # Network (NFS, AFS, RFS, etc.) directories to put in the database.
  53. : ${NETPATHS=}
  54.  
  55. # Directories to not put in the database, which would otherwise be.
  56. : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
  57.  
  58. # The same, in the form of a regex that find can use.
  59. test -z "$PRUNEREGEX" &&
  60.   PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
  61.  
  62. # The database file to build.
  63. : ${LOCATE_DB=/local/var/locatedb}
  64.  
  65. # Directory to hold intermediate files.
  66. : ${TMPDIR=/tmp}
  67.  
  68. # The user to search network directories as.
  69. : ${NETUSER=daemon}
  70.  
  71. # The directory containing the subprograms.
  72. : ${LIBEXECDIR=/gnu/lib/libexec}
  73.  
  74. # The directory containing find.
  75. : ${BINDIR=/gnu/bin}
  76.  
  77. # The names of the utilities to run to build the database.
  78. : ${find=find}
  79. : ${frcode=frcode}
  80. : ${bigram=bigram}
  81. : ${code=code}
  82.  
  83. PATH=$LIBEXECDIR:$BINDIR:/bin:$PATH export PATH
  84.  
  85. # Make and code the file list.
  86. # Sort case insensitively for users' convenience.
  87.  
  88. if test $old = no; then
  89.  
  90. # FIXME figure out how to sort null-terminated strings, and use -print0.
  91. {
  92. if test -n "$SEARCHPATHS"; then
  93.   $find $SEARCHPATHS \
  94.   \( -fstype nfs -o -fstype NFS -o -type d -regex "$PRUNEREGEX" \) -prune -o -print
  95. fi
  96.  
  97. if test -n "$NETPATHS"; then
  98.   su $NETUSER -c \
  99.   "$find $NETPATHS \\( -type d -regex \"$PRUNEREGEX\" -prune \\) -o -print"
  100. fi
  101. } | sort -f | $frcode > $LOCATE_DB.n
  102.  
  103. # To avoid breaking locate while this script is running, put the
  104. # results in a temp file, then rename it atomically.
  105. if test -s $LOCATE_DB.n; then
  106.   rm -f $LOCATE_DB
  107.   mv $LOCATE_DB.n $LOCATE_DB
  108.   chmod 644 $LOCATE_DB
  109. else
  110.   echo "updatedb: new database would be empty" >&2
  111.   rm -f $LOCATE_DB.n
  112. fi
  113.  
  114. else # old
  115.  
  116. bigrams=$TMPDIR/f.bigrams$$
  117. filelist=$TMPDIR/f.list$$
  118. trap 'rm -f $bigrams $filelist; exit' 1 15
  119.  
  120. # Alphabetize subdirectories before file entries using tr.  James says:
  121. # "to get everything in monotonic collating sequence, to avoid some
  122. # breakage i'll have to think about."
  123. {
  124. if test -n "$SEARCHPATHS"; then
  125.   $find $SEARCHPATHS \
  126.   \( -fstype nfs -o -fstype NFS -o -type d -regex "$PRUNEREGEX" \) -prune -o -print
  127. fi
  128. if test -n "$NETPATHS"; then
  129.   su $NETUSER -c \
  130.   "$find $NETPATHS \\( -type d -regex \"$PRUNEREGEX\" -prune \\) -o -print"
  131. fi
  132. } | tr / '\001' | sort -f | tr '\001' / > $filelist
  133.  
  134. # Compute the (at most 128) most common bigrams in the file list.
  135. $bigram < $filelist | sort | uniq -c | sort -nr |
  136.   awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
  137.  
  138. # Code the file list.
  139. $code $bigrams < $filelist > $LOCATE_DB
  140. chmod 644 $LOCATE_DB
  141.  
  142. rm -f $bigrams $filelist $errs
  143.  
  144. fi
  145.