OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
2 // Use of this source code is governed under the Apache License, Version 2.0 | |
3 // that can be found in the LICENSE file. | |
4 | |
5 package dirwalk | |
6 | |
7 import ( | |
8 "bytes" | |
9 "io" | |
10 "io/ioutil" | |
11 "os" | |
12 "path/filepath" | |
13 ) | |
14 | |
15 func walkNoStatInternal(base string, files []string, smallFileLimit int64, callb ack WalkFunc) { | |
16 for _, name := range files { | |
17 path := filepath.Join(base, name) | |
18 | |
19 file, err := os.Open(path) | |
20 if err != nil { | |
21 callback(path, -1, nil, err) | |
22 continue | |
23 } | |
24 | |
25 block := make([]byte, smallFileLimit) | |
26 count, err := file.Read(block) | |
27 if err != io.EOF && err != nil { | |
mcgreevy_g
2017/06/27 03:29:17
Note: it's valid for a Reader to return a nil erro
| |
28 // It is probably a directory, try and list it. | |
29 dir := file | |
30 | |
31 names, err := dir.Readdirnames(0) | |
32 if err != nil { | |
33 callback(path, -1, nil, err) | |
34 continue | |
35 } | |
36 walkNoStatInternal(path, names, smallFileLimit, callback ) | |
37 callback(path, -1, nil, nil) | |
38 } else { | |
39 // It was actually a file | |
40 if int64(count) == smallFileLimit { | |
41 // This file was bigger than the block size | |
mcgreevy_g
2017/06/27 03:29:17
Or, perhaps it was equal to the block size and io.
| |
42 callback(path, -1, multiReadCloser{io.MultiReade r(bytes.NewReader(block), file), []io.Closer{file}}, nil) | |
43 } else { | |
44 // This file was smaller than the block size | |
45 callback(path, int64(count), ioutil.NopCloser(by tes.NewReader(block[:count])), nil) | |
46 } | |
47 } | |
48 } | |
49 } | |
50 | |
51 // WalkNoStat is an implementation of a directory tree walker which avoids | |
52 // calling stat on every file. | |
53 // | |
54 // File systems have been heavily optimised for doing a directory walk in inode | |
55 // order. It can be an order of magnitude faster to walk the directory in this | |
56 // order so we do. | |
57 // | |
58 // Calling `stat` is also one of the most expensive things you can do (it is | |
59 // roughly equivalent to reading 8/16k of data). Hence, if you have a lot of | |
60 // small files then just reading their contents directly is more efficient. | |
61 // Rather then doing the stat, we assume everything is a file and just try to | |
62 // read a chunk. If the file is smaller than the block size, we know that we | |
63 // have the entire contents. Otherwise we know the file is bigger and can | |
64 // decide to do the stat. If the name turned out to be a directory, then we | |
65 // will get an error. | |
66 func WalkNoStat(root string, smallFileLimit int64, callback WalkFunc) { | |
67 paths := []string{root} | |
68 walkNoStatInternal("", paths, smallFileLimit, callback) | |
69 } | |
OLD | NEW |