From cda7616c826ce8607a530384fe70e95832e4be0d Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 6 Mar 2016 13:14:06 +0100 Subject: [PATCH] Remove tempdir for packerManager --- src/cmds/restic/cmd_init.go | 5 +-- src/cmds/restic/global.go | 5 +-- src/restic/archiver_duplication_test.go | 5 +-- src/restic/checker/checker_test.go | 6 ++-- src/restic/repository/packer_manager.go | 34 +++++++++----------- src/restic/repository/packer_manager_test.go | 19 ++--------- src/restic/repository/repository.go | 12 ++----- src/restic/test/backend.go | 5 +-- src/restic/test/helpers.go | 5 +-- 9 files changed, 27 insertions(+), 69 deletions(-) diff --git a/src/cmds/restic/cmd_init.go b/src/cmds/restic/cmd_init.go index 069977cb6..887d34166 100644 --- a/src/cmds/restic/cmd_init.go +++ b/src/cmds/restic/cmd_init.go @@ -26,10 +26,7 @@ func (cmd CmdInit) Execute(args []string) error { "enter password again: ") } - s, err := repository.New(be) - if err != nil { - return err - } + s := repository.New(be) err = s.Init(cmd.global.password) if err != nil { diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index 1de0bfe76..49a2ace30 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -208,10 +208,7 @@ func (o GlobalOptions) OpenRepository() (*repository.Repository, error) { return nil, err } - s, err := repository.New(be) - if err != nil { - return nil, err - } + s := repository.New(be) if o.password == "" { o.password = o.ReadPassword("enter password for repository: ") diff --git a/src/restic/archiver_duplication_test.go b/src/restic/archiver_duplication_test.go index 564cdca00..ffcbacee4 100644 --- a/src/restic/archiver_duplication_test.go +++ b/src/restic/archiver_duplication_test.go @@ -78,10 +78,7 @@ func testArchiverDuplication(t *testing.T) { t.Fatal(err) } - repo, err := repository.New(forgetfulBackend()) - if err != nil { - t.Fatal(err) - } + repo := repository.New(forgetfulBackend()) err = repo.Init("foo") if err != nil { diff --git a/src/restic/checker/checker_test.go b/src/restic/checker/checker_test.go index 5db7d945e..85135a98c 100644 --- a/src/restic/checker/checker_test.go +++ b/src/restic/checker/checker_test.go @@ -239,8 +239,7 @@ func induceError(data []byte) { func TestCheckerModifiedData(t *testing.T) { be := mem.New() - repo, err := repository.New(be) - OK(t, err) + repo := repository.New(be) OK(t, repo.Init(TestPassword)) arch := restic.NewArchiver(repo) @@ -249,8 +248,7 @@ func TestCheckerModifiedData(t *testing.T) { t.Logf("archived as %v", id.Str()) beError := &errorBackend{Backend: be} - checkRepo, err := repository.New(beError) - OK(t, err) + checkRepo := repository.New(beError) OK(t, checkRepo.SearchKey(TestPassword)) chkr := checker.New(checkRepo) diff --git a/src/restic/repository/packer_manager.go b/src/restic/repository/packer_manager.go index bb1c62d93..9622ebb9a 100644 --- a/src/restic/repository/packer_manager.go +++ b/src/restic/repository/packer_manager.go @@ -25,7 +25,7 @@ type packerManager struct { pm sync.Mutex packs []*pack.Packer - tempdir string + pool sync.Pool } const minPackSize = 4 * 1024 * 1024 @@ -34,23 +34,21 @@ const maxPackers = 200 // NewPackerManager returns an new packer manager which writes temporary files // to a temporary directory -func NewPackerManager(be Saver, key *crypto.Key) (pm *packerManager, err error) { - pm = &packerManager{ +func NewPackerManager(be Saver, key *crypto.Key) *packerManager { + return &packerManager{ be: be, key: key, + pool: sync.Pool{ + New: func() interface{} { + return make([]byte, (minPackSize+maxPackSize)/2) + }, + }, } - - pm.tempdir, err = ioutil.TempDir("", fmt.Sprintf("restic-packs-%d-", os.Getpid())) - if err != nil { - return nil, err - } - - return pm, nil } // findPacker returns a packer for a new blob of size bytes. Either a new one is // created or one is returned that already has some blobs. -func (r *packerManager) findPacker(size uint) (*pack.Packer, error) { +func (r *packerManager) findPacker(size uint) (packer *pack.Packer, err error) { r.pm.Lock() defer r.pm.Unlock() @@ -69,7 +67,7 @@ func (r *packerManager) findPacker(size uint) (*pack.Packer, error) { // no suitable packer found, return new debug.Log("Repo.findPacker", "create new pack for %d bytes", size) - tmpfile, err := ioutil.TempFile(r.tempdir, "restic-pack-") + tmpfile, err := ioutil.TempFile("", "restic-temp-pack-") if err != nil { return nil, err } @@ -122,6 +120,11 @@ func (r *Repository) savePacker(p *pack.Packer) error { debug.Log("Repo.savePacker", "saved as %v", h) + err = os.Remove(tmpfile.Name()) + if err != nil { + return err + } + // update blobs in the index for _, b := range p.Blobs() { debug.Log("Repo.savePacker", " updating blob %v to pack %v", b.ID.Str(), id.Str()) @@ -144,10 +147,3 @@ func (r *packerManager) countPacker() int { return len(r.packs) } - -// removeTempdir deletes the temporary directory. -func (r *packerManager) removeTempdir() error { - err := os.RemoveAll(r.tempdir) - r.tempdir = "" - return err -} diff --git a/src/restic/repository/packer_manager_test.go b/src/restic/repository/packer_manager_test.go index 79a7d9904..53129a7fe 100644 --- a/src/restic/repository/packer_manager_test.go +++ b/src/restic/repository/packer_manager_test.go @@ -120,10 +120,7 @@ func TestPackerManager(t *testing.T) { rnd := rand.New(rand.NewSource(23)) be := mem.New() - pm, err := NewPackerManager(be, crypto.NewRandomKey()) - if err != nil { - t.Fatal(err) - } + pm := NewPackerManager(be, crypto.NewRandomKey()) blobBuf := make([]byte, maxBlobSize) @@ -131,20 +128,13 @@ func TestPackerManager(t *testing.T) { bytes += flushRemainingPacks(t, rnd, be, pm) t.Logf("saved %d bytes", bytes) - err = pm.removeTempdir() - if err != nil { - t.Fatal(err) - } } func BenchmarkPackerManager(t *testing.B) { rnd := rand.New(rand.NewSource(23)) be := &fakeBackend{} - pm, err := NewPackerManager(be, crypto.NewRandomKey()) - if err != nil { - t.Fatal(err) - } + pm := NewPackerManager(be, crypto.NewRandomKey()) blobBuf := make([]byte, maxBlobSize) t.ResetTimer() @@ -156,9 +146,4 @@ func BenchmarkPackerManager(t *testing.B) { bytes += flushRemainingPacks(t, rnd, be, pm) t.Logf("saved %d bytes", bytes) - - err = pm.removeTempdir() - if err != nil { - t.Fatal(err) - } } diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index eb86902b6..5d28ee4a7 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -27,19 +27,14 @@ type Repository struct { } // New returns a new repository with backend be. -func New(be backend.Backend) (*Repository, error) { - pm, err := NewPackerManager(be, nil) - if err != nil { - return nil, err - } - +func New(be backend.Backend) *Repository { repo := &Repository{ be: be, idx: NewMasterIndex(), - packerManager: pm, + packerManager: NewPackerManager(be, nil), } - return repo, nil + return repo } // Find loads the list of all blobs of type t and searches for names which start @@ -304,7 +299,6 @@ func (r *Repository) Flush() error { } } r.packs = r.packs[:0] - return nil } diff --git a/src/restic/test/backend.go b/src/restic/test/backend.go index 4d9ca6a44..5516cecdf 100644 --- a/src/restic/test/backend.go +++ b/src/restic/test/backend.go @@ -61,10 +61,7 @@ func SetupRepo() *repository.Repository { panic(err) } - repo, err := repository.New(b) - if err != nil { - panic(err) - } + repo := repository.New(b) err = repo.Init(TestPassword) if err != nil { panic(err) diff --git a/src/restic/test/helpers.go b/src/restic/test/helpers.go index c43e15b70..4c3280fba 100644 --- a/src/restic/test/helpers.go +++ b/src/restic/test/helpers.go @@ -213,10 +213,7 @@ func OpenLocalRepo(t testing.TB, dir string) *repository.Repository { be, err := local.Open(dir) OK(t, err) - repo, err := repository.New(be) - if err != nil { - t.Fatal(err) - } + repo := repository.New(be) err = repo.SearchKey(TestPassword) OK(t, err)