fs: implement lutimes by arcanis · Pull Request #33399 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions doc/api/fs.md
24 changes: 24 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,28 @@ function futimesSync(fd, atime, mtime) {
handleErrorFromBinding(ctx);
}

function lutimes(path, atime, mtime, callback) {
callback = makeCallback(callback);
path = getValidatedPath(path);

const req = new FSReqCallback();
req.oncomplete = callback;
binding.lutimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
req);
}

function lutimesSync(path, atime, mtime) {
path = getValidatedPath(path);
const ctx = { path };
binding.lutimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
undefined, ctx);
handleErrorFromBinding(ctx);
}

function writeAll(fd, isUserFd, buffer, offset, length, callback) {
// write(fd, buffer, offset, length, position, callback)
fs.write(fd, buffer, offset, length, null, (writeErr, written) => {
Expand Down Expand Up @@ -1971,6 +1993,8 @@ module.exports = fs = {
linkSync,
lstat,
lstatSync,
lutimes,
lutimesSync,
mkdir,
mkdirSync,
mkdtemp,
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,14 @@ async function futimes(handle, atime, mtime) {
return binding.futimes(handle.fd, atime, mtime, kUsePromises);
}

async function lutimes(path, atime, mtime) {
path = getValidatedPath(path);
return binding.lutimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
kUsePromises);
}

async function realpath(path, options) {
options = getOptions(options, {});
path = getValidatedPath(path);
Expand Down Expand Up @@ -582,6 +590,7 @@ module.exports = {
lchown,
chown,
utimes,
lutimes,
realpath,
mkdtemp,
writeFile,
Expand Down
30 changes: 30 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,35 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
}
}

static void LUTimes(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();
CHECK_GE(argc, 3);

BufferValue path(env->isolate(), args[0]);
CHECK_NOT_NULL(*path);

CHECK(args[1]->IsNumber());
const double atime = args[1].As<Number>()->Value();

CHECK(args[2]->IsNumber());
const double mtime = args[2].As<Number>()->Value();

FSReqBase* req_wrap_async = GetReqWrap(args, 3);
if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req)
AsyncCall(env, req_wrap_async, args, "lutime", UTF8, AfterNoArgs,
uv_fs_lutime, *path, atime, mtime);
} else { // lutimes(path, atime, mtime, undefined, ctx)
CHECK_EQ(argc, 5);
FSReqWrapSync req_wrap_sync;
FS_SYNC_TRACE_BEGIN(lutimes);
SyncCall(env, args[4], &req_wrap_sync, "lutime",
uv_fs_lutime, *path, atime, mtime);
FS_SYNC_TRACE_END(lutimes);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nicer to share this code with UTimes(). The JS bindings too, although that's not as much code.

The FS_SYNC_TRACE_BEGIN/FS_SYNC_TRACE_END labels are off, by the way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to keep roughly the same pattern as for lchown and lchmod, which are both duplicated similarly.

Will update the traces 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but remember the rule of three: duplicating something once? Probably okay. Duplicating it twice? Time to refactor.


static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Expand Down Expand Up @@ -2399,6 +2428,7 @@ void Initialize(Local<Object> target,

env->SetMethod(target, "utimes", UTimes);
env->SetMethod(target, "futimes", FUTimes);
env->SetMethod(target, "lutimes", LUTimes);

env->SetMethod(target, "mkdtemp", Mkdtemp);

Expand Down
73 changes: 45 additions & 28 deletions test/parallel/test-fs-utimes.js