Debian Codegraph @

kyaml/filesys/fsondisk.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package filesys
5
6 import (
7 "fmt"
8 "log"
9 "os"
10 "path/filepath"
11
12 "sigs.k8s.io/kustomize/kyaml/errors"
13 )
14
15 var _ FileSystem = fsOnDisk{}
16
17 // fsOnDisk implements FileSystem using the local filesystem.
18 type fsOnDisk struct{}
19
20 // MakeFsOnDisk makes an instance of fsOnDisk.
21 func MakeFsOnDisk() FileSystem {
22 return fsOnDisk{}
23 }
24
25 // Create delegates to os.Create.
26 func (fsOnDisk) Create(name string) (File, error) { return os.Create(name) }
27
28 // Mkdir delegates to os.Mkdir.
29 func (fsOnDisk) Mkdir(name string) error {
30 return os.Mkdir(name, 0777|os.ModeDir)
31 }
32
33 // MkdirAll delegates to os.MkdirAll.
34 func (fsOnDisk) MkdirAll(name string) error {
35 return os.MkdirAll(name, 0777|os.ModeDir)
36 }
37
38 // RemoveAll delegates to os.RemoveAll.
39 func (fsOnDisk) RemoveAll(name string) error {
40 return os.RemoveAll(name)
41 }
42
43 // Open delegates to os.Open.
44 func (fsOnDisk) Open(name string) (File, error) { return os.Open(name) }
45
46 // CleanedAbs converts the given path into a
47 // directory and a file name, where the directory
48 // is represented as a ConfirmedDir and all that implies.
49 // If the entire path is a directory, the file component
50 // is an empty string.
51 func (x fsOnDisk) CleanedAbs(
52 path string) (ConfirmedDir, string, error) {
53 absRoot, err := filepath.Abs(path)
54 if err != nil {
55 return "", "", fmt.Errorf(
56 "abs path error on '%s' : %v", path, err)
57 }
58 deLinked, err := filepath.EvalSymlinks(absRoot)
59 if err != nil {
60 return "", "", fmt.Errorf(
61 "evalsymlink failure on '%s' : %w", path, err)
62 }
63 if x.IsDir(deLinked) {
64 return ConfirmedDir(deLinked), "", nil
65 }
66 d := filepath.Dir(deLinked)
67 if !x.IsDir(d) {
68 // Programmer/assumption error.
69 log.Fatalf("first part of '%s' not a directory", deLinked)
70 }
71 if d == deLinked {
72 // Programmer/assumption error.
73 log.Fatalf("d '%s' should be a subset of deLinked", d)
74 }
75 f := filepath.Base(deLinked)
76 if filepath.Join(d, f) != deLinked {
77 // Programmer/assumption error.
78 log.Fatalf("these should be equal: '%s', '%s'",
79 filepath.Join(d, f), deLinked)
80 }
81 return ConfirmedDir(d), f, nil
82 }
83
84 // Exists returns true if os.Stat succeeds.
85 func (fsOnDisk) Exists(name string) bool {
86 _, err := os.Stat(name)
87 return err == nil
88 }
89
90 // Glob returns the list of matching files
91 func (fsOnDisk) Glob(pattern string) ([]string, error) {
92 var result []string
93 allFilePaths, err := filepath.Glob(pattern)
94 if err != nil {
95 return nil, err
96 }
97 if IsHiddenFilePath(pattern) {
98 result = allFilePaths
99 } else {
100 result = RemoveHiddenFiles(allFilePaths)
101 }
102 return result, nil
103 }
104
105 // IsDir delegates to os.Stat and FileInfo.IsDir
106 func (fsOnDisk) IsDir(name string) bool {
107 info, err := os.Stat(name)
108 if err != nil {
109 return false
110 }
111 return info.IsDir()
112 }
113
114 // ReadDir delegates to os.ReadDir
115 func (fsOnDisk) ReadDir(name string) ([]string, error) {
116 dirEntries, err := os.ReadDir(name)
117 if err != nil {
118 return nil, err
119 }
120 result := make([]string, len(dirEntries))
121 for i := range dirEntries {
122 result[i] = dirEntries[i].Name()
123 }
124 return result, nil
125 }
126
127 // ReadFile delegates to os.ReadFile.
128 func (fsOnDisk) ReadFile(name string) ([]byte, error) {
129 content, err := os.ReadFile(name)
130 return content, errors.Wrap(err)
131 }
132
133 // WriteFile delegates to os.WriteFile with read/write permissions.
134 func (fsOnDisk) WriteFile(name string, c []byte) error {
135 return errors.Wrap(os.WriteFile(name, c, 0666)) //nolint:gosec
136 }
137
138 // Walk delegates to filepath.Walk.
139 func (fsOnDisk) Walk(path string, walkFn filepath.WalkFunc) error {
140 return filepath.Walk(path, walkFn)
141 }