Skip to content

Commit

Permalink
Require explicit data directory
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Sep 19, 2022
1 parent d1ffde3 commit 4a8dba1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 4 additions & 10 deletions cmd/litefs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func (m *Main) Close() (err error) {

func (m *Main) Run(ctx context.Context) (err error) {
if m.Config.MountDir == "" {
return fmt.Errorf("mount path required")
return fmt.Errorf("mount directory required")
} else if m.Config.DataDir == "" {
return fmt.Errorf("data directory required")
}

// Enforce exactly one lease mode.
Expand Down Expand Up @@ -275,15 +277,7 @@ func (m *Main) initConsul(ctx context.Context) (err error) {
}

func (m *Main) initStore(ctx context.Context) error {
// Load the data directory from the config.
// Default to use a hidden directory next to the mount, if not specified.
dataDir := m.Config.DataDir
if dataDir == "" {
dir, file := filepath.Split(m.Config.MountDir)
dataDir = filepath.Join(dir, "."+file)
}

m.Store = litefs.NewStore(dataDir, m.Config.Candidate)
m.Store = litefs.NewStore(m.Config.DataDir, m.Config.Candidate)
m.Store.StrictVerify = m.Config.StrictVerify
m.Store.RetentionDuration = m.Config.Retention.Duration
m.Store.RetentionMonitorInterval = m.Config.Retention.MonitorInterval
Expand Down
12 changes: 9 additions & 3 deletions cmd/litefs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,18 @@ func TestConfigExample(t *testing.T) {
}
}

func newMain(tb testing.TB, mountDir string, peer *main.Main) *main.Main {
func newMain(tb testing.TB, dir string, peer *main.Main) *main.Main {
tb.Helper()

tb.Cleanup(func() {
if err := os.RemoveAll(mountDir); err != nil {
if err := os.RemoveAll(dir); err != nil {
tb.Fatalf("cannot remove temp directory: %s", err)
}
})

m := main.NewMain()
m.Config.MountDir = mountDir
m.Config.MountDir = filepath.Join(dir, "mnt")
m.Config.DataDir = filepath.Join(dir, "data")
m.Config.Debug = *debug
m.Config.StrictVerify = true
m.Config.HTTP.Addr = ":0"
Expand All @@ -545,6 +546,11 @@ func newMain(tb testing.TB, mountDir string, peer *main.Main) *main.Main {
LockDelay: 1 * time.Second,
}

// Ensure mount directory exists.
if err := os.MkdirAll(m.Config.MountDir, 0777); err != nil {
tb.Fatal(err)
}

// Use peer's consul key, if passed in.
if peer != nil && peer.Config.Consul != nil {
m.Config.Consul.Key = peer.Config.Consul.Key
Expand Down

0 comments on commit 4a8dba1

Please sign in to comment.