From bb593077b3a01a9528c3467d6519219b0ffd83b9 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 5 Mar 2024 10:48:40 +0100 Subject: [PATCH] store: new function to retrieve underlying file add a new function to retrieve the underlying big data file for the layers store. Signed-off-by: Giuseppe Scrivano --- layers.go | 12 ++++++++++++ store.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/layers.go b/layers.go index f1325262b5..bad4701fa3 100644 --- a/layers.go +++ b/layers.go @@ -1798,6 +1798,18 @@ func (r *layerStore) BigData(id, key string) (io.ReadCloser, error) { return os.Open(r.datapath(layer.ID, key)) } +// Requires startReading or startWriting. +func (r *layerStore) BigDataFilePath(id, key string) (string, error) { + if key == "" { + return "", fmt.Errorf("can't retrieve layer big data value for empty name: %w", ErrInvalidBigDataName) + } + layer, ok := r.lookup(id) + if !ok { + return "", fmt.Errorf("locating layer with ID %q: %w", id, ErrLayerUnknown) + } + return r.datapath(layer.ID, key), nil +} + // Requires startWriting. func (r *layerStore) SetBigData(id, key string, data io.Reader) error { if !r.lockfile.IsReadWrite() { diff --git a/store.go b/store.go index c6f1251893..a87aaedb04 100644 --- a/store.go +++ b/store.go @@ -127,6 +127,10 @@ type roLayerBigDataStore interface { // with this ID. BigData(id, key string) (io.ReadCloser, error) + // BigDataFilePath returns the path to the underlying file, if the big data + // is backed by a file. Otherwise it returns the empty string. + BigDataFilePath(id, key string) (string, error) + // BigDataNames() returns a list of the names of previously-stored pieces of // data. BigDataNames(id string) ([]string, error) @@ -440,6 +444,10 @@ type Store interface { // associated with a layer. LayerBigData(id, key string) (io.ReadCloser, error) + // LayerBigDataFile retrieves the backing file for the layers big data, + // when used. + LayerBigDataFilePath(id, key string) (string, error) + // SetLayerBigData stores a (possibly large) chunk of named data // associated with a layer. SetLayerBigData(id, key string, data io.Reader) error @@ -2105,6 +2113,28 @@ func (s *store) LayerBigData(id, key string) (io.ReadCloser, error) { return nil, fmt.Errorf("locating layer with ID %q: %w", id, ErrLayerUnknown) } +// LayerBigDataFile retrieves the backing file for the layers big data, +// when used. +func (s *store) LayerBigDataFilePath(id, key string) (string, error) { + foundLayer := false + if res, done, err := readAllLayerStores(s, func(store roLayerStore) (string, bool, error) { + path, err := store.BigDataFilePath(id, key) + if err == nil { + return path, true, nil + } + if store.Exists(id) { + foundLayer = true + } + return "", false, nil + }); done { + return res, err + } + if foundLayer { + return "", fmt.Errorf("locating item named %q for layer with ID %q: %w", key, id, os.ErrNotExist) + } + return "", fmt.Errorf("locating layer with ID %q: %w", id, ErrLayerUnknown) +} + // SetLayerBigData stores a (possibly large) chunk of named data // associated with a layer. func (s *store) SetLayerBigData(id, key string, data io.Reader) error {