|
| 1 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 2 | + |
| 3 | +import { getNodeFs, isDeno } from "./_utils.ts"; |
| 4 | +import { mapError } from "./_map_error.ts"; |
| 5 | + |
| 6 | +/** Changes the access (`atime`) and modification (`mtime`) times of a file |
| 7 | + * system object referenced by `path`. Given times are either in seconds |
| 8 | + * (UNIX epoch time) or as `Date` objects. |
| 9 | + * |
| 10 | + * Requires `allow-write` permission for the target path |
| 11 | + * |
| 12 | + * @example Usage |
| 13 | + * |
| 14 | + * ```ts |
| 15 | + * import { assert } from "@std/assert" |
| 16 | + * import { utime } from "@std/fs/unstable-utime"; |
| 17 | + * import { stat } from "@std/fs/unstable-stat" |
| 18 | + * |
| 19 | + * const newAccessDate = new Date() |
| 20 | + * const newModifiedDate = new Date() |
| 21 | + * |
| 22 | + * const fileBefore = await Deno.stat("README.md") |
| 23 | + * await Deno.utime("README.md", newAccessDate, newModifiedDate) |
| 24 | + * const fileAfter = await Deno.stat("README.md") |
| 25 | + * |
| 26 | + * assert(fileBefore.atime !== fileAfter.atime) |
| 27 | + * assert(fileBefore.mtime !== fileAfter.mtime) |
| 28 | + * ``` |
| 29 | + * @tags allow-write |
| 30 | + * @category File System |
| 31 | + * @param path The path to the file to be updated |
| 32 | + * @param atime The new access time |
| 33 | + * @param mtime The new modification time |
| 34 | + */ |
| 35 | +export async function utime( |
| 36 | + path: string | URL, |
| 37 | + atime: number | Date, |
| 38 | + mtime: number | Date, |
| 39 | +): Promise<void> { |
| 40 | + if (isDeno) { |
| 41 | + await Deno.utime(path, atime, mtime); |
| 42 | + } else { |
| 43 | + try { |
| 44 | + await getNodeFs().promises.utimes(path, atime, mtime); |
| 45 | + return; |
| 46 | + } catch (error) { |
| 47 | + throw mapError(error); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** Synchronously changes the access (`atime`) and modification (`mtime`) |
| 53 | + * times of the file stream resource. Given times are either in seconds |
| 54 | + * (UNIX epoch time) or as `Date` objects. |
| 55 | + * |
| 56 | + * Requires `allow-write` permission for the target path |
| 57 | + * |
| 58 | + * @example Usage |
| 59 | + * |
| 60 | + * ```ts |
| 61 | + * import { assert } from "@std/assert" |
| 62 | + * import { utimeSync } from "@std/fs/unstable-utime"; |
| 63 | + * import { stat } from "@std/fs/unstable-stat" |
| 64 | + * |
| 65 | + * const newAccessDate = new Date() |
| 66 | + * const newModifiedDate = new Date() |
| 67 | + * |
| 68 | + * const fileBefore = await Deno.stat("README.md") |
| 69 | + * Deno.utimeSync("README.md", newAccessDate, newModifiedDate) |
| 70 | + * const fileAfter = await Deno.stat("README.md") |
| 71 | + * |
| 72 | + * assert(fileBefore.atime !== fileAfter.atime) |
| 73 | + * assert(fileBefore.mtime !== fileAfter.mtime) |
| 74 | + * ``` |
| 75 | + * @tags allow-write |
| 76 | + * @category File System |
| 77 | + * @param path The path to the file to be updated |
| 78 | + * @param atime The new access time |
| 79 | + * @param mtime The new modification time |
| 80 | + */ |
| 81 | +export function utimeSync( |
| 82 | + path: string | URL, |
| 83 | + atime: number | Date, |
| 84 | + mtime: number | Date, |
| 85 | +): void { |
| 86 | + if (isDeno) { |
| 87 | + return Deno.utimeSync(path, atime, mtime); |
| 88 | + } else { |
| 89 | + try { |
| 90 | + getNodeFs().utimesSync(path, atime, mtime); |
| 91 | + } catch (error) { |
| 92 | + throw mapError(error); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments