tnew index and search commands - tomb - the crypto undertaker
 (HTM) git clone git://parazyd.org/tomb.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 12a7760895753baddf4d05ec114cc6c4980daa3a
 (DIR) parent 2b6a38f1d799d4c1824ff70c83b3de5cfe2e3363
 (HTM) Author: Jaromil <jaromil@dyne.org>
       Date:   Sat, 30 Mar 2013 17:29:51 +0100
       
       new index and search commands
       
       now it is possible to index all filenames contained in tombs using
       updatedb(8) and then automatically search for them using locate in all
       open tombs. Documentation was updated accordingly.
       
       Diffstat:
         M doc/tomb.1                          |      19 ++++++++++++++++---
         M src/tomb                            |      74 +++++++++++++++++++++++++++++++
       
       2 files changed, 90 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/doc/tomb.1 b/doc/tomb.1
       t@@ -73,13 +73,26 @@ options (default: rw,noatime,nodev).
        
        .B
        .IP "list"
       -
        List all the tombs found open, including information about the time
       -they were opened and the hooks that they mounted. If the \fIfirst
       -argument\fR is present, then shows only the tomb named that way or
       +they were opened and the hooks that they mounted. If the first
       +argument is present, then shows only the tomb named that way or
        returns an error if its not found.
        
        .B
       +.IP "index"
       +Creates or updates the \fIsearch index\fR of a tomb, or all tombs currently
       +opened if none is specified. Indexes are created using updatedb(8) and
       +enable users to run quick search commands using simple word patterns.
       +
       +.B
       +.IP "search"
       +Searches through all tombs currently open for filenames matching one
       +or more text patterns given as arguments. Search returns a list of
       +files found inside the tombs that have been previously indexed using
       +locate(1). The option \fI--regex\fR can be used to interpret all
       +patterns as extended regexps.
       +
       +.B
        .IP "close"
        Closes a currently open tomb.  If more tombs are open, the first
        argument should be used to specify the name of the tomb to be closed,
 (DIR) diff --git a/src/tomb b/src/tomb
       t@@ -384,6 +384,9 @@ Commands:
        
         open    open an existing TOMB
        
       + index   update the search indexes of tombs
       + search  looks for filenames matching text patterns
       +
         list    list open TOMBs
        
         close   close a TOMB (or all)
       t@@ -1661,6 +1664,65 @@ resize_tomb() {
        }
        
        # }}}
       +
       +# {{{ - Index
       +# index files in all tombs for search
       +# $1 is optional, to specify a tomb
       +index_tombs() {
       +    { command -v updatedb > /dev/null } || { 
       +        die "Cannot index tombs on this system: updatedb not installed" }
       +    
       +    if [ $1 ]; then
       +        # list a specific tomb
       +        mounted_tombs=`mount -l |
       +    awk -vtomb="[$1]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $1 ";" $3 ";" $5 ";" $6 ";" $7 }'`
       +    else
       +        # list all open tombs
       +        mounted_tombs=`mount -l |
       +    awk '/^\/dev\/mapper\/tomb/ { print $1 ";" $3 ";" $5 ";" $6 ";" $7 }'`
       +    fi
       +    if ! [ $mounted_tombs ]; then
       +        if [ $1 ]; then
       +            die "There seems to be no open tomb engraved as [$1]"
       +        else
       +            die "I can't see any open tomb, may they all rest in peace."
       +        fi
       +    fi
       +    yes "Creating and updating search indexes"
       +    for t in ${(f)mounted_tombs}; do
       +        mapper=`basename ${t[(ws:;:)1]}`
       +        tombname=${t[(ws:;:)5]}
       +        tombmount=${t[(ws:;:)2]}
       +        say "Indexing $tombname"
       +        updatedb -l 0 -o ${tombmount}/.updatedb -U ${tombmount}
       +        say "search index updated"
       +    done
       +}
       +search_tombs() {
       +    { command -v locate > /dev/null } || { 
       +        die "Cannot index tombs on this system: updatedb not installed" }
       +
       +    # list all open tombs
       +    mounted_tombs=(`mount -l |
       +    awk '/^\/dev\/mapper\/tomb/ { print $1 ";" $3 ";" $5 ";" $6 ";" $7 }'`)
       +    { test ${#mounted_tombs} = 0 } && {
       +            die "I can't see any open tomb, may they all rest in peace." }
       +    yes "Searching for: $fg_bold[white]${=PARAM}$fg_no_bold[white]"
       +    for t in ${(f)mounted_tombs}; do
       +        mapper=`basename ${t[(ws:;:)1]}`
       +        tombname=${t[(ws:;:)5]}
       +        tombmount=${t[(ws:;:)2]}
       +        { test -r ${tombmount}/.updatedb } || {
       +            no "skipping tomb $tombname: not indexed"
       +            no "run 'tomb index' to create indexes"
       +            continue }
       +        say "Searching in tomb $tombname"
       +        locate -d ${tombmount}/.updatedb -e -i ${=PARAM}
       +        say "Matches found: `locate -d ${tombmount}/.updatedb -e -i -c ${=PARAM}`"
       +    done
       +        
       +}
       +
        # {{{ - List
        # list all tombs mounted in a readable format
        # $1 is optional, to specify a tomb
       t@@ -1950,6 +2012,10 @@ main() {
            subcommands_opts[help]=""
            subcommands_opts[slam]=""
            subcommands_opts[list]="-get-mountpoint"
       +
       +    subcommands_opts[index]=""
       +    subcommands_opts[search]=""
       +
            subcommands_opts[help]=""
            subcommands_opts[bury]=""
            subcommands_opts[exhume]=""
       t@@ -2085,6 +2151,14 @@ main() {
                status)
                    launch_status $PARAM[1]
                    ;;
       +
       +        index)
       +            index_tombs $PARAM[1]
       +            ;;
       +        search)
       +            search_tombs ${=PARAM}
       +            ;;
       +
                help)
                    usage
                    ;;