fix(filesystem): gracefully handle unavailable directories

Previously, the server would crash if any configured directory was
unavailable (e.g., unmounted external drive). Now it:

- Filters out inaccessible directories with a warning
- Continues operating with remaining accessible directories
- Only fails if NO directories are accessible

Fixes #2815
This commit is contained in:
lunov
2026-01-10 18:39:55 +07:00
committed by nulone
parent 55a3056a04
commit 2dfa15dc6e
2 changed files with 117 additions and 8 deletions

View File

@@ -56,19 +56,28 @@ let allowedDirectories = await Promise.all(
})
);
// Validate that all directories exist and are accessible
await Promise.all(allowedDirectories.map(async (dir) => {
// Filter to only accessible directories, warn about inaccessible ones
const accessibleDirectories: string[] = [];
for (const dir of allowedDirectories) {
try {
const stats = await fs.stat(dir);
if (!stats.isDirectory()) {
console.error(`Error: ${dir} is not a directory`);
process.exit(1);
if (stats.isDirectory()) {
accessibleDirectories.push(dir);
} else {
console.error(`Warning: ${dir} is not a directory, skipping`);
}
} catch (error) {
console.error(`Error accessing directory ${dir}:`, error);
process.exit(1);
console.error(`Warning: Cannot access directory ${dir}, skipping`);
}
}));
}
// Exit only if ALL paths are inaccessible (and some were specified)
if (accessibleDirectories.length === 0 && allowedDirectories.length > 0) {
console.error("Error: None of the specified directories are accessible");
process.exit(1);
}
allowedDirectories = accessibleDirectories;
// Initialize the global allowedDirectories in lib.ts
setAllowedDirectories(allowedDirectories);