mirror of
https://github.com/octoleo/restic.git
synced 2024-11-17 02:25:12 +00:00
Merge pull request #952 from restic/remove-temp-files
Unlink tempfiles before using them
This commit is contained in:
commit
fbf5a8123b
@ -4,11 +4,11 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"restic/errors"
|
"restic/errors"
|
||||||
|
"restic/fs"
|
||||||
"restic/hashing"
|
"restic/hashing"
|
||||||
|
|
||||||
"restic"
|
"restic"
|
||||||
@ -668,7 +668,7 @@ func checkPack(r restic.Repository, id restic.ID) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
packfile, err := ioutil.TempFile("", "restic-temp-check-")
|
packfile, err := fs.TempFile("", "restic-temp-check-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "TempFile")
|
return errors.Wrap(err, "TempFile")
|
||||||
}
|
}
|
||||||
|
@ -116,3 +116,12 @@ func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
|
|||||||
func Walk(root string, walkFn filepath.WalkFunc) error {
|
func Walk(root string, walkFn filepath.WalkFunc) error {
|
||||||
return filepath.Walk(fixpath(root), walkFn)
|
return filepath.Walk(fixpath(root), walkFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveIfExists removes a file, returning no error if it does not exist.
|
||||||
|
func RemoveIfExists(filename string) error {
|
||||||
|
err := os.Remove(filename)
|
||||||
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
package fs
|
package fs
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
// fixpath returns an absolute path on windows, so restic can open long file
|
// fixpath returns an absolute path on windows, so restic can open long file
|
||||||
// names.
|
// names.
|
||||||
@ -17,3 +20,18 @@ func fixpath(name string) string {
|
|||||||
func MkdirAll(path string, perm os.FileMode) error {
|
func MkdirAll(path string, perm os.FileMode) error {
|
||||||
return os.MkdirAll(fixpath(path), perm)
|
return os.MkdirAll(fixpath(path), perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TempFile creates a temporary file which has already been deleted (on
|
||||||
|
// supported platforms)
|
||||||
|
func TempFile(dir, prefix string) (f *os.File, err error) {
|
||||||
|
f, err = ioutil.TempFile(dir, prefix)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.Remove(f.Name()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -85,3 +86,8 @@ func MkdirAll(path string, perm os.FileMode) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TempFile creates a temporary file.
|
||||||
|
func TempFile(dir, prefix string) (f *os.File, err error) {
|
||||||
|
return ioutil.TempFile(dir, prefix)
|
||||||
|
}
|
||||||
|
@ -3,7 +3,6 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"restic"
|
"restic"
|
||||||
"sync"
|
"sync"
|
||||||
@ -78,9 +77,9 @@ func (r *packerManager) findPacker(size uint) (packer *Packer, err error) {
|
|||||||
|
|
||||||
// no suitable packer found, return new
|
// no suitable packer found, return new
|
||||||
debug.Log("create new pack for %d bytes", size)
|
debug.Log("create new pack for %d bytes", size)
|
||||||
tmpfile, err := ioutil.TempFile("", "restic-temp-pack-")
|
tmpfile, err := fs.TempFile("", "restic-temp-pack-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "ioutil.TempFile")
|
return nil, errors.Wrap(err, "fs.TempFile")
|
||||||
}
|
}
|
||||||
|
|
||||||
hw := hashing.NewWriter(tmpfile, sha256.New())
|
hw := hashing.NewWriter(tmpfile, sha256.New())
|
||||||
@ -132,7 +131,7 @@ func (r *Repository) savePacker(p *Packer) error {
|
|||||||
return errors.Wrap(err, "close tempfile")
|
return errors.Wrap(err, "close tempfile")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = fs.Remove(p.tmpfile.Name())
|
err = fs.RemoveIfExists(p.tmpfile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Remove")
|
return errors.Wrap(err, "Remove")
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"restic"
|
"restic"
|
||||||
"restic/backend/mem"
|
"restic/backend/mem"
|
||||||
"restic/crypto"
|
"restic/crypto"
|
||||||
|
"restic/fs"
|
||||||
"restic/mock"
|
"restic/mock"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -59,7 +60,7 @@ func saveFile(t testing.TB, be Saver, f *os.File, id restic.ID) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Remove(f.Name()); err != nil {
|
if err := fs.RemoveIfExists(f.Name()); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,10 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"restic"
|
"restic"
|
||||||
"restic/crypto"
|
"restic/crypto"
|
||||||
"restic/debug"
|
"restic/debug"
|
||||||
|
"restic/fs"
|
||||||
"restic/hashing"
|
"restic/hashing"
|
||||||
"restic/pack"
|
"restic/pack"
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ func Repack(repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet
|
|||||||
// load the complete pack into a temp file
|
// load the complete pack into a temp file
|
||||||
h := restic.Handle{Type: restic.DataFile, Name: packID.String()}
|
h := restic.Handle{Type: restic.DataFile, Name: packID.String()}
|
||||||
|
|
||||||
tempfile, err := ioutil.TempFile("", "restic-temp-repack-")
|
tempfile, err := fs.TempFile("", "restic-temp-repack-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "TempFile")
|
return errors.Wrap(err, "TempFile")
|
||||||
}
|
}
|
||||||
@ -115,7 +114,7 @@ func Repack(repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet
|
|||||||
return errors.Wrap(err, "Close")
|
return errors.Wrap(err, "Close")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove(tempfile.Name()); err != nil {
|
if err = fs.RemoveIfExists(tempfile.Name()); err != nil {
|
||||||
return errors.Wrap(err, "Remove")
|
return errors.Wrap(err, "Remove")
|
||||||
}
|
}
|
||||||
if p != nil {
|
if p != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user