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

Side by Side Diff: common/dirwalk/tests/tools/walkdir/main.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 main
6
7 // Walk a given directory and perform an action on the files found.
8
9 import (
10 "flag"
11 "fmt"
12 "io"
13 "io/ioutil"
14 "log"
15 "os"
16
17 "github.com/luci/luci-go/common/dirwalk"
18 )
19
20 func mainImpl() error {
21 method := flag.String("method", "basic", "Method used to walk the tree")
22 dir := flag.String("dir", "", "Directory to walk")
23 do := flag.String("do", "nothing", "Action to perform on the files")
24 smallFileSize := flag.Int64("smallfilesize", 64*1024, "Size to consider a small file")
25 repeat := flag.Int("repeat", 1, "Repeat the walk x times")
26 verbose := flag.Bool("v", false, "verbose mode")
27
28 flag.Parse()
29
30 if !*verbose {
31 log.SetOutput(ioutil.Discard)
32 }
33
34 if _, err := os.Stat(*dir); err != nil {
35 return err
36 }
37
38 var base *BaseFileProcessor
39 var proc FileProcessor
40 switch *do {
41 case "nothing":
42 base = &BaseFileProcessor{}
43 proc = base
44 case "print":
45 p := &PrintFileProcessor{obuf: os.Stderr}
46 base = &p.BaseFileProcessor
47 proc = p
48 case "size":
49 p := &SizeFileProcessor{obuf: os.Stderr}
50 base = &p.BaseFileProcessor
51 proc = p
52 case "read":
53 p := &ReadFileProcessor{}
54 base = &p.BaseFileProcessor
55 proc = p
56 case "hash":
57 p := &HashFileProcessor{obuf: os.Stderr}
58 base = &p.BaseFileProcessor
59 proc = p
60 case "phash":
61 p := CreateParallelHashFileProcessor(os.Stderr)
62 base = &p.BaseFileProcessor
63 proc = p
64 case "verify":
65 p := &VerifyFileProcessor{rootDir: *dir}
66 base = &p.BaseFileProcessor
67 proc = p
68 default:
69 log.Fatalf("Invalid action '%s'", *do)
70 }
71
72 for i := 0; i < *repeat; i++ {
73 base.smallFiles = 0
74 base.largeFiles = 0
75
76 callback := func(path string, size int64, r io.ReadCloser, err e rror) {
77 if err != nil {
78 proc.Error(path, err)
79 return
80 }
81 if r == nil {
82 proc.Dir(path)
83 return
84 }
85
86 if (size >= 0) && (size < *smallFileSize) {
87 proc.SmallFile(path, r)
88 } else {
89 proc.LargeFile(path, r)
90 }
91 }
92
93 switch *method {
94 case "basic":
95 dirwalk.WalkBasic(*dir, callback)
96 case "nostat":
97 dirwalk.WalkNoStat(*dir, *smallFileSize, callback)
98 case "parallel":
99 dirwalk.WalkParallel(*dir, callback)
100 default:
101 return fmt.Errorf("Invalid walk method '%s'", *method)
102 }
103 proc.Finished()
104 fmt.Printf("Found %d small files and %d large files in %d dirs\n ", base.smallFiles, base.largeFiles, base.dirs)
105 }
106
107 fmt.Fprintf(os.Stderr, "Found %d small files and %d large files in %d di rs\n", base.smallFiles, base.largeFiles, base.dirs)
108 return nil
109 }
110
111 func main() {
112 if err := mainImpl(); err != nil {
113 fmt.Fprintf(os.Stderr, "walkdir: %s.\n", err)
114 os.Exit(1)
115 }
116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698