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

@ -316,7 +316,7 @@ func (ac *accountCache) scanAccounts() error {
)
for _, p := range creates.List() {
if a := readAccount(p.(string),buf); a != nil {
if a := readAccount(p.(string), buf); a != nil {
ac.add(*a)
}
}

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) {
t.Parallel()
@ -95,8 +117,11 @@ func TestWatchNoDir(t *testing.T) {
t.Parallel()
// Create ks but not the directory that it watches.
rand.Seed(time.Now().UnixNano())
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watch-test-%d-%d", os.Getpid(), rand.Int()))
dir, err := newTempDir()
if err != nil {
t.Fatal(err)
}
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
list := ks.Accounts()
@ -320,9 +345,11 @@ func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
func TestUpdatedKeyfileContents(t *testing.T) {
t.Parallel()
// Create a temporary kesytore to test with
rand.Seed(time.Now().UnixNano())
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watch-test-%d-%d", os.Getpid(), rand.Int()))
dir, err := newTempDir()
if err != nil {
t.Fatal(err)
}
ks := NewKeyStore(dir, LightScryptN, LightScryptP)
list := ks.Accounts()

View file

@ -26,6 +26,8 @@ import (
"testing"
"time"
"path/filepath"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"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) {
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 {
t.Fatal(err)
}

View file

@ -75,36 +75,12 @@ func (w *watcher) loop() {
w.running = true
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 {
select {
case <-w.quit:
return
case ei := <-w.ev:
// fmt.Printf("Event: %v\n", ei)
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
}
}
}