-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
62 lines (52 loc) · 1.25 KB
/
file.go
File metadata and controls
62 lines (52 loc) · 1.25 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
)
type fileAccess struct {
srcPath string
}
type fileSource struct {
fullPath string
subPath string
info os.FileInfo
}
func newFileAccess(srcPath string) (fa *fileAccess) {
fa = new(fileAccess)
isDir, _ := isDirectory(srcPath)
fa.srcPath = normalizePath(srcPath, isDir)
return
}
func newFileSource(srcPath string, subPath string, info os.FileInfo) (fs *fileSource) {
fs = new(fileSource)
fs.fullPath = srcPath
fs.subPath = subPath
fs.info = info
return
}
// SourceAccess
func (fa *fileAccess) EachSource(callback FileSourceFunc) error {
return filepath.Walk(fa.srcPath, func (fullPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
subPath := toSubPath(fa.srcPath, fullPath)
err = callback(newFileSource(fullPath, subPath, info))
if err != nil {
fmt.Println("EachSource return error:", err)
}
return err
})
}
// FileSource
func (fs *fileSource) SubPath() string {
return fs.subPath
}
func (fs *fileSource) IsDir() bool {
return fs.info.IsDir()
}
func (fs *fileSource) Reader() (io.ReadCloser, error) {
return os.Open(fs.fullPath)
}