Skip to content

Commit

Permalink
feat(FileStore): CreateTemp accepts pattern used for file name
Browse files Browse the repository at this point in the history
- closes #583
  • Loading branch information
frrist committed Jul 26, 2021
1 parent 46b5224 commit 49a0bdf
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
9 changes: 7 additions & 2 deletions filestore/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ func (fs fileStore) Delete(p Path) error {
return os.Remove(full)
}

func (fs fileStore) CreateTemp() (File, error) {
f, err := ioutil.TempFile(fs.base, "fstmp")
func (fs fileStore) CreateTemp(pattern string) (File, error) {
if pattern == "" {
pattern = "fstmp"
} else {
pattern = fmt.Sprintf("fstmp_%s", pattern)
}
f, err := ioutil.TempFile(fs.base, pattern)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion filestore/filestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ func Test_CreateFile(t *testing.T) {
func Test_CreateTempFile(t *testing.T) {
store, err := NewLocalFileStore(baseDir)
require.NoError(t, err)
file, err := store.CreateTemp()
file, err := store.CreateTemp("pattern")
require.NoError(t, err)
require.Contains(t, file.Path(), "fstmp_pattern")
defer func() {
err := store.Delete(file.Path())
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion filestore/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ type FileStore interface {
Store(p Path, f File) (Path, error)
Delete(p Path) error

CreateTemp() (File, error)
CreateTemp(pattern string) (File, error)
}
2 changes: 1 addition & 1 deletion shared_testutil/test_filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (fs *TestFileStore) Delete(p filestore.Path) error {
}

// CreateTemp will create a temporary file from the provided set of temporary files
func (fs *TestFileStore) CreateTemp() (filestore.File, error) {
func (fs *TestFileStore) CreateTemp(pattern string) (filestore.File, error) {
if len(fs.availableTempFiles) == 0 {
return nil, TestErrTempFile
}
Expand Down
2 changes: 1 addition & 1 deletion storagemarket/impl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (p *Provider) ImportDataForDeal(ctx context.Context, propCid cid.Cid, data
return xerrors.Errorf("failed getting deal %s: %w", propCid, err)
}

tempfi, err := p.fs.CreateTemp()
tempfi, err := p.fs.CreateTemp(propCid.String())
if err != nil {
return xerrors.Errorf("failed to create temp file for data import: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion storagemarket/impl/providerutils/providerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func GeneratePieceCommitmentWithMetadata(
payloadCid cid.Cid,
selector ipld.Node,
storeID *multistore.StoreID) (cid.Cid, abi.UnpaddedPieceSize, filestore.Path, error) {
metadataFile, err := fileStore.CreateTemp()
metadataFile, err := fileStore.CreateTemp(payloadCid.String())
if err != nil {
return cid.Cid{}, 0, "", err
}
Expand Down

0 comments on commit 49a0bdf

Please sign in to comment.