Skip to content

Commit 22bbaa4

Browse files
authored
Merge commit from fork
1 parent d6318eb commit 22bbaa4

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎packages/node/src/node-hfs.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,12 @@ export class NodeHfsImpl {
359359
* @throws {Error} If the source file is a directory.
360360
* @throws {Error} If the destination file is a directory.
361361
*/
362-
copy(source, destination) {
362+
async copy(source, destination) {
363+
const stat = await this.#fsp.lstat(source);
364+
if (stat.isSymbolicLink()) {
365+
const target = await this.#fsp.readlink(source);
366+
return this.#fsp.symlink(target, destination);
367+
}
363368
return this.#fsp.copyFile(source, destination);
364369
}
365370

@@ -393,7 +398,10 @@ export class NodeHfsImpl {
393398
const fromEntryPath = path.join(sourceStr, entry.name);
394399
const toEntryPath = path.join(destinationStr, entry.name);
395400

396-
if (entry.isDirectory) {
401+
if (entry.isSymlink) {
402+
const target = await this.#fsp.readlink(fromEntryPath);
403+
await this.#fsp.symlink(target, toEntryPath);
404+
} else if (entry.isDirectory) {
397405
await this.copyAll(fromEntryPath, toEntryPath);
398406
} else {
399407
await this.copy(fromEntryPath, toEntryPath);

‎packages/node/tests/node-hfs.test.js‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { NodeHfsImpl } from "../src/node-hfs.js";
1313
import assert from "node:assert";
1414
import fsp from "node:fs/promises";
1515
import { fileURLToPath } from "node:url";
16+
import os from "node:os";
1617
import path from "node:path";
1718
import { HfsImplTester } from "@humanfs/test";
1819

@@ -256,4 +257,87 @@ describe("NodeHfsImpl Customizations", () => {
256257
);
257258
});
258259
});
260+
261+
describe("copy()", () => {
262+
it("should recreate a symlink rather than copy the target file contents", async () => {
263+
const tmpDir = await fsp.mkdtemp(
264+
path.join(os.tmpdir(), "humanfs-copy-symlink-"),
265+
);
266+
try {
267+
const secret = path.join(tmpDir, "secret.txt");
268+
const link = path.join(tmpDir, "link.txt");
269+
const dest = path.join(tmpDir, "dest.txt");
270+
271+
await fsp.writeFile(secret, "TOPSECRET");
272+
273+
try {
274+
await fsp.symlink(secret, link);
275+
} catch (err) {
276+
if (err.code === "EPERM") {
277+
return; // symlinks require elevated privileges on this OS; skip
278+
}
279+
throw err;
280+
}
281+
282+
const impl = new NodeHfsImpl({ fsp });
283+
await impl.copy(link, dest);
284+
285+
// destination must be a symlink, not a plain file with secret contents
286+
const destStat = await fsp.lstat(dest);
287+
assert.ok(
288+
destStat.isSymbolicLink(),
289+
"destination should be a symlink",
290+
);
291+
292+
// the symlink target must point back to the original secret path
293+
const linkTarget = await fsp.readlink(dest);
294+
assert.strictEqual(linkTarget, secret);
295+
} finally {
296+
await fsp.rm(tmpDir, { recursive: true });
297+
}
298+
});
299+
});
300+
301+
describe("copyAll()", () => {
302+
it("should recreate symlinks rather than copy target file contents", async () => {
303+
const tmpDir = await fsp.mkdtemp(
304+
path.join(os.tmpdir(), "humanfs-copyall-symlink-"),
305+
);
306+
try {
307+
const secret = path.join(tmpDir, "secret.txt");
308+
const src = path.join(tmpDir, "src");
309+
const dst = path.join(tmpDir, "dst");
310+
311+
await fsp.mkdir(src);
312+
await fsp.writeFile(secret, "TOPSECRET");
313+
314+
try {
315+
await fsp.symlink(secret, path.join(src, "link.txt"));
316+
} catch (err) {
317+
if (err.code === "EPERM") {
318+
return; // symlinks require elevated privileges on this OS; skip
319+
}
320+
throw err;
321+
}
322+
323+
const impl = new NodeHfsImpl({ fsp });
324+
await impl.copyAll(src, dst);
325+
326+
const copiedLink = path.join(dst, "link.txt");
327+
const copiedStat = await fsp.lstat(copiedLink);
328+
329+
// the copied entry must be a symlink, not a plain file
330+
assert.ok(
331+
copiedStat.isSymbolicLink(),
332+
"copied entry should be a symlink",
333+
);
334+
335+
// the symlink must point to the original target, not contain its data
336+
const linkTarget = await fsp.readlink(copiedLink);
337+
assert.strictEqual(linkTarget, secret);
338+
} finally {
339+
await fsp.rm(tmpDir, { recursive: true });
340+
}
341+
});
342+
});
259343
});

0 commit comments

Comments
 (0)