Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit b50a8b5

Browse files
committed
gps: handle symlinks properly
We now delete anything that looks like a symlink if its called "vendor" while pruning. Hopefully, you didn't make a bad decision by relying on the magical properties of symlinks. Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent 07a486b commit b50a8b5

File tree

3 files changed

+308
-230
lines changed

3 files changed

+308
-230
lines changed

gps/filesystem.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,9 @@ func deriveFilesystemState(root string) (filesystemState, error) {
104104
l := fsLink{path: relPath}
105105

106106
l.to, err = filepath.EvalSymlinks(path)
107-
if strings.HasSuffix(err.Error(), "too many links") {
107+
if err != nil && strings.HasSuffix(err.Error(), "too many links") {
108108
l.circular = true
109-
} else if err != nil {
110-
return err
111-
}
112-
113-
if _, err := os.Stat(l.to); os.IsNotExist(err) {
109+
} else if err != nil && os.IsNotExist(err) {
114110
l.broken = true
115111
} else if err != nil {
116112
return err

gps/filesystem_test.go

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package gps
66

77
import (
8+
"fmt"
89
"os"
910
"path/filepath"
1011
"reflect"
@@ -99,28 +100,121 @@ func (tc fsTestCase) setup(t *testing.T) {
99100

100101
func TestDeriveFilesystemState(t *testing.T) {
101102
testcases := []struct {
102-
name string
103-
state filesystemState
103+
name string
104+
fs fsTestCase
104105
}{
105106
{
106-
name: "simple-case",
107-
state: filesystemState{},
107+
name: "simple-case",
108+
fs: fsTestCase{
109+
before: filesystemState{
110+
dirs: []string{
111+
"simple-dir",
112+
},
113+
files: []string{
114+
"simple-file",
115+
},
116+
},
117+
after: filesystemState{
118+
dirs: []string{
119+
"simple-dir",
120+
},
121+
files: []string{
122+
"simple-file",
123+
},
124+
},
125+
},
126+
},
127+
{
128+
name: "simple-symlink-case",
129+
fs: fsTestCase{
130+
before: filesystemState{
131+
dirs: []string{
132+
"simple-dir",
133+
},
134+
files: []string{
135+
"simple-file",
136+
},
137+
links: []fsLink{
138+
fsLink{
139+
path: "link",
140+
to: "nonexisting",
141+
broken: true,
142+
},
143+
},
144+
},
145+
after: filesystemState{
146+
dirs: []string{
147+
"simple-dir",
148+
},
149+
files: []string{
150+
"simple-file",
151+
},
152+
links: []fsLink{
153+
fsLink{
154+
path: "link",
155+
to: "",
156+
broken: true,
157+
},
158+
},
159+
},
160+
},
161+
},
162+
{
163+
name: "complex-symlink-case",
164+
fs: fsTestCase{
165+
before: filesystemState{
166+
links: []fsLink{
167+
fsLink{
168+
path: "link1",
169+
to: "link2",
170+
circular: true,
171+
},
172+
fsLink{
173+
path: "link2",
174+
to: "link1",
175+
circular: true,
176+
},
177+
},
178+
},
179+
after: filesystemState{
180+
links: []fsLink{
181+
fsLink{
182+
path: "link1",
183+
to: "",
184+
circular: true,
185+
},
186+
fsLink{
187+
path: "link2",
188+
to: "",
189+
circular: true,
190+
},
191+
},
192+
},
193+
},
108194
},
109195
}
110196

111197
for _, tc := range testcases {
112198
h := test.NewHelper(t)
113-
defer h.Cleanup()
114199

115200
h.TempDir(tc.name)
116201

202+
tc.fs.before.root = h.Path(tc.name)
203+
tc.fs.after.root = h.Path(tc.name)
204+
205+
tc.fs.setup(t)
206+
117207
state, err := deriveFilesystemState(h.Path(tc.name))
118208
if err != nil {
119209
t.Fatal(err)
120210
}
121211

122-
if !reflect.DeepEqual(tc.state, state) {
212+
if !reflect.DeepEqual(tc.fs.after, state) {
213+
fmt.Println(tc.fs.after)
214+
fmt.Println(state)
123215
t.Fatal("filesystem state mismatch")
124216
}
217+
218+
h.Cleanup()
125219
}
126220
}

0 commit comments

Comments
 (0)