forked from willscott/memphis
/
file.go
72 lines (63 loc) · 1.19 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
package memphis
import (
"io"
"os"
"time"
)
// File holds the metadata of a FS object
type File struct {
name string
mode os.FileMode
uid uint32
gid uint32
createTime time.Time
modTime time.Time
contents FileContent
}
// Name returns the file name
func (f *File) Name() string {
return f.name
}
// Size returns the file's size
func (f *File) Size() int64 {
return f.contents.Size()
}
// Mode returns the file's mode
func (f *File) Mode() os.FileMode {
return f.mode
}
// ModTime returns when the file was modified
func (f *File) ModTime() time.Time {
return f.modTime
}
// IsDir returns if the file is a directory (no)
func (f *File) IsDir() bool {
return false
}
// Sys is a wildcard in the OS interface
func (f *File) Sys() interface{} {
return nil
}
// Bytes returns a direct buffer of the contents of the file
func (f *File) Bytes() []byte {
if f.contents == nil {
return []byte{}
}
n := int64(0)
l := f.contents.Size()
buf := make([]byte, l)
for n < l {
a, e := f.contents.ReadAt(buf[n:], n)
n += int64(a)
if n == l || e == io.EOF {
return buf
}
if e != nil {
return []byte{}
}
if a == 0 {
return []byte{}
}
}
return buf
}