Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.open fails on directory #58

Closed
kirilknysh opened this issue Jan 5, 2018 · 7 comments
Closed

fs.open fails on directory #58

kirilknysh opened this issue Jan 5, 2018 · 7 comments

Comments

@kirilknysh
Copy link
Contributor

memfs version: 2.6.0
OS version: macOS High Sierra 10.13.2 (17C88)

fs.open with read flag fails on directory. Though Nodejs' fs module allows to "open" folder.

A short code snippet to reproduce the issue:

const fs = require('fs');
const path = require('path');

const memfs = require('memfs');

const vol = new memfs.Volume();
const dir = path.resolve(process.cwd(), 'open-test');
vol.mkdirpSync(dir);
const fsmem = memfs.createFsFromVolume(vol);

fs.open(dir, 'r', (err, fd) => {
    console.log('fs', err);
});

fsmem.open(dir, 'r', (err, fd) => {
    console.log('memfs', err);
});

produces the output:

fs null
memfs { Error: EISDIR: illegal operation on a directory, open '/Users/kirilknysh/Projects/memfs-open/open-test'
    at createError (/Users/kirilknysh/Projects/memfs-open/node_modules/memfs/lib/volume.js:105:17)
    at throwError (/Users/kirilknysh/Projects/memfs-open/node_modules/memfs/lib/volume.js:114:11)
    at Volume.openLink (/Users/kirilknysh/Projects/memfs-open/node_modules/memfs/lib/volume.js:716:13)
    at Volume.openFile (/Users/kirilknysh/Projects/memfs-open/node_modules/memfs/lib/volume.js:748:25)
    at Volume.openBase (/Users/kirilknysh/Projects/memfs-open/node_modules/memfs/lib/volume.js:752:25)
    at Immediate.<anonymous> (/Users/kirilknysh/Projects/memfs-open/node_modules/memfs/lib/volume.js:623:39)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5) code: 'EISDIR' }

Why to open directory? Sometimes, to ensure that a new file is created and exists in directory listing - fsync on parent directory must be called. Some details could be found https://www.quora.com/When-should-you-fsync-the-containing-directory-in-addition-to-the-file-itself .

@kirilknysh kirilknysh changed the title fs.open fails on directory fs.open fails on directory Jan 5, 2018
@streamich
Copy link
Owner

It should probably emulate Node.js behaviour:

Note: The behavior of fs.open() is platform-specific for some flags. As such, opening a directory on macOS and Linux with the 'a+' flag - see example below - will return an error. In contrast, on Windows and FreeBSD, a file descriptor will be returned.

// macOS and Linux
fs.open('<directory>', 'a+', (err, fd) => {
  // => [Error: EISDIR: illegal operation on a directory, open <directory>]
});

// Windows and FreeBSD
fs.open('<directory>', 'a+', (err, fd) => {
  // => null, <fd>
});

@kirilknysh
Copy link
Contributor Author

For a+ flag - yes, and this is correct. But for r flag - the behaviour is different.

@kirilknysh
Copy link
Contributor Author

Also, the open(2) docs say:

image

@kirilknysh
Copy link
Contributor Author

Please, take a look at #59

@streamich
Copy link
Owner

Looks good, thx, I've merged it.

@streamich
Copy link
Owner

It has been released here:

https://github.com/streamich/memfs/releases/tag/v2.6.1

@kirilknysh
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants