From edbd6ad58498307b028cf40f8b87a672334f8a34 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Wed, 10 May 2017 19:48:22 +0200 Subject: [PATCH] Add fs.TempFile and fs.RemoveIfExists --- src/restic/checker/checker.go | 4 ++-- src/restic/fs/file.go | 9 +++++++++ src/restic/fs/file_unix.go | 20 +++++++++++++++++++- src/restic/fs/file_windows.go | 6 ++++++ src/restic/repository/packer_manager.go | 7 +++---- src/restic/repository/packer_manager_test.go | 3 ++- src/restic/repository/repack.go | 7 +++---- 7 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/restic/checker/checker.go b/src/restic/checker/checker.go index 1fd73888c..80dee3005 100644 --- a/src/restic/checker/checker.go +++ b/src/restic/checker/checker.go @@ -4,11 +4,11 @@ import ( "crypto/sha256" "fmt" "io" - "io/ioutil" "os" "sync" "restic/errors" + "restic/fs" "restic/hashing" "restic" @@ -668,7 +668,7 @@ func checkPack(r restic.Repository, id restic.ID) error { return err } - packfile, err := ioutil.TempFile("", "restic-temp-check-") + packfile, err := fs.TempFile("", "restic-temp-check-") if err != nil { return errors.Wrap(err, "TempFile") } diff --git a/src/restic/fs/file.go b/src/restic/fs/file.go index e23da2bcb..1d15a0300 100644 --- a/src/restic/fs/file.go +++ b/src/restic/fs/file.go @@ -116,3 +116,12 @@ func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { func Walk(root string, walkFn filepath.WalkFunc) error { 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 +} diff --git a/src/restic/fs/file_unix.go b/src/restic/fs/file_unix.go index 4c6da3964..4815fcc8b 100644 --- a/src/restic/fs/file_unix.go +++ b/src/restic/fs/file_unix.go @@ -2,7 +2,10 @@ package fs -import "os" +import ( + "io/ioutil" + "os" +) // fixpath returns an absolute path on windows, so restic can open long file // names. @@ -17,3 +20,18 @@ func fixpath(name string) string { func MkdirAll(path string, perm os.FileMode) error { 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 +} diff --git a/src/restic/fs/file_windows.go b/src/restic/fs/file_windows.go index 8f1df7967..f8cbd1964 100644 --- a/src/restic/fs/file_windows.go +++ b/src/restic/fs/file_windows.go @@ -1,6 +1,7 @@ package fs import ( + "io/ioutil" "os" "path/filepath" "strings" @@ -85,3 +86,8 @@ func MkdirAll(path string, perm os.FileMode) error { } return nil } + +// TempFile creates a temporary file. +func TempFile(dir, prefix string) (f *os.File, err error) { + return ioutil.TempFile(dir, prefix) +} diff --git a/src/restic/repository/packer_manager.go b/src/restic/repository/packer_manager.go index e3f49f389..07f051ba5 100644 --- a/src/restic/repository/packer_manager.go +++ b/src/restic/repository/packer_manager.go @@ -3,7 +3,6 @@ package repository import ( "crypto/sha256" "io" - "io/ioutil" "os" "restic" "sync" @@ -78,9 +77,9 @@ func (r *packerManager) findPacker(size uint) (packer *Packer, err error) { // no suitable packer found, return new 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 { - return nil, errors.Wrap(err, "ioutil.TempFile") + return nil, errors.Wrap(err, "fs.TempFile") } hw := hashing.NewWriter(tmpfile, sha256.New()) @@ -132,7 +131,7 @@ func (r *Repository) savePacker(p *Packer) error { return errors.Wrap(err, "close tempfile") } - err = fs.Remove(p.tmpfile.Name()) + err = fs.RemoveIfExists(p.tmpfile.Name()) if err != nil { return errors.Wrap(err, "Remove") } diff --git a/src/restic/repository/packer_manager_test.go b/src/restic/repository/packer_manager_test.go index 37718a5ea..2ca44de0b 100644 --- a/src/restic/repository/packer_manager_test.go +++ b/src/restic/repository/packer_manager_test.go @@ -7,6 +7,7 @@ import ( "restic" "restic/backend/mem" "restic/crypto" + "restic/fs" "restic/mock" "testing" ) @@ -59,7 +60,7 @@ func saveFile(t testing.TB, be Saver, f *os.File, id restic.ID) { t.Fatal(err) } - if err := os.Remove(f.Name()); err != nil { + if err := fs.RemoveIfExists(f.Name()); err != nil { t.Fatal(err) } } diff --git a/src/restic/repository/repack.go b/src/restic/repository/repack.go index 7cd1c5fed..ef11034cb 100644 --- a/src/restic/repository/repack.go +++ b/src/restic/repository/repack.go @@ -3,11 +3,10 @@ package repository import ( "crypto/sha256" "io" - "io/ioutil" - "os" "restic" "restic/crypto" "restic/debug" + "restic/fs" "restic/hashing" "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 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 { 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") } - if err = os.Remove(tempfile.Name()); err != nil { + if err = fs.RemoveIfExists(tempfile.Name()); err != nil { return errors.Wrap(err, "Remove") } if p != nil {