Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 25, 2025

The copyFile and copyFileSync operations were incorrectly failing when trying to copy readonly files (e.g., files with mode 0o400). This behavior was inconsistent with Node.js's native filesystem, which allows copying readonly files since copying only requires read access to the source file.

Root Cause

The issue was in the permission check logic in src/core/Superblock.ts at line 452. The condition !(flagsNum & O_RDONLY) was always evaluating to true because O_RDONLY = 0. This caused write permission checks to be performed even when opening files for read-only access.

// Before (incorrect)
if (!(flagsNum & O_RDONLY)) {  // Always true since O_RDONLY = 0
  if (!node.canWrite()) {
    throw createError(ERROR_CODE.EACCES, 'open', link.getPath());
  }
}

Solution

Fixed the permission check logic to properly distinguish between read and write access:

// After (correct)
// For read access: check if flags are O_RDONLY or O_RDWR (i.e., not only O_WRONLY)
if ((flagsNum & (O_RDONLY | O_RDWR | O_WRONLY)) !== O_WRONLY) {
  if (!node.canRead()) {
    throw createError(ERROR_CODE.EACCES, 'open', link.getPath());
  }
}
// For write access: check if flags are O_WRONLY or O_RDWR
if (flagsNum & (O_WRONLY | O_RDWR)) {
  if (!node.canWrite()) {
    throw createError(ERROR_CODE.EACCES, 'open', link.getPath());
  }
}

Testing

Added comprehensive test coverage for both synchronous and asynchronous copyFile operations on readonly files:

// This now works correctly (previously threw EACCES)
const vol = create({ '/foo': 'hello world' });
vol.chmodSync('/foo', 0o400); // read-only for owner
vol.copyFileSync('/foo', '/bar'); // ✅ succeeds

The fix maintains backward compatibility and doesn't affect existing permission checks for truly inaccessible files (e.g., files with mode 0o333 that lack read permissions).

Fixes #1175.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 25, 2025 19:29
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
…erations

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Cannot copy readonly file Aug 25, 2025
Copilot AI requested a review from streamich August 25, 2025 19:36
@streamich streamich marked this pull request as ready for review August 26, 2025 13:21
@streamich streamich merged commit bccc0b0 into master Aug 26, 2025
20 checks passed
@streamich streamich deleted the copilot/fix-1175 branch August 26, 2025 13:25
@github-actions
Copy link
Contributor

🎉 This PR is included in version 4.38.2 🎉

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

2 participants