From 76b712c2fbf24dda72cf56106eba550d117a4ab5 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Mon, 4 Sep 2017 20:47:51 -0700 Subject: [PATCH] moved creation of repofs entries to snapshot.Manager --- cli/command_cat.go | 6 +++++- cli/command_ls.go | 12 +++++++----- cli/command_mount.go | 8 +++++--- cli/command_object_cleanup.go | 4 +++- cli/command_object_show.go | 6 +++++- cli/command_repository_migrate.go | 4 ++-- cli/objref.go | 4 ++-- snapshot/all_sources.go | 8 +++----- snapshot/repofs.go | 6 +++--- 9 files changed, 35 insertions(+), 23 deletions(-) diff --git a/cli/command_cat.go b/cli/command_cat.go index cc295f33b..bbe594b78 100644 --- a/cli/command_cat.go +++ b/cli/command_cat.go @@ -4,6 +4,8 @@ "io" "os" + "github.com/kopia/kopia/snapshot" + kingpin "gopkg.in/alecthomas/kingpin.v2" ) @@ -16,7 +18,9 @@ func runCatCommand(context *kingpin.ParseContext) error { rep := mustOpenRepository(nil) defer rep.Close() - oid, err := parseObjectID(*catCommandPath, rep) + mgr := snapshot.NewManager(rep) + + oid, err := parseObjectID(mgr, *catCommandPath) if err != nil { return err } diff --git a/cli/command_ls.go b/cli/command_ls.go index 54b321617..3d59d0ac7 100644 --- a/cli/command_ls.go +++ b/cli/command_ls.go @@ -25,7 +25,9 @@ func runLSCommand(context *kingpin.ParseContext) error { rep := mustOpenRepository(nil) defer rep.Close() - oid, err := parseObjectID(*lsCommandPath, rep) + mgr := snapshot.NewManager(rep) + + oid, err := parseObjectID(mgr, *lsCommandPath) if err != nil { return err } @@ -38,15 +40,15 @@ func runLSCommand(context *kingpin.ParseContext) error { } } - return listDirectory(rep, prefix, oid, "") + return listDirectory(mgr, prefix, oid, "") } func init() { lsCommand.Action(runLSCommand) } -func listDirectory(rep *repo.Repository, prefix string, oid repo.ObjectID, indent string) error { - d := snapshot.Directory(rep, oid) +func listDirectory(mgr *snapshot.Manager, prefix string, oid repo.ObjectID, indent string) error { + d := mgr.DirectoryEntry(oid) entries, err := d.Readdir() if err != nil { @@ -93,7 +95,7 @@ func listDirectory(rep *repo.Repository, prefix string, oid repo.ObjectID, inden } fmt.Println(info) if *lsCommandRecursive && m.FileMode().IsDir() { - listDirectory(rep, prefix+m.Name+"/", objectID, indent+" ") + listDirectory(mgr, prefix+m.Name+"/", objectID, indent+" ") } } diff --git a/cli/command_mount.go b/cli/command_mount.go index e9f64231c..8fb46afef 100644 --- a/cli/command_mount.go +++ b/cli/command_mount.go @@ -23,6 +23,8 @@ func runMountCommand(context *kingpin.ParseContext) error { rep := mustOpenRepository(nil) + + mgr := snapshot.NewManager(rep) var entry fs.Directory if *mountCacheRefreshInterval > 0 { @@ -38,13 +40,13 @@ func runMountCommand(context *kingpin.ParseContext) error { } if *mountObjectID == "all" { - entry = snapshot.AllSources(rep) + entry = mgr.AllSourcesEntry() } else { - oid, err := parseObjectID(*mountObjectID, rep) + oid, err := parseObjectID(mgr, *mountObjectID) if err != nil { return err } - entry = snapshot.Directory(rep, oid) + entry = mgr.DirectoryEntry(oid) } if *mountTraceFS { diff --git a/cli/command_object_cleanup.go b/cli/command_object_cleanup.go index b97679875..3e964cc32 100644 --- a/cli/command_object_cleanup.go +++ b/cli/command_object_cleanup.go @@ -112,6 +112,7 @@ type cleanupContext struct { sync.Mutex repo *repo.Repository + mgr *snapshot.Manager inuse map[string]bool visited map[string]bool queue *cleanupWorkQueue @@ -130,7 +131,7 @@ func findAliveBlocks(ctx *cleanupContext, wi *cleanupWorkItem) error { } if wi.isDirectory { - entries, err := snapshot.Directory(ctx.repo, wi.oid).Readdir() + entries, err := ctx.mgr.DirectoryEntry(wi.oid).Readdir() if err != nil { return err @@ -167,6 +168,7 @@ func runCleanupCommand(context *kingpin.ParseContext) error { ctx := &cleanupContext{ repo: rep, + mgr: mgr, inuse: map[string]bool{}, visited: map[string]bool{}, queue: q, diff --git a/cli/command_object_show.go b/cli/command_object_show.go index de041a41e..228cac550 100644 --- a/cli/command_object_show.go +++ b/cli/command_object_show.go @@ -6,6 +6,8 @@ "io/ioutil" "os" + "github.com/kopia/kopia/snapshot" + "github.com/kopia/kopia/repo" kingpin "gopkg.in/alecthomas/kingpin.v2" @@ -23,8 +25,10 @@ func runShowCommand(context *kingpin.ParseContext) error { rep := mustOpenRepository(nil) defer rep.Close() + mgr := snapshot.NewManager(rep) + for _, oidString := range *showObjectIDs { - oid, err := parseObjectID(oidString, rep) + oid, err := parseObjectID(mgr, oidString) if err != nil { return err } diff --git a/cli/command_repository_migrate.go b/cli/command_repository_migrate.go index c5f75711e..c5f6a62cd 100644 --- a/cli/command_repository_migrate.go +++ b/cli/command_repository_migrate.go @@ -58,7 +58,7 @@ func runMigrateCommand(context *kingpin.ParseContext) error { } for _, m := range filterSnapshotsToMigrate(snapshots) { - d := snapshot.Directory(sourceRepo, m.RootObjectID) + d := sourceSM.DirectoryEntry(m.RootObjectID) newm, err := uploader.Upload(d, &m.Source, nil) if err != nil { return fmt.Errorf("error migrating shapshot %v @ %v: %v", m.Source, m.StartTime, err) @@ -80,7 +80,7 @@ func runMigrateCommand(context *kingpin.ParseContext) error { if err != nil { return err } - d := snapshot.Directory(sourceRepo, dirOID) + d := sourceSM.DirectoryEntry(dirOID) newm, err := uploader.Upload(d, &snapshot.SourceInfo{Host: "temp"}, nil) if err != nil { return fmt.Errorf("error migrating directory %v: %v", dirOID, err) diff --git a/cli/objref.go b/cli/objref.go index 1bca2b0dd..366927aa1 100644 --- a/cli/objref.go +++ b/cli/objref.go @@ -10,7 +10,7 @@ ) // ParseObjectID interprets the given ID string and returns corresponding repo.ObjectID. -func parseObjectID(id string, r *repo.Repository) (repo.ObjectID, error) { +func parseObjectID(mgr *snapshot.Manager, id string) (repo.ObjectID, error) { head, tail := splitHeadTail(id) if len(head) == 0 { return repo.NullObjectID, fmt.Errorf("invalid object ID: %v", id) @@ -25,7 +25,7 @@ func parseObjectID(id string, r *repo.Repository) (repo.ObjectID, error) { return oid, nil } - dir := snapshot.Directory(r, oid) + dir := mgr.DirectoryEntry(oid) if err != nil { return repo.NullObjectID, err } diff --git a/snapshot/all_sources.go b/snapshot/all_sources.go index 6b23aae0a..06a6692b4 100644 --- a/snapshot/all_sources.go +++ b/snapshot/all_sources.go @@ -51,9 +51,7 @@ func (s *repositoryAllSources) Readdir() (fs.Entries, error) { return result, nil } -// AllSources returns fs.Directory that contains the list of all snapshot sources found in the repository. -func AllSources(r *repo.Repository) fs.Directory { - sm := NewManager(r) - - return &repositoryAllSources{repo: r, snapshotManager: sm} +// AllSourcesEntry returns fs.Directory that contains the list of all snapshot sources found in the repository. +func (m *Manager) AllSourcesEntry() fs.Directory { + return &repositoryAllSources{repo: m.repository, snapshotManager: m} } diff --git a/snapshot/repofs.go b/snapshot/repofs.go index 8b253874c..5d9ae9218 100644 --- a/snapshot/repofs.go +++ b/snapshot/repofs.go @@ -117,10 +117,10 @@ func withMetadata(r repo.ObjectReader, md *fs.EntryMetadata) fs.Reader { return &entryMetadataReadCloser{r, md} } -// Directory returns fs.Directory based on repository object with the specified ID. +// DirectoryEntry returns fs.Directory based on repository object with the specified ID. // The existence or validity of the directory object is not validated until its contents are read. -func Directory(r *repo.Repository, objectID repo.ObjectID) fs.Directory { - d := newRepoEntry(r, &dir.Entry{ +func (m *Manager) DirectoryEntry(objectID repo.ObjectID) fs.Directory { + d := newRepoEntry(m.repository, &dir.Entry{ EntryMetadata: fs.EntryMetadata{ Name: "/", Permissions: 0555,