From 789e954e4584c3f350c2b98e170585a1dcd1310d Mon Sep 17 00:00:00 2001 From: Jan Cajthaml Date: Sun, 17 May 2020 21:04:49 +0200 Subject: [PATCH] ensure directory before writing file --- storage_encrypted.go | 8 +++++++- storage_plaintext.go | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/storage_encrypted.go b/storage_encrypted.go index f968eee..5b8ed9c 100644 --- a/storage_encrypted.go +++ b/storage_encrypted.go @@ -136,7 +136,7 @@ func (storage EncryptedStorage) ReadFileFully(path string) ([]byte, error) { // already exists func (storage EncryptedStorage) WriteFileExclusive(path string, data []byte) error { filename := filepath.Clean(storage.Root + "/" + path) - if err := os.MkdirAll(filename, 0600); err != nil { + if err := os.MkdirAll(filepath.Dir(filename), 0600); err != nil { return err } // FIXME inline @@ -163,6 +163,9 @@ func (storage EncryptedStorage) WriteFileExclusive(path string, data []byte) err // not exist func (storage EncryptedStorage) WriteFile(path string, data []byte) error { filename := filepath.Clean(storage.Root + "/" + path) + if err := os.MkdirAll(filepath.Dir(filename), 0600); err != nil { + return err + } // FIXME inline out, err := storage.Encrypt(data) if err != nil { @@ -187,6 +190,9 @@ func (storage EncryptedStorage) WriteFile(path string, data []byte) error { // not exist func (storage EncryptedStorage) AppendFile(path string, data []byte) error { filename := filepath.Clean(storage.Root + "/" + path) + if err := os.MkdirAll(filepath.Dir(filename), 0600); err != nil { + return err + } fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_TRUNC|syscall.O_NONBLOCK, 0600) if err != nil { return err diff --git a/storage_plaintext.go b/storage_plaintext.go index 27a4af3..c2ff0fb 100644 --- a/storage_plaintext.go +++ b/storage_plaintext.go @@ -91,7 +91,7 @@ func (storage PlaintextStorage) ReadFileFully(path string) ([]byte, error) { // already exists func (storage PlaintextStorage) WriteFileExclusive(path string, data []byte) error { filename := filepath.Clean(storage.Root + "/" + path) - if err := os.MkdirAll(filename, 0600); err != nil { + if err := os.MkdirAll(filepath.Dir(filename), 0600); err != nil { return err } fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_EXCL|syscall.O_NONBLOCK, 0600) @@ -113,6 +113,9 @@ func (storage PlaintextStorage) WriteFileExclusive(path string, data []byte) err // not exist func (storage PlaintextStorage) WriteFile(path string, data []byte) error { filename := filepath.Clean(storage.Root + "/" + path) + if err := os.MkdirAll(filepath.Dir(filename), 0600); err != nil { + return err + } fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_TRUNC|syscall.O_NONBLOCK, 0600) if err != nil { return err @@ -132,7 +135,7 @@ func (storage PlaintextStorage) WriteFile(path string, data []byte) error { // not exist func (storage PlaintextStorage) AppendFile(path string, data []byte) error { filename := filepath.Clean(storage.Root + "/" + path) - if err := os.MkdirAll(filename, 0600); err != nil { + if err := os.MkdirAll(filepath.Dir(filename), 0600); err != nil { return err } fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_APPEND|syscall.O_NONBLOCK, 0600)