accounts/keystore: canonicalize path to temp directory

This commit is contained in:
Martin Holst Swende 2017-11-30 10:00:27 +01:00
parent abd14b6ddc
commit eaa880ad28
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
4 changed files with 44 additions and 31 deletions

View file

@ -51,6 +51,28 @@ var (
} }
) )
// newTempDir returns the name of new temporary folder (not created yet)
func newTempDir() (string, error) {
// On OSX, there's a problem
// https://stackoverflow.com/questions/45122459/docker-mounts-denied-the-paths-are-not-shared-from-os-x-and-are-not-known/45123074#45123074
//
// > /var in macOS is a symbolic link into /private.
//
// And when creating a tempdir, it returns a path into '/var/folders...'.
// However, if we start watching that directory, the notify-events will contain the
// canonical paths, and thus e.g. deleted/updated files won't match our existing files.
// TLDR; we need to use the canonical path, which we obtain via EvalSymlinks
rand.Seed(time.Now().UnixNano())
tmpdir, err := filepath.EvalSymlinks(os.TempDir())
if err != nil {
return "", err
}
return filepath.Join(tmpdir, fmt.Sprintf("eth-keystore-watch-test-%d-%d", os.Getpid(), rand.Int())), nil
}
func TestWatchNewFile(t *testing.T) { func TestWatchNewFile(t *testing.T) {
t.Parallel() t.Parallel()
@ -95,8 +117,11 @@ func TestWatchNoDir(t *testing.T) {
t.Parallel() t.Parallel()
// Create ks but not the directory that it watches. // Create ks but not the directory that it watches.
rand.Seed(time.Now().UnixNano()) dir, err := newTempDir()
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watch-test-%d-%d", os.Getpid(), rand.Int())) if err != nil {
t.Fatal(err)
}
ks := NewKeyStore(dir, LightScryptN, LightScryptP) ks := NewKeyStore(dir, LightScryptN, LightScryptP)
list := ks.Accounts() list := ks.Accounts()
@ -320,9 +345,11 @@ func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
func TestUpdatedKeyfileContents(t *testing.T) { func TestUpdatedKeyfileContents(t *testing.T) {
t.Parallel() t.Parallel()
// Create a temporary kesytore to test with dir, err := newTempDir()
rand.Seed(time.Now().UnixNano())
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watch-test-%d-%d", os.Getpid(), rand.Int())) if err != nil {
t.Fatal(err)
}
ks := NewKeyStore(dir, LightScryptN, LightScryptP) ks := NewKeyStore(dir, LightScryptN, LightScryptP)
list := ks.Accounts() list := ks.Accounts()

View file

@ -26,6 +26,8 @@ import (
"testing" "testing"
"time" "time"
"path/filepath"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
@ -375,7 +377,15 @@ func checkEvents(t *testing.T, want []walletEvent, have []walletEvent) {
} }
func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) { func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) {
d, err := ioutil.TempDir("", "eth-keystore-test") var (
d string
err error
)
d, err = ioutil.TempDir("", "eth-keystore-test")
if err != nil {
t.Fatal(err)
}
d, err = filepath.EvalSymlinks(d)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -75,36 +75,12 @@ func (w *watcher) loop() {
w.running = true w.running = true
w.ac.mu.Unlock() w.ac.mu.Unlock()
// Wait for file system events and reload.
// When an event occurs, the reload call is delayed a bit so that
// multiple events arriving quickly only cause a single reload.
//var (
//debounceDuration = 500 * time.Millisecond
//rescanTriggered = false
//debounce = time.NewTimer(0)
//)
// Ignore initial trigger
//if !debounce.Stop() {
// <-debounce.C
//}
//defer debounce.Stop()
for { for {
select { select {
case <-w.quit: case <-w.quit:
return return
case ei := <-w.ev: case ei := <-w.ev:
// fmt.Printf("Event: %v\n", ei)
ei.Path()
w.ac.checkFile(ei.Path()) w.ac.checkFile(ei.Path())
// w.ac.scanAccounts()
// Trigger the scan (with delay), if not already triggered
// if !rescanTriggered {
// debounce.Reset(debounceDuration)
// rescanTriggered = true
// }
// case <-debounce.C:
// w.ac.scanAccounts()
// rescanTriggered = false
} }
} }
} }