Skip to content

Commit

Permalink
ensure directory before writing file
Browse files Browse the repository at this point in the history
  • Loading branch information
jancajthaml authored May 17, 2020
1 parent 4d37ac4 commit 789e954
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion storage_encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions storage_plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 789e954

Please sign in to comment.