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