| 1 | package fsutilscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/ |
| 2 | |
| 3 | import ( |
| 4 | "osscip-go gomod github.com/golang/go/src go1.21 os/" |
| 5 | "path/filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/" |
| 6 | "sortscip-go gomod github.com/golang/go/src go1.21 sort/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.21 strings/" |
| 8 | "syscallscip-go gomod github.com/golang/go/src go1.21 syscall/" |
| 9 | |
| 10 | "github.com/pkg/errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/" |
| 11 | ) |
| 12 | |
| 13 | type parentscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent# struct { |
| 14 | dirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#dir. string |
| 15 | lastscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#last. string |
| 16 | } |
| 17 | |
| 18 | type Validatorscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator# struct { |
| 19 | parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs. []parentscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent# |
| 20 | } |
| 21 | |
| 22 | func (v *Validatorscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#) HandleChangescip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#HandleChange().(kind ChangeKindscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/ChangeKind#, p string, fi osscip-go gomod github.com/golang/go/src go1.21 os/.FileInfoscip-go gomod github.com/golang/go/src go1.21 os/FileInfo#, err error) (retErr error) { |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | // test that all paths are in order and all parent dirs were present |
| 27 | if v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs. == nil { |
| 28 | v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs. = make([]parentscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#, 1, 10) |
| 29 | } |
| 30 | if p != filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Clean().(p) { |
| 31 | return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.WithStackscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/WithStack().(&osscip-go gomod github.com/golang/go/src go1.21 os/.PathErrorscip-go gomod github.com/golang/go/src go1.21 os/PathError#{Pathscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Path.: p, Errscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Err.: syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.EINVALscip-go gomod github.com/golang/go/src go1.21 syscall/EINVAL., Opscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Op.: "unclean path"}) |
| 32 | } |
| 33 | if filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.IsAbsscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/IsAbs().(p) { |
| 34 | return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.WithStackscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/WithStack().(&osscip-go gomod github.com/golang/go/src go1.21 os/.PathErrorscip-go gomod github.com/golang/go/src go1.21 os/PathError#{Pathscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Path.: p, Errscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Err.: syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.EINVALscip-go gomod github.com/golang/go/src go1.21 syscall/EINVAL., Opscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Op.: "absolute path"}) |
| 35 | } |
| 36 | dir := filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Dir().(p) |
| 37 | base := filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Base().(p) |
| 38 | if dir == "." { |
| 39 | dir = "" |
| 40 | } |
| 41 | if dir == ".." || stringsscip-go gomod github.com/golang/go/src go1.21 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21 strings/HasPrefix().(p, filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.FromSlashscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/FromSlash().("../")) { |
| 42 | return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.WithStackscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/WithStack().(&osscip-go gomod github.com/golang/go/src go1.21 os/.PathErrorscip-go gomod github.com/golang/go/src go1.21 os/PathError#{Pathscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Path.: p, Errscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Err.: syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.EINVALscip-go gomod github.com/golang/go/src go1.21 syscall/EINVAL., Opscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Op.: "escape check"}) |
| 43 | } |
| 44 | |
| 45 | // find a parent dir from saved records |
| 46 | i := sortscip-go gomod github.com/golang/go/src go1.21 sort/.Searchscip-go gomod github.com/golang/go/src go1.21 sort/Search().(len(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.), func(i int) bool { |
| 47 | return ComparePathscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/ComparePath().(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[len(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.)-1-i].dirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#dir., dir) <= 0 |
| 48 | }) |
| 49 | i = len(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.) - 1 - i |
| 50 | if i != len(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.)-1 { // skipping back to grandparent |
| 51 | v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs. = v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[:i+1] |
| 52 | } |
| 53 | |
| 54 | if dir != v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[len(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.)-1].dirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#dir. || v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[i].lastscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#last. >= base { |
| 55 | return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Errorfscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Errorf().("changes out of order: %q %q", p, filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Join().(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[i].dirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#dir., v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[i].lastscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#last.)) |
| 56 | } |
| 57 | v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs.[i].lastscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#last. = base |
| 58 | if kind != ChangeKindDeletescip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/ChangeKindDelete. && fi.IsDirscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FileInfo#IsDir().() { |
| 59 | v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs. = append(v.parentDirsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Validator#parentDirs., parentscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#{ |
| 60 | dirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#dir.: filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Join().(dir, base), |
| 61 | lastscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/parent#last.: "", |
| 62 | }) |
| 63 | } |
| 64 | // todo: validate invalid mode combinations |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | func ComparePathscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/ComparePath().(p1, p2 string) int { |
| 69 | // byte-by-byte comparison to be compatible with str<>str |
| 70 | min := minscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/min().(len(p1), len(p2)) |
| 71 | for i := 0; i < min; i++ { |
| 72 | switch { |
| 73 | case p1[i] == p2[i]: |
| 74 | continue |
| 75 | case p2[i] != filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator. && p1[i] < p2[i] || p1[i] == filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.: |
| 76 | return -1 |
| 77 | default: |
| 78 | return 1 |
| 79 | } |
| 80 | } |
| 81 | return len(p1) - len(p2) |
| 82 | } |
| 83 | |
| 84 | func minscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/min().(x, y int) int { |
| 85 | if x < y { |
| 86 | return x |
| 87 | } |
| 88 | return y |
| 89 | } |