fix: handle permission denied when creating DATA_DIR (#1005)
Fallback to default user directory (~/.9router) when configured DATA_DIR is not writable (EACCES/EPERM). Other errors still throw. Co-authored-by: Thiên Toán <toanalien@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,14 +1,29 @@
|
|||||||
|
import fs from "node:fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
|
|
||||||
const APP_NAME = "9router";
|
const APP_NAME = "9router";
|
||||||
|
|
||||||
export function getDataDir() {
|
function defaultDir() {
|
||||||
if (process.env.DATA_DIR) return process.env.DATA_DIR;
|
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), APP_NAME);
|
return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), APP_NAME);
|
||||||
}
|
}
|
||||||
return path.join(os.homedir(), `.${APP_NAME}`);
|
return path.join(os.homedir(), `.${APP_NAME}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getDataDir() {
|
||||||
|
const configured = process.env.DATA_DIR;
|
||||||
|
if (!configured) return defaultDir();
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(configured, { recursive: true });
|
||||||
|
return configured;
|
||||||
|
} catch (e) {
|
||||||
|
if (e?.code === "EACCES" || e?.code === "EPERM") {
|
||||||
|
console.warn(`[DATA_DIR] '${configured}' not writable → fallback ~/.${APP_NAME}`);
|
||||||
|
return defaultDir();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const DATA_DIR = getDataDir();
|
export const DATA_DIR = getDataDir();
|
||||||
|
|||||||
@@ -1,13 +1,29 @@
|
|||||||
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
|
|
||||||
// Single source of truth for data directory — matches localDb.js logic
|
const APP_NAME = "9router";
|
||||||
function getDataDir() {
|
|
||||||
if (process.env.DATA_DIR) return process.env.DATA_DIR;
|
function defaultDir() {
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), "9router");
|
return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), APP_NAME);
|
||||||
|
}
|
||||||
|
return path.join(os.homedir(), `.${APP_NAME}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDataDir() {
|
||||||
|
const configured = process.env.DATA_DIR;
|
||||||
|
if (!configured) return defaultDir();
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(configured, { recursive: true });
|
||||||
|
return configured;
|
||||||
|
} catch (e) {
|
||||||
|
if (e?.code === "EACCES" || e?.code === "EPERM") {
|
||||||
|
console.warn(`[DATA_DIR] '${configured}' not writable → fallback ~/.${APP_NAME}`);
|
||||||
|
return defaultDir();
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
return path.join(os.homedir(), ".9router");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const DATA_DIR = getDataDir();
|
const DATA_DIR = getDataDir();
|
||||||
|
|||||||
Reference in New Issue
Block a user