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

Commit

Permalink
internal/gps: refactor prune and filesystem functions
Browse files Browse the repository at this point in the history
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 <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Sep 24, 2017
1 parent ceb67da commit 4db54a5
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 347 deletions.
2 changes: 1 addition & 1 deletion hack/lint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ set -e
PKGS=$(go list ./... | grep -vF /vendor/)
go vet $PKGS
golint $PKGS
megacheck -unused.exported -ignore "github.com/golang/dep/internal/test/test.go:U1000 github.com/golang/dep/internal/gps/prune.go:U1000" $PKGS
megacheck -unused.exported -ignore "github.com/golang/dep/internal/test/test.go:U1000" $PKGS
77 changes: 77 additions & 0 deletions internal/gps/filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gps

import (
"os"
"path/filepath"
)

// filesystemState represents the state of a file system.
type filesystemState struct {
root string
dirs []string
files []string
links []fsLink
}

// fsLink represents a symbolic link.
type fsLink struct {
path string
to string
}

// calculateFilesystemState returns a filesystemState based on the state of
// the filesystem on root.
func calculateFilesystemState(root string) (filesystemState, error) {
fs := filesystemState{
root: root,
}

err := filepath.Walk(fs.root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if path == fs.root {
return nil
}

relPath, err := filepath.Rel(fs.root, path)
if err != nil {
return err
}

if (info.Mode() & os.ModeSymlink) != 0 {
eval, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}

fs.links = append(fs.links, fsLink{
path: relPath,
to: eval,
})

return nil
}

if info.IsDir() {
fs.dirs = append(fs.dirs, relPath)

return nil
}

fs.files = append(fs.files, relPath)

return nil
})

if err != nil {
return filesystemState{}, err
}

return fs, nil
}
46 changes: 10 additions & 36 deletions internal/gps/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,10 @@ import (

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

// fspath represents a file system path in an OS-agnostic way.
type fsPath []string

func (f fsPath) String() string { return filepath.Join(f...) }

func (f fsPath) prepend(prefix string) fsPath {
p := fsPath{filepath.FromSlash(prefix)}
return append(p, f...)
}

type fsTestCase struct {
before, after filesystemState
}

// filesystemState represents the state of a file system. It has a setup method
// which inflates its state to the actual host file system, and an assert
// method which checks that the actual file system matches the described state.
type filesystemState struct {
root string
dirs []fsPath
files []fsPath
links []fsLink
}

// assert makes sure that the fs state matches the state of the actual host
// file system
func (fs filesystemState) assert(t *testing.T) {
Expand All @@ -44,13 +24,13 @@ func (fs filesystemState) assert(t *testing.T) {
linkMap := make(map[string]bool)

for _, d := range fs.dirs {
dirMap[d.prepend(fs.root).String()] = true
dirMap[filepath.Join(fs.root, d)] = true
}
for _, f := range fs.files {
fileMap[f.prepend(fs.root).String()] = true
fileMap[filepath.Join(fs.root, f)] = true
}
for _, l := range fs.links {
linkMap[l.path.prepend(fs.root).String()] = true
linkMap[filepath.Join(fs.root, l.path)] = true
}

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

// fsLink represents a symbolic link.
type fsLink struct {
path fsPath
to string
}

// setup inflates fs onto the actual host file system
func (fs filesystemState) setup(t *testing.T) {
fs.setupDirs(t)
Expand All @@ -121,17 +95,17 @@ func (fs filesystemState) setup(t *testing.T) {

func (fs filesystemState) setupDirs(t *testing.T) {
for _, dir := range fs.dirs {
p := dir.prepend(fs.root)
if err := os.MkdirAll(p.String(), 0777); err != nil {
p := filepath.Join(fs.root, dir)
if err := os.MkdirAll(p, 0777); err != nil {
t.Fatalf("os.MkdirAll(%q, 0777) err=%q", p, err)
}
}
}

func (fs filesystemState) setupFiles(t *testing.T) {
for _, file := range fs.files {
p := file.prepend(fs.root)
f, err := os.Create(p.String())
p := filepath.Join(fs.root, file)
f, err := os.Create(p)
if err != nil {
t.Fatalf("os.Create(%q) err=%q", p, err)
}
Expand All @@ -143,15 +117,15 @@ func (fs filesystemState) setupFiles(t *testing.T) {

func (fs filesystemState) setupLinks(t *testing.T) {
for _, link := range fs.links {
p := link.path.prepend(fs.root)
p := filepath.Join(fs.root, link.path)

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

if err := os.Symlink(to, p.String()); err != nil {
if err := os.Symlink(to, p); err != nil {
t.Fatalf("os.Symlink(%q, %q) err=%q", to, p, err)
}
}
Expand Down
Loading

0 comments on commit 4db54a5

Please sign in to comment.