From 8ff8ec0a3650e658c08dd2e2e46ca4e8dbe24fe3 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Wed, 1 Jan 2020 12:50:39 -0800 Subject: [PATCH] snapshotfs: expose functions to round-trip between fs.Entry and DirEntry --- snapshot/snapshotfs/repofs.go | 13 +++++++++---- snapshot/snapshotfs/source_snapshots.go | 2 +- snapshot/snapshotfs/upload.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/snapshot/snapshotfs/repofs.go b/snapshot/snapshotfs/repofs.go index e6bd4dfae..6d3d81173 100644 --- a/snapshot/snapshotfs/repofs.go +++ b/snapshot/snapshotfs/repofs.go @@ -62,6 +62,10 @@ func (e *repositoryEntry) Owner() fs.OwnerInfo { } } +func (e *repositoryEntry) DirEntry() *snapshot.DirEntry { + return e.metadata +} + type repositoryDirectory struct { repositoryEntry summary *fs.DirectorySummary @@ -97,7 +101,7 @@ func (rd *repositoryDirectory) Readdir(ctx context.Context) (fs.Entries, error) entries := make(fs.Entries, len(metadata)) for i, m := range metadata { - entries[i], err = newRepoEntry(rd.repo, m) + entries[i], err = EntryFromDirEntry(rd.repo, m) if err != nil { return nil, errors.Wrapf(err, "error parsing entry %v", m) } @@ -133,7 +137,8 @@ func (rsl *repositorySymlink) Readlink(ctx context.Context) (string, error) { return string(b), nil } -func newRepoEntry(r *repo.Repository, md *snapshot.DirEntry) (fs.Entry, error) { +// EntryFromDirEntry returns a filesystem entry based on the directory entry. +func EntryFromDirEntry(r *repo.Repository, md *snapshot.DirEntry) (fs.Entry, error) { re := repositoryEntry{ metadata: md, repo: r, @@ -175,7 +180,7 @@ func withFileInfo(r object.Reader, e fs.Entry) fs.Reader { // 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 DirectoryEntry(rep *repo.Repository, objectID object.ID, dirSummary *fs.DirectorySummary) fs.Directory { - d, _ := newRepoEntry(rep, &snapshot.DirEntry{ + d, _ := EntryFromDirEntry(rep, &snapshot.DirEntry{ Name: "/", Permissions: 0555, Type: snapshot.EntryTypeDirectory, @@ -193,7 +198,7 @@ func SnapshotRoot(rep *repo.Repository, man *snapshot.Manifest) (fs.Entry, error return nil, errors.New("manifest root object ID") } - return newRepoEntry(rep, man.RootEntry) + return EntryFromDirEntry(rep, man.RootEntry) } var _ fs.Directory = &repositoryDirectory{} diff --git a/snapshot/snapshotfs/source_snapshots.go b/snapshot/snapshotfs/source_snapshots.go index 45d284c99..57ec0b9f7 100644 --- a/snapshot/snapshotfs/source_snapshots.go +++ b/snapshot/snapshotfs/source_snapshots.go @@ -86,7 +86,7 @@ func (s *sourceSnapshots) Readdir(ctx context.Context) (fs.Entries, error) { de.DirSummary = m.RootEntry.DirSummary } - e, err := newRepoEntry(s.rep, de) + e, err := EntryFromDirEntry(s.rep, de) if err != nil { return nil, errors.Wrap(err, "unable to create entry") } diff --git a/snapshot/snapshotfs/upload.go b/snapshot/snapshotfs/upload.go index 66fcd3f30..cfe9d5a35 100644 --- a/snapshot/snapshotfs/upload.go +++ b/snapshot/snapshotfs/upload.go @@ -702,7 +702,7 @@ func (u *Uploader) maybeOpenDirectoryFromManifest(man *snapshot.Manifest) fs.Dir return nil } - ent, err := newRepoEntry(u.repo, man.RootEntry) + ent, err := EntryFromDirEntry(u.repo, man.RootEntry) if err != nil { log.Warningf("invalid previous manifest root entry %v: %v", man.RootEntry, err) return nil