-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace_file.go
More file actions
43 lines (40 loc) · 1001 Bytes
/
Copy pathworkspace_file.go
File metadata and controls
43 lines (40 loc) · 1001 Bytes
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
package hpatch
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"unicode/utf8"
)
func (w filesystemWorkspace) readFile(ctx context.Context, path string) (loadedFile, error) {
if err := ctx.Err(); err != nil {
return loadedFile{}, err
}
file, err := w.open(path)
if err != nil {
reason := reasonOther
if errors.Is(err, fs.ErrNotExist) {
reason = reasonFileMissing
}
return loadedFile{}, withReason(reason, fmt.Errorf("reading %s: %w", path, err))
}
defer func() {
_ = file.Close()
}()
info, err := file.Stat()
if err != nil {
return loadedFile{}, fmt.Errorf("reading %s: %w", path, err)
}
if !info.Mode().IsRegular() {
return loadedFile{}, fmt.Errorf("%s is not a regular file", path)
}
content, err := io.ReadAll(file)
if err != nil {
return loadedFile{}, fmt.Errorf("reading %s: %w", path, err)
}
if !utf8.Valid(content) {
return loadedFile{}, fmt.Errorf("%s is not UTF-8", path)
}
return loadedFile{content: string(content), mode: info.Mode()}, nil
}