Skip to content

Commit

Permalink
filecache: use os.CreateTemp to create files (tetratelabs#2088)
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel authored and evacchi committed Feb 28, 2024
1 parent 3a6874f commit a7b0013
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 56 deletions.
43 changes: 6 additions & 37 deletions internal/filecache/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"os"
"path"
"sync"
"path/filepath"
)

// New returns a new Cache implemented by fileCache.
Expand All @@ -23,64 +23,37 @@ func newFileCache(dir string) *fileCache {
// Note: this can be expanded to do binary signing/verification, set TTL on each entry, etc.
type fileCache struct {
dirPath string
mux sync.RWMutex
}

type fileReadCloser struct {
*os.File
fc *fileCache
}

func (fc *fileCache) path(key Key) string {
return path.Join(fc.dirPath, hex.EncodeToString(key[:]))
}

func (fc *fileCache) Get(key Key) (content io.ReadCloser, ok bool, err error) {
// TODO: take lock per key for more efficiency vs the complexity of impl.
fc.mux.RLock()
unlock := fc.mux.RUnlock
defer func() {
if unlock != nil {
unlock()
}
}()

f, err := os.Open(fc.path(key))
if errors.Is(err, os.ErrNotExist) {
return nil, false, nil
} else if err != nil {
return nil, false, err
} else {
// Unlock is done inside the content.Close() at the call site.
unlock = nil
return &fileReadCloser{File: f, fc: fc}, true, nil
return f, true, nil
}
}

// Close wraps the os.File Close to release the read lock on fileCache.
func (f *fileReadCloser) Close() (err error) {
defer f.fc.mux.RUnlock()
err = f.File.Close()
return
}

func (fc *fileCache) Add(key Key, content io.Reader) (err error) {
// TODO: take lock per key for more efficiency vs the complexity of impl.
fc.mux.Lock()
defer fc.mux.Unlock()

// Use rename for an atomic write
path := fc.path(key)
file, err := os.Create(path + ".tmp")
dirPath, fileName := filepath.Split(path)

file, err := os.CreateTemp(dirPath, fileName+".*.tmp")
if err != nil {
return
}
defer func() {
file.Close()
if err != nil {
_ = os.Remove(file.Name())
}
}()
defer file.Close()
if _, err = io.Copy(file, content); err != nil {
return
}
Expand All @@ -95,10 +68,6 @@ func (fc *fileCache) Add(key Key, content io.Reader) (err error) {
}

func (fc *fileCache) Delete(key Key) (err error) {
// TODO: take lock per key for more efficiency vs the complexity of impl.
fc.mux.Lock()
defer fc.mux.Unlock()

err = os.Remove(fc.path(key))
if errors.Is(err, os.ErrNotExist) {
err = nil
Expand Down
19 changes: 0 additions & 19 deletions internal/filecache/file_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@ import (
"github.com/tetratelabs/wazero/internal/testing/require"
)

func TestFileReadCloser_Close(t *testing.T) {
fc := newFileCache(t.TempDir())
key := Key{1, 2, 3}

err := fc.Add(key, bytes.NewReader([]byte{1, 2, 3, 4}))
require.NoError(t, err)

c, ok, err := fc.Get(key)
require.NoError(t, err)
require.True(t, ok)

// At this point, file is not closed, therefore TryLock should fail.
require.False(t, fc.mux.TryLock())

// Close, and then TryLock should succeed this time.
require.NoError(t, c.Close())
require.True(t, fc.mux.TryLock())
}

func TestFileCache_Add(t *testing.T) {
fc := newFileCache(t.TempDir())

Expand Down

0 comments on commit a7b0013

Please sign in to comment.