Skip to content

[WasmFS] Fix argument name for open in the Node backend. NFC#26597

Open
kleisauke wants to merge 1 commit intoemscripten-core:mainfrom
kleisauke:wasmfs-fix-arg-name
Open

[WasmFS] Fix argument name for open in the Node backend. NFC#26597
kleisauke wants to merge 1 commit intoemscripten-core:mainfrom
kleisauke:wasmfs-fix-arg-name

Conversation

@kleisauke
Copy link
Copy Markdown
Collaborator

Split out from #24733.


// Open the file and return the underlying file descriptor.
[[nodiscard]] int _wasmfs_node_open(const char* path, const char* mode);
[[nodiscard]] int _wasmfs_node_open(const char* path, const char* flags);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't mode the correct name for the string arguments like w+ etc? i.e. its a string which matches FILE *fdopen(int fd, const char *mode); so mode makes sense to me.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to follow Node.js conventions here:
https://nodejs.org/api/fs.html#fsopensyncpath-flags-mode

At first, I wondered "Why is mode being passed to flags?" and made this change to prevent that confusion.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.. well thats confusing. C calls the r/w/a string things mode.. not flags, but node calls is falgs. I guess maybe worth adding a comment for this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit confusing indeed. I think it's only a issue with fs.open{sync,}() though, as that accepts flags (which can also be a number) and mode (corresponding to mode_t in native code).

int open(const char *filename, int flags, ...)
{
mode_t mode = 0;
if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
int fd = __sys_open_cp(filename, flags, mode);

As possible follow-up, perhaps we could do?:

Suggested change
[[nodiscard]] int _wasmfs_node_open(const char* path, const char* flags);
[[nodiscard]] int _wasmfs_node_open(const char* path, int flags);

That would make it less confusing, as it would align with POSIX open(2) (and would avoid the need of UTF8ToString()).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... actually, we can't change const char* flags to int flags as Node.js's fs.constants are derived from the underlying file system constants, which can vary.

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

Successfully merging this pull request may close these issues.

2 participants