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 main |
| 6 |
| 7 // Walk a given directory and perform an action on the files found. |
| 8 |
| 9 import ( |
| 10 "errors" |
| 11 "flag" |
| 12 "fmt" |
| 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", "simple", "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 stats *BaseWalker |
| 39 var obs dirwalk.WalkObserver |
| 40 switch *do { |
| 41 case "nothing": |
| 42 o := &BaseWalker{} |
| 43 stats = o |
| 44 obs = o |
| 45 case "print": |
| 46 o := &PrintWalker{obuf: os.Stderr} |
| 47 stats = &o.BaseWalker |
| 48 obs = o |
| 49 case "size": |
| 50 o := &SizeWalker{obuf: os.Stderr} |
| 51 stats = &o.BaseWalker |
| 52 obs = o |
| 53 case "read": |
| 54 o := &ReadWalker{} |
| 55 stats = &o.BaseWalker |
| 56 obs = o |
| 57 case "hash": |
| 58 o := &HashWalker{obuf: os.Stderr} |
| 59 stats = &o.BaseWalker |
| 60 obs = o |
| 61 case "phash": |
| 62 o := CreateParallelHashWalker(os.Stderr) |
| 63 stats = &o.BaseWalker |
| 64 obs = o |
| 65 default: |
| 66 log.Fatalf("Invalid action '%s'", *do) |
| 67 } |
| 68 |
| 69 for i := 0; i < *repeat; i++ { |
| 70 stats.smallfiles = 0 |
| 71 stats.largefiles = 0 |
| 72 |
| 73 switch *method { |
| 74 case "simple": |
| 75 dirwalk.WalkBasic(*dir, *smallfilesize, obs) |
| 76 case "nostat": |
| 77 dirwalk.WalkNoStat(*dir, *smallfilesize, obs) |
| 78 case "parallel": |
| 79 dirwalk.WalkParallel(*dir, *smallfilesize, obs) |
| 80 default: |
| 81 return errors.New(fmt.Sprintf("Invalid walk method '%s'"
, *method)) |
| 82 } |
| 83 fmt.Printf("Found %d small files and %d large files\n", stats.sm
allfiles, stats.largefiles) |
| 84 } |
| 85 fmt.Fprintf(os.Stderr, "Found %d small files and %d large files\n", stat
s.smallfiles, stats.largefiles) |
| 86 return nil |
| 87 } |
| 88 |
| 89 func main() { |
| 90 if err := mainImpl(); err != nil { |
| 91 fmt.Fprintf(os.Stderr, "walkdir: %s.\n", err) |
| 92 os.Exit(1) |
| 93 } |
| 94 } |
OLD | NEW |