Browse Source

Create nested directories as needed to store keys

pull/1782/head
Ethan Frey 8 years ago
parent
commit
76ace96925
2 changed files with 19 additions and 10 deletions
  1. +2
    -2
      storage/filestorage/main.go
  2. +17
    -8
      storage/filestorage/main_test.go

+ 2
- 2
storage/filestorage/main.go View File

@ -35,8 +35,8 @@ type FileStore struct {
// dir should be an absolute path of a directory owner by this user. It will // dir should be an absolute path of a directory owner by this user. It will
// be created if it doesn't exist already. // be created if it doesn't exist already.
func New(dir string) FileStore { func New(dir string) FileStore {
err := os.Mkdir(dir, dirPerm)
if err != nil && !os.IsExist(err) {
err := os.MkdirAll(dir, dirPerm)
if err != nil {
panic(err) panic(err)
} }
return FileStore{dir} return FileStore{dir}


+ 17
- 8
storage/filestorage/main_test.go View File

@ -83,15 +83,24 @@ func TestDirectoryHandling(t *testing.T) {
newDir := path.Join(os.TempDir(), "file-test-dir") newDir := path.Join(os.TempDir(), "file-test-dir")
_, err := os.Open(newDir) _, err := os.Open(newDir)
assert.True(os.IsNotExist(err)) assert.True(os.IsNotExist(err))
defer os.RemoveAll(newDir)
// now, check with two levels deep....
parentDir := path.Join(os.TempDir(), "missing-dir")
nestedDir := path.Join(parentDir, "lots", "of", "levels", "here")
_, err = os.Open(parentDir)
assert.True(os.IsNotExist(err))
defer os.RemoveAll(parentDir)
// create a new storage, and verify it creates the directory with good permissions // create a new storage, and verify it creates the directory with good permissions
New(newDir)
defer os.RemoveAll(newDir)
d, err := os.Open(newDir)
require.Nil(err)
defer d.Close()
for _, dir := range []string{newDir, nestedDir, newDir} {
New(dir)
d, err := os.Open(dir)
require.Nil(err)
defer d.Close()
stat, err := d.Stat()
require.Nil(err)
assert.Equal(dirPerm, stat.Mode()&os.ModePerm)
stat, err := d.Stat()
require.Nil(err)
assert.Equal(dirPerm, stat.Mode()&os.ModePerm)
}
} }

Loading…
Cancel
Save