Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: common/dirwalk/walk_basic.go

Issue 2054763004: luci-go/common/dirwalk: Code for walking a directory tree efficiently Base URL: https://github.com/luci/luci-go@master
Patch Set: Major rewrite of the code. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698