forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileinfo.go
121 lines (104 loc) · 2.91 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package rig
import (
"encoding/json"
"io/fs"
"path"
"strings"
"time"
)
// FileMode is used to set the type of allowed operations when opening remote files
type FileMode int
const (
ModeRead FileMode = 1 // ModeRead = Read only
ModeWrite FileMode = 2 // ModeWrite = Write only
ModeReadWrite FileMode = ModeRead | ModeWrite // ModeReadWrite = Read and Write
ModeCreate FileMode = 4 | ModeWrite // ModeCreate = Create a new file or truncate an existing one. Includes write permission.
ModeAppend FileMode = 8 | ModeCreate // ModeAppend = Append to an existing file. Includes create and write permissions.
)
// Check interfaces
var (
_ fs.FileInfo = &FileInfo{}
_ fs.DirEntry = &FileInfo{}
)
// 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"`
fsys 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 ErrCommandFailed.Wrapf("unmarshal fileinfo: %w", err)
}
f.FModTime = time.Unix(f.ModtimeS, 0)
f.FName = strings.ReplaceAll(f.FName, "\\", "/")
var newmode fs.FileMode
if f.FMode&1 != 0 { // "Readonly"
newmode = 0o444
}
if f.FMode&16 != 0 { // "Directory"
newmode |= fs.ModeDir | 0o544
}
if f.FMode&64 != 0 { // "Device"
newmode |= fs.ModeCharDevice
}
if f.FMode&4096 != 0 { // "Offline"
newmode |= fs.ModeIrregular
}
if f.FMode&1024 != 0 { // "ReparsePoint"
newmode |= fs.ModeIrregular
newmode |= fs.ModeSymlink
}
if f.FMode&256 != 0 { // "Temporary"
newmode |= fs.ModeTemporary
}
f.FMode = newmode
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.fsys
}
// 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
}