Skip to content

Commit

Permalink
windows: fs/promises: fix when writing to file opened in append mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Apr 10, 2024
1 parent baf0d7c commit ef6b1cf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5458,6 +5458,10 @@ pub const NodeFS = struct {
}

if (Environment.isWindows) {
if (args.flag == .a) {
return Maybe(Return.WriteFile).success;
}

const rc = std.os.windows.kernel32.SetEndOfFile(fd.cast());
if (rc == 0) {
return .{
Expand Down
32 changes: 26 additions & 6 deletions src/js/node/fs.promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const kTransfer = Symbol("kTransfer");
const kTransferList = Symbol("kTransferList");
const kDeserialize = Symbol("kDeserialize");
const kEmptyObject = ObjectFreeze({ __proto__: null });
const kFlag = Symbol("kFlag");

function watch(
filename: string | Buffer | URL,
Expand Down Expand Up @@ -175,7 +176,7 @@ const exports = {
mkdir: fs.mkdir.bind(fs),
mkdtemp: fs.mkdtemp.bind(fs),
open: async (path, flags, mode) => {
return new FileHandle(await fs.open(path, flags, mode));
return new FileHandle(await fs.open(path, flags, mode), flags);
},
read: fs.read.bind(fs),
write: fs.write.bind(fs),
Expand Down Expand Up @@ -240,11 +241,12 @@ export default exports;
// These functions await the result so that errors propagate correctly with
// async stack traces and so that the ref counting is correct.
var FileHandle = (private_symbols.FileHandle = class FileHandle extends EventEmitter {
constructor(fd) {
constructor(fd, flag) {
super();
this[kFd] = fd ? fd : -1;
this[kRefs] = 1;
this[kClosePromise] = null;
this[kFlag] = flag;
}

getAsyncId() {
Expand All @@ -255,13 +257,23 @@ export default exports;
return this[kFd];
}

async appendFile(data, options) {
async appendFile(data, options: object | string | undefined) {
const fd = this[kFd];
throwEBADFIfNecessary(writeFile, fd);
let encoding = "utf8";
let flush = false;

if (options == null || typeof options === "function") {
} else if (typeof options === "string") {
encoding = options;
} else {
encoding = options?.encoding ?? encoding;
flush = options?.flush ?? flush;
}

try {
this[kRef]();
return await writeFile(fd, data, options);
return await writeFile(fd, data, { encoding, flush, flag: this[kFlag] });
} finally {
this[kUnref]();
}
Expand Down Expand Up @@ -415,13 +427,21 @@ export default exports;
}
}

async writeFile(data, options) {
async writeFile(data: string, options: object | string | undefined = "utf8") {
const fd = this[kFd];
throwEBADFIfNecessary(writeFile, fd);
let encoding: string = "utf8";

if (options == null || typeof options === "function") {
} else if (typeof options === "string") {
encoding = options;
} else {
encoding = options?.encoding ?? encoding;
}

try {
this[kRef]();
return await writeFile(fd, data, options);
return await writeFile(fd, data, { encoding, flag: this[kFlag] });
} finally {
this[kUnref]();
}
Expand Down
16 changes: 16 additions & 0 deletions test/js/node/fs/promises.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const open = fsPromises.open;
const copyFile = fsPromises.copyFile;
const statfs = fsPromises.statfs;
const unlink = fsPromises.unlink;
const readFile = fsPromises.readFile;

//
//
Expand Down Expand Up @@ -186,3 +187,18 @@ describe("more", () => {
});
});
});

test("writing to file in append mode works", async () => {
const tempFile = os.tmpdir() + "/" + Date.now() + ".txt";

const f = await open(tempFile, "a");

await f.writeFile("test\n");
await f.appendFile("test\n");
await f.write("test\n");
await f.datasync();

await f.close();

expect((await readFile(tempFile)).toString()).toEqual("test\ntest\ntest\n");
});

0 comments on commit ef6b1cf

Please sign in to comment.