summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-05-27 15:46:15 +0000
committerLibravatar GitHub <noreply@github.com>2024-05-27 17:46:15 +0200
commit1e7b32490dfdccddd04f46d4b0416b48d749d51b (patch)
tree62a11365933a5a11e0800af64cbdf9172e5e6e7a /vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go
parent[chore] Small styling + link issues (#2933) (diff)
downloadgotosocial-1e7b32490dfdccddd04f46d4b0416b48d749d51b.tar.xz
[experiment] add alternative wasm sqlite3 implementation available via build-tag (#2863)
This allows for building GoToSocial with [SQLite transpiled to WASM](https://github.com/ncruces/go-sqlite3) and accessed through [Wazero](https://wazero.io/).
Diffstat (limited to 'vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go')
-rw-r--r--vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go b/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go
new file mode 100644
index 000000000..940a79a8d
--- /dev/null
+++ b/vendor/github.com/tetratelabs/wazero/internal/filecache/file_cache.go
@@ -0,0 +1,76 @@
+package filecache
+
+import (
+ "encoding/hex"
+ "errors"
+ "io"
+ "os"
+ "path"
+ "path/filepath"
+)
+
+// New returns a new Cache implemented by fileCache.
+func New(dir string) Cache {
+ return newFileCache(dir)
+}
+
+func newFileCache(dir string) *fileCache {
+ return &fileCache{dirPath: dir}
+}
+
+// fileCache persists compiled functions into dirPath.
+//
+// Note: this can be expanded to do binary signing/verification, set TTL on each entry, etc.
+type fileCache struct {
+ dirPath string
+}
+
+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) {
+ 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 {
+ return f, true, nil
+ }
+}
+
+func (fc *fileCache) Add(key Key, content io.Reader) (err error) {
+ path := fc.path(key)
+ 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())
+ }
+ }()
+ if _, err = io.Copy(file, content); err != nil {
+ return
+ }
+ if err = file.Sync(); err != nil {
+ return
+ }
+ if err = file.Close(); err != nil {
+ return
+ }
+ err = os.Rename(file.Name(), path)
+ return
+}
+
+func (fc *fileCache) Delete(key Key) (err error) {
+ err = os.Remove(fc.path(key))
+ if errors.Is(err, os.ErrNotExist) {
+ err = nil
+ }
+ return
+}