Remove tempdir for packerManager

This commit is contained in:
Alexander Neumann 2016-03-06 13:14:06 +01:00
parent 015cea0c50
commit cda7616c82
9 changed files with 27 additions and 69 deletions

View file

@ -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 {

View file

@ -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: ")

View file

@ -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 {

View file

@ -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)

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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)

View file

@ -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)