diff --git a/plugins/storage/server/api/files.ts b/plugins/storage/server/api/files.ts index 5b52f02534..810da4ca08 100644 --- a/plugins/storage/server/api/files.ts +++ b/plugins/storage/server/api/files.ts @@ -83,7 +83,7 @@ router.post( await attachment.destroy(); if (err instanceof Error && err.message.includes("permission denied")) { throw Error( - `Permission denied writing to "${key}". Check the host machine file system permissions.` + `Permission denied writing to "${key}". Check that uid ${process.getuid?.() ?? "unknown"} can write to the storage directory on the host machine.` ); } throw err; diff --git a/plugins/storage/server/index.ts b/plugins/storage/server/index.ts index d5a9f20321..a2bf4e9198 100644 --- a/plugins/storage/server/index.ts +++ b/plugins/storage/server/index.ts @@ -1,4 +1,10 @@ -import { existsSync, mkdirSync } from "node:fs"; +import { + accessSync, + constants, + existsSync, + mkdirSync, + statSync, +} from "node:fs"; import { toError } from "@shared/utils/error"; import env from "@server/env"; import Logger from "@server/logging/Logger"; @@ -16,6 +22,7 @@ if (env.FILE_STORAGE === "local") { mkdirSync(rootDir, { recursive: true }); Logger.debug("utils", `Created ${rootDir} for local storage`); } + checkWritable(rootDir); } catch (err) { Logger.fatal( `Failed to create directory for local file storage at ${env.FILE_STORAGE_LOCAL_ROOT_DIR}`, @@ -41,3 +48,32 @@ if (enabled) { }, ]); } + +/** + * Logs an error that explains how to fix the permissions when the process + * cannot write to or enter the local file storage directory. + * + * @param rootDir the local file storage directory. + */ +function checkWritable(rootDir: string) { + try { + accessSync(rootDir, constants.W_OK | constants.X_OK); + } catch (err) { + if (process.getuid === undefined || process.getgid === undefined) { + Logger.error( + `Local file storage directory ${rootDir} is not writable`, + toError(err) + ); + return; + } + + const uid = process.getuid(); + const gid = process.getgid(); + const owner = statSync(rootDir); + + Logger.error( + `Local file storage directory ${rootDir} is not writable. Outline runs as uid ${uid} and gid ${gid}, but the directory is owned by uid ${owner.uid} and gid ${owner.gid}. Run "chown -R ${uid}:${gid}" on the directory on the host, or run the container as the directory owner with user: "${owner.uid}:${owner.gid}".`, + toError(err) + ); + } +}