Path: blob/master/src/packages/backend/misc/touch.ts
1447 views
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { open, utimes } from "node:fs/promises";67// touch the file in a similar manner as "touch" in linux8export async function touch(9path: string,10createIfMissing = true11): Promise<boolean> {12try {13const now = new Date();14await utimes(path, now, now);15return true;16} catch (err) {17try {18if (createIfMissing && "ENOENT" === err.code) {19const fd = await open(path, "a");20await fd.close();21return true;22}23} finally {24return false;25}26}27}282930