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

Commit e4c3218

Browse files
committed
internal/gps: update stripVendor functions
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent 23e8a3c commit e4c3218

File tree

3 files changed

+53
-59
lines changed

3 files changed

+53
-59
lines changed

internal/gps/prune.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,7 @@ func pruneNonGoFiles(baseDir string, logger *log.Logger) error {
214214
return errors.Wrap(err, "could not prune non-Go files")
215215
}
216216

217-
if err := deleteFiles(files); err != nil {
218-
return err
219-
}
220-
221-
return nil
217+
return deleteFiles(files)
222218
}
223219

224220
// calculateNonGoFiles returns a list of all non-Go files within baseDir.
@@ -279,11 +275,7 @@ func pruneGoTestFiles(baseDir string, logger *log.Logger) error {
279275
return errors.Wrap(err, "could not prune Go test files")
280276
}
281277

282-
if err := deleteFiles(files); err != nil {
283-
return err
284-
}
285-
286-
return nil
278+
return deleteFiles(files)
287279
}
288280

289281
// calculateGoTestFiles walks over baseDir and returns a list of all

internal/gps/strip_vendor.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,27 @@ func stripVendor(path string, info os.FileInfo, err error) error {
1616
return err
1717
}
1818

19-
if info.Name() == "vendor" {
20-
if _, err := os.Lstat(path); err != nil {
19+
// Skip anything not named vendor
20+
if info.Name() != "vendor" {
21+
return nil
22+
}
23+
24+
// If the file is a symlink to a directory, delete the symlink.
25+
if (info.Mode() & os.ModeSymlink) != 0 {
26+
realInfo, err := os.Stat(path)
27+
if err != nil {
2128
return err
2229
}
23-
24-
if (info.Mode() & os.ModeSymlink) != 0 {
25-
realInfo, err := os.Stat(path)
26-
if err != nil {
27-
return err
28-
}
29-
if realInfo.IsDir() {
30-
return os.Remove(path)
31-
}
30+
if realInfo.IsDir() {
31+
return os.Remove(path)
3232
}
33+
}
3334

34-
if info.IsDir() {
35-
if err := os.RemoveAll(path); err != nil {
36-
return err
37-
}
38-
return filepath.SkipDir
35+
if info.IsDir() {
36+
if err := os.RemoveAll(path); err != nil {
37+
return err
3938
}
40-
41-
return nil
39+
return filepath.SkipDir
4240
}
4341

4442
return nil

internal/gps/strip_vendor_windows.go

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,42 @@ func stripVendor(path string, info os.FileInfo, err error) error {
1414
return err
1515
}
1616

17-
if info.Name() == "vendor" {
18-
if _, err := os.Lstat(path); err == nil {
19-
symlink := (info.Mode() & os.ModeSymlink) != 0
20-
dir := info.IsDir()
21-
22-
switch {
23-
case symlink && dir:
24-
// This could be a windows junction directory. Support for these in the
25-
// standard library is spotty, and we could easily delete an important
26-
// folder if we called os.Remove or os.RemoveAll. Just skip these.
27-
//
28-
// TODO: If we could distinguish between junctions and Windows symlinks,
29-
// we might be able to safely delete symlinks, even though junctions are
30-
// dangerous.
31-
return filepath.SkipDir
32-
33-
case symlink:
34-
realInfo, err := os.Stat(path)
35-
if err != nil {
36-
return err
37-
}
38-
if realInfo.IsDir() {
39-
return os.Remove(path)
40-
}
41-
42-
case dir:
43-
if err := os.RemoveAll(path); err != nil {
44-
return err
45-
}
46-
return filepath.SkipDir
47-
}
17+
if info.Name() != "vendor" {
18+
return nil
19+
}
20+
21+
if _, err := os.Lstat(path); err != nil {
22+
return nil
23+
}
24+
25+
symlink := (info.Mode() & os.ModeSymlink) != 0
26+
dir := info.IsDir()
27+
28+
switch {
29+
case symlink && dir:
30+
// This could be a windows junction directory. Support for these in the
31+
// standard library is spotty, and we could easily delete an important
32+
// folder if we called os.Remove or os.RemoveAll. Just skip these.
33+
//
34+
// TODO: If we could distinguish between junctions and Windows symlinks,
35+
// we might be able to safely delete symlinks, even though junctions are
36+
// dangerous.
37+
return filepath.SkipDir
38+
39+
case symlink:
40+
realInfo, err := os.Stat(path)
41+
if err != nil {
42+
return err
43+
}
44+
if realInfo.IsDir() {
45+
return os.Remove(path)
46+
}
47+
48+
case dir:
49+
if err := os.RemoveAll(path); err != nil {
50+
return err
4851
}
52+
return filepath.SkipDir
4953
}
5054

5155
return nil

0 commit comments

Comments
 (0)