2015-03-28 10:50:23 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2016-08-21 15:46:23 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/backend"
|
|
|
|
"restic/debug"
|
2016-03-28 13:24:18 +00:00
|
|
|
"restic/fs"
|
2015-03-28 10:50:23 +00:00
|
|
|
)
|
|
|
|
|
2016-01-24 19:23:50 +00:00
|
|
|
// Local is a backend in a local directory.
|
2015-03-28 10:50:23 +00:00
|
|
|
type Local struct {
|
2016-01-26 21:07:51 +00:00
|
|
|
p string
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 21:09:29 +00:00
|
|
|
func paths(dir string) []string {
|
|
|
|
return []string{
|
2015-03-28 10:50:23 +00:00
|
|
|
dir,
|
|
|
|
filepath.Join(dir, backend.Paths.Data),
|
|
|
|
filepath.Join(dir, backend.Paths.Snapshots),
|
2015-04-26 13:48:35 +00:00
|
|
|
filepath.Join(dir, backend.Paths.Index),
|
2015-03-28 10:50:23 +00:00
|
|
|
filepath.Join(dir, backend.Paths.Locks),
|
|
|
|
filepath.Join(dir, backend.Paths.Keys),
|
|
|
|
filepath.Join(dir, backend.Paths.Temp),
|
|
|
|
}
|
2016-01-26 21:09:29 +00:00
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
|
2016-01-26 21:09:29 +00:00
|
|
|
// Open opens the local backend as specified by config.
|
|
|
|
func Open(dir string) (*Local, error) {
|
2015-05-03 14:43:27 +00:00
|
|
|
// test if all necessary dirs are there
|
2016-01-26 21:09:29 +00:00
|
|
|
for _, d := range paths(dir) {
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
if _, err := fs.Stat(d); err != nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return nil, errors.Errorf("%s does not exist", d)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:07:51 +00:00
|
|
|
return &Local{p: dir}, nil
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates all the necessary files and directories for a new local
|
2015-05-04 18:39:45 +00:00
|
|
|
// backend at dir. Afterwards a new config blob should be created.
|
2015-03-28 10:50:23 +00:00
|
|
|
func Create(dir string) (*Local, error) {
|
2015-05-04 18:39:45 +00:00
|
|
|
// test if config file already exists
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
_, err := fs.Lstat(filepath.Join(dir, backend.Paths.Config))
|
2015-03-28 10:50:23 +00:00
|
|
|
if err == nil {
|
2015-05-03 14:43:27 +00:00
|
|
|
return nil, errors.New("config file already exists")
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create paths for data, refs and temp
|
2016-01-26 21:09:29 +00:00
|
|
|
for _, d := range paths(dir) {
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
err := fs.MkdirAll(d, backend.Modes.Dir)
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// open backend
|
|
|
|
return Open(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location returns this backend's location (the directory name).
|
|
|
|
func (b *Local) Location() string {
|
|
|
|
return b.p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct path for given Type and name.
|
|
|
|
func filename(base string, t backend.Type, name string) string {
|
2015-05-03 14:43:27 +00:00
|
|
|
if t == backend.Config {
|
|
|
|
return filepath.Join(base, "config")
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
return filepath.Join(dirname(base, t, name), name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct directory for given Type.
|
|
|
|
func dirname(base string, t backend.Type, name string) string {
|
|
|
|
var n string
|
|
|
|
switch t {
|
|
|
|
case backend.Data:
|
|
|
|
n = backend.Paths.Data
|
|
|
|
if len(name) > 2 {
|
|
|
|
n = filepath.Join(n, name[:2])
|
|
|
|
}
|
|
|
|
case backend.Snapshot:
|
|
|
|
n = backend.Paths.Snapshots
|
2015-04-26 13:48:35 +00:00
|
|
|
case backend.Index:
|
|
|
|
n = backend.Paths.Index
|
2015-03-28 10:50:23 +00:00
|
|
|
case backend.Lock:
|
|
|
|
n = backend.Paths.Locks
|
|
|
|
case backend.Key:
|
|
|
|
n = backend.Paths.Keys
|
|
|
|
}
|
|
|
|
return filepath.Join(base, n)
|
|
|
|
}
|
|
|
|
|
2016-08-07 12:50:24 +00:00
|
|
|
// Load returns the data stored in the backend for h at the given offset and
|
|
|
|
// saves it in p. Load has the same semantics as io.ReaderAt, with one
|
|
|
|
// exception: when off is lower than zero, it is treated as an offset relative
|
|
|
|
// to the end of the file.
|
2016-01-23 13:12:12 +00:00
|
|
|
func (b *Local) Load(h backend.Handle, p []byte, off int64) (n int, err error) {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Load", "Load %v, length %v at %v", h, len(p), off)
|
2016-01-23 16:08:03 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
f, err := fs.Open(filename(b.p, h.Type, h.Name))
|
2016-01-23 13:12:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
e := f.Close()
|
|
|
|
if err == nil && e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-08-07 12:50:24 +00:00
|
|
|
switch {
|
|
|
|
case off > 0:
|
2016-01-23 13:12:12 +00:00
|
|
|
_, err = f.Seek(off, 0)
|
2016-08-07 12:50:24 +00:00
|
|
|
case off < 0:
|
|
|
|
_, err = f.Seek(off, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-01-23 13:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return io.ReadFull(f, p)
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:12:53 +00:00
|
|
|
// writeToTempfile saves p into a tempfile in tempdir.
|
|
|
|
func writeToTempfile(tempdir string, p []byte) (filename string, err error) {
|
|
|
|
tmpfile, err := ioutil.TempFile(tempdir, "temp-")
|
2016-01-24 00:15:35 +00:00
|
|
|
if err != nil {
|
2016-01-26 21:12:53 +00:00
|
|
|
return "", err
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
n, err := tmpfile.Write(p)
|
2016-01-24 00:15:35 +00:00
|
|
|
if err != nil {
|
2016-01-26 21:12:53 +00:00
|
|
|
return "", err
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n != len(p) {
|
2016-01-26 21:12:53 +00:00
|
|
|
return "", errors.New("not all bytes writen")
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
if err = tmpfile.Sync(); err != nil {
|
2016-01-26 21:12:53 +00:00
|
|
|
return "", err
|
2016-01-24 15:59:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:24:18 +00:00
|
|
|
err = fs.ClearCache(tmpfile)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
err = tmpfile.Close()
|
|
|
|
if err != nil {
|
2016-01-26 21:12:53 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpfile.Name(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores data in the backend at the handle.
|
|
|
|
func (b *Local) Save(h backend.Handle, p []byte) (err error) {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Save", "Save %v, length %v", h, len(p))
|
2016-01-26 21:12:53 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2016-01-24 15:59:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:12:53 +00:00
|
|
|
tmpfile, err := writeToTempfile(filepath.Join(b.p, backend.Paths.Temp), p)
|
|
|
|
debug.Log("local.Save", "saved %v (%d bytes) to %v", h, len(p), tmpfile)
|
2016-08-28 10:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-26 21:12:53 +00:00
|
|
|
|
|
|
|
filename := filename(b.p, h.Type, h.Name)
|
2016-01-24 15:59:38 +00:00
|
|
|
|
|
|
|
// test if new path already exists
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
if _, err := fs.Stat(filename); err == nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("Rename(): file %v already exists", filename)
|
2016-01-24 15:59:38 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 20:13:24 +00:00
|
|
|
// create directories if necessary, ignore errors
|
|
|
|
if h.Type == backend.Data {
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
err = fs.MkdirAll(filepath.Dir(filename), backend.Modes.Dir)
|
2016-01-24 20:13:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
err = fs.Rename(tmpfile, filename)
|
2016-01-24 15:59:38 +00:00
|
|
|
debug.Log("local.Save", "save %v: rename %v -> %v: %v",
|
2016-01-26 21:12:53 +00:00
|
|
|
h, filepath.Base(tmpfile), filepath.Base(filename), err)
|
2016-01-24 15:59:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set mode to read-only
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
fi, err := fs.Stat(filename)
|
2016-01-24 15:59:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:12:53 +00:00
|
|
|
return setNewFileMode(filename, fi)
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 22:27:58 +00:00
|
|
|
// Stat returns information about a blob.
|
|
|
|
func (b *Local) Stat(h backend.Handle) (backend.BlobInfo, error) {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Stat", "Stat %v", h)
|
2016-01-23 22:27:58 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return backend.BlobInfo{}, err
|
|
|
|
}
|
|
|
|
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
fi, err := fs.Stat(filename(b.p, h.Type, h.Name))
|
2016-01-23 22:27:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return backend.BlobInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend.BlobInfo{Size: fi.Size()}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// 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) {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Test", "Test %v %v", t, name)
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
_, err := fs.Stat(filename(b.p, t, name))
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
|
|
|
func (b *Local) Remove(t backend.Type, name string) error {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Remove", "Remove %v %v", t, name)
|
2015-08-14 13:30:36 +00:00
|
|
|
fn := filename(b.p, t, name)
|
|
|
|
|
2015-08-19 20:02:47 +00:00
|
|
|
// reset read-only flag
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
err := fs.Chmod(fn, 0666)
|
2015-08-19 20:02:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
return fs.Remove(fn)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:43:04 +00:00
|
|
|
func isFile(fi os.FileInfo) bool {
|
|
|
|
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func readdir(d string) (fileInfos []os.FileInfo, err error) {
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
f, e := fs.Open(d)
|
2016-02-24 21:43:04 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
e := f.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return f.Readdir(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// listDir returns a list of all files in d.
|
|
|
|
func listDir(d string) (filenames []string, err error) {
|
|
|
|
fileInfos, err := readdir(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fileInfos {
|
|
|
|
if isFile(fi) {
|
|
|
|
filenames = append(filenames, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filenames, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// listDirs returns a list of all files in directories within d.
|
|
|
|
func listDirs(dir string) (filenames []string, err error) {
|
|
|
|
fileInfos, err := readdir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fileInfos {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := listDir(filepath.Join(dir, fi.Name()))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filenames = append(filenames, files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filenames, nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// List returns a channel that yields all names of blobs of type t. A
|
2015-06-28 07:44:06 +00:00
|
|
|
// goroutine is started for this. If the channel done is closed, sending
|
2015-03-28 10:50:23 +00:00
|
|
|
// stops.
|
|
|
|
func (b *Local) List(t backend.Type, done <-chan struct{}) <-chan string {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.List", "List %v", t)
|
2016-02-24 21:43:04 +00:00
|
|
|
lister := listDir
|
2015-04-29 20:30:00 +00:00
|
|
|
if t == backend.Data {
|
2016-02-24 21:43:04 +00:00
|
|
|
lister = listDirs
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan string)
|
2016-02-24 21:43:04 +00:00
|
|
|
items, err := lister(filepath.Join(dirname(b.p, t, "")))
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
2016-02-24 21:43:04 +00:00
|
|
|
for _, m := range items {
|
2015-03-28 10:50:23 +00:00
|
|
|
if m == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- m:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the repository and all files.
|
2015-08-14 13:30:36 +00:00
|
|
|
func (b *Local) Delete() error {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Delete", "Delete()")
|
Fix 567 (#570)
* 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
2016-08-15 19:59:13 +00:00
|
|
|
return fs.RemoveAll(b.p)
|
2015-08-14 13:30:36 +00:00
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
|
2015-08-14 13:30:36 +00:00
|
|
|
// Close closes all open files.
|
|
|
|
func (b *Local) Close() error {
|
2016-08-07 12:50:24 +00:00
|
|
|
debug.Log("backend.local.Close", "Close()")
|
2016-01-26 21:07:51 +00:00
|
|
|
// this does not need to do anything, all open files are closed within the
|
|
|
|
// same function.
|
2015-08-14 13:30:36 +00:00
|
|
|
return nil
|
|
|
|
}
|