From 072627bd98c490a0c41100be0e3ce8b4d20791da Mon Sep 17 00:00:00 2001 From: Andrew Gillis Date: Fri, 3 Nov 2023 09:13:10 -0700 Subject: [PATCH] Fix use of path where filepath is needed (#210) Need to use the filepath package when manipulating filesystem paths. Close open files there were no getting closed. --- blob/local_store.go | 36 +++++++++++++++++----------------- integration/ribs/ribs_store.go | 33 +++++++++++++++++-------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/blob/local_store.go b/blob/local_store.go index df8acfd..8daa488 100644 --- a/blob/local_store.go +++ b/blob/local_store.go @@ -5,7 +5,7 @@ import ( "errors" "io" "os" - "path" + "path/filepath" ) var _ Store = (*LocalStore)(nil) @@ -51,7 +51,7 @@ func (l *LocalStore) Put(_ context.Context, reader io.ReadCloser) (*Descriptor, os.Remove(dest.Name()) return nil, err } - if err = os.Rename(dest.Name(), path.Join(l.dir, id.String()+".bin")); err != nil { + if err = os.Rename(dest.Name(), filepath.Join(l.dir, id.String()+".bin")); err != nil { return nil, err } stat, err := dest.Stat() @@ -68,29 +68,29 @@ func (l *LocalStore) Put(_ context.Context, reader io.ReadCloser) (*Descriptor, // Get Retrieves the content of blob. // If no blob is found for the given id, ErrBlobNotFound is returned. func (l *LocalStore) Get(_ context.Context, id ID) (io.ReadSeekCloser, error) { - switch blob, err := os.Open(path.Join(l.dir, id.String()+".bin")); { - case err == nil: - return blob, nil - case errors.Is(err, os.ErrNotExist): - return nil, ErrBlobNotFound - default: + blob, err := os.Open(filepath.Join(l.dir, id.String()+".bin")) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, ErrBlobNotFound + } return nil, err } + return blob, nil } // Describe gets the description of the blob for the given id. // If no blob is found for the given id, ErrBlobNotFound is returned. func (l *LocalStore) Describe(ctx context.Context, id ID) (*Descriptor, error) { - switch stat, err := os.Stat(path.Join(l.dir, id.String()+".bin")); { - case err == nil: - return &Descriptor{ - ID: id, - Size: uint64(stat.Size()), - ModificationTime: stat.ModTime(), - }, nil - case errors.Is(err, os.ErrNotExist): - return nil, ErrBlobNotFound - default: + stat, err := os.Stat(filepath.Join(l.dir, id.String()+".bin")) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, ErrBlobNotFound + } return nil, err } + return &Descriptor{ + ID: id, + Size: uint64(stat.Size()), + ModificationTime: stat.ModTime(), + }, nil } diff --git a/integration/ribs/ribs_store.go b/integration/ribs/ribs_store.go index 78d149a..1389e38 100644 --- a/integration/ribs/ribs_store.go +++ b/integration/ribs/ribs_store.go @@ -8,7 +8,7 @@ import ( "fmt" "io" "os" - "path" + "path/filepath" "time" "github.com/filecoin-project/lotus/chain/types" @@ -56,12 +56,12 @@ type ( // NewRibsStore instantiates a new experimental RIBS store. func NewRibsStore(dir string, ks types.KeyStore) (*RibsStore, error) { - dir = path.Clean(dir) - rbdealDir := path.Join(dir, "rbdeal") + dir = filepath.Clean(dir) + rbdealDir := filepath.Join(dir, "rbdeal") if err := os.Mkdir(rbdealDir, 0750); err != nil && !errors.Is(err, os.ErrExist) { return nil, fmt.Errorf("failed to create RIBS deal directory: %w", err) } - indexDir := path.Join(dir, "index") + indexDir := filepath.Join(dir, "index") if err := os.Mkdir(indexDir, 0750); err != nil && !errors.Is(err, os.ErrExist) { return nil, fmt.Errorf("failed to create RIBS internal directory: %w", err) } @@ -144,11 +144,12 @@ SplitLoop: }, Chunks: chunkCids, } - index, err := os.Create(path.Join(r.indexDir, id.String())) + index, err := os.Create(filepath.Join(r.indexDir, id.String())) if err != nil { return nil, err } - if err := json.NewEncoder(index).Encode(storedBlob); err != nil { + defer index.Close() + if err = json.NewEncoder(index).Encode(storedBlob); err != nil { return nil, err } return storedBlob.Descriptor, nil @@ -176,17 +177,19 @@ func (r *RibsStore) Describe(ctx context.Context, id blob.ID) (*blob.Descriptor, } func (r *RibsStore) describeRibsStoredBlob(_ context.Context, id blob.ID) (*ribsStoredBlob, error) { - switch index, err := os.Open(path.Join(r.indexDir, id.String())); { - case err == nil: - var storedBlob ribsStoredBlob - err := json.NewDecoder(index).Decode(&storedBlob) - // TODO: populate descriptor status with Filecoin chain data about the stored blob. - return &storedBlob, err - case errors.Is(err, os.ErrNotExist): - return nil, blob.ErrBlobNotFound - default: + index, err := os.Open(filepath.Join(r.indexDir, id.String())) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, blob.ErrBlobNotFound + } return nil, err } + defer index.Close() + + var storedBlob ribsStoredBlob + err = json.NewDecoder(index).Decode(&storedBlob) + // TODO: populate descriptor status with Filecoin chain data about the stored blob. + return &storedBlob, err } func (r *RibsStore) Shutdown(_ context.Context) error {