-
Notifications
You must be signed in to change notification settings - Fork 23
/
fileinfo.go
88 lines (75 loc) · 1.86 KB
/
fileinfo.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
87
88
package remotefs
import (
"encoding/json"
"fmt"
"io/fs"
"path"
"strings"
"time"
)
// Check interfaces.
var (
_ fs.FileInfo = (*FileInfo)(nil)
_ fs.DirEntry = (*FileInfo)(nil)
)
// FileInfo implements fs.FileInfo for stat on remote files.
type FileInfo struct {
FName string `json:"name"`
FSize int64 `json:"size"`
FMode fs.FileMode `json:"mode"`
FUnix fs.FileMode `json:"unixMode"`
FModTime time.Time `json:"-"`
FIsDir bool `json:"isDir"`
ModtimeS int64 `json:"modTime"`
fs fs.FS
}
// UnmarshalJSON implements json.Unmarshaler.
func (f *FileInfo) UnmarshalJSON(b []byte) error {
type fileInfo *FileInfo
fi := fileInfo(f)
if err := json.Unmarshal(b, fi); err != nil {
return fmt.Errorf("unmarshal fileinfo: %w", err)
}
f.FModTime = time.Unix(f.ModtimeS, 0)
f.FName = strings.ReplaceAll(f.FName, "\\", "/")
return nil
}
// Name returns the file name.
func (f *FileInfo) Name() string {
return path.Base(f.FName)
}
// FullPath returns the full path.
func (f *FileInfo) FullPath() string {
return f.FName
}
// Size returns the file size.
func (f *FileInfo) Size() int64 {
return f.FSize
}
// Mode returns the file permission mode.
func (f *FileInfo) Mode() fs.FileMode {
if f.FUnix != 0 {
return f.FUnix
}
return f.FMode
}
// ModTime returns the last modification time of a file.
func (f *FileInfo) ModTime() time.Time {
return f.FModTime
}
// IsDir returns true if the file path points to a directory.
func (f *FileInfo) IsDir() bool {
return f.FIsDir
}
// Sys returns the underlying data source.
func (f *FileInfo) Sys() any {
return f.fs
}
// Type returns the file type. It's here to satisfy fs.DirEntry interface.
func (f *FileInfo) Type() fs.FileMode {
return f.Mode().Type()
}
// Info returns self. It's here to satisfy fs.DirEntry interface.
func (f *FileInfo) Info() (fs.FileInfo, error) {
return f, nil
}