diff --git a/electron/document-files.cjs b/electron/document-files.cjs index 6e7ec03..7a1f1c6 100644 --- a/electron/document-files.cjs +++ b/electron/document-files.cjs @@ -3,6 +3,9 @@ const fsp = fs.promises; const path = require("node:path"); const crypto = require("node:crypto"); const { pipeline } = require("node:stream/promises"); +const { promisify } = require("node:util"); + +const fallbackRealpath = promisify(fs.realpath); const MAX_DOCUMENT_BYTES = 300 * 1024 * 1024; const MAX_HTML_BYTES = 48 * 1024 * 1024; @@ -48,10 +51,20 @@ function checkDiskPath(file) { || file.slice(2).includes(":"))) throw new Error("Choose a regular file."); } +// On Windows WinFsp drives (such as rclone mount without --network-mode), native realpath can fail +// with UNKNOWN even though lstat/open work. Fall back to Node's JS realpath implementation. +async function resolvePath(file, { platform = process.platform, native = fsp.realpath, fallback = fallbackRealpath } = {}) { + try { return await native(file); } + catch (err) { + if (platform !== "win32" || err?.code !== "UNKNOWN") throw err; + return await fallback(file); + } +} + async function stageWord(file, source, limit = MAX_DOCUMENT_BYTES) { checkDiskPath(file); const selected = await fsp.lstat(file, { bigint: true }); - const resolved = await fsp.realpath(file); + const resolved = await resolvePath(file); checkDiskPath(resolved); const folder = path.dirname(resolved); const directory = await fsp.stat(folder, { bigint: true }); @@ -107,4 +120,4 @@ async function rememberPdf(profile, staged, index, data) { } finally { await fsp.rm(temp, { force: true }); } } -module.exports = { MAX_DOCUMENT_BYTES, MAX_HTML_BYTES, checkDiskPath, tooLarge, readRegular, stageWord, sameFile, pdfName, cachedPdf, rememberPdf }; +module.exports = { MAX_DOCUMENT_BYTES, MAX_HTML_BYTES, checkDiskPath, resolvePath, tooLarge, readRegular, stageWord, sameFile, pdfName, cachedPdf, rememberPdf }; diff --git a/electron/main.cjs b/electron/main.cjs index fdd4478..c28c201 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -13,7 +13,7 @@ const { safePdfName } = require("./filenames.cjs"); const { csp } = require("./csp.cjs"); const { WORD_FILE, openWord } = require("./convert.cjs"); const { registerPrinting } = require("./printing.cjs"); -const { MAX_DOCUMENT_BYTES, checkDiskPath, readRegular } = require("./document-files.cjs"); +const { MAX_DOCUMENT_BYTES, checkDiskPath, resolvePath, readRegular } = require("./document-files.cjs"); if (!app.isPackaged && process.env.FOLIO_TEST_USER_DATA) app.setPath("userData", process.env.FOLIO_TEST_USER_DATA); // Automated tests drive a window that never appears, so a local run doesn't take over the screen or focus. @@ -95,7 +95,7 @@ async function sendFile(file) { if (WORD_FILE.test(file)) return sendWordFile(file, target); try { checkDiskPath(file); - const data = await readRegular(await fs.promises.realpath(file)); + const data = await readRegular(await resolvePath(file)); if (!target.isDestroyed()) target.webContents.send("folio:open-file", { name: path.basename(file), data }); } catch (err) { dialog.showErrorBox("Couldn't open file", `${file}\n\n${err.userMessage ?? err.message}`); diff --git a/scripts/test-word-files.cjs b/scripts/test-word-files.cjs index c3c805a..5dc890b 100644 --- a/scripts/test-word-files.cjs +++ b/scripts/test-word-files.cjs @@ -5,7 +5,7 @@ const os = require("node:os"); const path = require("node:path"); const { execFileSync } = require("node:child_process"); const JSZip = require("jszip"); -const { stageWord, readRegular, cachedPdf, rememberPdf } = require("../electron/document-files.cjs"); +const { stageWord, readRegular, resolvePath, cachedPdf, rememberPdf } = require("../electron/document-files.cjs"); const { boundedParts } = require("../electron/docx-parts.cjs"); const { publish } = require("../electron/word-publish.cjs"); const { systemPowerShell } = require("../electron/windows-powershell.cjs"); @@ -68,6 +68,24 @@ test("a symlink never authorizes a neighboring export, and files cannot redirect await assert.rejects(readRegular(link), /regular file/); }); +test("resolvePath falls back to JS realpath only on Windows UNKNOWN errors", async (t) => { + const root = await folder(t); + const file = path.join(root, "sample.pdf"); + await fs.writeFile(file, "%PDF-1.4\n"); + const expected = await fs.realpath(file); + assert.equal(await resolvePath(file), expected); + + const unknown = () => Promise.reject(Object.assign(new Error("UNKNOWN: unknown error, realpath"), { code: "UNKNOWN" })); + const fallback = await resolvePath(file, { platform: "win32", native: unknown }); + assert.equal(fallback, require("node:fs").realpathSync(file)); + assert.equal((await readRegular(fallback)).toString("utf8"), "%PDF-1.4\n"); + await assert.rejects(resolvePath(file, { platform: "darwin", native: unknown }), { code: "UNKNOWN" }); + await assert.rejects(resolvePath(file, { platform: "linux", native: unknown }), { code: "UNKNOWN" }); + + const enoent = () => Promise.reject(Object.assign(new Error("ENOENT"), { code: "ENOENT" })); + await assert.rejects(resolvePath(file, { platform: "win32", native: enoent }), { code: "ENOENT" }); +}); + test("a PDF that grows after its descriptor is sized is rejected", async (t) => { const root = await folder(t); const file = path.join(root, "growing.pdf");