Open a file unless already an fd
npm install fs-maybe-openOpen a file unless it's already a file descriptor.
    
``js
const maybeOpen = require('fs-maybe-open')
, fs = require('fs')
function readExactly(fdOrFile, pos, length, done) {
maybeOpen(fdOrFile, 'r', function (err, fd, maybeClose) {
if (err) return done(err)
fs.read(fd, Buffer(length), 0, length, pos, function (err, bytesRead, buf) {
if (err) return maybeClose(done, err)
if (bytesRead !== length) {
return maybeClose(done, new Error('End of file'))
}
maybeClose(done, null, buf)
})
})
}
`
The maybeOpen function has the same signature as [fs.open(path, flags[, mode], callback)](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback). Except:
- If path is a file descriptor, opening is a noopcallback
- The open also receives a maybeClose(callback, err, ...args) function, which calls fs.close for you if path was a filename. An error from fs.close (if any) will be combined with your error (if any).
With npm do:
```
npm install fs-maybe-open
MIT © Vincent Weevers