-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.go
505 lines (464 loc) · 13.5 KB
/
os.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package helper
// Package file os.go contains the helper functions for file system operations.
import (
"bufio"
"crypto/sha512"
"embed"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/user"
"path/filepath"
"sort"
"strings"
"unicode/utf8"
)
const (
// WriteWriteRead is the file mode for read and write access.
// The file owner and group has read and write access, and others have read access.
WriteWriteRead fs.FileMode = 0o664
DSStore = ".DS_Store" // DSStore is the macOS directory service store file.
TempBase = "defacto2-server" // TempBase is the base subdirectory for temporary files.
DirWriteReadRead = 0o755 // Directory permissions.
)
// Extension is a file extension with a count of files.
type Extension struct {
Name string // Name is the file extension.
Count int64 // Count is the number of files with the extension.
}
// CountExts returns the file extensions and the number of files in the given directory.
func CountExts(dir string) ([]Extension, error) {
exts := make(map[string]int64)
files, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("count extensions read directory: %w", err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if file.Name() == DSStore {
continue
}
ext := strings.ToLower(filepath.Ext(file.Name()))
exts[ext]++
}
extensions := make([]Extension, 0, len(exts))
for k, v := range exts {
if k == "" {
k = "uuid"
}
extensions = append(extensions, Extension{Name: k, Count: v})
}
sort.Slice(extensions, func(i, j int) bool {
return extensions[i].Count > extensions[j].Count
})
return extensions, nil
}
// Count returns the number of files in the given directory.
func Count(dir string) (int, error) {
i := 0
st, err := os.Stat(dir)
if err != nil {
return 0, fmt.Errorf("count os.stat %w", err)
}
if !st.IsDir() {
return 0, fmt.Errorf("%w: %s", ErrDirPath, dir)
}
files, err := os.ReadDir(dir)
if err != nil {
return 0, fmt.Errorf("count os.readdir %w", err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if file.Name() == DSStore {
continue
}
i++
}
return i, nil
}
// DiskUsage returns the total size of the files in the given directory.
func DiskUsage(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintln(io.Discard, err)
return nil
}
if !info.IsDir() {
size += info.Size()
}
return nil
})
if err != nil {
return 0, fmt.Errorf("disk usage %w", err)
}
return size, nil
}
// Duplicate is a workaround for renaming files across different devices.
// A cross device can also be a different file system such as a Docker volume.
// It returns the number of bytes written to the new file.
// The function returns an error if the newpath already exists.
func Duplicate(oldpath, newpath string) (int64, error) {
const createNoTruncate = os.O_CREATE | os.O_WRONLY | os.O_EXCL
return duplicate(oldpath, newpath, createNoTruncate)
}
// DuplicateOW is a workaround for renaming files across different devices.
// A cross device can also be a different file system such as a Docker volume.
// It returns the number of bytes written to the new file.
// The function will truncate and overwrite the newpath if it already exists.
func DuplicateOW(oldpath, newpath string) (int64, error) {
const createTruncate = os.O_CREATE | os.O_WRONLY | os.O_TRUNC
return duplicate(oldpath, newpath, createTruncate)
}
func duplicate(oldpath, newpath string, flag int) (int64, error) {
src, err := os.Open(oldpath)
if err != nil {
return 0, fmt.Errorf("duplicate os.open %w", err)
}
defer src.Close()
dst, err := os.OpenFile(newpath, flag, WriteWriteRead)
if err != nil {
return 0, fmt.Errorf("duplicate os.create %w", err)
}
defer dst.Close()
written, err := io.Copy(dst, src)
if err != nil {
return 0, fmt.Errorf("duplicate io.copy %w", err)
}
return written, nil
}
// File returns true if the named file exists on the system.
func File(name string) bool {
s, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false
}
return false
}
if s.IsDir() {
return false
}
return true
}
// Files returns the filenames in the given directory.
func Files(dir string) ([]string, error) {
st, err := os.Stat(dir)
if err != nil {
return nil, fmt.Errorf("files os.stat %w", err)
}
if !st.IsDir() {
return nil, fmt.Errorf("%w: %s", ErrDirPath, dir)
}
files, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("files os.readdir: %w", err)
}
names := []string{}
for _, file := range files {
if file.IsDir() {
continue
}
if file.Name() == DSStore {
continue
}
names = append(names, file.Name())
}
return names, nil
}
// FileMatch returns true if the two named files are the same.
// It returns false if the files are of different lengths or
// if an error occurs while reading the files.
// The read buffer size is 4096 bytes.
func FileMatch(name1, name2 string) (bool, error) {
f1, err := os.Open(name1)
if err != nil {
return false, fmt.Errorf("file match os.open %s: %w", name1, err)
}
defer f1.Close()
f2, err := os.Open(name2)
if err != nil {
return false, fmt.Errorf("file match os.open %s: %w", name2, err)
}
defer f2.Close()
const maxSize = 4096
buf1 := make([]byte, maxSize)
buf2 := make([]byte, maxSize)
for {
n1, err1 := f1.Read(buf1)
n2, err2 := f2.Read(buf2)
if err1 != nil || err2 != nil {
if err1 == io.EOF && err2 == io.EOF {
break
} else if err1 == io.EOF || err2 == io.EOF {
return false, ErrDiffLength
}
return false, fmt.Errorf("file match %w: %s, %s", ErrRead, name1, name2)
}
if n1 != n2 || string(buf1[:n1]) != string(buf2[:n2]) {
return false, nil
}
}
return true, nil
}
// Finds returns true if the name is found in the collection of names.
func Finds(name string, names ...string) bool {
for _, n := range names {
if n == name {
return true
}
}
return false
}
// Integrity returns the sha384 hash of the named embed file.
// This is intended to be used for Subresource Integrity (SRI)
// verification with integrity attributes in HTML script and link tags.
func Integrity(name string, fs embed.FS) (string, error) {
b, err := fs.ReadFile(name)
if err != nil {
return "", fmt.Errorf("integrity fs.readfile %w", err)
}
return IntegrityBytes(b), nil
}
// IntegrityFile returns the sha384 hash of the named file.
// This can be used as a link cache buster.
func IntegrityFile(name string) (string, error) {
b, err := os.ReadFile(name)
if err != nil {
return "", fmt.Errorf("integrity os.readfile %w", err)
}
return IntegrityBytes(b), nil
}
// IntegrityBytes returns the sha384 hash of the given byte slice.
func IntegrityBytes(b []byte) string {
sum := sha512.Sum384(b)
b64 := base64.StdEncoding.EncodeToString(sum[:])
return "sha384-" + b64
}
// Lines returns the number of lines in the named file.
func Lines(name string) (int, error) {
file, err := os.Open(name)
if err != nil {
return 0, fmt.Errorf("integrity os.open %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
lines := 0
for scanner.Scan() {
lines++
}
if err := scanner.Err(); err != nil {
return 0, fmt.Errorf("integrity scanner.scan %w", err)
}
return lines, nil
}
// MkContent returns the destination directory for the extracted archive content.
// The directory is created if it does not exist. The directory is named after the source file.
func MkContent(src string) (string, error) {
name := strings.TrimSpace(strings.ToLower(filepath.Base(src)))
dir := TmpDir()
pattern := "artifact-content-" + name
dst := filepath.Join(dir, pattern)
if st, err := os.Stat(dst); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dst, DirWriteReadRead); err != nil {
return "", fmt.Errorf("mkcontent %w", err)
}
return dst, nil
}
return dst, nil
} else if !st.IsDir() {
return "", fmt.Errorf("mkcontent %w: %s", ErrNoDir, dir)
}
return dst, nil
}
// Owner returns the running user and group of the web application.
// The function returns the group names and the username of the owner.
func Owner() ([]string, string, error) {
curr, err := user.Current()
if err != nil {
return nil, "", fmt.Errorf("owner user current %w", err)
}
grps, err := curr.GroupIds()
if err != nil {
return nil, "", fmt.Errorf("owner user group ids %w", err)
}
groups := make([]string, len(grps))
for i, id := range grps {
group, err := user.LookupId(id)
if err != nil {
continue
}
groups[i] = group.Name
}
return groups, curr.Username, nil
}
// RenameFile renames a file from oldpath to newpath.
// It returns an error if the oldpath does not exist or is a directory,
// newpath already exists, or the rename fails.
func RenameFile(oldpath, newpath string) error {
st, err := os.Stat(oldpath)
if err != nil {
return fmt.Errorf("rename file os.stat %w", err)
}
if st.IsDir() {
return fmt.Errorf("rename file oldpath %w: %s", ErrFilePath, oldpath)
}
if _, err = os.Stat(newpath); err == nil {
return fmt.Errorf("rename file newpath %w: %s", ErrExistPath, newpath)
}
if err := os.Rename(oldpath, newpath); err != nil {
var linkErr *os.LinkError
if errors.As(err, &linkErr) && linkErr.Err.Error() == "invalid cross-device link" {
return RenameCrossDevice(oldpath, newpath)
}
return fmt.Errorf("rename file os.rename %w", err)
}
return nil
}
// RenameFileOW renames a file from oldpath to newpath.
// It returns an error if the oldpath does not exist or is a directory
// or the rename fails.
func RenameFileOW(oldpath, newpath string) error {
st, err := os.Stat(newpath)
if err == nil && st.IsDir() {
_ = os.Remove(newpath)
}
return RenameFile(oldpath, newpath)
}
// RenameCrossDevice is a workaround for renaming files across different devices.
// A cross device can also be a different file system such as a Docker volume.
func RenameCrossDevice(oldpath, newpath string) error {
src, err := os.Open(oldpath)
if err != nil {
return fmt.Errorf("rename cross device open source, %w", err)
}
defer src.Close()
dst, err := os.Create(newpath)
if err != nil {
return fmt.Errorf("rename cross device create new, %w", err)
}
defer dst.Close()
if _, err = io.Copy(dst, src); err != nil {
return fmt.Errorf("rename cross device copy %w", err)
}
if fi, err := os.Stat(oldpath); err != nil {
defer os.Remove(newpath)
return fmt.Errorf("rename cross device stat %w", err)
} else if fi.Size() == 0 {
defer os.Remove(newpath)
defer os.Remove(oldpath)
return fmt.Errorf("rename cross device empty file, %w", os.ErrNotExist)
}
defer os.Remove(oldpath)
return nil
}
// Size returns the size of the named file.
// If the file does not exist, it returns -1.
func Size(name string) int64 {
st, err := os.Stat(name)
if err != nil {
return -1
}
return st.Size()
}
// Stat stats the named file or directory to confirm it exists on the system.
func Stat(name string) bool {
if _, err := os.Stat(name); err != nil {
return false
}
return true
}
// StrongIntegrity returns the SHA-386 checksum value of the named file.
func StrongIntegrity(name string) (string, error) {
// strong hashes require the named file to be reopened after being read.
f, err := os.Open(name)
if err != nil {
return "", fmt.Errorf("strong integrity open %w: %s", err, name)
}
defer f.Close()
strong, err := Sum386(f)
if err != nil {
return "", fmt.Errorf("strong integrity %w", err)
}
return strong, nil
}
// Sum386 returns the SHA-386 checksum value of the open file.
func Sum386(f *os.File) (string, error) {
if f == nil {
return "", ErrOSFile
}
strong := sha512.New384()
if _, err := io.Copy(strong, f); err != nil {
return "", fmt.Errorf("sha386 checksum %s: %w", f.Name(), err)
}
s := hex.EncodeToString(strong.Sum(nil))
return s, nil
}
// TempDir returns the temporary directory for the server,
// which is a subdirectory of the system temp directory.
func TmpDir() string {
path := filepath.Join(os.TempDir(), TempBase)
if err := os.MkdirAll(path, DirWriteReadRead); err != nil {
return os.TempDir()
}
return path
}
// Touch creates a new, empty named file.
// If the file already exists, an error is returned.
func Touch(name string) error {
file, err := os.OpenFile(name, os.O_CREATE|os.O_EXCL, WriteWriteRead)
if err != nil {
return fmt.Errorf("touch open file %w", err)
}
if err := file.Close(); err != nil {
return fmt.Errorf("touch file close %w", err)
}
return nil
}
// TouchW creates a new named file with the given data.
// If the file already exists, an error is returned.
func TouchW(name string, data ...byte) (int, error) {
file, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY, WriteWriteRead)
if err != nil {
return 0, fmt.Errorf("touch write open file %w", err)
}
if len(data) == 0 {
if err := file.Close(); err != nil {
return 0, fmt.Errorf("touch write open file close %w", err)
}
return 0, nil
}
i, err := file.Write(data)
if err != nil {
return 0, fmt.Errorf("touch write file write %w", err)
}
if err := file.Close(); err != nil {
return 0, fmt.Errorf("touch write file write close %w", err)
}
return i, nil
}
// UTF8 returns true if the named file is a valid UTF-8 encoded file.
// The function reads the first 512 bytes of the file to determine the encoding.
func UTF8(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, fmt.Errorf("utf8 open %w", err)
}
defer f.Close()
const sample = 512
buf := make([]byte, sample)
_, err = f.Read(buf)
if err != nil {
return false, fmt.Errorf("utf8 read %w", err)
}
return utf8.Valid(buf), nil
}