-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileinfo.go
103 lines (91 loc) · 2.84 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
package fs
import (
"errors"
iofs "io/fs"
"os"
"time"
)
// FileInfo is a snapshot of a file's stat information.
// In comparison to io/fs.FileInfo it's not an interface
// but a struct with public fields.
type FileInfo struct {
File File
Name string
Exists bool
IsDir bool
IsRegular bool
IsHidden bool
Size int64
Modified time.Time
Permissions Permissions
}
// Validate returns an error if the FileInfo is invalid.
func (i *FileInfo) Validate() error {
if i == nil {
return errors.New("<nil> FileInfo")
}
if i.File == "" || i.Name == "" {
return ErrEmptyPath
}
return nil
}
// NewFileInfo returns a FileInfo using the
// data from an io/fs.FileInfo as snapshot
// of an existing file.
// Use NewNonExistingFileInfo to get
// a FileInfo for non existing file.
func NewFileInfo(file File, info iofs.FileInfo, hidden bool) *FileInfo {
mode := info.Mode()
return &FileInfo{
File: file,
Name: info.Name(),
Exists: true,
IsDir: mode.IsDir(),
IsRegular: mode.IsRegular(),
IsHidden: hidden,
Size: info.Size(),
Modified: info.ModTime(),
Permissions: Permissions(mode.Perm()),
}
}
// NewNonExistingFileInfo returns a FileInfo
// for a potentially non existing file.
// FileInfo.Exists will be false, but the
// file may exist at any point of time.
// IsHidden will be true if the name starts with a dot.
func NewNonExistingFileInfo(file File) *FileInfo {
name := file.Name()
return &FileInfo{
File: file,
Name: name,
Exists: false,
IsHidden: len(name) > 0 && name[0] == '.',
}
}
// StdFileInfo returns an io/fs.FileInfo wrapper
// for the data stored in the FileInfo struct.
func (i *FileInfo) StdFileInfo() iofs.FileInfo { return fileInfo{i} }
// fileInfo implements os.FileInfo and fs.FileInfo for a given FileInfo
type fileInfo struct{ i *FileInfo }
func (f fileInfo) Name() string { return f.i.Name }
func (f fileInfo) Size() int64 { return f.i.Size }
func (f fileInfo) Mode() os.FileMode { return f.i.Permissions.FileMode(f.i.IsDir) }
func (f fileInfo) ModTime() time.Time { return f.i.Modified }
func (f fileInfo) IsDir() bool { return f.i.IsDir }
func (f fileInfo) Sys() any { return nil }
// type NameSizeProvider interface {
// Name() string
// Size() int64
// }
// // FSFileInfoFromNameSizeProvider wraps a NameSizeProvider as a non-directory fs.FileInfo
// // that returns 0666 as mode and the current time as modified time.
// func FSFileInfoFromNameSizeProvider(ns NameSizeProvider) fs.FileInfo {
// return nameSizeInfo{ns}
// }
// type nameSizeInfo struct {
// NameSizeProvider
// }
// func (nameSizeInfo) Mode() os.FileMode { return 0666 }
// func (nameSizeInfo) ModTime() time.Time { return time.Now() }
// func (nameSizeInfo) IsDir() bool { return false }
// func (nameSizeInfo) Sys() any { return nil }