From 301f2d19a2c9ae72d436b412e3c009112631c736 Mon Sep 17 00:00:00 2001 From: William Stein Date: Sat, 29 Oct 2022 13:17:35 -0700 Subject: [PATCH] fix: support calling `utimes` on a directory (#866) * fix #391 -- "utimes" doesn't work for directories - "EISDIR: illegal operation on a directory, open ..." error - https://github.com/streamich/memfs/issues/391 * run prettier * test: adjust spec title Co-authored-by: Gareth Jones --- src/__tests__/volume.test.ts | 13 ++++++++++--- src/volume.ts | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index 9950c2473..7f393a31a 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -968,10 +968,17 @@ describe('volume', () => { }); describe('.utimesSync(path, atime, mtime)', () => { const vol = new Volume(); + vol.mkdirSync('/foo'); it('Set times on file', () => { - vol.writeFileSync('/lol', '12345'); - vol.utimesSync('/lol', 1234, 12345); - const stats = vol.statSync('/lol'); + vol.writeFileSync('/foo/lol', '12345'); + vol.utimesSync('/foo/lol', 1234, 12345); + const stats = vol.statSync('/foo/lol'); + expect(Math.round(stats.atime.getTime() / 1000)).toBe(1234); + expect(Math.round(stats.mtime.getTime() / 1000)).toBe(12345); + }); + it('Sets times on a directory', () => { + vol.utimesSync('/foo', 1234, 12345); + const stats = vol.statSync('/foo'); expect(Math.round(stats.atime.getTime() / 1000)).toBe(1234); expect(Math.round(stats.mtime.getTime() / 1000)).toBe(12345); }); diff --git a/src/volume.ts b/src/volume.ts index 4f635324a..18cbf7801 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -1881,7 +1881,7 @@ export class Volume { } private utimesBase(filename: string, atime: number, mtime: number) { - const fd = this.openSync(filename, 'r+'); + const fd = this.openSync(filename, 'r'); try { this.futimesBase(fd, atime, mtime); } finally {