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

ERL-1298: Ram files cannot be open in raw mode #4377

Closed
OTP-Maintainer opened this issue Jul 1, 2020 · 4 comments
Closed

ERL-1298: Ram files cannot be open in raw mode #4377

OTP-Maintainer opened this issue Jul 1, 2020 · 4 comments
Labels
bug Issue is reported as a bug priority:medium team:VM Assigned to OTP team VM
Milestone

Comments

@OTP-Maintainer
Copy link

Original reporter: JIRAUSER15707
Affected version: OTP-22.0
Fixed in version: OTP-24.0
Component: kernel
Migrated from: https://bugs.erlang.org/browse/ERL-1298


When opening a file with both "ram" and "raw" modes I expect to open a ram file that is interfaced through a file descriptor (hence "raw") instead of via a process, but instead of a ram file what is opened is a disk file; that is, the "ram" mode is simply ignored.

For example, the following code opens the file named "hello" on disk instead of ram, and writes " world" to it,
{code:java}
Erlang/OTP 22 [erts-10.7.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Eshell V10.7.1  (abort with ^G)
1> {ok, File} = file:open(<<"hello">>, [ram, raw, write, read]).
{ok,{file_descriptor,raw_file_io_list,
                     {file_descriptor,prim_file,
                                      #{handle => #Ref<0.708897726.1743126542.99290>,
                                        owner => <0.80.0>,r_ahead_size => 0,
                                        r_buffer => #Ref<0.708897726.1743126530.99559>}}}}
2> file:position(File, eof).
{ok,36}
3> file:write(File, <<" world">>).
ok
4> {ok, Size} = file:position(File, eof).
{ok,42}
5> file:position(File, bof).
{ok,0}
6> {ok, Bytes} = file:read(File, Size).
{ok," world"}
7> file:close(File).
ok
8>{code}
My expectation was to see Bytes taking the value of "hello world" not " world", and for no disk file to be created.

In the next snippet, observe how the just created file e opened again and grows:

 

 
{code:java}
Erlang/OTP 22 [erts-10.7.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Eshell V10.7.1 (abort with ^G)
1> {ok, File} = file:open(<<"hello">>, [ram, raw, write, read]).
{ok,{file_descriptor,raw_file_io_list,
 {file_descriptor,prim_file,
 #{handle => #Ref<0.1917879854.3013476362.109932>,
 owner => <0.80.0>,r_ahead_size => 0,
 r_buffer => #Ref<0.1917879854.3013476354.110173>}}}}
2> file:position(File, eof).
{ok,12}
3> file:write(File, <<" world">>).
ok
4> {ok, Size} = file:position(File, eof).
{ok,18}
5> file:position(File, bof).
{ok,0}
6> {ok, Bytes} = file:read(File, Size).
{ok," world world"}
7> file:close(File).
ok
8>
{code}
 

I am not clear if this is a bug or an undocumented limitation of "file:open" function, so either the codebase must be fixed to support both modes simultaneously or the documentation should take note of this limitation.

Thanks.

 
@OTP-Maintainer
Copy link
Author

josevalim said:

"ram" and "raw" modes are exclusive. What is happening here is that, if both are given, "raw" always wins, as you can see here: [https://github.com/erlang/otp/blob/eaab8962c2f75a286f8e36d278613e308ec7e4bc/lib/kernel/src/file.erl#L518]

 

Perhaps a clause should be added to raise if both are given, but that could be seen as a breaking change. If that's the case, documenting if both raw and ram is given, raw "wins".

 

I will be glad to submit a PR either way.

@OTP-Maintainer
Copy link
Author

lukas said:

We would gladly accept a PR to master that throws an exception if both ram and raw are given.

@OTP-Maintainer
Copy link
Author

josevalim said:

Done here: https://github.com/erlang/otp/pull/2723

@OTP-Maintainer
Copy link
Author

josevalim said:

Btw, this can be closed. :)

@OTP-Maintainer OTP-Maintainer added bug Issue is reported as a bug team:VM Assigned to OTP team VM priority:medium labels Feb 10, 2021
@OTP-Maintainer OTP-Maintainer added this to the OTP-24.0 milestone Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug priority:medium team:VM Assigned to OTP team VM
Projects
None yet
Development

No branches or pull requests

1 participant