Skip to content

Commit

Permalink
feat: add path validation on database
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Mar 24, 2024
1 parent 20fa1bd commit bf8203a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/infra/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"encoding/gob"
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/cgi-fr/silo/pkg/silo"
"github.com/cockroachdb/pebble"
Expand Down Expand Up @@ -188,6 +191,10 @@ func (b Backend) Close() error {
}

func NewBackend(path string) (Backend, error) {
if err := checkDirectory(path); err != nil {
return Backend{}, fmt.Errorf("unable to open database %v : %w", path, err)
}

database, err := pebble.Open(path, &pebble.Options{Logger: BackendLogger{}}) //nolint:exhaustruct
if err != nil {
return Backend{}, fmt.Errorf("unable to open database %v : %w", path, err)
Expand All @@ -205,3 +212,38 @@ func (l BackendLogger) Infof(format string, args ...interface{}) {
func (l BackendLogger) Fatalf(format string, args ...interface{}) {
log.Error().Msgf(format, args...)
}

var ErrPathIsNotValid = errors.New("path is not valid")

func checkDirectory(path string) error {
// if path does not exists => ok
if stats, err := os.Stat(path); os.IsNotExist(err) {
return nil
} else if !stats.IsDir() {
// if path exists but is file => ko
return fmt.Errorf("%w '%s'", ErrPathIsNotValid, path)
}

dir, err := os.Open(path)
if err != nil {
return fmt.Errorf("%w", err)
}
defer dir.Close()

// if path is empty directory => ok
if _, err := dir.Readdirnames(1); errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return fmt.Errorf("%w", err)
}

// if path is not empty but CURRENT file does not exists => ko
if stats, err := os.Stat(filepath.Join(path, "CURRENT")); os.IsNotExist(err) {
return fmt.Errorf("%w '%s'", ErrPathIsNotValid, path)
} else if stats.IsDir() {
// if CURRENT exists but is a directory => ko
return fmt.Errorf("%w '%s'", ErrPathIsNotValid, path)
}

return nil
}

0 comments on commit bf8203a

Please sign in to comment.