-
Notifications
You must be signed in to change notification settings - Fork 467
/
actualstateentry.go
201 lines (177 loc) · 4.46 KB
/
actualstateentry.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package chezmoi
import (
"errors"
"io/fs"
)
// An ActualStateEntry represents the actual state of an entry in the
// filesystem.
type ActualStateEntry interface {
EntryState() (*EntryState, error)
Path() AbsPath
Remove(system System) error
OriginString() string
}
// A ActualStateAbsent represents the absence of an entry in the filesystem.
type ActualStateAbsent struct {
absPath AbsPath
}
// A ActualStateDir represents the state of a directory in the filesystem.
type ActualStateDir struct {
absPath AbsPath
perm fs.FileMode
}
// A ActualStateFile represents the state of a file in the filesystem.
type ActualStateFile struct {
absPath AbsPath
perm fs.FileMode
*lazyContents
}
// A ActualStateSymlink represents the state of a symlink in the filesystem.
type ActualStateSymlink struct {
absPath AbsPath
*lazyLinkname
}
// NewActualStateEntry returns a new ActualStateEntry populated with absPath
// from system.
func NewActualStateEntry(system System, absPath AbsPath, fileInfo fs.FileInfo, err error) (ActualStateEntry, error) {
if fileInfo == nil {
fileInfo, err = system.Lstat(absPath)
}
switch {
case errors.Is(err, fs.ErrNotExist):
return &ActualStateAbsent{
absPath: absPath,
}, nil
case err != nil:
return nil, err
}
switch fileInfo.Mode().Type() {
case 0:
return &ActualStateFile{
absPath: absPath,
perm: fileInfo.Mode().Perm(),
lazyContents: newLazyContentsFunc(func() ([]byte, error) {
return system.ReadFile(absPath)
}),
}, nil
case fs.ModeDir:
return &ActualStateDir{
absPath: absPath,
perm: fileInfo.Mode().Perm(),
}, nil
case fs.ModeSymlink:
return &ActualStateSymlink{
absPath: absPath,
lazyLinkname: newLazyLinknameFunc(func() (string, error) {
linkname, err := system.Readlink(absPath)
if err != nil {
return "", err
}
return normalizeLinkname(linkname), nil
}),
}, nil
default:
return nil, &unsupportedFileTypeError{
absPath: absPath,
mode: fileInfo.Mode(),
}
}
}
// EntryState returns s's entry state.
func (s *ActualStateAbsent) EntryState() (*EntryState, error) {
return &EntryState{
Type: EntryStateTypeRemove,
}, nil
}
// Path returns s's path.
func (s *ActualStateAbsent) Path() AbsPath {
return s.absPath
}
// Remove removes s.
func (s *ActualStateAbsent) Remove(system System) error {
return nil
}
// OriginString returns s's origin.
func (s *ActualStateAbsent) OriginString() string {
return s.absPath.String()
}
// EntryState returns s's entry state.
func (s *ActualStateDir) EntryState() (*EntryState, error) {
return &EntryState{
Type: EntryStateTypeDir,
Mode: fs.ModeDir | s.perm,
}, nil
}
// Path returns s's path.
func (s *ActualStateDir) Path() AbsPath {
return s.absPath
}
// Remove removes s.
func (s *ActualStateDir) Remove(system System) error {
return system.RemoveAll(s.absPath)
}
// OriginString returns s's origin.
func (s *ActualStateDir) OriginString() string {
return s.absPath.String()
}
// EntryState returns s's entry state.
func (s *ActualStateFile) EntryState() (*EntryState, error) {
contents, err := s.Contents()
if err != nil {
return nil, err
}
contentsSHA256, err := s.ContentsSHA256()
if err != nil {
return nil, err
}
return &EntryState{
Type: EntryStateTypeFile,
Mode: s.perm,
ContentsSHA256: HexBytes(contentsSHA256),
contents: contents,
}, nil
}
// Path returns s's path.
func (s *ActualStateFile) Path() AbsPath {
return s.absPath
}
// Perm returns s's perm.
func (s *ActualStateFile) Perm() fs.FileMode {
return s.perm
}
// Remove removes s.
func (s *ActualStateFile) Remove(system System) error {
return system.RemoveAll(s.absPath)
}
// OriginString returns s's origin.
func (s *ActualStateFile) OriginString() string {
return s.absPath.String()
}
// EntryState returns s's entry state.
func (s *ActualStateSymlink) EntryState() (*EntryState, error) {
linkname, err := s.Linkname()
if err != nil {
return nil, err
}
linknameSHA256, err := s.LinknameSHA256()
if err != nil {
return nil, err
}
return &EntryState{
Type: EntryStateTypeSymlink,
ContentsSHA256: HexBytes(linknameSHA256),
contents: []byte(linkname),
}, nil
}
// Path returns s's path.
func (s *ActualStateSymlink) Path() AbsPath {
return s.absPath
}
// Remove removes s.
func (s *ActualStateSymlink) Remove(system System) error {
return system.RemoveAll(s.absPath)
}
// OriginString returns s's origin.
func (s *ActualStateSymlink) OriginString() string {
return s.absPath.String()
}