| 1 | package fsutil |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | gofs "io/fs" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "sort" |
| 10 | strings "strings" |
| 11 | "syscall" |
| 12 | |
| 13 | "github.com/pkg/errors" |
| 14 | "github.com/tonistiigi/fsutil/types" |
| 15 | ) |
| 16 | |
| 17 | func FollowLinks(fs FS, paths []string) ([]string, error) { |
| 18 | r := &symlinkResolver{fs: fs, resolved: map[string]struct{}{}} |
| 19 | for _, p := range paths { |
| 20 | if err := r.append(p); err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | } |
| 24 | res := make([]string, 0, len(r.resolved)) |
| 25 | for r := range r.resolved { |
| 26 | res = append(res, filepath.ToSlash(r)) |
| 27 | } |
| 28 | sort.Strings(res) |
| 29 | return dedupePaths(res), nil |
| 30 | } |
| 31 | |
| 32 | type symlinkResolver struct { |
| 33 | fs FS |
| 34 | resolved map[string]struct{} |
| 35 | } |
| 36 | |
| 37 | func (r *symlinkResolver) append(p string) error { |
| 38 | if runtime.GOOS == "windows" && filepath.IsAbs(filepath.FromSlash(p)) { |
| 39 | absParts := strings.SplitN(p, ":", 2) |
| 40 | if len(absParts) == 2 { |
| 41 | p = absParts[1] |
| 42 | } |
| 43 | } |
| 44 | p = filepath.Join(".", p) |
| 45 | current := "." |
| 46 | for { |
| 47 | parts := strings.SplitN(p, string(filepath.Separator), 2) |
| 48 | current = filepath.Join(current, parts[0]) |
| 49 | |
| 50 | targets, err := r.readSymlink(current, true) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | p = "" |
| 55 | if len(parts) == 2 { |
| 56 | p = parts[1] |
| 57 | } |
| 58 | |
| 59 | if p == "" || targets != nil { |
| 60 | if _, ok := r.resolved[current]; ok { |
| 61 | return nil |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if targets != nil { |
| 66 | r.resolved[current] = struct{}{} |
| 67 | for _, target := range targets { |
| 68 | if err := r.append(filepath.Join(target, p)); err != nil { |
| 69 | return err |
| 70 | } |
| 71 | } |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | if p == "" { |
| 76 | r.resolved[current] = struct{}{} |
| 77 | return nil |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (r *symlinkResolver) readSymlink(p string, allowWildcard bool) ([]string, error) { |
| 83 | base := filepath.Base(p) |
| 84 | if allowWildcard && containsWildcards(base) { |
| 85 | fis, err := readDir(r.fs, filepath.Dir(p)) |
| 86 | if err != nil { |
| 87 | if isNotFound(err) { |
| 88 | return nil, nil |
| 89 | } |
| 90 | return nil, errors.Wrap(err, "readdir") |
| 91 | } |
| 92 | var out []string |
| 93 | for _, f := range fis { |
| 94 | if ok, _ := filepath.Match(base, f.Name()); ok { |
| 95 | res, err := r.readSymlink(filepath.Join(filepath.Dir(p), f.Name()), false) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | out = append(out, res...) |
| 100 | } |
| 101 | } |
| 102 | return out, nil |
| 103 | } |
| 104 | |
| 105 | entry, err := statFile(r.fs, p) |
| 106 | if err != nil { |
| 107 | if isNotFound(err) { |
| 108 | return nil, nil |
| 109 | } |
| 110 | return nil, errors.WithStack(err) |
| 111 | } |
| 112 | if entry == nil { |
| 113 | return nil, nil |
| 114 | } |
| 115 | if entry.Type()&os.ModeSymlink == 0 { |
| 116 | return nil, nil |
| 117 | } |
| 118 | |
| 119 | fi, err := entry.Info() |
| 120 | if err != nil { |
| 121 | return nil, errors.WithStack(err) |
| 122 | } |
| 123 | stat, ok := fi.Sys().(*types.Stat) |
| 124 | if !ok { |
| 125 | return nil, errors.WithStack(&os.PathError{Path: p, Err: syscall.EBADMSG, Op: "fileinfo without stat info"}) |
| 126 | } |
| 127 | |
| 128 | link := filepath.Clean(stat.Linkname) |
| 129 | if filepath.IsAbs(link) { |
| 130 | return []string{link}, nil |
| 131 | } |
| 132 | return []string{ |
| 133 | filepath.Join(string(filepath.Separator), filepath.Join(filepath.Dir(p), link)), |
| 134 | }, nil |
| 135 | } |
| 136 | |
| 137 | func statFile(fs FS, root string) (os.DirEntry, error) { |
| 138 | var out os.DirEntry |
| 139 | |
| 140 | root = filepath.FromSlash(filepath.Clean(root)) |
| 141 | if root == string(filepath.Separator) || root == "." { |
| 142 | return nil, nil |
| 143 | } |
| 144 | |
| 145 | err := fs.Walk(context.TODO(), root, func(p string, entry os.DirEntry, err error) error { |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | if p != root { |
| 150 | return errors.Errorf("expected single entry %q but got %q", root, p) |
| 151 | } |
| 152 | out = entry |
| 153 | if entry.IsDir() { |
| 154 | return filepath.SkipDir |
| 155 | } |
| 156 | return nil |
| 157 | }) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
| 162 | if out == nil { |
| 163 | return nil, errors.Wrapf(os.ErrNotExist, "readFile %s", root) |
| 164 | } |
| 165 | return out, nil |
| 166 | } |
| 167 | |
| 168 | func readDir(fs FS, root string) ([]os.DirEntry, error) { |
| 169 | var out []os.DirEntry |
| 170 | |
| 171 | root = filepath.FromSlash(filepath.Clean(root)) |
| 172 | if root == string(filepath.Separator) || root == "." { |
| 173 | root = "." |
| 174 | out = make([]gofs.DirEntry, 0) |
| 175 | } |
| 176 | |
| 177 | err := fs.Walk(context.TODO(), root, func(p string, entry os.DirEntry, err error) error { |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | if p == root { |
| 182 | if !entry.IsDir() { |
| 183 | return errors.WithStack(&os.PathError{Op: "walk", Path: root, Err: syscall.ENOTDIR}) |
| 184 | } |
| 185 | out = make([]gofs.DirEntry, 0) |
| 186 | return nil |
| 187 | } |
| 188 | if out == nil { |
| 189 | return errors.Errorf("expected to read parent entry %q before child %q", root, p) |
| 190 | } |
| 191 | out = append(out, entry) |
| 192 | if entry.IsDir() { |
| 193 | return filepath.SkipDir |
| 194 | } |
| 195 | return nil |
| 196 | }) |
| 197 | if err != nil { |
| 198 | return nil, err |
| 199 | } |
| 200 | |
| 201 | if out == nil && root != "." { |
| 202 | return nil, errors.Wrapf(os.ErrNotExist, "readDir %s", root) |
| 203 | } |
| 204 | return out, nil |
| 205 | } |
| 206 | |
| 207 | func containsWildcards(name string) bool { |
| 208 | isWindows := runtime.GOOS == "windows" |
| 209 | for i := 0; i < len(name); i++ { |
| 210 | ch := name[i] |
| 211 | if ch == '\\' && !isWindows { |
| 212 | i++ |
| 213 | } else if ch == '*' || ch == '?' || ch == '[' { |
| 214 | return true |
| 215 | } |
| 216 | } |
| 217 | return false |
| 218 | } |
| 219 | |
| 220 | // dedupePaths expects input as a sorted list |
| 221 | func dedupePaths(in []string) []string { |
| 222 | out := make([]string, 0, len(in)) |
| 223 | var last string |
| 224 | for _, s := range in { |
| 225 | // if one of the paths is root there is no filter |
| 226 | if s == "." { |
| 227 | return nil |
| 228 | } |
| 229 | if strings.HasPrefix(s, last+"/") { |
| 230 | continue |
| 231 | } |
| 232 | out = append(out, s) |
| 233 | last = s |
| 234 | } |
| 235 | return out |
| 236 | } |