2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00
* Patch for  https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).

as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.

I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."

* fixed building tests

* Restructured patches
Add Wrapper for filepath.Walk

* using \\?\ requires absolute pathes to be used.
Now all tests run

* used gofmt on the code

* Restructured Code. No patches dir, integrate the file functions into restic/fs/

There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string

* Changed the last os.Open() calls to fs.Open() after extending the File interface

* fixed name-clash of restic.fs and fuse.fs detected by travis

* fixed fmt with gofmt

* c&p failure: removed fixpath() call.

* missing include

* fixed includes in linux variant

* Fix for Linux. Fd() is required on File interface

* done gofmt
This commit is contained in:
Jan Stürtz 2016-08-15 21:59:13 +02:00 committed by Alexander Neumann
parent 4b8b625b90
commit b108966b12
17 changed files with 280 additions and 52 deletions

View File

@ -12,6 +12,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"restic/fs"
) )
// Context contains repository meta-data. // Context contains repository meta-data.
@ -134,7 +136,7 @@ func GetBlob(c *Context) http.HandlerFunc {
dir := vars[1] dir := vars[1]
name := vars[2] name := vars[2]
path := filepath.Join(c.path, dir, name) path := filepath.Join(c.path, dir, name)
file, err := os.Open(path) file, err := fs.Open(path)
if err != nil { if err != nil {
http.Error(w, "404 not found", 404) http.Error(w, "404 not found", 404)
return return
@ -152,7 +154,7 @@ func SaveBlob(c *Context) http.HandlerFunc {
name := vars[2] name := vars[2]
path := filepath.Join(c.path, dir, name) path := filepath.Join(c.path, dir, name)
tmp := path + "_tmp" tmp := path + "_tmp"
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600) tf, err := fs.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil { if err != nil {
http.Error(w, "500 internal server error", 500) http.Error(w, "500 internal server error", 500)
return return

View File

@ -32,7 +32,8 @@ import (
"encoding/csv" "encoding/csv"
"io" "io"
"log" "log"
"os"
"restic/fs"
) )
// lookup passwords in a htpasswd file // lookup passwords in a htpasswd file
@ -47,7 +48,7 @@ type HtpasswdFile struct {
// file and returns them. If an error is encountered, it is returned, together // file and returns them. If an error is encountered, it is returned, together
// with a nil-Pointer for the HtpasswdFile. // with a nil-Pointer for the HtpasswdFile.
func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) { func NewHtpasswdFromFile(path string) (*HtpasswdFile, error) {
r, err := os.Open(path) r, err := fs.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -10,6 +10,7 @@ import (
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/filter" "restic/filter"
"restic/fs"
"strings" "strings"
"time" "time"
@ -221,7 +222,7 @@ func (cmd CmdBackup) newArchiveStdinProgress() *restic.Progress {
// items exist at all. // items exist at all.
func filterExisting(items []string) (result []string, err error) { func filterExisting(items []string) (result []string, err error) {
for _, item := range items { for _, item := range items {
_, err := os.Lstat(item) _, err := fs.Lstat(item)
if err != nil && os.IsNotExist(err) { if err != nil && os.IsNotExist(err) {
continue continue
} }
@ -334,7 +335,7 @@ func (cmd CmdBackup) Execute(args []string) error {
// add patterns from file // add patterns from file
if cmd.ExcludeFile != "" { if cmd.ExcludeFile != "" {
file, err := os.Open(cmd.ExcludeFile) file, err := fs.Open(cmd.ExcludeFile)
if err != nil { if err != nil {
cmd.global.Warnf("error reading exclude patterns: %v", err) cmd.global.Warnf("error reading exclude patterns: %v", err)
return nil return nil

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"os" "os"
resticfs "restic/fs"
"restic/fuse" "restic/fuse"
systemFuse "bazil.org/fuse" systemFuse "bazil.org/fuse"
@ -55,9 +56,9 @@ func (cmd CmdMount) Execute(args []string) error {
} }
mountpoint := args[0] mountpoint := args[0]
if _, err := os.Stat(mountpoint); os.IsNotExist(err) { if _, err := resticfs.Stat(mountpoint); os.IsNotExist(err) {
cmd.global.Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint) cmd.global.Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint)
err = os.Mkdir(mountpoint, os.ModeDir|0700) err = resticfs.Mkdir(mountpoint, os.ModeDir|0700)
if err != nil { if err != nil {
return err return err
} }

View File

@ -780,7 +780,7 @@ func Scan(dirs []string, filter pipe.SelectFunc, p *Progress) (Stat, error) {
for _, dir := range dirs { for _, dir := range dirs {
debug.Log("Scan", "Start for %v", dir) debug.Log("Scan", "Start for %v", dir)
err := filepath.Walk(dir, func(str string, fi os.FileInfo, err error) error { err := fs.Walk(dir, func(str string, fi os.FileInfo, err error) error {
// TODO: integrate error reporting // TODO: integrate error reporting
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error for %v: %v\n", str, err) fmt.Fprintf(os.Stderr, "error for %v: %v\n", str, err)

View File

@ -34,7 +34,7 @@ func paths(dir string) []string {
func Open(dir string) (*Local, error) { func Open(dir string) (*Local, error) {
// test if all necessary dirs are there // test if all necessary dirs are there
for _, d := range paths(dir) { for _, d := range paths(dir) {
if _, err := os.Stat(d); err != nil { if _, err := fs.Stat(d); err != nil {
return nil, fmt.Errorf("%s does not exist", d) return nil, fmt.Errorf("%s does not exist", d)
} }
} }
@ -46,14 +46,14 @@ func Open(dir string) (*Local, error) {
// backend at dir. Afterwards a new config blob should be created. // backend at dir. Afterwards a new config blob should be created.
func Create(dir string) (*Local, error) { func Create(dir string) (*Local, error) {
// test if config file already exists // test if config file already exists
_, err := os.Lstat(filepath.Join(dir, backend.Paths.Config)) _, err := fs.Lstat(filepath.Join(dir, backend.Paths.Config))
if err == nil { if err == nil {
return nil, errors.New("config file already exists") return nil, errors.New("config file already exists")
} }
// create paths for data, refs and temp // create paths for data, refs and temp
for _, d := range paths(dir) { for _, d := range paths(dir) {
err := os.MkdirAll(d, backend.Modes.Dir) err := fs.MkdirAll(d, backend.Modes.Dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -105,7 +105,7 @@ func (b *Local) Load(h backend.Handle, p []byte, off int64) (n int, err error) {
return 0, err return 0, err
} }
f, err := os.Open(filename(b.p, h.Type, h.Name)) f, err := fs.Open(filename(b.p, h.Type, h.Name))
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -172,19 +172,19 @@ func (b *Local) Save(h backend.Handle, p []byte) (err error) {
filename := filename(b.p, h.Type, h.Name) filename := filename(b.p, h.Type, h.Name)
// test if new path already exists // test if new path already exists
if _, err := os.Stat(filename); err == nil { if _, err := fs.Stat(filename); err == nil {
return fmt.Errorf("Rename(): file %v already exists", filename) return fmt.Errorf("Rename(): file %v already exists", filename)
} }
// create directories if necessary, ignore errors // create directories if necessary, ignore errors
if h.Type == backend.Data { if h.Type == backend.Data {
err = os.MkdirAll(filepath.Dir(filename), backend.Modes.Dir) err = fs.MkdirAll(filepath.Dir(filename), backend.Modes.Dir)
if err != nil { if err != nil {
return err return err
} }
} }
err = os.Rename(tmpfile, filename) err = fs.Rename(tmpfile, filename)
debug.Log("local.Save", "save %v: rename %v -> %v: %v", debug.Log("local.Save", "save %v: rename %v -> %v: %v",
h, filepath.Base(tmpfile), filepath.Base(filename), err) h, filepath.Base(tmpfile), filepath.Base(filename), err)
@ -193,7 +193,7 @@ func (b *Local) Save(h backend.Handle, p []byte) (err error) {
} }
// set mode to read-only // set mode to read-only
fi, err := os.Stat(filename) fi, err := fs.Stat(filename)
if err != nil { if err != nil {
return err return err
} }
@ -207,7 +207,7 @@ func (b *Local) Stat(h backend.Handle) (backend.BlobInfo, error) {
return backend.BlobInfo{}, err return backend.BlobInfo{}, err
} }
fi, err := os.Stat(filename(b.p, h.Type, h.Name)) fi, err := fs.Stat(filename(b.p, h.Type, h.Name))
if err != nil { if err != nil {
return backend.BlobInfo{}, err return backend.BlobInfo{}, err
} }
@ -217,7 +217,7 @@ func (b *Local) Stat(h backend.Handle) (backend.BlobInfo, error) {
// Test returns true if a blob of the given type and name exists in the backend. // Test returns true if a blob of the given type and name exists in the backend.
func (b *Local) Test(t backend.Type, name string) (bool, error) { func (b *Local) Test(t backend.Type, name string) (bool, error) {
_, err := os.Stat(filename(b.p, t, name)) _, err := fs.Stat(filename(b.p, t, name))
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false, nil return false, nil
@ -233,12 +233,12 @@ func (b *Local) Remove(t backend.Type, name string) error {
fn := filename(b.p, t, name) fn := filename(b.p, t, name)
// reset read-only flag // reset read-only flag
err := os.Chmod(fn, 0666) err := fs.Chmod(fn, 0666)
if err != nil { if err != nil {
return err return err
} }
return os.Remove(fn) return fs.Remove(fn)
} }
func isFile(fi os.FileInfo) bool { func isFile(fi os.FileInfo) bool {
@ -246,7 +246,7 @@ func isFile(fi os.FileInfo) bool {
} }
func readdir(d string) (fileInfos []os.FileInfo, err error) { func readdir(d string) (fileInfos []os.FileInfo, err error) {
f, e := os.Open(d) f, e := fs.Open(d)
if e != nil { if e != nil {
return nil, e return nil, e
} }
@ -336,7 +336,7 @@ func (b *Local) List(t backend.Type, done <-chan struct{}) <-chan string {
// Delete removes the repository and all files. // Delete removes the repository and all files.
func (b *Local) Delete() error { func (b *Local) Delete() error {
return os.RemoveAll(b.p) return fs.RemoveAll(b.p)
} }
// Close closes all open files. // Close closes all open files.

View File

@ -4,9 +4,10 @@ package local
import ( import (
"os" "os"
"restic/fs"
) )
// set file to readonly // set file to readonly
func setNewFileMode(f string, fi os.FileInfo) error { func setNewFileMode(f string, fi os.FileInfo) error {
return os.Chmod(f, fi.Mode()&os.FileMode(^uint32(0222))) return fs.Chmod(f, fi.Mode()&os.FileMode(^uint32(0222)))
} }

View File

@ -11,6 +11,7 @@ import (
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/fs"
"restic/repository" "restic/repository"
) )
@ -43,7 +44,7 @@ func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error)
if err != nil { if err != nil {
return false, err return false, err
} }
fd, err := os.Open(filename) fd, err := fs.Open(filename)
defer fd.Close() defer fd.Close()
if err != nil { if err != nil {
@ -70,12 +71,12 @@ func (c *Cache) Store(t backend.Type, subtype string, id backend.ID) (io.WriteCl
} }
dirname := filepath.Dir(filename) dirname := filepath.Dir(filename)
err = os.MkdirAll(dirname, 0700) err = fs.MkdirAll(dirname, 0700)
if err != nil { if err != nil {
return nil, err return nil, err
} }
file, err := os.Create(filename) file, err := fs.Create(filename)
if err != nil { if err != nil {
debug.Log("Cache.Store", "error creating file %v: %v", filename, err) debug.Log("Cache.Store", "error creating file %v: %v", filename, err)
return nil, err return nil, err
@ -93,7 +94,7 @@ func (c *Cache) Load(t backend.Type, subtype string, id backend.ID) (io.ReadClos
return nil, err return nil, err
} }
return os.Open(filename) return fs.Open(filename)
} }
func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error { func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error {
@ -102,7 +103,7 @@ func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error {
return err return err
} }
err = os.Remove(filename) err = fs.Remove(filename)
debug.Log("Cache.purge", "Remove file %v: %v", filename, err) debug.Log("Cache.purge", "Remove file %v: %v", filename, err)
if err != nil && os.IsNotExist(err) { if err != nil && os.IsNotExist(err) {
@ -157,7 +158,7 @@ func (c *Cache) list(t backend.Type) ([]cacheEntry, error) {
return nil, fmt.Errorf("cache not supported for type %v", t) return nil, fmt.Errorf("cache not supported for type %v", t)
} }
fd, err := os.Open(dir) fd, err := fs.Open(dir)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return []cacheEntry{}, nil return []cacheEntry{}, nil
@ -228,10 +229,10 @@ func getWindowsCacheDir() (string, error) {
cachedir = os.TempDir() cachedir = os.TempDir()
} }
cachedir = filepath.Join(cachedir, "restic") cachedir = filepath.Join(cachedir, "restic")
fi, err := os.Stat(cachedir) fi, err := fs.Stat(cachedir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = os.MkdirAll(cachedir, 0700) err = fs.MkdirAll(cachedir, 0700)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -266,14 +267,14 @@ func getXDGCacheDir() (string, error) {
cachedir = filepath.Join(home, ".cache", "restic") cachedir = filepath.Join(home, ".cache", "restic")
} }
fi, err := os.Stat(cachedir) fi, err := fs.Stat(cachedir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = os.MkdirAll(cachedir, 0700) err = fs.MkdirAll(cachedir, 0700)
if err != nil { if err != nil {
return "", err return "", err
} }
fi, err = os.Stat(cachedir) fi, err = fs.Stat(cachedir)
debug.Log("getCacheDir", "create cache dir %v", cachedir) debug.Log("getCacheDir", "create cache dir %v", cachedir)
} }

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"restic/fs"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -48,7 +49,7 @@ func initDebugLogger() {
fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile) fmt.Fprintf(os.Stderr, "debug log file %v\n", debugfile)
f, err := os.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600) f, err := fs.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0600)
if err == nil { if err == nil {
_, err = f.Seek(2, 0) _, err = f.Seek(2, 0)
@ -59,7 +60,7 @@ func initDebugLogger() {
} }
if err != nil && os.IsNotExist(err) { if err != nil && os.IsNotExist(err) {
f, err = os.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600) f, err = fs.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600)
} }
if err != nil { if err != nil {

View File

@ -11,5 +11,9 @@ type File interface {
io.Writer io.Writer
io.Closer io.Closer
Fd() uintptr
Readdirnames(n int) ([]string, error)
Readdir(int) ([]os.FileInfo, error)
Seek(int64, int) (int64, error)
Stat() (os.FileInfo, error) Stat() (os.FileInfo, error)
} }

View File

@ -2,14 +2,126 @@
package fs package fs
import "os" import (
"os"
"path/filepath"
"runtime"
)
func fixpath(name string) string {
if runtime.GOOS == "windows" {
abspath, err := filepath.Abs(name)
if err == nil {
return "\\\\?\\" + abspath
}
}
return name
}
// Open opens a file for reading. // Open opens a file for reading.
func Open(name string) (File, error) { func Open(name string) (File, error) {
return os.OpenFile(name, os.O_RDONLY, 0) return os.OpenFile(fixpath(name), os.O_RDONLY, 0)
} }
// ClearCache syncs and then removes the file's content from the OS cache. // ClearCache syncs and then removes the file's content from the OS cache.
func ClearCache(f File) error { func ClearCache(f File) error {
return nil return nil
} }
// Chmod changes the mode of the named file to mode.
func Chmod(name string, mode os.FileMode) error {
return os.Chmod(fixpath(name), mode)
}
// Mkdir creates a new directory with the specified name and permission bits.
// If there is an error, it will be of type *PathError.
func Mkdir(name string, perm os.FileMode) error {
return os.Mkdir(fixpath(name), perm)
}
// MkdirAll creates a directory named path,
// along with any necessary parents, and returns nil,
// or else returns an error.
// The permission bits perm are used for all
// directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing
// and returns nil.
func MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(fixpath(path), perm)
}
// Readlink returns the destination of the named symbolic link.
// If there is an error, it will be of type *PathError.
func Readlink(name string) (string, error) {
return os.Readlink(fixpath(name))
}
// Remove removes the named file or directory.
// If there is an error, it will be of type *PathError.
func Remove(name string) error {
return os.Remove(fixpath(name))
}
// RemoveAll removes path and any children it contains.
// It removes everything it can but returns the first error
// it encounters. If the path does not exist, RemoveAll
// returns nil (no error).
func RemoveAll(path string) error {
return os.RemoveAll(fixpath(path))
}
// Rename renames (moves) oldpath to newpath.
// If newpath already exists, Rename replaces it.
// OS-specific restrictions may apply when oldpath and newpath are in different directories.
// If there is an error, it will be of type *LinkError.
func Rename(oldpath, newpath string) error {
return os.Rename(fixpath(oldpath), fixpath(newpath))
}
// Symlink creates newname as a symbolic link to oldname.
// If there is an error, it will be of type *LinkError.
func Symlink(oldname, newname string) error {
return os.Symlink(fixpath(oldname), fixpath(newname))
}
// Stat returns a FileInfo structure describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (os.FileInfo, error) {
return os.Stat(fixpath(name))
}
// Lstat returns the FileInfo structure describing the named file.
// If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (os.FileInfo, error) {
return os.Lstat(fixpath(name))
}
// Create creates the named file with mode 0666 (before umask), truncating
// it if it already exists. If successful, methods on the returned
// File can be used for I/O; the associated file descriptor has mode
// O_RDWR.
// If there is an error, it will be of type *PathError.
func Create(name string) (*os.File, error) {
return os.Create(fixpath(name))
}
// OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
return os.OpenFile(fixpath(name), flag, perm)
}
// Walk walks the file tree rooted at root, calling walkFn for each file or
// directory in the tree, including root. All errors that arise visiting files
// and directories are filtered by walkFn. The files are walked in lexical
// order, which makes the output deterministic but means that for very
// large directories Walk can be inefficient.
// Walk does not follow symbolic links.
func Walk(root string, walkFn filepath.WalkFunc) error {
return filepath.Walk(fixpath(root), walkFn)
}

View File

@ -4,6 +4,7 @@ package fs
import ( import (
"os" "os"
"path/filepath"
"syscall" "syscall"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@ -65,3 +66,101 @@ func ClearCache(file File) error {
return unix.Fadvise(int(f.Fd()), 0, 0, _POSIX_FADV_DONTNEED) return unix.Fadvise(int(f.Fd()), 0, 0, _POSIX_FADV_DONTNEED)
} }
// Chmod changes the mode of the named file to mode.
func Chmod(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}
// Mkdir creates a new directory with the specified name and permission bits.
// If there is an error, it will be of type *PathError.
func Mkdir(name string, perm os.FileMode) error {
return os.Mkdir(name, perm)
}
// MkdirAll creates a directory named path,
// along with any necessary parents, and returns nil,
// or else returns an error.
// The permission bits perm are used for all
// directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing
// and returns nil.
func MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
// Readlink returns the destination of the named symbolic link.
// If there is an error, it will be of type *PathError.
func Readlink(name string) (string, error) {
return os.Readlink(name)
}
// Remove removes the named file or directory.
// If there is an error, it will be of type *PathError.
func Remove(name string) error {
return os.Remove(name)
}
// RemoveAll removes path and any children it contains.
// It removes everything it can but returns the first error
// it encounters. If the path does not exist, RemoveAll
// returns nil (no error).
func RemoveAll(path string) error {
return os.RemoveAll(path)
}
// Rename renames (moves) oldpath to newpath.
// If newpath already exists, Rename replaces it.
// OS-specific restrictions may apply when oldpath and newpath are in different directories.
// If there is an error, it will be of type *LinkError.
func Rename(oldpath, newpath string) error {
return os.Rename(oldpath, newpath)
}
// Symlink creates newname as a symbolic link to oldname.
// If there is an error, it will be of type *LinkError.
func Symlink(oldname, newname string) error {
return os.Symlink(oldname, newname)
}
// Stat returns a FileInfo structure describing the named file.
// If there is an error, it will be of type *PathError.
func Stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
// Lstat returns the FileInfo structure describing the named file.
// If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow the link.
// If there is an error, it will be of type *PathError.
func Lstat(name string) (os.FileInfo, error) {
return os.Lstat(name)
}
// Create creates the named file with mode 0666 (before umask), truncating
// it if it already exists. If successful, methods on the returned
// File can be used for I/O; the associated file descriptor has mode
// O_RDWR.
// If there is an error, it will be of type *PathError.
func Create(name string) (*os.File, error) {
return os.Create(name)
}
// OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
// methods on the returned File can be used for I/O.
// If there is an error, it will be of type *PathError.
func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
return os.OpenFile(name, flag, perm)
}
// Walk walks the file tree rooted at root, calling walkFn for each file or
// directory in the tree, including root. All errors that arise visiting files
// and directories are filtered by walkFn. The files are walked in lexical
// order, which makes the output deterministic but means that for very
// large directories Walk can be inefficient.
// Walk does not follow symbolic links.
func Walk(root string, walkFn filepath.WalkFunc) error {
return filepath.Walk(root, walkFn)
}

View File

@ -15,6 +15,7 @@ import (
"github.com/juju/errors" "github.com/juju/errors"
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/fs"
"restic/pack" "restic/pack"
"restic/repository" "restic/repository"
) )
@ -157,7 +158,7 @@ func (node Node) restoreMetadata(path string) error {
} }
if node.Type != "symlink" { if node.Type != "symlink" {
err = os.Chmod(path, node.Mode) err = fs.Chmod(path, node.Mode)
if err != nil { if err != nil {
return errors.Annotate(err, "Chmod") return errors.Annotate(err, "Chmod")
} }
@ -196,7 +197,7 @@ func (node Node) RestoreTimestamps(path string) error {
} }
func (node Node) createDirAt(path string) error { func (node Node) createDirAt(path string) error {
err := os.Mkdir(path, node.Mode) err := fs.Mkdir(path, node.Mode)
if err != nil { if err != nil {
return errors.Annotate(err, "Mkdir") return errors.Annotate(err, "Mkdir")
} }
@ -205,7 +206,7 @@ func (node Node) createDirAt(path string) error {
} }
func (node Node) createFileAt(path string, repo *repository.Repository) error { func (node Node) createFileAt(path string, repo *repository.Repository) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600) f, err := fs.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close() defer f.Close()
if err != nil { if err != nil {
@ -243,7 +244,7 @@ func (node Node) createSymlinkAt(path string) error {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return nil return nil
} }
err := os.Symlink(node.LinkTarget, path) err := fs.Symlink(node.LinkTarget, path)
if err != nil { if err != nil {
return errors.Annotate(err, "Symlink") return errors.Annotate(err, "Symlink")
} }
@ -477,7 +478,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
node.Links = uint64(stat.nlink()) node.Links = uint64(stat.nlink())
case "dir": case "dir":
case "symlink": case "symlink":
node.LinkTarget, err = os.Readlink(path) node.LinkTarget, err = fs.Readlink(path)
case "dev": case "dev":
node.Device = uint64(stat.rdev()) node.Device = uint64(stat.rdev())
case "chardev": case "chardev":

View File

@ -1,16 +1,17 @@
package restic package restic
import ( import (
"os"
"path/filepath" "path/filepath"
"syscall" "syscall"
"unsafe" "unsafe"
"restic/fs"
"github.com/juju/errors" "github.com/juju/errors"
) )
func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
dir, err := os.Open(filepath.Dir(path)) dir, err := fs.Open(filepath.Dir(path))
defer dir.Close() defer dir.Close()
if err != nil { if err != nil {
return err return err

View File

@ -8,6 +8,7 @@ import (
"sort" "sort"
"restic/debug" "restic/debug"
"restic/fs"
) )
type Result interface{} type Result interface{}
@ -59,7 +60,7 @@ func (e Dir) Result() chan<- Result { return e.result }
// a sorted list of directory entries. // a sorted list of directory entries.
// taken from filepath/path.go // taken from filepath/path.go
func readDirNames(dirname string) ([]string, error) { func readDirNames(dirname string) ([]string, error) {
f, err := os.Open(dirname) f, err := fs.Open(dirname)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -90,7 +91,7 @@ func walk(basedir, dir string, selectFunc SelectFunc, done <-chan struct{}, jobs
panic(err) panic(err)
} }
info, err := os.Lstat(dir) info, err := fs.Lstat(dir)
if err != nil { if err != nil {
debug.Log("pipe.walk", "error for %v: %v, res %p", dir, err, res) debug.Log("pipe.walk", "error for %v: %v, res %p", dir, err, res)
select { select {
@ -135,7 +136,7 @@ func walk(basedir, dir string, selectFunc SelectFunc, done <-chan struct{}, jobs
for _, name := range names { for _, name := range names {
subpath := filepath.Join(dir, name) subpath := filepath.Join(dir, name)
fi, statErr := os.Lstat(subpath) fi, statErr := fs.Lstat(subpath)
if !selectFunc(subpath, fi) { if !selectFunc(subpath, fi) {
debug.Log("pipe.walk", "file %v excluded by filter", subpath) debug.Log("pipe.walk", "file %v excluded by filter", subpath)
continue continue

View File

@ -10,6 +10,7 @@ import (
"restic/backend" "restic/backend"
"restic/crypto" "restic/crypto"
"restic/debug" "restic/debug"
"restic/fs"
"restic/pack" "restic/pack"
) )
@ -93,7 +94,7 @@ func (r *Repository) savePacker(p *pack.Packer) error {
} }
tmpfile := p.Writer().(*os.File) tmpfile := p.Writer().(*os.File)
f, err := os.Open(tmpfile.Name()) f, err := fs.Open(tmpfile.Name())
if err != nil { if err != nil {
return err return err
} }
@ -120,7 +121,7 @@ func (r *Repository) savePacker(p *pack.Packer) error {
debug.Log("Repo.savePacker", "saved as %v", h) debug.Log("Repo.savePacker", "saved as %v", h)
err = os.Remove(tmpfile.Name()) err = fs.Remove(tmpfile.Name())
if err != nil { if err != nil {
return err return err
} }

View File

@ -7,6 +7,7 @@ import (
"restic/backend" "restic/backend"
"restic/debug" "restic/debug"
"restic/fs"
"restic/repository" "restic/repository"
"github.com/juju/errors" "github.com/juju/errors"
@ -99,7 +100,7 @@ func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error {
debug.Log("Restorer.restoreNodeTo", "create intermediate paths") debug.Log("Restorer.restoreNodeTo", "create intermediate paths")
// Create parent directories and retry // Create parent directories and retry
err = os.MkdirAll(filepath.Dir(dstPath), 0700) err = fs.MkdirAll(filepath.Dir(dstPath), 0700)
if err == nil || err == os.ErrExist { if err == nil || err == os.ErrExist {
err = node.CreateAt(dstPath, res.repo) err = node.CreateAt(dstPath, res.repo)
} }