Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ node_js:
- '6'
- '8'
- node
os:
- windows
- osx
- linux
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,25 @@ function open (self, mode, req) {

function onopen (err, fd) {
if (err) return req.callback(err)

var old = self.fd
self.fd = fd
if (!old) return oncloseoldfd(null)

// if we are moving from readonly -> readwrite, close the old fd
fs.close(old, oncloseoldfd)
// fs.open on Linux fails with an error on a directory, but on Windows it
// returns a file descriptor to the dir, which we don't want.
// https://nodejs.org/api/fs.html#fs_file_system_flags
fs.fstat(fd, function (err, stats) {
if (err) return onerrorafteropen(err)
if (stats.isDirectory()) {
var error = new Error('EISDIR: illegal operation on a directory, open \'' + self.filename + '\'')
error.code = 'EISDIR'
return onerrorafteropen(error)
}

if (!old) return oncloseoldfd(null)

// if we are moving from readonly -> readwrite, close the old fd
fs.close(old, oncloseoldfd)
})
}

function oncloseoldfd (err) {
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ tape('write/read big chunks', function (t) {
}
})

tape('destroy', function (t) {
var name = gen()
var file = raf(name)

file.write(0, Buffer.from('hi'), function (err) {
t.error(err, 'no error')
file.read(0, 2, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.from('hi'))
file.destroy(ondestroy)
})
})

function ondestroy (err) {
t.error(err, 'no error')
fs.stat(name, function (err) {
t.same(err && err.code, 'ENOENT', 'should be removed')
t.end()
})
}
})

tape('rmdir option', function (t) {
var name = path.join('rmdir', ++i + '', 'folder', 'test.txt')
var file = raf(name, {rmdir: true, directory: tmp})
Expand Down