2015-03-28 10:50:23 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2020-12-19 11:39:48 +00:00
|
|
|
"hash"
|
2015-03-28 10:50:23 +00:00
|
|
|
"io"
|
2020-11-02 09:34:21 +00:00
|
|
|
"io/ioutil"
|
2015-03-28 10:50:23 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-09 19:42:15 +00:00
|
|
|
"syscall"
|
2015-03-28 10:50:23 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/backend"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
2020-12-16 12:58:02 +00:00
|
|
|
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
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 {
|
2017-03-25 12:20:03 +00:00
|
|
|
Config
|
2021-08-07 17:50:00 +00:00
|
|
|
sem *backend.Semaphore
|
2017-03-26 19:53:26 +00:00
|
|
|
backend.Layout
|
2022-04-26 17:15:09 +00:00
|
|
|
backend.Modes
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 20:14:00 +00:00
|
|
|
// ensure statically that *Local implements restic.Backend.
|
2016-08-31 20:39:36 +00:00
|
|
|
var _ restic.Backend = &Local{}
|
|
|
|
|
2017-04-02 17:54:11 +00:00
|
|
|
const defaultLayout = "default"
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
func open(ctx context.Context, cfg Config) (*Local, error) {
|
2020-09-19 20:01:32 +00:00
|
|
|
l, err := backend.ParseLayout(ctx, &backend.LocalFilesystem{}, cfg.Layout, defaultLayout, cfg.Path)
|
2017-04-02 15:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-26 19:53:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
sem, err := backend.NewSemaphore(cfg.Connections)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:15:09 +00:00
|
|
|
fi, err := fs.Stat(l.Filename(restic.Handle{Type: restic.ConfigFile}))
|
|
|
|
m := backend.DeriveModesFromFileInfo(fi, err)
|
|
|
|
debug.Log("using (%03O file, %03O dir) permissions", m.File, m.Dir)
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
return &Local{
|
|
|
|
Config: cfg,
|
|
|
|
Layout: l,
|
|
|
|
sem: sem,
|
2022-04-26 17:15:09 +00:00
|
|
|
Modes: m,
|
2021-08-07 17:50:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens the local backend as specified by config.
|
|
|
|
func Open(ctx context.Context, cfg Config) (*Local, error) {
|
|
|
|
debug.Log("open local backend at %v (layout %q)", cfg.Path, cfg.Layout)
|
|
|
|
return open(ctx, cfg)
|
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.
|
2020-09-19 20:01:32 +00:00
|
|
|
func Create(ctx context.Context, cfg Config) (*Local, error) {
|
2017-04-02 17:54:11 +00:00
|
|
|
debug.Log("create local backend at %v (layout %q)", cfg.Path, cfg.Layout)
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
be, err := open(ctx, cfg)
|
2017-04-02 17:54:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-04 18:39:45 +00:00
|
|
|
// test if config file already exists
|
2017-04-02 17:54:11 +00:00
|
|
|
_, err = fs.Lstat(be.Filename(restic.Handle{Type: restic.ConfigFile}))
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 16:56:01 +00:00
|
|
|
// create paths for data and refs
|
2017-03-26 19:53:26 +00:00
|
|
|
for _, d := range be.Paths() {
|
2022-04-26 17:15:09 +00:00
|
|
|
err := fs.MkdirAll(d, be.Modes.Dir)
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
2021-06-07 17:26:25 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:53:26 +00:00
|
|
|
return be, nil
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
func (b *Local) Connections() uint {
|
|
|
|
return b.Config.Connections
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Location returns this backend's location (the directory name).
|
|
|
|
func (b *Local) Location() string {
|
2017-03-25 12:20:03 +00:00
|
|
|
return b.Path
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
// Hasher may return a hash function for calculating a content hash for the backend
|
|
|
|
func (b *Local) Hasher() hash.Hash {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
// IsNotExist returns true if the error is caused by a non existing file.
|
|
|
|
func (b *Local) IsNotExist(err error) bool {
|
2021-06-07 17:26:25 +00:00
|
|
|
return errors.Is(err, os.ErrNotExist)
|
2017-06-15 11:40:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 21:12:53 +00:00
|
|
|
// Save stores data in the backend at the handle.
|
2020-12-16 12:58:02 +00:00
|
|
|
func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) (err error) {
|
2017-01-22 11:32:20 +00:00
|
|
|
debug.Log("Save %v", h)
|
2016-01-26 21:12:53 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return backoff.Permanent(err)
|
2016-01-24 15:59:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:34:21 +00:00
|
|
|
finalname := b.Filename(h)
|
|
|
|
dir := filepath.Dir(finalname)
|
2016-01-24 15:59:38 +00:00
|
|
|
|
2020-12-16 12:58:02 +00:00
|
|
|
defer func() {
|
2020-12-20 20:12:27 +00:00
|
|
|
// Mark non-retriable errors as such
|
|
|
|
if errors.Is(err, syscall.ENOSPC) || os.IsPermission(err) {
|
2020-12-16 12:58:02 +00:00
|
|
|
err = backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
b.sem.GetToken()
|
|
|
|
defer b.sem.ReleaseToken()
|
|
|
|
|
2020-11-02 09:34:21 +00:00
|
|
|
// Create new file with a temporary name.
|
|
|
|
tmpname := filepath.Base(finalname) + "-tmp-"
|
|
|
|
f, err := tempFile(dir, tmpname)
|
2018-01-05 16:51:09 +00:00
|
|
|
|
|
|
|
if b.IsNotExist(err) {
|
|
|
|
debug.Log("error %v: creating dir", err)
|
|
|
|
|
|
|
|
// error is caused by a missing directory, try to create it
|
2022-04-26 17:15:09 +00:00
|
|
|
mkdirErr := fs.MkdirAll(dir, b.Modes.Dir)
|
2018-01-05 16:51:09 +00:00
|
|
|
if mkdirErr != nil {
|
2020-11-02 09:34:21 +00:00
|
|
|
debug.Log("error creating dir %v: %v", dir, mkdirErr)
|
2018-01-05 16:51:09 +00:00
|
|
|
} else {
|
|
|
|
// try again
|
2020-11-02 09:34:21 +00:00
|
|
|
f, err = tempFile(dir, tmpname)
|
2018-01-05 16:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:53:26 +00:00
|
|
|
if err != nil {
|
2021-06-07 17:26:25 +00:00
|
|
|
return errors.WithStack(err)
|
2017-03-26 19:53:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:34:21 +00:00
|
|
|
defer func(f *os.File) {
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close() // Double Close is harmless.
|
|
|
|
// Remove after Rename is harmless: we embed the final name in the
|
|
|
|
// temporary's name and no other goroutine will get the same data to
|
|
|
|
// Save, so the temporary name should never be reused by another
|
|
|
|
// goroutine.
|
|
|
|
_ = fs.Remove(f.Name())
|
|
|
|
}
|
|
|
|
}(f)
|
|
|
|
|
2017-03-26 19:53:26 +00:00
|
|
|
// save data, then sync
|
2020-12-18 22:41:29 +00:00
|
|
|
wbytes, err := io.Copy(f, rd)
|
2017-03-26 19:53:26 +00:00
|
|
|
if err != nil {
|
2021-06-07 17:26:25 +00:00
|
|
|
return errors.WithStack(err)
|
2017-03-26 19:53:26 +00:00
|
|
|
}
|
2020-12-18 22:41:29 +00:00
|
|
|
// sanity check
|
|
|
|
if wbytes != rd.Length() {
|
|
|
|
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", wbytes, rd.Length())
|
|
|
|
}
|
2017-03-26 19:53:26 +00:00
|
|
|
|
2020-11-02 09:34:21 +00:00
|
|
|
// Ignore error if filesystem does not support fsync.
|
2021-07-09 15:11:39 +00:00
|
|
|
err = f.Sync()
|
|
|
|
syncNotSup := errors.Is(err, syscall.ENOTSUP)
|
|
|
|
if err != nil && !syncNotSup {
|
2020-11-02 09:34:21 +00:00
|
|
|
return errors.WithStack(err)
|
2017-03-26 19:53:26 +00:00
|
|
|
}
|
2016-01-24 15:59:38 +00:00
|
|
|
|
2020-11-02 09:34:21 +00:00
|
|
|
// Close, then rename. Windows doesn't like the reverse order.
|
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
if err = os.Rename(f.Name(), finalname); err != nil {
|
2021-06-07 17:26:25 +00:00
|
|
|
return errors.WithStack(err)
|
2016-01-24 15:59:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 15:11:39 +00:00
|
|
|
// Now sync the directory to commit the Rename.
|
|
|
|
if !syncNotSup {
|
|
|
|
err = fsyncDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:46:19 +00:00
|
|
|
// try to mark file as read-only to avoid accidential modifications
|
2020-10-06 16:28:01 +00:00
|
|
|
// ignore if the operation fails as some filesystems don't allow the chmod call
|
|
|
|
// e.g. exfat and network file systems with certain mount options
|
2022-04-26 17:15:09 +00:00
|
|
|
err = setFileReadonly(finalname, b.Modes.File)
|
2020-10-06 16:28:01 +00:00
|
|
|
if err != nil && !os.IsPermission(err) {
|
2021-06-07 17:26:25 +00:00
|
|
|
return errors.WithStack(err)
|
2020-10-06 16:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:34:21 +00:00
|
|
|
var tempFile = ioutil.TempFile // Overridden by test.
|
2020-12-16 12:58:02 +00:00
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
|
|
|
// given offset.
|
|
|
|
func (b *Local) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
|
|
|
return backend.DefaultLoad(ctx, h, length, offset, b.openReader, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Local) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-01-23 17:11:10 +00:00
|
|
|
debug.Log("Load %v, length %v, offset %v", h, length, offset)
|
2017-01-22 21:01:12 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return nil, backoff.Permanent(err)
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
b.sem.GetToken()
|
2017-03-26 19:53:26 +00:00
|
|
|
f, err := fs.Open(b.Filename(h))
|
2017-01-22 21:01:12 +00:00
|
|
|
if err != nil {
|
2021-08-07 17:50:00 +00:00
|
|
|
b.sem.ReleaseToken()
|
2017-01-22 21:01:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset > 0 {
|
|
|
|
_, err = f.Seek(offset, 0)
|
|
|
|
if err != nil {
|
2021-08-07 17:50:00 +00:00
|
|
|
b.sem.ReleaseToken()
|
2017-06-03 15:39:57 +00:00
|
|
|
_ = f.Close()
|
2017-01-22 21:01:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
r := b.sem.ReleaseTokenOnClose(f, nil)
|
|
|
|
|
2017-01-22 21:01:12 +00:00
|
|
|
if length > 0 {
|
2021-08-07 17:50:00 +00:00
|
|
|
return backend.LimitReadCloser(r, int64(length)), nil
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
return r, nil
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 22:27:58 +00:00
|
|
|
// Stat returns information about a blob.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (b *Local) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Stat %v", h)
|
2016-01-23 22:27:58 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return restic.FileInfo{}, backoff.Permanent(err)
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
b.sem.GetToken()
|
|
|
|
defer b.sem.ReleaseToken()
|
|
|
|
|
2017-03-26 19:53:26 +00:00
|
|
|
fi, err := fs.Stat(b.Filename(h))
|
2016-01-23 22:27:58 +00:00
|
|
|
if err != nil {
|
2021-06-07 17:26:25 +00:00
|
|
|
return restic.FileInfo{}, errors.WithStack(err)
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
return restic.FileInfo{Size: fi.Size(), Name: h.Name}, nil
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Test returns true if a blob of the given type and name exists in the backend.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (b *Local) Test(ctx context.Context, h restic.Handle) (bool, error) {
|
2017-01-25 16:48:35 +00:00
|
|
|
debug.Log("Test %v", h)
|
2021-08-07 17:50:00 +00:00
|
|
|
|
|
|
|
b.sem.GetToken()
|
|
|
|
defer b.sem.ReleaseToken()
|
|
|
|
|
2017-03-26 19:53:26 +00:00
|
|
|
_, err := fs.Stat(b.Filename(h))
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
2021-06-07 17:26:25 +00:00
|
|
|
if b.IsNotExist(err) {
|
2015-03-28 10:50:23 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-06-07 17:26:25 +00:00
|
|
|
return false, errors.WithStack(err)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (b *Local) Remove(ctx context.Context, h restic.Handle) error {
|
2017-01-25 16:48:35 +00:00
|
|
|
debug.Log("Remove %v", h)
|
2017-03-26 19:53:26 +00:00
|
|
|
fn := b.Filename(h)
|
2015-08-14 13:30:36 +00:00
|
|
|
|
2021-08-07 17:50:00 +00:00
|
|
|
b.sem.GetToken()
|
|
|
|
defer b.sem.ReleaseToken()
|
|
|
|
|
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)
|
2020-10-06 16:28:01 +00:00
|
|
|
if err != nil && !os.IsPermission(err) {
|
2021-06-07 17:26:25 +00:00
|
|
|
return errors.WithStack(err)
|
2015-08-19 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
// List runs fn for each file in the backend which has the type t. When an
|
|
|
|
// error occurs (or fn returns an error), List stops and returns it.
|
2020-11-19 15:46:42 +00:00
|
|
|
func (b *Local) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) (err error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("List %v", t)
|
2015-03-28 10:50:23 +00:00
|
|
|
|
2018-01-20 12:43:07 +00:00
|
|
|
basedir, subdirs := b.Basedir(t)
|
2020-11-19 15:46:42 +00:00
|
|
|
if subdirs {
|
|
|
|
err = visitDirs(ctx, basedir, fn)
|
|
|
|
} else {
|
2021-02-26 23:02:13 +00:00
|
|
|
err = visitFiles(ctx, basedir, fn, false)
|
2020-11-19 15:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.IsNotExist(err) {
|
|
|
|
debug.Log("ignoring non-existing directory")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following two functions are like filepath.Walk, but visit only one or
|
|
|
|
// two levels of directory structure (including dir itself as the first level).
|
|
|
|
// Also, visitDirs assumes it sees a directory full of directories, while
|
|
|
|
// visitFiles wants a directory full or regular files.
|
|
|
|
func visitDirs(ctx context.Context, dir string, fn func(restic.FileInfo) error) error {
|
|
|
|
d, err := fs.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := d.Readdirnames(-1)
|
2021-01-30 18:35:46 +00:00
|
|
|
if err != nil {
|
|
|
|
// ignore subsequent errors
|
|
|
|
_ = d.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Close()
|
2020-11-19 15:46:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range sub {
|
2021-02-26 23:02:13 +00:00
|
|
|
err = visitFiles(ctx, filepath.Join(dir, f), fn, true)
|
2018-01-20 12:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-19 15:46:42 +00:00
|
|
|
}
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
|
2021-02-26 23:02:13 +00:00
|
|
|
func visitFiles(ctx context.Context, dir string, fn func(restic.FileInfo) error, ignoreNotADirectory bool) error {
|
2020-11-19 15:46:42 +00:00
|
|
|
d, err := fs.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
|
2021-02-26 23:02:13 +00:00
|
|
|
if ignoreNotADirectory {
|
|
|
|
fi, err := d.Stat()
|
|
|
|
if err != nil || !fi.IsDir() {
|
2021-11-06 18:44:57 +00:00
|
|
|
// ignore subsequent errors
|
|
|
|
_ = d.Close()
|
2021-02-26 23:02:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:46:42 +00:00
|
|
|
sub, err := d.Readdir(-1)
|
2021-01-30 18:35:46 +00:00
|
|
|
if err != nil {
|
|
|
|
// ignore subsequent errors
|
|
|
|
_ = d.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Close()
|
2020-11-19 15:46:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-14 18:13:01 +00:00
|
|
|
|
2020-11-19 15:46:42 +00:00
|
|
|
for _, fi := range sub {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2018-01-20 18:34:38 +00:00
|
|
|
return ctx.Err()
|
2020-11-19 15:46:42 +00:00
|
|
|
default:
|
2018-01-20 18:34:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:46:42 +00:00
|
|
|
err := fn(restic.FileInfo{
|
|
|
|
Name: fi.Name(),
|
|
|
|
Size: fi.Size(),
|
|
|
|
})
|
2018-01-20 12:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-10 19:35:30 +00:00
|
|
|
}
|
2020-11-19 15:46:42 +00:00
|
|
|
return nil
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the repository and all files.
|
2017-10-14 11:38:17 +00:00
|
|
|
func (b *Local) Delete(ctx context.Context) error {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Delete()")
|
2017-03-25 12:20:03 +00:00
|
|
|
return fs.RemoveAll(b.Path)
|
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-09-27 20:35:08 +00:00
|
|
|
debug.Log("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
|
|
|
|
}
|