From 211da5be231076ed4f3b4485a880cfe1dca2eafe Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 16 Mar 2022 13:57:42 -0500 Subject: [PATCH] Remove box count check when trying to recover boxes The get box + stat operations are not that slow. No need to also go through listing out all the boxes in the vagrant box dir. --- internal/core/box_collection.go | 53 ++++++---------------------- internal/core/box_collection_test.go | 2 +- 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/internal/core/box_collection.go b/internal/core/box_collection.go index ad8b24fd5..6a39f39ef 100644 --- a/internal/core/box_collection.go +++ b/internal/core/box_collection.go @@ -228,58 +228,25 @@ func (b *BoxCollection) RecoverBoxes() (err error) { if err != nil { return err } - dirBoxes, err := b.listBoxDir() - if err != nil { - return err - } - // If the number of boxes in the box directory and in the database match then - // assume that the box collection is in order (eg. all the boxes in the box - // directory and db match) - if len(dirBoxes) == len(resp.Boxes) { - return - } - - // If the number of boxes in the boxes dir is less than in the db then there are - // extra boxes in the db. The extra boxes in the db should be removed from the db - // since they are not actually available. - // If the number of boxes in the boxes dir is more than in the db, then that's ok. - if len(dirBoxes) < len(resp.Boxes) { - for _, boxRef := range resp.Boxes { - box, erro := b.basis.client.GetBox(b.basis.ctx, &vagrant_server.GetBoxRequest{Box: boxRef}) - // If the box directory does not exist, then the box doesn't exist. - if _, err := os.Stat(box.Box.Directory); err != nil { - // Remove the box - _, erro := b.basis.client.DeleteBox(b.basis.ctx, &vagrant_server.DeleteBoxRequest{Box: boxRef}) - if erro != nil { - return erro - } - } + // Ensure that each box exists + for _, boxRef := range resp.Boxes { + box, erro := b.basis.client.GetBox(b.basis.ctx, &vagrant_server.GetBoxRequest{Box: boxRef}) + // If the box directory does not exist, then the box doesn't exist. + if _, err := os.Stat(box.Box.Directory); err != nil { + // Remove the box + _, erro := b.basis.client.DeleteBox(b.basis.ctx, &vagrant_server.DeleteBoxRequest{Box: boxRef}) if erro != nil { return erro } } + if erro != nil { + return erro + } } return } -func (b *BoxCollection) listBoxDir() (boxPaths []string, err error) { - boxPaths = []string{} - err = filepath.Walk(b.directory, - func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - // Each box has a metadata.json file. If that file is found, then - // the path belonds to a box. - if filepath.Base(path) == "metadata.json" { - boxPaths = append(boxPaths, strings.ReplaceAll(path, "metadata.json", "")) - } - return nil - }) - return -} - func (b *BoxCollection) generateDirectoryName(path string) (out string) { out = strings.ReplaceAll(path, ":", VagrantColon) return strings.ReplaceAll(out, "/", VagrantSlash) diff --git a/internal/core/box_collection_test.go b/internal/core/box_collection_test.go index ef51e05b6..ede84c161 100644 --- a/internal/core/box_collection_test.go +++ b/internal/core/box_collection_test.go @@ -154,7 +154,7 @@ func TestFind(t *testing.T) { require.NotNil(t, boxes) } -func TestRemoveBox(t *testing.T) { +func TestRemoveMissingBox(t *testing.T) { // Create initial box collection bc := newBoxCollection(t) td, err := ioutil.TempDir("/tmp", "box")