-
Notifications
You must be signed in to change notification settings - Fork 383
/
Copy pathcindex.go
195 lines (169 loc) · 4.54 KB
/
cindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime/pprof"
"slices"
"github.com/google/codesearch/index"
)
var usageMessage = `usage: cindex [-list] [-reset] [-zip] [path...]
Cindex prepares the trigram index for use by csearch. The index is the
file named by $CSEARCHINDEX, or else $HOME/.csearchindex.
The simplest invocation is
cindex path...
which adds the file or directory tree named by each path to the index.
For example:
cindex $HOME/src /usr/include
or, equivalently:
cindex $HOME/src
cindex /usr/include
If cindex is invoked with no paths, it reindexes the paths that have
already been added, in case the files have changed. Thus, 'cindex' by
itself is a useful command to run in a nightly cron job.
The -list flag causes cindex to list the paths it has indexed and exit.
The -zip flag causes cindex to index content inside ZIP files.
This feature is experimental and will almost certainly change
in the future, possibly in incompatible ways.
By default cindex adds the named paths to the index but preserves
information about other paths that might already be indexed
(the ones printed by cindex -list). The -reset flag causes cindex to
delete the existing index before indexing the new paths.
With no path arguments, cindex -reset removes the index.
`
func usage() {
fmt.Fprintf(os.Stderr, usageMessage)
os.Exit(2)
}
var (
listFlag = flag.Bool("list", false, "list indexed paths and exit")
resetFlag = flag.Bool("reset", false, "discard existing index")
verboseFlag = flag.Bool("verbose", false, "print extra information")
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to this file")
checkFlag = flag.Bool("check", false, "check index is well-formatted")
zipFlag = flag.Bool("zip", false, "index content in zip files")
statsFlag = flag.Bool("stats", false, "print index size statistics")
)
func main() {
log.SetPrefix("cindex: ")
flag.Usage = usage
flag.Parse()
if *listFlag {
ix := index.Open(index.File())
if *checkFlag {
if err := ix.Check(); err != nil {
log.Fatal(err)
}
}
for p := range ix.Roots().All() {
fmt.Printf("%s\n", p)
}
return
}
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *resetFlag && flag.NArg() == 0 {
os.Remove(index.File())
return
}
var roots []index.Path
if flag.NArg() == 0 {
ix := index.Open(index.File())
roots = slices.Collect(ix.Roots().All())
} else {
// Translate arguments to absolute paths so that
// we can generate the file list in sorted order.
for _, arg := range flag.Args() {
a, err := filepath.Abs(arg)
if err != nil {
log.Printf("%s: %s", arg, err)
continue
}
roots = append(roots, index.MakePath(a))
}
slices.SortFunc(roots, index.Path.Compare)
}
master := index.File()
if _, err := os.Stat(master); err != nil {
// Does not exist.
*resetFlag = true
}
file := master
if !*resetFlag {
file += "~"
if *checkFlag {
ix := index.Open(master)
if err := ix.Check(); err != nil {
log.Fatal(err)
}
}
}
ix := index.Create(file)
ix.Verbose = *verboseFlag
ix.Zip = *zipFlag
ix.AddRoots(roots)
for _, root := range roots {
log.Printf("index %s", root)
filepath.Walk(root.String(), func(path string, info os.FileInfo, err error) error {
if _, elem := filepath.Split(path); elem != "" {
// Skip various temporary or "hidden" files or directories.
if elem[0] == '.' || elem[0] == '#' || elem[0] == '~' || elem[len(elem)-1] == '~' {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
}
if err != nil {
log.Printf("%s: %s", path, err)
return nil
}
if info != nil && info.Mode()&os.ModeType == 0 {
if err := ix.AddFile(path); err != nil {
log.Printf("%s: %s", path, err)
return nil
}
}
return nil
})
}
log.Printf("flush index")
ix.Flush()
if !*resetFlag {
log.Printf("merge %s %s", master, file)
index.Merge(file+"~", master, file)
if *checkFlag {
ix := index.Open(file + "~")
if err := ix.Check(); err != nil {
log.Fatal(err)
}
}
os.Remove(file)
os.Rename(file+"~", master)
} else {
if *checkFlag {
ix := index.Open(file)
if err := ix.Check(); err != nil {
log.Fatal(err)
}
}
}
log.Printf("done")
if *statsFlag {
ix := index.Open(master)
ix.PrintStats()
}
return
}