-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.go
86 lines (68 loc) · 2 KB
/
file.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/vfs source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package vfs
import (
"errors"
"fmt"
"io"
"os"
)
var _ File = (*file)(nil)
var _ Gziper = (*file)(nil)
// File struct represents the virtual file or directory.
//
// Implements interface `vfs.File`.
type file struct {
*node
rs io.ReadSeeker
pos int
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// File and Directory operations
//______________________________________________________________________________
func (f *file) Read(b []byte) (int, error) {
return f.rs.Read(b)
}
func (f *file) Seek(offset int64, whence int) (int64, error) {
return f.rs.Seek(offset, whence)
}
func (f *file) Readdir(count int) ([]os.FileInfo, error) {
if !f.IsDir() {
return []os.FileInfo{}, &os.PathError{Op: "read", Path: f.NodeInfo.Path, Err: errors.New("vfs: cannot find the specified path")}
}
if f.pos >= len(f.node.childInfos) && count > 0 {
return nil, io.EOF
}
if count <= 0 || count > len(f.node.childInfos)-f.pos {
count = len(f.node.childInfos) - f.pos
}
ci := f.node.childInfos[f.pos : f.pos+count]
f.pos += count
return ci, nil
}
func (f *file) Readdirnames(count int) (names []string, err error) {
var list []string
infos, err := f.Readdir(count)
if err != nil {
return list, err
}
for _, v := range infos {
list = append(list, v.Name())
}
return list, nil
}
func (f *file) Stat() (os.FileInfo, error) {
return f, nil
}
func (f *file) Close() error {
if f.IsGzip() {
return f.rs.(io.Closer).Close()
}
return nil
}
// String method Stringer interface.
func (f file) String() string {
return fmt.Sprintf(`file(name=%s dir=%v gzip=%v size=%v, modtime=%v)`,
f.Name(), f.IsDir(), f.IsGzip(), f.Size(), f.ModTime())
}