janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.38k stars 217 forks source link

os/open fails to create a file unless it's opened read/write-only #1469

Closed sarna closed 1 week ago

sarna commented 1 week ago

When the file is opened in O_RDWR mode, the O_CREAT flag is discarded here: https://github.com/janet-lang/janet/blob/a5d6b2283834422a9fa9e79b5c7ad9b932b52568/src/core/os.c#L2671

So, this works:

repl:1:> (os/open "foo" :cr)
<core/stream <core/stream handle=6>>
repl:2:> (os/open "foo" :cw)
<core/stream <core/stream handle=7>>

But this doesn't:

repl:4:> (os/open "bar" :c)
error: No such file or directory
  in os/open [src/core/os.c] on line 2524
  in thunk [repl] (tail call) on line 4, column 1
repl:5:> (os/open "bar" :crw)
error: No such file or directory
  in os/open [src/core/os.c] on line 2524
  in thunk [repl] (tail call) on line 5, column 1

Kudos to @sogaiu for locating the bug on Zulip :)

sogaiu commented 1 week ago

@sarna May be as you hinted at here, it is a matter of changing the mentioned line to be:

 open_flags |= O_RDWR;

I tried the suggested change and it seemed to work better.

Care to make a PR?


Update: Ah, looks like it has been taken care of :)