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

Commit 4d7004b

Browse files
committed
internal/gps: refactor prune and filesystem functions
This commit moves filesystemState from being a test struct to become part of gps. It's used to optimize the prune function by calculting the filesystem state and determining the files to prune in-memory. Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent ceb67da commit 4d7004b

8 files changed

+425
-348
lines changed

hack/lint.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ set -e
99
PKGS=$(go list ./... | grep -vF /vendor/)
1010
go vet $PKGS
1111
golint $PKGS
12-
megacheck -unused.exported -ignore "github.com/golang/dep/internal/test/test.go:U1000 github.com/golang/dep/internal/gps/prune.go:U1000" $PKGS
12+
megacheck -unused.exported -ignore "github.com/golang/dep/internal/test/test.go:U1000" $PKGS

internal/gps/filesystem.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gps
6+
7+
import (
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
// filesystemState represents the state of a file system.
13+
type filesystemState struct {
14+
root string
15+
dirs []string
16+
files []string
17+
links []fsLink
18+
}
19+
20+
// fsLink represents a symbolic link.
21+
type fsLink struct {
22+
path string
23+
to string
24+
}
25+
26+
// deriveFilesystemState returns a filesystemState based on the state of
27+
// the filesystem on root.
28+
func deriveFilesystemState(root string) (filesystemState, error) {
29+
fs := filesystemState{
30+
root: root,
31+
}
32+
33+
err := filepath.Walk(fs.root, func(path string, info os.FileInfo, err error) error {
34+
if err != nil {
35+
return err
36+
}
37+
38+
if path == fs.root {
39+
return nil
40+
}
41+
42+
relPath, err := filepath.Rel(fs.root, path)
43+
if err != nil {
44+
return err
45+
}
46+
47+
if (info.Mode() & os.ModeSymlink) != 0 {
48+
eval, err := filepath.EvalSymlinks(path)
49+
if err != nil {
50+
return err
51+
}
52+
53+
fs.links = append(fs.links, fsLink{
54+
path: relPath,
55+
to: eval,
56+
})
57+
58+
return nil
59+
}
60+
61+
if info.IsDir() {
62+
fs.dirs = append(fs.dirs, relPath)
63+
64+
return nil
65+
}
66+
67+
fs.files = append(fs.files, relPath)
68+
69+
return nil
70+
})
71+
72+
if err != nil {
73+
return filesystemState{}, err
74+
}
75+
76+
return fs, nil
77+
}

internal/gps/filesystem_test.go

+10-36
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,10 @@ import (
1212

1313
// This file contains utilities for running tests around file system state.
1414

15-
// fspath represents a file system path in an OS-agnostic way.
16-
type fsPath []string
17-
18-
func (f fsPath) String() string { return filepath.Join(f...) }
19-
20-
func (f fsPath) prepend(prefix string) fsPath {
21-
p := fsPath{filepath.FromSlash(prefix)}
22-
return append(p, f...)
23-
}
24-
2515
type fsTestCase struct {
2616
before, after filesystemState
2717
}
2818

29-
// filesystemState represents the state of a file system. It has a setup method
30-
// which inflates its state to the actual host file system, and an assert
31-
// method which checks that the actual file system matches the described state.
32-
type filesystemState struct {
33-
root string
34-
dirs []fsPath
35-
files []fsPath
36-
links []fsLink
37-
}
38-
3919
// assert makes sure that the fs state matches the state of the actual host
4020
// file system
4121
func (fs filesystemState) assert(t *testing.T) {
@@ -44,13 +24,13 @@ func (fs filesystemState) assert(t *testing.T) {
4424
linkMap := make(map[string]bool)
4525

4626
for _, d := range fs.dirs {
47-
dirMap[d.prepend(fs.root).String()] = true
27+
dirMap[filepath.Join(fs.root, d)] = true
4828
}
4929
for _, f := range fs.files {
50-
fileMap[f.prepend(fs.root).String()] = true
30+
fileMap[filepath.Join(fs.root, f)] = true
5131
}
5232
for _, l := range fs.links {
53-
linkMap[l.path.prepend(fs.root).String()] = true
33+
linkMap[filepath.Join(fs.root, l.path)] = true
5434
}
5535

5636
err := filepath.Walk(fs.root, func(path string, info os.FileInfo, err error) error {
@@ -106,12 +86,6 @@ func (fs filesystemState) assert(t *testing.T) {
10686
}
10787
}
10888

109-
// fsLink represents a symbolic link.
110-
type fsLink struct {
111-
path fsPath
112-
to string
113-
}
114-
11589
// setup inflates fs onto the actual host file system
11690
func (fs filesystemState) setup(t *testing.T) {
11791
fs.setupDirs(t)
@@ -121,17 +95,17 @@ func (fs filesystemState) setup(t *testing.T) {
12195

12296
func (fs filesystemState) setupDirs(t *testing.T) {
12397
for _, dir := range fs.dirs {
124-
p := dir.prepend(fs.root)
125-
if err := os.MkdirAll(p.String(), 0777); err != nil {
98+
p := filepath.Join(fs.root, dir)
99+
if err := os.MkdirAll(p, 0777); err != nil {
126100
t.Fatalf("os.MkdirAll(%q, 0777) err=%q", p, err)
127101
}
128102
}
129103
}
130104

131105
func (fs filesystemState) setupFiles(t *testing.T) {
132106
for _, file := range fs.files {
133-
p := file.prepend(fs.root)
134-
f, err := os.Create(p.String())
107+
p := filepath.Join(fs.root, file)
108+
f, err := os.Create(p)
135109
if err != nil {
136110
t.Fatalf("os.Create(%q) err=%q", p, err)
137111
}
@@ -143,15 +117,15 @@ func (fs filesystemState) setupFiles(t *testing.T) {
143117

144118
func (fs filesystemState) setupLinks(t *testing.T) {
145119
for _, link := range fs.links {
146-
p := link.path.prepend(fs.root)
120+
p := filepath.Join(fs.root, link.path)
147121

148122
// On Windows, relative symlinks confuse filepath.Walk. This is golang/go
149123
// issue 17540. So, we'll just sigh and do absolute links, assuming they are
150124
// relative to the directory of link.path.
151-
dir := filepath.Dir(p.String())
125+
dir := filepath.Dir(p)
152126
to := filepath.Join(dir, link.to)
153127

154-
if err := os.Symlink(to, p.String()); err != nil {
128+
if err := os.Symlink(to, p); err != nil {
155129
t.Fatalf("os.Symlink(%q, %q) err=%q", to, p, err)
156130
}
157131
}

0 commit comments

Comments
 (0)