Skip to content

Commit 8324798

Browse files
authored
Copy over useful parts of container-diff (#424)
Signed-off-by: Appu Goundan <[email protected]>
1 parent 048f4eb commit 8324798

File tree

8 files changed

+861
-20
lines changed

8 files changed

+861
-20
lines changed

go.mod

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ module github.com/GoogleContainerTools/container-structure-test
33
go 1.22
44

55
require (
6-
github.com/GoogleContainerTools/container-diff v0.19.0
76
github.com/fsouza/go-dockerclient v1.10.2
87
github.com/google/go-cmp v0.6.0
98
github.com/google/go-containerregistry v0.19.0
109
github.com/joho/godotenv v1.5.1
10+
github.com/moby/sys/sequential v0.5.0
1111
github.com/opencontainers/image-spec v1.1.0
1212
github.com/pkg/errors v0.9.1
1313
github.com/sirupsen/logrus v1.9.3
1414
github.com/spf13/cobra v1.8.0
15-
golang.org/x/crypto v0.19.0
15+
golang.org/x/crypto v0.20.0
1616
gopkg.in/yaml.v2 v2.4.0
1717
)
1818

@@ -41,17 +41,16 @@ require (
4141
github.com/kr/text v0.2.0 // indirect
4242
github.com/mitchellh/go-homedir v1.1.0 // indirect
4343
github.com/moby/patternmatcher v0.6.0 // indirect
44-
github.com/moby/sys/sequential v0.5.0 // indirect
4544
github.com/moby/sys/user v0.1.0 // indirect
4645
github.com/moby/term v0.5.0 // indirect
4746
github.com/morikuni/aec v1.0.0 // indirect
4847
github.com/opencontainers/go-digest v1.0.0 // indirect
4948
github.com/spf13/pflag v1.0.5 // indirect
5049
github.com/vbatts/tar-split v0.11.5 // indirect
51-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 // indirect
52-
go.opentelemetry.io/otel v1.23.1 // indirect
53-
go.opentelemetry.io/otel/metric v1.23.1 // indirect
54-
go.opentelemetry.io/otel/trace v1.23.1 // indirect
50+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
51+
go.opentelemetry.io/otel v1.24.0 // indirect
52+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
53+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
5554
golang.org/x/mod v0.15.0 // indirect
5655
golang.org/x/sync v0.6.0 // indirect
5756
golang.org/x/sys v0.17.0 // indirect

go.sum

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9
22
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
33
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
44
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
5-
github.com/GoogleContainerTools/container-diff v0.19.0 h1:drqYeYNSg3XVK6CMjlWdELucr4YDQs3I3nYAgBsFpvY=
6-
github.com/GoogleContainerTools/container-diff v0.19.0/go.mod h1:KGejo9uccdErmW2R9PanwirruTOjaZwfBdp6m0+m2rw=
75
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
86
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
97
github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8=
@@ -105,27 +103,27 @@ github.com/vbatts/tar-split v0.11.5 h1:3bHCTIheBm1qFTcgh9oPu+nNBtX+XJIupG/vacinC
105103
github.com/vbatts/tar-split v0.11.5/go.mod h1:yZbwRsSeGjusneWgA781EKej9HF8vme8okylkAeNKLk=
106104
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
107105
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
108-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 h1:doUP+ExOpH3spVTLS0FcWGLnQrPct/hD/bCPbDRUEAU=
109-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0/go.mod h1:rdENBZMT2OE6Ne/KLwpiXudnAsbdrdBaqBvTN8M8BgA=
110-
go.opentelemetry.io/otel v1.23.1 h1:Za4UzOqJYS+MUczKI320AtqZHZb7EqxO00jAHE0jmQY=
111-
go.opentelemetry.io/otel v1.23.1/go.mod h1:Td0134eafDLcTS4y+zQ26GE8u3dEuRBiBCTUIRHaikA=
106+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk=
107+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw=
108+
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
109+
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
112110
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U=
113111
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE=
114112
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg=
115113
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU=
116-
go.opentelemetry.io/otel/metric v1.23.1 h1:PQJmqJ9u2QaJLBOELl1cxIdPcpbwzbkjfEyelTl2rlo=
117-
go.opentelemetry.io/otel/metric v1.23.1/go.mod h1:mpG2QPlAfnK8yNhNJAxDZruU9Y1/HubbC+KyH8FaCWI=
114+
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
115+
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
118116
go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o=
119117
go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A=
120-
go.opentelemetry.io/otel/trace v1.23.1 h1:4LrmmEd8AU2rFvU1zegmvqW7+kWarxtNOPyeL6HmYY8=
121-
go.opentelemetry.io/otel/trace v1.23.1/go.mod h1:4IpnpJFwr1mo/6HL8XIPJaE9y0+u1KcVmuW7dwFSVrI=
118+
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
119+
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
122120
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
123121
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
124122
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
125123
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
126124
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
127-
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
128-
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
125+
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
126+
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
129127
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
130128
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
131129
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=

internal/pkgutil/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This package was copied over from [container-diff](https://github.com/GoogleContainerTools/container-diff) to remove our dependency on it. That project is too hard to maintain.
2+
3+
project: https://github.com/GoogleContainerTools/container-diff
4+
5+
commit: ae4befd09f92caf735cdd63794ae2fa9f2efc5e3
6+
7+
path: ./pkg/util

internal/pkgutil/fs_utils.go

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Copyright 2018 Google, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pkgutil
18+
19+
import (
20+
"bytes"
21+
"io"
22+
"io/ioutil"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
27+
"github.com/sirupsen/logrus"
28+
)
29+
30+
// Directory stores a representation of a file directory.
31+
type Directory struct {
32+
Root string
33+
Content []string
34+
}
35+
36+
type DirectoryEntry struct {
37+
Name string
38+
Size int64
39+
}
40+
41+
func GetSize(path string) int64 {
42+
stat, err := os.Lstat(path)
43+
if err != nil {
44+
logrus.Errorf("Could not obtain size for %s: %s", path, err)
45+
return -1
46+
}
47+
if stat.IsDir() {
48+
size, err := getDirectorySize(path)
49+
if err != nil {
50+
logrus.Errorf("Could not obtain directory size for %s: %s", path, err)
51+
}
52+
return size
53+
}
54+
return stat.Size()
55+
}
56+
57+
// GetFileContents returns the contents of a file at the specified path
58+
func GetFileContents(path string) (*string, error) {
59+
if _, err := os.Lstat(path); os.IsNotExist(err) {
60+
return nil, err
61+
}
62+
63+
contents, err := ioutil.ReadFile(path)
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
strContents := string(contents)
69+
//If file is empty, return nil
70+
if strContents == "" {
71+
return nil, nil
72+
}
73+
return &strContents, nil
74+
}
75+
76+
func getDirectorySize(path string) (int64, error) {
77+
var size int64
78+
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
79+
if !info.IsDir() {
80+
size += info.Size()
81+
}
82+
return err
83+
})
84+
return size, err
85+
}
86+
87+
// GetDirectoryContents converts the directory starting at the provided path into a Directory struct.
88+
func GetDirectory(path string, deep bool) (Directory, error) {
89+
var directory Directory
90+
directory.Root = path
91+
var err error
92+
if deep {
93+
walkFn := func(currPath string, info os.FileInfo, err error) error {
94+
newContent := strings.TrimPrefix(currPath, directory.Root)
95+
if newContent != "" {
96+
directory.Content = append(directory.Content, newContent)
97+
}
98+
return nil
99+
}
100+
101+
err = filepath.Walk(path, walkFn)
102+
} else {
103+
contents, err := ioutil.ReadDir(path)
104+
if err != nil {
105+
return directory, err
106+
}
107+
108+
for _, file := range contents {
109+
fileName := "/" + file.Name()
110+
directory.Content = append(directory.Content, fileName)
111+
}
112+
}
113+
return directory, err
114+
}
115+
116+
func GetDirectoryEntries(d Directory) []DirectoryEntry {
117+
return CreateDirectoryEntries(d.Root, d.Content)
118+
}
119+
120+
func CreateDirectoryEntries(root string, entryNames []string) (entries []DirectoryEntry) {
121+
for _, name := range entryNames {
122+
entryPath := filepath.Join(root, name)
123+
size := GetSize(entryPath)
124+
125+
entry := DirectoryEntry{
126+
Name: name,
127+
Size: size,
128+
}
129+
entries = append(entries, entry)
130+
}
131+
return entries
132+
}
133+
134+
func CheckSameSymlink(f1name, f2name string) (bool, error) {
135+
link1, err := os.Readlink(f1name)
136+
if err != nil {
137+
return false, err
138+
}
139+
link2, err := os.Readlink(f2name)
140+
if err != nil {
141+
return false, err
142+
}
143+
return (link1 == link2), nil
144+
}
145+
146+
func CheckSameFile(f1name, f2name string) (bool, error) {
147+
// Check first if files differ in size and immediately return
148+
f1stat, err := os.Lstat(f1name)
149+
if err != nil {
150+
return false, err
151+
}
152+
f2stat, err := os.Lstat(f2name)
153+
if err != nil {
154+
return false, err
155+
}
156+
157+
if f1stat.Size() != f2stat.Size() {
158+
return false, nil
159+
}
160+
161+
// Next, check file contents
162+
f1, err := ioutil.ReadFile(f1name)
163+
if err != nil {
164+
return false, err
165+
}
166+
f2, err := ioutil.ReadFile(f2name)
167+
if err != nil {
168+
return false, err
169+
}
170+
171+
if !bytes.Equal(f1, f2) {
172+
return false, nil
173+
}
174+
return true, nil
175+
}
176+
177+
// HasFilepathPrefix checks if the given file path begins with prefix
178+
func HasFilepathPrefix(path, prefix string) bool {
179+
path = filepath.Clean(path)
180+
prefix = filepath.Clean(prefix)
181+
pathArray := strings.Split(path, "/")
182+
prefixArray := strings.Split(prefix, "/")
183+
184+
if len(pathArray) < len(prefixArray) {
185+
return false
186+
}
187+
for index := range prefixArray {
188+
if prefixArray[index] == pathArray[index] {
189+
continue
190+
}
191+
return false
192+
}
193+
return true
194+
}
195+
196+
// given a path to a directory, check if it has any contents
197+
func DirIsEmpty(path string) (bool, error) {
198+
f, err := os.Open(path)
199+
if err != nil {
200+
return false, err
201+
}
202+
defer f.Close()
203+
204+
_, err = f.Readdir(1)
205+
if err == io.EOF {
206+
return true, nil
207+
}
208+
return false, err
209+
}
210+
211+
// CleanFilePath removes characters from a given path that cannot be used
212+
// in paths by the underlying platform (e.g. Windows)
213+
func CleanFilePath(dirtyPath string) string {
214+
var windowsReplacements = []string{"<", "_", ">", "_", ":", "_", "?", "_", "*", "_", "?", "_", "|", "_"}
215+
replacer := strings.NewReplacer(windowsReplacements...)
216+
return filepath.Clean(replacer.Replace(dirtyPath))
217+
}

0 commit comments

Comments
 (0)