mirror of
https://github.com/dolthub/dolt.git
synced 2026-03-16 23:53:17 -05:00
added temp file method to utils/filesys
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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] == '.' {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user