Skip to content

Commit

Permalink
fix(node:fs): use the right copyFile constants (#5874)
Browse files Browse the repository at this point in the history
  • Loading branch information
paperdave authored Sep 22, 2023
1 parent 92a5d84 commit b05e10c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/bun.js/node/node_fs_constant.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ pub const Constants = struct {
};

/// Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists.
pub const COPYFILE_EXCL: i32 = 1 << Copyfile.exclusive;

pub const COPYFILE_EXCL: i32 = Copyfile.exclusive;
///
/// Constant for fs.copyFile. copy operation will attempt to create a copy-on-write reflink.
/// If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.
pub const COPYFILE_FICLONE: i32 = 1 << Copyfile.clone;
pub const COPYFILE_FICLONE: i32 = Copyfile.clone;
///
/// Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink.
/// If the underlying platform does not support copy-on-write, then the operation will fail with an error.
pub const COPYFILE_FICLONE_FORCE: i32 = 1 << Copyfile.force;
pub const COPYFILE_FICLONE_FORCE: i32 = Copyfile.force;
// File Open Constants
/// Constant for fs.open(). Flag indicating to open a file for read-only access.
pub const O_RDONLY = std.os.O.RDONLY;
Expand Down
31 changes: 30 additions & 1 deletion test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ describe("copyFileSync", () => {
expect(Bun.hash(readFileSync(tempdir + "/copyFileSync.dest.blob"))).toBe(Bun.hash(buffer.buffer));
});

it("constants are right", () => {
expect(fs.constants.COPYFILE_EXCL).toBe(1);
expect(fs.constants.COPYFILE_FICLONE).toBe(2);
expect(fs.constants.COPYFILE_FICLONE_FORCE).toBe(4);
});

it("FICLONE option does not error ever", () => {
const tempdir = `${tmpdir()}/fs.test.js/${Date.now()}.FICLONE/1234/hi`;
expect(existsSync(tempdir)).toBe(false);
expect(tempdir.includes(mkdirSync(tempdir, { recursive: true })!)).toBe(true);

// that don't exist
copyFileSync(import.meta.path, tempdir + "/copyFileSync.js", fs.constants.COPYFILE_FICLONE);
copyFileSync(import.meta.path, tempdir + "/copyFileSync.js", fs.constants.COPYFILE_FICLONE);
copyFileSync(import.meta.path, tempdir + "/copyFileSync.js", fs.constants.COPYFILE_FICLONE);
});

it("COPYFILE_EXCL works", () => {
const tempdir = `${tmpdir()}/fs.test.js/${Date.now()}.COPYFILE_EXCL/1234/hi`;
expect(existsSync(tempdir)).toBe(false);
expect(tempdir.includes(mkdirSync(tempdir, { recursive: true })!)).toBe(true);

// that don't exist
copyFileSync(import.meta.path, tempdir + "/copyFileSync.js", fs.constants.COPYFILE_EXCL);
expect(() => {
copyFileSync(import.meta.path, tempdir + "/copyFileSync.js", fs.constants.COPYFILE_EXCL);
}).toThrow();
});

if (process.platform === "linux") {
describe("should work when copyFileRange is not available", () => {
it("on large files", () => {
Expand Down Expand Up @@ -212,7 +241,7 @@ describe("copyFileSync", () => {

describe("mkdirSync", () => {
it("should create a directory", () => {
const tempdir = `${tmpdir()}/fs.test.js/${Date.now()}/1234/hi`;
const tempdir = `${tmpdir()}/fs.test.js/${Date.now()}.mkdirSync/1234/hi`;
expect(existsSync(tempdir)).toBe(false);
expect(tempdir.includes(mkdirSync(tempdir, { recursive: true })!)).toBe(true);
expect(existsSync(tempdir)).toBe(true);
Expand Down

0 comments on commit b05e10c

Please sign in to comment.