aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-10-15 14:54:25 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-10-21 10:22:10 +0200
commitdd564a9d29871f791d7856edf3b38142b5b2c5e3 (patch)
tree0172ebab17fe35630e372f4d7305a2cbd61f0ba6 /pkg
parente2605999d9d1fa4e98bcf850ed007cfd98f27383 (diff)
pkg/vcs: add repo OptPrecious and OptDontSandbox options
The pkg/vcs code assumed that we fully manage the repo within an autonomous program. In particular it tried to repair any errors by dropping and re-creating the repo. This does not work well for command-line tools that work with a user-provided repo. Add OptPrecious for such uses. Update #2171
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bisect/bisect_test.go2
-rw-r--r--pkg/vcs/akaros.go6
-rw-r--r--pkg/vcs/freebsd.go14
-rw-r--r--pkg/vcs/fuchsia.go6
-rw-r--r--pkg/vcs/git.go56
-rw-r--r--pkg/vcs/git_repo_test.go2
-rw-r--r--pkg/vcs/git_test_util.go4
-rw-r--r--pkg/vcs/linux.go10
-rw-r--r--pkg/vcs/netbsd.go14
-rw-r--r--pkg/vcs/openbsd.go14
-rw-r--r--pkg/vcs/testos.go4
-rw-r--r--pkg/vcs/vcs.go30
12 files changed, 75 insertions, 87 deletions
diff --git a/pkg/bisect/bisect_test.go b/pkg/bisect/bisect_test.go
index ee730b724..3bbc2e2ea 100644
--- a/pkg/bisect/bisect_test.go
+++ b/pkg/bisect/bisect_test.go
@@ -106,7 +106,7 @@ func createTestRepo(t *testing.T) string {
}
func runBisection(t *testing.T, baseDir string, test BisectionTest) (*Result, error) {
- r, err := vcs.NewRepo("test", "64", baseDir)
+ r, err := vcs.NewRepo("test", "64", baseDir, vcs.OptPrecious)
if err != nil {
t.Fatal(err)
}
diff --git a/pkg/vcs/akaros.go b/pkg/vcs/akaros.go
index 0d3e701fb..33dfb7850 100644
--- a/pkg/vcs/akaros.go
+++ b/pkg/vcs/akaros.go
@@ -12,10 +12,10 @@ type akaros struct {
dropbear *git
}
-func newAkaros(vm, dir string) *akaros {
+func newAkaros(dir string, opts []RepoOpt) *akaros {
return &akaros{
- git: newGit(dir, nil),
- dropbear: newGit(filepath.Join(dir, "dropbear"), nil),
+ git: newGit(dir, nil, opts),
+ dropbear: newGit(filepath.Join(dir, "dropbear"), nil, opts),
}
}
diff --git a/pkg/vcs/freebsd.go b/pkg/vcs/freebsd.go
deleted file mode 100644
index 222b15b87..000000000
--- a/pkg/vcs/freebsd.go
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2019 syzkaller project authors. All rights reserved.
-// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
-
-package vcs
-
-type freebsd struct {
- *git
-}
-
-func newFreeBSD(vm, dir string) *freebsd {
- return &freebsd{
- git: newGit(dir, nil),
- }
-}
diff --git a/pkg/vcs/fuchsia.go b/pkg/vcs/fuchsia.go
index 97e5f8b47..a604ec51b 100644
--- a/pkg/vcs/fuchsia.go
+++ b/pkg/vcs/fuchsia.go
@@ -12,16 +12,14 @@ import (
)
type fuchsia struct {
- vm string
dir string
repo *git
}
-func newFuchsia(vm, dir string) *fuchsia {
+func newFuchsia(dir string, opts []RepoOpt) *fuchsia {
return &fuchsia{
- vm: vm,
dir: dir,
- repo: newGit(dir, nil),
+ repo: newGit(dir, nil, opts),
}
}
diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go
index f487b60c2..aeb9d734d 100644
--- a/pkg/vcs/git.go
+++ b/pkg/vcs/git.go
@@ -23,16 +23,26 @@ import (
type git struct {
dir string
- sandbox bool
ignoreCC map[string]bool
+ precious bool
+ sandbox bool
}
-func newGit(dir string, ignoreCC map[string]bool) *git {
- return &git{
+func newGit(dir string, ignoreCC map[string]bool, opts []RepoOpt) *git {
+ git := &git{
dir: dir,
- sandbox: true,
ignoreCC: ignoreCC,
+ sandbox: true,
}
+ for _, opt := range opts {
+ switch opt {
+ case OptPrecious:
+ git.precious = true
+ case OptDontSandbox:
+ git.sandbox = false
+ }
+ }
+ return git
}
func filterEnv() []string {
@@ -86,11 +96,8 @@ func (git *git) Poll(repo, branch string) (*Commit, error) {
}
func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) {
- git.reset()
- if _, err := git.git("reset", "--hard"); err != nil {
- if err := git.initRepo(err); err != nil {
- return nil, err
- }
+ if err := git.repair(); err != nil {
+ return nil, err
}
_, err := git.git("fetch", repo, branch)
if err != nil {
@@ -103,11 +110,8 @@ func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) {
}
func (git *git) CheckoutCommit(repo, commit string) (*Commit, error) {
- git.reset()
- if _, err := git.git("reset", "--hard"); err != nil {
- if err := git.initRepo(err); err != nil {
- return nil, err
- }
+ if err := git.repair(); err != nil {
+ return nil, err
}
if err := git.fetchRemote(repo); err != nil {
return nil, err
@@ -124,8 +128,10 @@ func (git *git) fetchRemote(repo string) error {
}
func (git *git) SwitchCommit(commit string) (*Commit, error) {
- git.git("reset", "--hard")
- git.git("clean", "-fdx")
+ if !git.precious {
+ git.git("reset", "--hard")
+ git.git("clean", "-fdx")
+ }
if _, err := git.git("checkout", commit); err != nil {
return nil, err
}
@@ -133,6 +139,9 @@ func (git *git) SwitchCommit(commit string) (*Commit, error) {
}
func (git *git) clone(repo, branch string) error {
+ if git.precious {
+ return fmt.Errorf("won't reinit precious repo")
+ }
if err := git.initRepo(nil); err != nil {
return err
}
@@ -145,12 +154,23 @@ func (git *git) clone(repo, branch string) error {
return nil
}
-func (git *git) reset() {
+func (git *git) reset() error {
// This function tries to reset git repo state to a known clean state.
+ if git.precious {
+ return nil
+ }
git.git("reset", "--hard")
git.git("clean", "-fdx")
git.git("bisect", "reset")
- git.git("reset", "--hard")
+ _, err := git.git("reset", "--hard")
+ return err
+}
+
+func (git *git) repair() error {
+ if err := git.reset(); err != nil {
+ return git.initRepo(err)
+ }
+ return nil
}
func (git *git) initRepo(reason error) error {
diff --git a/pkg/vcs/git_repo_test.go b/pkg/vcs/git_repo_test.go
index 4deb1a381..a2610315f 100644
--- a/pkg/vcs/git_repo_test.go
+++ b/pkg/vcs/git_repo_test.go
@@ -28,7 +28,7 @@ func TestGitRepo(t *testing.T) {
defer os.RemoveAll(baseDir)
repo1 := CreateTestRepo(t, baseDir, "repo1")
repo2 := CreateTestRepo(t, baseDir, "repo2")
- repo := newGit(filepath.Join(baseDir, "repo"), nil)
+ repo := newGit(filepath.Join(baseDir, "repo"), nil, nil)
{
com, err := repo.Poll(repo1.Dir, "master")
if err != nil {
diff --git a/pkg/vcs/git_test_util.go b/pkg/vcs/git_test_util.go
index 5ef6c50c3..a1dbf0dd9 100644
--- a/pkg/vcs/git_test_util.go
+++ b/pkg/vcs/git_test_util.go
@@ -49,7 +49,7 @@ func MakeTestRepo(t *testing.T, dir string) *TestRepo {
Dir: dir,
name: filepath.Base(dir),
Commits: make(map[string]map[string]*Commit),
- repo: newGit(dir, ignoreCC),
+ repo: newGit(dir, ignoreCC, []RepoOpt{OptPrecious, OptDontSandbox}),
}
repo.Git("init")
repo.Git("config", "--add", "user.email", userEmail)
@@ -123,7 +123,7 @@ func CloneTestRepo(t *testing.T, baseDir, name string, originRepo *TestRepo) *Te
Dir: dir,
name: filepath.Base(dir),
Commits: make(map[string]map[string]*Commit),
- repo: newGit(dir, ignoreCC),
+ repo: newGit(dir, ignoreCC, []RepoOpt{OptPrecious, OptDontSandbox}),
}
repo.Git("clone", originRepo.Dir, repo.Dir)
return repo
diff --git a/pkg/vcs/linux.go b/pkg/vcs/linux.go
index c4e2a90a5..2d20af53c 100644
--- a/pkg/vcs/linux.go
+++ b/pkg/vcs/linux.go
@@ -26,15 +26,17 @@ type linux struct {
*git
}
-var _ Bisecter = new(linux)
-var _ ConfigMinimizer = new(linux)
+var (
+ _ Bisecter = new(linux)
+ _ ConfigMinimizer = new(linux)
+)
-func newLinux(dir string) *linux {
+func newLinux(dir string, opts []RepoOpt) *linux {
ignoreCC := map[string]bool{
"stable@vger.kernel.org": true,
}
return &linux{
- git: newGit(dir, ignoreCC),
+ git: newGit(dir, ignoreCC, opts),
}
}
diff --git a/pkg/vcs/netbsd.go b/pkg/vcs/netbsd.go
deleted file mode 100644
index 6b710f4ca..000000000
--- a/pkg/vcs/netbsd.go
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2018 syzkaller project authors. All rights reserved.
-// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
-
-package vcs
-
-type netbsd struct {
- *git
-}
-
-func newNetBSD(vm, dir string) *netbsd {
- return &netbsd{
- git: newGit(dir, nil),
- }
-}
diff --git a/pkg/vcs/openbsd.go b/pkg/vcs/openbsd.go
deleted file mode 100644
index f97290f99..000000000
--- a/pkg/vcs/openbsd.go
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright 2018 syzkaller project authors. All rights reserved.
-// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
-
-package vcs
-
-type openbsd struct {
- *git
-}
-
-func newOpenBSD(vm, dir string) *openbsd {
- return &openbsd{
- git: newGit(dir, nil),
- }
-}
diff --git a/pkg/vcs/testos.go b/pkg/vcs/testos.go
index ad6df6b1e..20a63973a 100644
--- a/pkg/vcs/testos.go
+++ b/pkg/vcs/testos.go
@@ -14,9 +14,9 @@ type testos struct {
var _ ConfigMinimizer = new(testos)
-func newTestos(dir string) *testos {
+func newTestos(dir string, opts []RepoOpt) *testos {
return &testos{
- git: newGit(dir, nil),
+ git: newGit(dir, nil, opts),
}
}
diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go
index 1f12fa85a..4185bf165 100644
--- a/pkg/vcs/vcs.go
+++ b/pkg/vcs/vcs.go
@@ -155,29 +155,39 @@ type BisectEnv struct {
KernelConfig []byte
}
-func NewRepo(os, vm, dir string) (Repo, error) {
+type RepoOpt int
+
+const (
+ // RepoPrecious is intended for command-line tools that work with a user-provided repo.
+ // Such repo won't be re-created to recover from errors, but rather return errors.
+ // If this option is not specified, the repo can be re-created from scratch to recover from any errors.
+ OptPrecious RepoOpt = iota
+ // Don't use sandboxing suitable for pkg/build.
+ OptDontSandbox
+)
+
+func NewRepo(os, vm, dir string, opts ...RepoOpt) (Repo, error) {
switch os {
case "linux":
- return newLinux(dir), nil
+ return newLinux(dir, opts), nil
case "akaros":
- return newAkaros(vm, dir), nil
+ return newAkaros(dir, opts), nil
case "fuchsia":
- return newFuchsia(vm, dir), nil
+ return newFuchsia(dir, opts), nil
case "openbsd":
- return newOpenBSD(vm, dir), nil
+ return newGit(dir, nil, opts), nil
case "netbsd":
- return newNetBSD(vm, dir), nil
+ return newGit(dir, nil, opts), nil
case "freebsd":
- return newFreeBSD(vm, dir), nil
+ return newGit(dir, nil, opts), nil
case "test":
- return newTestos(dir), nil
+ return newTestos(dir, opts), nil
}
return nil, fmt.Errorf("vcs is unsupported for %v", os)
}
func NewSyzkallerRepo(dir string) Repo {
- git := newGit(dir, nil)
- git.sandbox = false
+ git := newGit(dir, nil, []RepoOpt{OptDontSandbox})
return git
}