From 637aeff4b7d43146ff617f99034300eef70449d0 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 24 Jan 2024 14:07:10 +1100 Subject: [PATCH 1/5] feat: `Deno.FsFile.{utime,utimeSync}()` and deprecate `Deno.{futime,futimeSync}` --- cli/tests/unit/utime_test.ts | 42 +++++++++++++++++++++++++++ cli/tsc/dts/lib.deno.ns.d.ts | 40 +++++++++++++++++++++++++ ext/fs/30_fs.js | 8 +++++ ext/node/polyfills/_fs/_fs_futimes.ts | 5 ++-- runtime/js/90_deno_ns.js | 20 +++++++++++-- 5 files changed, 110 insertions(+), 5 deletions(-) diff --git a/cli/tests/unit/utime_test.ts b/cli/tests/unit/utime_test.ts index 48f4f405abfb88..5101cb376f3c3a 100644 --- a/cli/tests/unit/utime_test.ts +++ b/cli/tests/unit/utime_test.ts @@ -27,6 +27,27 @@ Deno.test( }, ); +Deno.test( + { permissions: { read: true, write: true } }, + async function fsFileUtimeSyncSuccess() { + const testDir = await Deno.makeTempDir(); + const filename = testDir + "/file.txt"; + using file = await Deno.open(filename, { + create: true, + write: true, + }); + + const atime = 1000; + const mtime = 50000; + await file.utime(atime, mtime); + await file.dataSync(); + + const fileInfo = Deno.statSync(filename); + assertEquals(fileInfo.atime, new Date(atime * 1000)); + assertEquals(fileInfo.mtime, new Date(mtime * 1000)); + }, +); + Deno.test( { permissions: { read: true, write: true } }, function futimeSyncSuccess() { @@ -48,6 +69,27 @@ Deno.test( }, ); +Deno.test( + { permissions: { read: true, write: true } }, + function futimeSyncSuccess() { + const testDir = Deno.makeTempDirSync(); + const filename = testDir + "/file.txt"; + using file = Deno.openSync(filename, { + create: true, + write: true, + }); + + const atime = 1000; + const mtime = 50000; + file.utimeSync(atime, mtime); + file.dataSyncSync(); + + const fileInfo = Deno.statSync(filename); + assertEquals(fileInfo.atime, new Date(atime * 1000)); + assertEquals(fileInfo.mtime, new Date(mtime * 1000)); + }, +); + Deno.test( { permissions: { read: true, write: true } }, function utimeSyncFileSuccess() { diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index 2b82fa152390cf..8fc2e1ba2c9ff6 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -2604,6 +2604,40 @@ declare namespace Deno { * @category I/O */ dataSyncSync(): void; + /** + * Changes the access (`atime`) and modification (`mtime`) times of a file + * stream resource referenced by `rid`. Given times are either in seconds + * (UNIX epoch time) or as `Date` objects. + * + * ```ts + * using file = await Deno.open("file.txt", { create: true, write: true }); + * await file.utime(1556495550, new Date()); + * ``` + * + * @category File System + */ + utime( + rid: number, + atime: number | Date, + mtime: number | Date, + ): Promise; + /** + * Synchronously changes the access (`atime`) and modification (`mtime`) times + * of a file stream resource referenced by `rid`. Given times are either in + * seconds (UNIX epoch time) or as `Date` objects. + * + * ```ts + * using file = Deno.openSync("file.txt", { create: true, write: true }); + * file.utime(1556495550, new Date()); + * ``` + * + * @category File System + */ + utimeSync( + rid: number, + atime: number | Date, + mtime: number | Date, + ): void; /** Close the file. Closing a file when you are finished with it is * important to avoid leaking resources. * @@ -5347,6 +5381,9 @@ declare namespace Deno { * Deno.futimeSync(file.rid, 1556495550, new Date()); * ``` * + * @deprecated Use {@linkcode Deno.FsFile.utimeSync} instead. + * {@linkcode Deno.futimeSync} will be removed in Deno 2.0. + * * @category File System */ export function futimeSync( @@ -5365,6 +5402,9 @@ declare namespace Deno { * await Deno.futime(file.rid, 1556495550, new Date()); * ``` * + * @deprecated Use {@linkcode Deno.FsFile.utime} instead. + * {@linkcode Deno.futime} will be removed in Deno 2.0. + * * @category File System */ export function futime( diff --git a/ext/fs/30_fs.js b/ext/fs/30_fs.js index 6923741eea730c..585f1287fcfa8e 100644 --- a/ext/fs/30_fs.js +++ b/ext/fs/30_fs.js @@ -757,6 +757,14 @@ class FsFile { op_fs_fsync_sync(this.rid); } + async utime(atime, mtime) { + await futime(this.#rid, atime, mtime); + } + + utimeSync(atime, mtime) { + futimeSync(this.#rid, atime, mtime); + } + [SymbolDispose]() { core.tryClose(this.rid); } diff --git a/ext/node/polyfills/_fs/_fs_futimes.ts b/ext/node/polyfills/_fs/_fs_futimes.ts index 8a29db26b36000..ecf9653d1fdacd 100644 --- a/ext/node/polyfills/_fs/_fs_futimes.ts +++ b/ext/node/polyfills/_fs/_fs_futimes.ts @@ -4,6 +4,7 @@ // deno-lint-ignore-file prefer-primordials import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; +import { FsFile } from "ext:deno_fs/30_fs.js"; function getValidTime( time: number | string | Date, @@ -38,7 +39,7 @@ export function futimes( atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); - Deno.futime(fd, atime, mtime).then(() => callback(null), callback); + new FsFile(fd).utime(atime, mtime).then(() => callback(null), callback); } export function futimesSync( @@ -49,5 +50,5 @@ export function futimesSync( atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); - Deno.futimeSync(fd, atime, mtime); + new FsFile(fd).utimeSync(atime, mtime); } diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index 25ba2ef26a8883..0617c32f0e93d5 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -1,6 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { core } from "ext:core/mod.js"; +import { core, internals } from "ext:core/mod.js"; const { op_net_listen_udp, op_net_listen_unixpacket, @@ -80,8 +80,22 @@ const denoNs = { truncate: fs.truncate, ftruncateSync: fs.ftruncateSync, ftruncate: fs.ftruncate, - futime: fs.futime, - futimeSync: fs.futimeSync, + async futime(rid, atime, mtime) { + internals.warnOnDeprecatedApi( + "Deno.futime()", + new Error().stack, + "Use `Deno.FsFile.utime()` instead.", + ); + await fs.futime(rid, atime, mtime); + }, + futimeSync(rid, atime, mtime) { + internals.warnOnDeprecatedApi( + "Deno.futimeSync()", + new Error().stack, + "Use `Deno.FsFile.utimeSync()` instead.", + ); + fs.futimeSync(rid, atime, mtime); + }, errors: errors.errors, inspect: console.inspect, env: os.env, From 9cd4c4126b294bbc7b6f79f3f02dc6c3ae343067 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 24 Jan 2024 14:55:02 +1100 Subject: [PATCH 2/5] fix --- cli/tsc/dts/lib.deno.ns.d.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index 8fc2e1ba2c9ff6..96577d2c2b38a3 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -2616,11 +2616,7 @@ declare namespace Deno { * * @category File System */ - utime( - rid: number, - atime: number | Date, - mtime: number | Date, - ): Promise; + utime(atime: number | Date, mtime: number | Date): Promise; /** * Synchronously changes the access (`atime`) and modification (`mtime`) times * of a file stream resource referenced by `rid`. Given times are either in @@ -2633,11 +2629,7 @@ declare namespace Deno { * * @category File System */ - utimeSync( - rid: number, - atime: number | Date, - mtime: number | Date, - ): void; + utimeSync(atime: number | Date, mtime: number | Date): void; /** Close the file. Closing a file when you are finished with it is * important to avoid leaking resources. * From a32290fe7e6dd9643d876e0dfbf494097cf26d29 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 24 Jan 2024 14:56:15 +1100 Subject: [PATCH 3/5] comments --- ext/node/polyfills/_fs/_fs_futimes.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/node/polyfills/_fs/_fs_futimes.ts b/ext/node/polyfills/_fs/_fs_futimes.ts index ecf9653d1fdacd..fe1962009a2020 100644 --- a/ext/node/polyfills/_fs/_fs_futimes.ts +++ b/ext/node/polyfills/_fs/_fs_futimes.ts @@ -39,6 +39,7 @@ export function futimes( atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); + // TODO: Treat `fd` as real file descriptor. new FsFile(fd).utime(atime, mtime).then(() => callback(null), callback); } @@ -50,5 +51,6 @@ export function futimesSync( atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); + // TODO: Treat `fd` as real file descriptor. new FsFile(fd).utimeSync(atime, mtime); } From 443a6e4d4344831e53c157c7aea66ab999cef137 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 24 Jan 2024 14:58:27 +1100 Subject: [PATCH 4/5] suggestions --- cli/tsc/dts/lib.deno.ns.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index 96577d2c2b38a3..883969cc37d31f 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -2605,9 +2605,9 @@ declare namespace Deno { */ dataSyncSync(): void; /** - * Changes the access (`atime`) and modification (`mtime`) times of a file - * stream resource referenced by `rid`. Given times are either in seconds - * (UNIX epoch time) or as `Date` objects. + * Changes the access (`atime`) and modification (`mtime`) times of the + * file stream resource. Given times are either in seconds (UNIX epoch + * time) or as `Date` objects. * * ```ts * using file = await Deno.open("file.txt", { create: true, write: true }); @@ -2618,9 +2618,9 @@ declare namespace Deno { */ utime(atime: number | Date, mtime: number | Date): Promise; /** - * Synchronously changes the access (`atime`) and modification (`mtime`) times - * of a file stream resource referenced by `rid`. Given times are either in - * seconds (UNIX epoch time) or as `Date` objects. + * Synchronously changes the access (`atime`) and modification (`mtime`) + * times of the file stream resource. Given times are either in seconds + * (UNIX epoch time) or as `Date` objects. * * ```ts * using file = Deno.openSync("file.txt", { create: true, write: true }); From c72ea0d900f811b0e4591f903af3e64458aec162 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 24 Jan 2024 15:03:32 +1100 Subject: [PATCH 5/5] fix --- ext/node/polyfills/_fs/_fs_futimes.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/node/polyfills/_fs/_fs_futimes.ts b/ext/node/polyfills/_fs/_fs_futimes.ts index fe1962009a2020..9bd41e114fa9f6 100644 --- a/ext/node/polyfills/_fs/_fs_futimes.ts +++ b/ext/node/polyfills/_fs/_fs_futimes.ts @@ -39,7 +39,7 @@ export function futimes( atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); - // TODO: Treat `fd` as real file descriptor. + // TODO(@littledivy): Treat `fd` as real file descriptor. new FsFile(fd).utime(atime, mtime).then(() => callback(null), callback); } @@ -51,6 +51,6 @@ export function futimesSync( atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); - // TODO: Treat `fd` as real file descriptor. + // TODO(@littledivy): Treat `fd` as real file descriptor. new FsFile(fd).utimeSync(atime, mtime); }