mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 23:50:45 +00:00
Handle unsupported copyFile operations on writable GVFS/FUSE destinations using the existing exclusive bounded copy path. New files accept the mount's creation permissions; replacing an existing file still requires equivalent protection, and failures retain recoverable data. Only explicitly optional permission/timestamp updates may tolerate permission refusal. Content writes, close errors, required protection, and rollback preservation remain strict. Current-head review and required checks were verified before merging. Related to #3611. Keep the report open pending delivery and actual environment confirmation.
107 lines
4.6 KiB
JavaScript
107 lines
4.6 KiB
JavaScript
"use strict";
|
|
|
|
const fs = require("node:fs");
|
|
|
|
// Publish a prepared regular file without replacing any destination entry.
|
|
// Hardlinks make complete bytes visible atomically. FAT-like filesystems use an
|
|
// exclusively created handle instead: partial contents can be visible there,
|
|
// but no pathname cleanup may remove a concurrent writer's replacement.
|
|
// Resolves with the published inode identity ({ dev, ino, size }) so callers can
|
|
// verify an open destination handle before later metadata stamping.
|
|
async function publishLocalFileExclusive(source, target, assertNotCancelled = () => {}, preparedHandle, options = {}) {
|
|
assertNotCancelled();
|
|
if (!options.copyOnly) {
|
|
try {
|
|
await fs.promises.link(source, target);
|
|
// The hardlink shares the published inode; stat either name.
|
|
const linkedStat = await fs.promises.lstat(source);
|
|
return { dev: linkedStat.dev, ino: linkedStat.ino, size: linkedStat.size };
|
|
} catch (error) {
|
|
// EISDIR is the misleading libuv mapping for Win32 ERROR_INVALID_FUNCTION
|
|
// when the volume has no hard-link support (exFAT/FAT32); the source here
|
|
// is always a regular prepared file, never a directory (nodejs/node#65817).
|
|
if (!["ENOTSUP", "EOPNOTSUPP", "ENOSYS", "EPERM", "EACCES", "EXDEV", "EISDIR"].includes(error?.code)) throw error;
|
|
}
|
|
}
|
|
|
|
let input;
|
|
let output;
|
|
let failure;
|
|
let publishedIdentity;
|
|
let timestampsPreserved = true;
|
|
try {
|
|
input = preparedHandle ?? await fs.promises.open(source, "r");
|
|
const stat = await input.stat();
|
|
assertNotCancelled();
|
|
output = await fs.promises.open(target, "wx", stat.mode & 0o7777);
|
|
const buffer = Buffer.allocUnsafe(1024 * 1024);
|
|
let position = 0;
|
|
while (position < stat.size) {
|
|
assertNotCancelled();
|
|
const { bytesRead } = await input.read(buffer, 0, Math.min(buffer.length, stat.size - position), position);
|
|
if (!bytesRead) throw new Error("Prepared local file ended before publication completed");
|
|
let written = 0;
|
|
while (written < bytesRead) {
|
|
assertNotCancelled();
|
|
const { bytesWritten } = await output.write(buffer, written, bytesRead - written, position + written);
|
|
if (!bytesWritten) throw new Error("Local publication made no write progress");
|
|
written += bytesWritten;
|
|
}
|
|
position += bytesRead;
|
|
}
|
|
assertNotCancelled();
|
|
// Restore metadata after writes (which may clear special permission bits),
|
|
// through the owned handle rather than a potentially replaced pathname.
|
|
const mode = stat.mode & 0o7777;
|
|
if (((await output.stat()).mode & 0o7777) !== mode) {
|
|
try {
|
|
await output.chmod(mode);
|
|
} catch (error) {
|
|
// New files follow the mount's access policy (GVFS can synthesize mode
|
|
// bits). Replacements must still preserve the validated protection.
|
|
if (!isOptionalMetadataError(error) || options.preserveMode !== false) throw error;
|
|
}
|
|
if (options.preserveMode !== false && ((await output.stat()).mode & 0o7777) !== mode) {
|
|
throw new Error("Cannot preserve local destination permissions");
|
|
}
|
|
}
|
|
try {
|
|
await output.utimes(stat.atime, stat.mtime);
|
|
} catch (error) {
|
|
if (!isOptionalMetadataError(error) || options.requireTimestamps !== false) throw error;
|
|
timestampsPreserved = false;
|
|
}
|
|
const ownedStat = await output.stat();
|
|
const targetStat = await fs.promises.lstat(target);
|
|
if (!targetStat.isFile() || targetStat.dev !== ownedStat.dev || targetStat.ino !== ownedStat.ino) {
|
|
throw new Error("Local download target changed during replacement");
|
|
}
|
|
publishedIdentity = { dev: ownedStat.dev, ino: ownedStat.ino, size: ownedStat.size, timestampsPreserved };
|
|
} catch (error) {
|
|
failure = error;
|
|
} finally {
|
|
for (const handle of [output, preparedHandle ? undefined : input]) {
|
|
try { await handle?.close(); } catch (error) { failure ??= error; }
|
|
}
|
|
}
|
|
if (failure) {
|
|
// Once exclusive creation succeeded, leave the partial destination intact.
|
|
// The caller must retain its complete prepared file and any original backup.
|
|
if (output) failure.localPublicationIncomplete = true;
|
|
throw failure;
|
|
}
|
|
return publishedIdentity;
|
|
}
|
|
|
|
// Only optional chmod/utimes may ignore permission refusal. Never use this
|
|
// predicate for creation, data writes, close, or required permission changes.
|
|
function isOptionalMetadataError(error) {
|
|
return isMetadataUnsupportedError(error) || ["EPERM", "EACCES"].includes(error?.code);
|
|
}
|
|
|
|
function isMetadataUnsupportedError(error) {
|
|
return ["ENOTSUP", "EOPNOTSUPP", "ENOSYS"].includes(error?.code);
|
|
}
|
|
|
|
module.exports = { publishLocalFileExclusive, isMetadataUnsupportedError };
|