Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

fs.unlink() treats permissions differently on Windows vs. Unix #3006

@arturadib

Description

@arturadib

The following test creates a file with read-only permissions in a writeable directory, and attempts to unlink the file. It passes on Linux/Darwin, but on Windows it throws the EPERM exception:

var fs = require('fs'),
    path = require('path'),
    assert = require('assert'),
    dir = 'tmp_'+Date.now(),
    file = dir+'/file.txt';

var existsSync = fs.existsSync || path.existsSync;

console.log('working on dir:', dir);

fs.mkdirSync(dir);
fs.writeFileSync(file, 'asdf');
fs.chmodSync(file, '0444'); // -r--r--r--
fs.unlinkSync(file);

assert.equal(existsSync(file), false); // file should be erased since dir is writeable

Apparently on Windows fs.unlink() expects the file to be writeable in order to remove it, whereas on Unix it suffices for the parent directory to be writeable in order to remove/move files (see e.g. http://www.greenend.org.uk/rjk/tech/perms.html).

Should the unlink behavior be normalized across platforms, or is this behavior really supposed to be platform-dependent?

PS: The issue is true for both fs.unlink() and fs.unlinkSync()


Used: Node version 0.6.14
Tested on: Windows 7, Windows 2008, Mac OS X, and Ubuntu

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions