| 154 | // * or if it contains a line longer than maxLineLen bytes, |
| 155 | // * or if it contains more than maxTextTrigrams distinct trigrams. |
| 156 | const ( |
| 157 | maxFileLen = 1 << 30 |
| 158 | // MaxLineLen specifies the maximum line length of an indexed file, |
| 159 | // in bytes, including the newline. Code which reads indexed files |
| 160 | // (like regexp.Grep) can rely on len(line) <= MaxLineLen for every line. |
| 161 | MaxLineLen = 2000 |
| 162 | maxTextTrigrams = 20000 |
| 163 | ) |
| 164 | |
| 165 | func (w *Writer) AddFile(fn, name string) error { |
| 166 | w.set.Reset() |
| 167 | f, err := os.Open(fn) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | defer f.Close() |
| 172 | |
| 173 | st, err := f.Stat() |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | if st.Size() > maxFileLen { |
| 178 | return errors.New("too long, ignoring") |
| 179 | } |
| 180 | if st.Size() < 3 { |
| 181 | return errors.New("too short, ignoring") |
| 182 | } |
| 183 | |
| 184 | var ( |
| 185 | c byte |
| 186 | tv uint32 |
| 187 | i = 0 |
| 188 | n = 0 |
| 189 | linelen = 0 |
| 190 | buf = w.inbuf[:0] |
| 191 | entries = make([]uint64, st.Size()-2) |
| 192 | ) |
| 193 | for { |
| 194 | tv = (tv << 8) & (1<<24 - 1) |
| 195 | if i >= len(buf) { |
| 196 | n, err := f.Read(buf[:cap(buf)]) |
| 197 | if n == 0 { |
| 198 | if err != nil { |
| 199 | if err == io.EOF { |
| 200 | break |
| 201 | } |
| 202 | return err |
| 203 | } |
| 204 | return errors.New("0-length read") |
| 205 | } |
| 206 | buf = buf[:n] |
| 207 | i = 0 |
| 208 | } |
| 209 | c = buf[i] |
| 210 | i++ |
| 211 | tv |= uint32(c) |
| 212 | if linelen++; linelen > MaxLineLen { |
| 213 | return errors.New("very long lines, ignoring") |