-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfile.go
228 lines (195 loc) · 5.64 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (C) 2022 Emanuele Rocca
package main
import (
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
)
// PetsFile is the central data structure of the system: it is the in-memory
// representation of a configuration file (eg: sshd_config)
type PetsFile struct {
// Absolute path to the configuration file
Source string
Pkgs []PetsPackage
// Full destination path where the file has to be installed
Dest string
// Directory where the file has to be installed. This is only set in
// case we have to create the destination directory
Directory string
User *user.User
Group *user.Group
// use string instead of os.FileMode to avoid converting back and forth
Mode string
Pre *exec.Cmd
Post *exec.Cmd
// Is this a symbolic link or an actual file to be copied?
Link bool
}
func NewPetsFile() *PetsFile {
return &PetsFile{
Source: "",
Dest: "",
Mode: "",
Link: false,
}
}
// NeedsCopy returns PetsCause UPDATE if Source needs to be copied over Dest,
// CREATE if the Destination file does not exist yet, NONE otherwise.
func (pf *PetsFile) NeedsCopy() PetsCause {
if pf.Link || pf.Source == "" {
return NONE
}
shaSource, err := Sha256(pf.Source)
if err != nil {
log.Printf("[ERROR] cannot determine sha256 of Source file %s: %v\n", pf.Source, err)
return NONE
}
shaDest, err := Sha256(pf.Dest)
if os.IsNotExist(err) {
return CREATE
} else if err != nil {
log.Printf("[ERROR] cannot determine sha256 of Dest file %s: %v\n", pf.Dest, err)
return NONE
}
if shaSource == shaDest {
log.Printf("[DEBUG] same sha256 for %s and %s: %s\n", pf.Source, pf.Dest, shaSource)
return NONE
}
log.Printf("[DEBUG] sha256[%s]=%s != sha256[%s]=%s\n", pf.Source, shaSource, pf.Dest, shaDest)
return UPDATE
}
// NeedsLink returns PetsCause LINK if a symbolic link using Source as TARGET
// and Dest as LINK_NAME needs to be created. See ln(1) for the most confusing
// terminology.
func (pf *PetsFile) NeedsLink() PetsCause {
if !pf.Link || pf.Source == "" || pf.Dest == "" {
return NONE
}
fi, err := os.Lstat(pf.Dest)
if os.IsNotExist(err) {
// Dest does not exist yet. Happy path, we are gonna create it!
return LINK
}
if err != nil {
// There was an error calling lstat, putting all my money on
// permission denied.
log.Printf("[ERROR] cannot lstat Dest file %s: %v\n", pf.Dest, err)
return NONE
}
// We are here because Dest already exists and lstat succeeded. At this
// point there are two options:
// (1) Dest is already a link to Source \o/
// (2) Dest is a file, or a directory, or a link to something else /o\
//
// In any case there is no action to take, but let's come up with a valid
// excuse for not doing anything.
// Easy case first: Dest exists and it is not a symlink
if fi.Mode()&os.ModeSymlink == 0 {
log.Printf("[ERROR] %s already exists\n", pf.Dest)
return NONE
}
// Dest is a symlink
path, err := filepath.EvalSymlinks(pf.Dest)
if err != nil {
log.Printf("[ERROR] cannot EvalSymlinks() Dest file %s: %v\n", pf.Dest, err)
} else if pf.Source == path {
// Happy path
log.Printf("[DEBUG] %s is a symlink to %s already\n", pf.Dest, pf.Source)
} else {
log.Printf("[ERROR] %s is a symlink to %s instead of %s\n", pf.Dest, path, pf.Source)
}
return NONE
}
// NeedsDir returns PetsCause DIR if there is no directory at Directory,
// meaning that it has to be created. Most of this is suspiciously similar to
// NeedsLink above.
func (pf *PetsFile) NeedsDir() PetsCause {
if pf.Directory == "" {
return NONE
}
fi, err := os.Lstat(pf.Directory)
if os.IsNotExist(err) {
// Directory does not exist yet. Happy path, we are gonna create it!
return DIR
}
if err != nil {
// There was an error calling lstat, putting all my money on
// permission denied.
log.Printf("[ERROR] cannot lstat Directory %s: %v\n", pf.Directory, err)
return NONE
}
// We are here because Directory already exists and lstat succeeded. At this
// point there are two options:
// (1) Dest is a directory \o/
// (2) Dest is a file, a symlink, or something else (a squirrel?) /o\
//
// In any case there is no action to take, but let's come up with a valid
// excuse for not doing anything.
if !fi.IsDir() {
log.Printf("[ERROR] %s already exists and it is not a directory\n", pf.Directory)
}
return NONE
}
func (pf *PetsFile) IsValid(pathErrorOK bool) bool {
// Check if the specified package(s) exists
for _, pkg := range pf.Pkgs {
if !pkg.IsValid() {
return false
}
}
// Check pre-update validation command if the file has changed.
if pf.NeedsCopy() != NONE && !runPre(pf, pathErrorOK) {
return false
}
return true
}
func (pf *PetsFile) AddDest(dest string) {
pf.Dest = dest
pf.Directory = filepath.Dir(dest)
}
func (pf *PetsFile) AddLink(dest string) {
pf.Dest = dest
pf.Directory = filepath.Dir(dest)
pf.Link = true
}
func (pf *PetsFile) AddUser(userName string) error {
user, err := user.Lookup(userName)
if err != nil {
// TODO: one day we may add support for creating users
return err
}
pf.User = user
return nil
}
func (pf *PetsFile) AddGroup(groupName string) error {
group, err := user.LookupGroup(groupName)
if err != nil {
// TODO: one day we may add support for creating groups
return err
}
pf.Group = group
return nil
}
func (pf *PetsFile) AddMode(mode string) error {
_, err := StringToFileMode(mode)
if err == nil {
// The specified 'mode' string is valid.
pf.Mode = mode
}
return err
}
func (pf *PetsFile) AddPre(pre string) {
preArgs := strings.Fields(pre)
if len(preArgs) > 0 {
pf.Pre = NewCmd(preArgs)
}
}
func (pf *PetsFile) AddPost(post string) {
postArgs := strings.Fields(post)
if len(postArgs) > 0 {
pf.Post = NewCmd(postArgs)
}
}