2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

Add fs.TempFile and fs.RemoveIfExists

This commit is contained in:
Alexander Neumann 2017-05-10 19:48:22 +02:00
parent faf11c4a46
commit edbd6ad584
7 changed files with 44 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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