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 /** |
| 6 |
| 7 This function works strangely for performance reasons, I'll try and explain belo
w. |
| 8 |
| 9 File systems have been heavily optimised for doing a directory walk in inode |
| 10 order. It can be an order of magnitude faster to walk the directory this way. |
| 11 *However*, we want out output to be in sorted order so it is deterministic. |
| 12 |
| 13 Calling `stat` a file is one of the most expensive things you can do. It is |
| 14 equivalent to reading 64/128k of data. Hence, if you have a lot of small files |
| 15 then just reading their contents directly is more efficient. |
| 16 |
| 17 **/ |
| 18 package dirtools |
| 19 |
| 20 import ( |
| 21 "io/ioutil" |
| 22 "os" |
| 23 "path/filepath" |
| 24 ) |
| 25 |
| 26 /** |
| 27 SmallFile and LargeFile must be called in sorted order. |
| 28 */ |
| 29 type WalkObserver interface { |
| 30 SmallFile(filename string, alldata []byte) |
| 31 LargeFile(filename string) |
| 32 |
| 33 //StartDir(dirname string) error |
| 34 //FinishDir(dirname string) |
| 35 |
| 36 Error(pathname string, err error) |
| 37 } |
| 38 |
| 39 func WalkBasic(root string, smallfile_limit int64, obs WalkObserver) { |
| 40 filepath.Walk(root, func(path string, info os.FileInfo, err error) error
{ |
| 41 if err != nil { |
| 42 obs.Error(path, err) |
| 43 return nil |
| 44 } |
| 45 |
| 46 if info.Size() < smallfile_limit { |
| 47 data, err := ioutil.ReadFile(path) |
| 48 if err != nil { |
| 49 obs.Error(path, err) |
| 50 return nil |
| 51 } |
| 52 if int64(len(data)) != info.Size() { |
| 53 panic("file size was wrong!") |
| 54 } |
| 55 obs.SmallFile(path, data) |
| 56 } else { |
| 57 obs.LargeFile(path) |
| 58 } |
| 59 return nil |
| 60 }) |
| 61 } |
OLD | NEW |