added temp file method to utils/filesys

This commit is contained in:
Andy Arthur
2022-07-20 17:01:51 -07:00
parent db7515ca12
commit 226da75db3
4 changed files with 30 additions and 0 deletions

View File

@@ -71,6 +71,9 @@ type WritableFS interface {
// MoveFile will move a file from the srcPath in the filesystem to the destPath
MoveFile(srcPath, destPath string) error
// TempDir returns the path of a new temporary directory.
TempDir() string
}
// FSIterCB specifies the signature of the function that will be called for every item found while iterating.

View File

@@ -103,6 +103,20 @@ func TestFilesystems(t *testing.T) {
dataRead, err = fs.ReadFile(movedFilePath)
require.NoError(t, err)
require.Equal(t, dataRead, data)
tmp := fs.TempDir()
require.NotEmpty(t, tmp)
fp2 := filepath.Join(tmp, "data.txt")
wrc, err := fs.OpenForWrite(fp2, os.ModePerm)
require.NoError(t, err)
require.NoError(t, wrc.Close())
// Test writing/reading random data to tmp file
err = fs.WriteFile(fp2, data)
require.NoError(t, err)
dataRead, err = fs.ReadFile(fp2)
require.NoError(t, err)
require.Equal(t, dataRead, data)
})
}
}

View File

@@ -16,8 +16,10 @@ package filesys
import (
"bytes"
"encoding/base32"
"errors"
"io"
"math/rand"
"os"
"path/filepath"
"strings"
@@ -559,6 +561,13 @@ func (fs *InMemFS) LastModified(path string) (t time.Time, exists bool) {
return time.Time{}, false
}
func (fs *InMemFS) TempDir() string {
buf := make([]byte, 16)
rand.Read(buf)
s := base32.HexEncoding.EncodeToString(buf)
return "/var/folders/gc/" + s + "/T/"
}
func (fs *InMemFS) pathToNative(path string) string {
if len(path) >= 1 {
if path[0] == '.' {

View File

@@ -318,3 +318,7 @@ func (fs *localFS) LastModified(path string) (t time.Time, exists bool) {
return stat.ModTime(), true
}
func (fs *localFS) TempDir() string {
return os.TempDir()
}