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

Unified Diff: common/dirwalk/tests/tools/walkdir/fileprocessor.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/tests/tools/walkdir/fileprocessor.go
diff --git a/common/dirwalk/tests/tools/walkdir/fileprocessor.go b/common/dirwalk/tests/tools/walkdir/fileprocessor.go
new file mode 100644
index 0000000000000000000000000000000000000000..64048e0b1ff24c1be2281a1a54564cdfba6eb568
--- /dev/null
+++ b/common/dirwalk/tests/tools/walkdir/fileprocessor.go
@@ -0,0 +1,63 @@
+// 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 main
+
+import (
+ "io"
+ "log"
+ "sync/atomic"
+)
+
+// FileProcessor is a basic interface for processing files provided by the
+// directory walking functions.
+type FileProcessor interface {
+ // Dir is called when a directory has been finished.
+ Dir(path string)
+
+ // SmallFile is called for processing a file which has been classed as "small".
+ SmallFile(path string, r io.ReadCloser)
+
+ // LargeFile is called for processing a file which has been classed as "large".
+ LargeFile(path string, r io.ReadCloser)
+
+ // Error is called when an error occurs on a path.
+ Error(path string, err error)
+
+ // Finished is called when the directory walk is finished.
+ Finished()
+}
+
+// BaseFileProcessor implements FileProcessor and counts the number of files of
+// each type.
+type BaseFileProcessor struct {
+ smallFiles uint64
+ largeFiles uint64
+ dirs uint64
+}
+
+func (p *BaseFileProcessor) Dir(path string) {
+ atomic.AddUint64(&p.dirs, 1)
+}
+
+func (p *BaseFileProcessor) SmallFile(path string, r io.ReadCloser) {
+ atomic.AddUint64(&p.smallFiles, 1)
+ if r != nil {
+ r.Close()
+ }
+}
+
+func (p *BaseFileProcessor) LargeFile(path string, r io.ReadCloser) {
+ atomic.AddUint64(&p.largeFiles, 1)
+ if r != nil {
+ r.Close()
+ }
+}
+
+func (p *BaseFileProcessor) Error(path string, err error) {
+ log.Fatalf("%s:%s", path, err)
+}
+
+func (p *BaseFileProcessor) Finished() {
+}

Powered by Google App Engine
This is Rietveld 408576698