-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.go
More file actions
80 lines (66 loc) · 1.57 KB
/
input.go
File metadata and controls
80 lines (66 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"io"
"io/fs"
"log"
"path/filepath"
)
const chanSize = 1024
func inputFromArgs(args []string) <-chan *Checksums {
files := make(chan *Checksums, min(len(args), chanSize))
go func() {
defer close(files)
for _, arg := range args {
files <- &Checksums{file: arg}
}
}()
return files
}
// Used by the -r option
func inputFromDir(args []string, followSymlinks bool) <-chan *Checksums {
isSymlink := func(d fs.DirEntry) bool { return d.Type()&fs.ModeType == fs.ModeSymlink }
walkDir := filepath.WalkDir
if fsys != nil {
walkDir = func(root string, fn fs.WalkDirFunc) error { return fs.WalkDir(fsys, root, fn) }
}
files := make(chan *Checksums, chanSize)
go func() {
defer close(files)
for _, arg := range args {
_ = walkDir(arg, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Print(err)
} else if followSymlinks && isSymlink(d) || !d.IsDir() && !isSymlink(d) {
files <- &Checksums{file: path}
}
return nil
})
}
}()
return files
}
// Used by the -i option
func inputFromFile(f io.ReadCloser, zeroTerminated bool) <-chan *Checksums {
files := make(chan *Checksums, chanSize)
go func() {
defer close(files)
defer f.Close()
scanner, err := getScanner(f, zeroTerminated)
if err != nil {
log.Fatal(err)
}
for scanner.Scan() {
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
file := scanner.Text()
if file != "" {
files <- &Checksums{file: scanner.Text()}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}()
return files
}