forked from chrislusf/gleam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfs_local.go
65 lines (56 loc) · 1.23 KB
/
vfs_local.go
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
package filesystem
import (
"io/ioutil"
"log"
"os"
"strings"
)
type LocalFileSystem struct {
}
func (fs *LocalFileSystem) Accept(fl *FileLocation) bool {
return !strings.HasPrefix(fl.Location, "hdfs://") && !strings.HasPrefix(fl.Location, "s3://")
}
func (fs *LocalFileSystem) Open(fl *FileLocation) (VirtualFile, error) {
osFile, err := os.Open(fl.Location)
return &VirtualFileLocal{osFile}, err
}
func (fs *LocalFileSystem) List(fl *FileLocation) (fileLocations []*FileLocation, err error) {
files, err := ioutil.ReadDir(fl.Location)
if err != nil {
return nil, err
}
for _, file := range files {
fileLocations = append(fileLocations, &FileLocation{fl.Location + "/" + file.Name()})
}
return
}
func (fs *LocalFileSystem) IsDir(fl *FileLocation) bool {
f, err := os.Open(fl.Location)
if err != nil {
log.Println(err)
return false
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
log.Println(err)
return false
}
switch mode := fi.Mode(); {
case mode.IsDir():
return true
case mode.IsRegular():
return false
}
return false
}
type VirtualFileLocal struct {
*os.File
}
func (vf *VirtualFileLocal) Size() int64 {
fileInfo, err := vf.File.Stat()
if err != nil {
return 0
}
return fileInfo.Size()
}