navidrome/db/pglite/setup.go
Deluan b5dc2e43b6 fix(pglite): mount a dedicated scratch dir as the guest /tmp
The whole pglite data dir was mounted as the guest's /tmp, which also
exposed the cluster twice. The guest only needs its password file and
the shared-memory stand-ins there, so <DataFolder>/pglite/tmp is mounted
instead. The cluster and /dev mounts are unchanged.
2026-09-05 01:34:35 -04:00

33 lines
1.1 KiB
Go

package pglite
import (
"crypto/rand"
"fmt"
"os"
"path/filepath"
)
// setupDataDir prepares the host directories the guest mounts (cluster, /dev/urandom stand-in,
// initdb password file) and reports whether initdb still has to run.
func setupDataDir(dataDir string) (fresh bool, err error) {
for _, dir := range []string{"pgdata", "dev", filepath.Join("tmp", "pglite")} {
if err := os.MkdirAll(filepath.Join(dataDir, dir), 0o700); err != nil {
return false, fmt.Errorf("creating %s: %w", dir, err)
}
}
urandom := filepath.Join(dataDir, "dev", "urandom")
if !fileExists(urandom) {
seed := make([]byte, 256)
if _, err := rand.Read(seed); err != nil {
return false, fmt.Errorf("generating random bytes: %w", err)
}
if err := os.WriteFile(urandom, seed, 0o600); err != nil {
return false, fmt.Errorf("writing urandom: %w", err)
}
}
// initdb's --pwfile; the bridge never checks it.
if err := os.WriteFile(filepath.Join(dataDir, "tmp", "pglite", "password"), []byte("password\n"), 0o600); err != nil {
return false, fmt.Errorf("writing password file: %w", err)
}
return !fileExists(filepath.Join(dataDir, "pgdata", "PG_VERSION")), nil
}