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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: common/dirwalk/walk_basic.go
diff --git a/common/dirwalk/walk_basic.go b/common/dirwalk/walk_basic.go
new file mode 100644
index 0000000000000000000000000000000000000000..b179af5aa828aeee8db1650375f56bbe6a766330
--- /dev/null
+++ b/common/dirwalk/walk_basic.go
@@ -0,0 +1,49 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package dirwalk
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+// WalkBasic is the trivial implementation of a directory tree walker using
+// built in filepath.Walk function.
+func WalkBasic(root string, callback WalkFunc) {
+ dirs := newStringStack()
+ dirs.push("")
+ filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ callback(path, -1, nil, err)
+ return nil
+ }
+
+ for true {
+ if strings.HasPrefix(path, dirs.peek()) {
+ break
+ }
+ callback(dirs.pop(), -1, nil, nil)
+ }
+
+ if info.IsDir() {
+ dirs.push(path)
+ } else {
+ f, err := os.Open(path)
+ if err != nil {
+ callback(path, -1, nil, err)
+ return nil
+ }
+ callback(path, info.Size(), f, nil)
+ }
+ return nil
+ })
+ for true {
+ if dirs.peek() == "" {
+ break
+ }
+ callback(dirs.pop(), -1, nil, nil)
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698