fix: file timestamp was changed when meta-data was stored

Massimo Melina committed Jun 30, 2024 at 00:20 UTC 2c4bd94287f8694fcc5746cd3d32abd0b41a9a21
1 file changed +6 -1
src/fileAttr.ts
+6 -1
@@ -4,6 +4,8 @@ import { promisify } from 'util'
4 import { access } from 'fs/promises'
5 import { tryJson } from './cross'
6 import { onProcessExit } from './first'
7 +import { utimes, stat } from 'node:fs/promises'
8 +import { IS_WINDOWS } from './const'
9 // @ts-ignore
10 const fsx = import('fs-x-attributes').then(x => ({ set: promisify(x.set), get: promisify(x.get) }),
11 () => console.log('fs-x-attributes not available') )
@@ -15,8 +17,11 @@ if (existsSync(FN))
17 fileAttrDb.open(FN)
18 const FILE_ATTR_PREFIX = 'user.hfs.' // user. prefix to be linux compatible
19 export async function storeFileAttr(path: string, k: string, v: any) {
18 - if (await fsx.then(x => x?.set(path, FILE_ATTR_PREFIX + k, JSON.stringify(v)).then(() => 1, () => 0)))
20 + const s = await stat(path)
21 + if (await fsx.then(x => x?.set(path, FILE_ATTR_PREFIX + k, JSON.stringify(v)).then(() => 1, () => 0))) {
22 + if (IS_WINDOWS) utimes(path, s.atime, s.mtime) // restore timestamps, necessary only on Windows
23 return true
24 + }
25 // fallback to our kv-storage
26 if (!fileAttrDb.isOpen())
27 await fileAttrDb.open(FN)