func makeConfig() empa.Config { var c empa.Config // Number of real available CPUs c.NCPUs = runtime.NumCPU() // Number of processes to start c.NSubprocs = c.NCPUs // Buffer input and output to reduce goroutines switches c.InCount = 1024 c.OutCount = 1024 c.InEOL = empa.EOL('\n') c.OutEOL = empa.EOL('\n') return c }
func main() { var ( dir string file string zero bool ) conf := makeConfig() flag.StringVar(&dir, "dir", "", "Directory `DIR` to scan as input source") flag.StringVar(&file, "file", "", "File `FILE` to read as input source") flag.IntVar(&conf.ProcID, "w", 0, "Worker number `N` of this process") flag.IntVar(&conf.NProcs, "wg", 0, "Total number `N` of workers that are being run") flag.IntVar(&conf.NSubprocs, "p", conf.NSubprocs, "Number of processes `N` to run in parallel") flag.BoolVar(&zero, "0", false, "Read input and write to subprocesses lines delimited by null character instead of newline") flag.Parse() args := flag.Args() if len(args) < 1 { flag.Usage() os.Exit(1) } if zero { conf.InEOL = empa.EOL('\x00') } r := empa.NewRunner(conf) r.Run(os.Stderr, os.Stdout) // Create and start NSubprocs subprocesses. Failure to startup is fatal. for i := 0; i < conf.NSubprocs; i++ { cmd := empa.NewCmd(args[0], args[1:]...) if err := r.Start(i, cmd); err != nil { log.Fatal("ep: ", err) } } // Three modes of operation: // 1. Input generated by walking the filesystem tree // 2. Input generated by reading a physical file // 3. Input generated by reading stdin. switch true { case dir != "": go walkDir(r, dir) case file != "": f, err := os.Open(file) if err != nil { log.Fatal("ep: ", err) } go feedReader(r, f, conf.InEOL) default: go feedReader(r, os.Stdin, conf.InEOL) } r.Wait() }
func main() { var ( algo string h hash.Hash null bool eol empa.EOL = '\n' ) flag.StringVar(&algo, "t", "sha1", "Type `T` of hash function to use; available are: md5, sha1, sha256, sha512") flag.BoolVar(&null, "0", false, "Read null separated lines") flag.Parse() switch strings.ToLower(algo) { case "md5": h = md5.New() case "sha1": h = sha1.New() case "sha256": h = sha256.New() case "sha512": h = sha512.New() default: log.Fatal("Invalid hash type specified; available are: md5, sha1, sha256, sha512") } if null { eol = empa.EOL('\x00') } sc := bufio.NewScanner(os.Stdin) sc.Split(eol.ScanLines) for sc.Scan() { fname := sc.Text() if err := proc(h, fname); err != nil { fmt.Fprintf(os.Stderr, "%s: %s\n", fname, err) } } if err := sc.Err(); err != nil { log.Fatal("epsum: ", err) } }