1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-22 07:43:03 +00:00
This commit is contained in:
Alexander Neumann 2024-04-07 09:58:44 +02:00
parent b3f38686ee
commit 9da6e7c329
2 changed files with 133 additions and 120 deletions

View file

@ -65,6 +65,18 @@ func New(ctx context.Context, repo restic.Repository, cfg Config) (*ROFS, error)
return rofs, nil return rofs, nil
} }
func (rofs *ROFS) updateSnapshots(ctx context.Context) error {
entries, err := buildSnapshotEntries(ctx, rofs.repo, rofs.cfg)
if err != nil {
return err
}
rofs.entries = entries
return nil
}
func buildSnapshotEntries(ctx context.Context, repo restic.Repository, cfg Config) (map[string]rofsEntry, error) { func buildSnapshotEntries(ctx context.Context, repo restic.Repository, cfg Config) (map[string]rofsEntry, error) {
var snapshots restic.Snapshots var snapshots restic.Snapshots
err := cfg.Filter.FindAll(ctx, repo, repo, nil, func(_ string, sn *restic.Snapshot, _ error) error { err := cfg.Filter.FindAll(ctx, repo, repo, nil, func(_ string, sn *restic.Snapshot, _ error) error {
@ -83,25 +95,12 @@ func buildSnapshotEntries(ctx context.Context, repo restic.Repository, cfg Confi
list := make(map[string]rofsEntry) list := make(map[string]rofsEntry)
list["foo"] = NewMemFile("foo", []byte("foobar content of file foo"), time.Now()) list["foo"] = NewMemFile("foo", []byte("foobar content of file foo"), time.Now())
list["snapshots"] = NewMemFile("snapshots", []byte("here goes the snapshot list"), time.Now())
// list["snapshots"] = NewSnapshotsDir(cfg.PathTemplates, cfg.TimeTemplate) list["snapshots"] = NewSnapshotsDir(ctx, repo, cfg.PathTemplates, cfg.TimeTemplate)
return list, nil return list, nil
} }
func (rofs *ROFS) updateSnapshots(ctx context.Context) error {
entries, err := buildSnapshotEntries(ctx, rofs.repo, rofs.cfg)
if err != nil {
return err
}
rofs.entries = entries
return nil
}
// Open opens the named file. // Open opens the named file.
// //
// When Open returns an error, it should be of type *PathError // When Open returns an error, it should be of type *PathError

View file

@ -1,134 +1,148 @@
package rofs package rofs
// // SnapshotsDir implements a tree of snapshots in repo as a file system in various sub-directories. import (
// type SnapshotsDir struct { "context"
// lastUpdate time.Time "io"
"io/fs"
"time"
// pathTemplates []string "github.com/restic/restic/internal/debug"
// timeTemplate string "github.com/restic/restic/internal/restic"
)
// // list of top-level directories // SnapshotsDir implements a tree of snapshots in repo as a file system in various sub-directories.
// entries []rofsEntry type SnapshotsDir struct {
// } lastUpdate time.Time
// // ensure that the interface is implemented pathTemplates []string
// var _ rofsEntry = &SnapshotsDir{} timeTemplate string
// // NewSnapshotsDir initializes a new top-level snapshots directory. // list of top-level directories
// func NewSnapshotsDir(pathTemplates []string, timeTemplate string) *SnapshotsDir { entries []rofsEntry
// dir := &SnapshotsDir{
// pathTemplates: pathTemplates,
// timeTemplate: timeTemplate,
// lastUpdate: time.Now(),
// }
// // testnames := []string{"foo", "bar", "baz", "snapshots"} repo restic.Repository
// // for _, name := range testnames { }
// // dir.entries = append(dir.entries,
// // fs.FileInfoToDirEntry(FileInfo{
// // name: name,
// // mode: 0644,
// // modtime: time.Now(),
// // }))
// // }
// // slices.SortFunc(dir.entries, func(a, b fs.DirEntry) int { // ensure that the interface is implemented
// // if a.Name() == b.Name() { var _ rofsEntry = &SnapshotsDir{}
// // return 0
// // }
// // if a.Name() < b.Name() { // NewSnapshotsDir initializes a new top-level snapshots directory.
// // return 1 func NewSnapshotsDir(ctx context.Context, repo restic.Repository, pathTemplates []string, timeTemplate string) *SnapshotsDir {
// // } dir := &SnapshotsDir{
pathTemplates: pathTemplates,
timeTemplate: timeTemplate,
lastUpdate: time.Now(),
// // return -1 repo: repo,
// // }) }
// // // prepare for readdir with positive n // testnames := []string{"foo", "bar", "baz", "snapshots"}
// // dir.entriesRemaining = dir.entries // for _, name := range testnames {
// dir.entries = append(dir.entries,
// fs.FileInfoToDirEntry(FileInfo{
// name: name,
// mode: 0644,
// modtime: time.Now(),
// }))
// }
// return dir // slices.SortFunc(dir.entries, func(a, b fs.DirEntry) int {
// } // if a.Name() == b.Name() {
// return 0
// }
// // ensure that it implements all necessary interfaces. // if a.Name() < b.Name() {
// var _ fs.ReadDirFile = &SnapshotsDir{} // return 1
// }
// // Close closes the snapshots dir. // return -1
// func (dir *SnapshotsDir) Close() error { // })
// debug.Log("Close()")
// // reset readdir list // // prepare for readdir with positive n
// // dir.entriesRemaining = dir.entries // dir.entriesRemaining = dir.entries
// return nil return dir
// } }
// // Read is not implemented for a dir. // ensure that it implements all necessary interfaces.
// func (dir *SnapshotsDir) Read([]byte) (int, error) { var _ fs.ReadDirFile = &SnapshotsDir{}
// return 0, &fs.PathError{
// Op: "read",
// Err: fs.ErrInvalid,
// }
// }
// // Stat returns information about the dir. // Close closes the snapshots dir.
// func (dir *SnapshotsDir) Stat() (fs.FileInfo, error) { func (dir *SnapshotsDir) Close() error {
// debug.Log("Stat(root)") debug.Log("Close()")
// fi := FileInfo{ // reset readdir list
// name: "root", // use special name, this is the root node // dir.entriesRemaining = dir.entries
// size: 0,
// modtime: dir.lastUpdate,
// mode: 0755,
// }
// return fi, nil return nil
// } }
// // ReadDir returns a list of entries. // Read is not implemented for a dir.
// func (dir *SnapshotsDir) ReadDir(n int) ([]fs.DirEntry, error) { func (dir *SnapshotsDir) Read([]byte) (int, error) {
// if n < 0 { return 0, &fs.PathError{
// debug.Log("Readdir(root, %v), return %v entries", n, len(dir.entries)) Op: "read",
// return dir.entries, nil Err: fs.ErrInvalid,
// } }
}
// // complicated pointer handling // Stat returns information about the dir.
// if n > len(dir.entriesRemaining) { func (dir *SnapshotsDir) Stat() (fs.FileInfo, error) {
// n = len(dir.entriesRemaining) debug.Log("Stat(root)")
// }
// if n == 0 { fi := FileInfo{
// return nil, io.EOF name: "root", // use special name, this is the root node
// } size: 0,
modtime: dir.lastUpdate,
mode: 0755,
}
// list := dir.entriesRemaining[:n] return fi, nil
// dir.entriesRemaining = dir.entriesRemaining[n:] }
// return list, nil // ReadDir returns a list of entries.
// } func (dir *SnapshotsDir) ReadDir(n int) ([]fs.DirEntry, error) {
if n < 0 {
debug.Log("Readdir(root, %v), return %v entries", n, len(dir.entries))
return dir.entries, nil
}
// // DirEntry returns meta data about the dir snapshots dir itself. // complicated pointer handling
// func (dir *SnapshotsDir) DirEntry() fs.DirEntry { if n > len(dir.entriesRemaining) {
// return dirEntry{ n = len(dir.entriesRemaining)
// fileInfo: FileInfo{ }
// name: "snapshots",
// mode: fs.ModeDir | 0755,
// modtime: dir.lastUpdate,
// },
// }
// }
// // Open opens the dir for reading. if n == 0 {
// func (dir *SnapshotsDir) Open() (fs.File, error) { return nil, io.EOF
// d := &openDir{ }
// path: "snapshots",
// fileInfo: FileInfo{
// name: "snapshots",
// mode: fs.ModeDir | 0555,
// modtime: dir.lastUpdate,
// },
// entries: dirMap2DirEntry(dir.entries),
// }
// return d list := dir.entriesRemaining[:n]
// } dir.entriesRemaining = dir.entriesRemaining[n:]
return list, nil
}
// DirEntry returns meta data about the dir snapshots dir itself.
func (dir *SnapshotsDir) DirEntry() fs.DirEntry {
return dirEntry{
fileInfo: FileInfo{
name: "snapshots",
mode: fs.ModeDir | 0755,
modtime: dir.lastUpdate,
},
}
}
// Open opens the dir for reading.
func (dir *SnapshotsDir) Open() (fs.File, error) {
d := &openDir{
path: "snapshots",
fileInfo: FileInfo{
name: "snapshots",
mode: fs.ModeDir | 0555,
modtime: dir.lastUpdate,
},
entries: dirMap2DirEntry(dir.entries),
}
return d
}