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 "os" |
| 9 "path/filepath" |
| 10 "strings" |
| 11 ) |
| 12 |
| 13 // WalkBasic is the trivial implementation of a directory tree walker using |
| 14 // built in filepath.Walk function. |
| 15 func WalkBasic(root string, callback WalkFunc) { |
| 16 dirs := newStringStack() |
| 17 dirs.push("") |
| 18 filepath.Walk(root, func(path string, info os.FileInfo, err error) error
{ |
| 19 if err != nil { |
| 20 callback(path, -1, nil, err) |
| 21 return nil |
| 22 } |
| 23 |
| 24 for true { |
| 25 if strings.HasPrefix(path, dirs.peek()) { |
| 26 break |
| 27 } |
| 28 callback(dirs.pop(), -1, nil, nil) |
| 29 } |
| 30 |
| 31 if info.IsDir() { |
| 32 dirs.push(path) |
| 33 } else { |
| 34 f, err := os.Open(path) |
| 35 if err != nil { |
| 36 callback(path, -1, nil, err) |
| 37 return nil |
| 38 } |
| 39 callback(path, info.Size(), f, nil) |
| 40 } |
| 41 return nil |
| 42 }) |
| 43 for true { |
| 44 if dirs.peek() == "" { |
| 45 break |
| 46 } |
| 47 callback(dirs.pop(), -1, nil, nil) |
| 48 } |
| 49 } |
OLD | NEW |