{"record":{"id":"6e70ecc544ced129","repo":"benbjohnson/litestream","slug":"recreate-l0-directory-w","errorCode":null,"errorMessage":"recreate L0 directory: %w","messagePattern":"recreate L0 directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"db.go","lineNumber":1620,"sourceCode":"\t}\n\n\t// Check if database is behind replica\n\tif dbPos.TXID >= replicaInfo.MaxTXID {\n\t\treturn nil // Database is ahead or equal\n\t}\n\n\tdb.Logger.Info(\"detected database behind replica\",\n\t\t\"db_txid\", dbPos.TXID,\n\t\t\"replica_txid\", replicaInfo.MaxTXID)\n\n\t// Clear local L0 files\n\tl0Dir := db.LTXLevelDir(0)\n\tif err := os.RemoveAll(l0Dir); err != nil && !os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"remove L0 directory: %w\", err)\n\t}\n\tdb.invalidatePosCache()\n\tif err := internal.MkdirAll(l0Dir, db.dirInfo); err != nil {\n\t\treturn fmt.Errorf(\"recreate L0 directory: %w\", err)\n\t}\n\n\t// Fetch latest L0 LTX file from replica\n\tminTXID, maxTXID := replicaInfo.MinTXID, replicaInfo.MaxTXID\n\treader, err := db.Replica.Client.OpenLTXFile(ctx, 0, minTXID, maxTXID, 0, 0)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"open remote L0 file: %w\", err)\n\t}\n\tdefer func() { _ = reader.Close() }()\n\n\t// Write to temp file and atomically rename\n\tlocalPath := db.LTXPath(0, minTXID, maxTXID)\n\ttmpPath := localPath + \".tmp\"\n\n\ttmpFile, err := os.Create(tmpPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create temp L0 file: %w\", err)\n\t}","sourceCodeStart":1602,"sourceCodeEnd":1638,"githubUrl":"https://github.com/benbjohnson/litestream/blob/4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3/db.go#L1602-L1638","documentation":"After clearing the L0 directory, checkDatabaseBehindReplica recreates it with internal.MkdirAll using the configured directory mode. This error wraps a failure to recreate the level-0 LTX directory. Without it, Litestream cannot write the fetched LTX file locally.","triggerScenarios":"Immediately after a successful RemoveAll of the L0 dir, internal.MkdirAll(l0Dir, db.dirInfo) fails — parent directory missing/unwritable, or the directory was recreated by another process with restrictive permissions between the two calls.","commonSituations":"The database directory itself was deleted or moved while litestream runs; parent dir permissions changed (mkdir-permission-denied); disk full preventing directory creation; SELinux/AppArmor blocking mkdir in the data path.","solutions":["Ensure the database's parent directory exists and is writable by the litestream user (mkdir -p, chown).","Check disk space (df -h) and quota on the data volume.","Check SELinux/AppAudit denial logs (ausearch -m avc) if permissions look correct.","Compare with how litestream creates the dir at startup — if startup works but this fails, another process is interfering with the LTX directory."],"exampleFix":"// before: directory missing at runtime\n// after: ensure data dir exists before starting litestream\n$ install -d -o litestream -g litestream /var/lib/db\n$ systemctl start litestream","handlingStrategy":"validation","validationCode":"// Ensure parent dir exists and is writable before litestream starts\nif err := os.MkdirAll(dbDir, 0o755); err != nil {\n    log.Fatalf(\"cannot prepare db dir: %v\", err)\n}","typeGuard":"null","tryCatchPattern":"if err := internal.MkdirAll(l0Dir, db.dirInfo); err != nil {\n    return fmt.Errorf(\"recreate L0 directory: %w\", err)\n} // on EACCES/ENOENT, fix ownership/existence of the parent dir and retry","preventionTips":["Create the data directory with correct ownership before starting the service","Do not delete the database directory while litestream is running","Check SELinux/AppArmor policies for the litestream binary","Monitor disk space and inode availability"],"tags":["litestream","filesystem","mkdir","permissions","l0"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3","analyzedAt":"2026-09-06T18:29:25.564Z","contentChangedAt":"2026-09-06T18:29:25.564Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}