Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 9, 2025

This PR fixes an issue where readFile and readFileSync incorrectly updated both atime (access time) and ctime (change time) when reading files. According to POSIX standards and real Node.js behavior, reading a file should only update atime, not ctime.

Problem

The issue occurred because:

  1. The atime setter in Node.ts automatically updated ctime whenever atime was set
  2. getBuffer() called setBuffer() when initializing empty buffers, which triggered touch() and updated both mtime and ctime

This caused memfs to behave differently from the native Node.js fs module:

// With memfs (before fix)
await fs.promises.writeFile('/test.txt', 'content');
const before = (await fs.promises.stat('/test.txt')).ctimeMs;
await fs.promises.readFile('/test.txt'); // This incorrectly updated ctime
const after = (await fs.promises.stat('/test.txt')).ctimeMs;
console.log(before !== after); // true (wrong!)

// With native Node.js fs
// The ctime remains unchanged after readFile

Solution

Core fixes:

  • Removed automatic ctime update from the atime setter in src/core/Node.ts
  • Fixed getBuffer() to directly initialize empty buffers without calling setBuffer() (which was triggering unnecessary touch() calls)

Test updates:

  • Updated existing Node-level tests to expect correct behavior
  • Added comprehensive integration tests specifically for the readFile/readFileSync ctime behavior

Result

  • readFile and readFileSync now correctly update only atime when reading files
  • ctime is only updated for actual metadata/content changes (chmod, chown, write operations, etc.)
  • Behavior now matches real Node.js fs implementation
  • All existing tests pass

The changes are minimal and surgical, affecting only the specific timestamp update logic without changing any other filesystem behavior.

Fixes #1177.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] readFile changes ctime fix: prevent readFile from updating ctime when only accessing files Sep 9, 2025
@Copilot Copilot AI requested a review from streamich September 9, 2025 13:06
Copilot finished work on behalf of streamich September 9, 2025 13:06
@streamich streamich marked this pull request as ready for review September 9, 2025 13:31
@streamich streamich merged commit c9c6e25 into master Sep 9, 2025
20 checks passed
@streamich streamich deleted the copilot/fix-memfs-ctime-issue branch September 9, 2025 13:37
Copy link

github-actions bot commented Sep 9, 2025

🎉 This PR is included in version 4.38.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

readFile changes ctime
2 participants