Quantcast

Problem in file:rawopen/2

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem in file:rawopen/2

Sumith Gamage
Dear All,

I use following code segment to copy directory in Linux.

    io:fwrite("~p:~p:copy_file(~p, ~p, ~p): Called.~n", [?MODULE, ?LINE,
Src, Dest, Opts]),
    {ok, InFd} = file:rawopen(Src, {binary, read}),
    io:fwrite("~p:~p:copy_file: Opend read file (~p, ~p).~n", [?MODULE,
?LINE, Src, InFd]),
    {ok, OutFd} = file:rawopen(Dest, {binary, write}),
    io:fwrite("~p:~p:copy_file: Opend write file (~p, ~p).~n", [?MODULE,
?LINE, Dest, OutFd]),

It works fine.

I used the same code in Windows it fails with the same even with the same
Erlang version (I tried with 5.5.5 & 5.6 both).

Here is the output (with version 5.5.5):

target_system:199:copy_file("tmp/erts-5.6/bin/epmd", "tmp/bin/epmd",
[preserve])
: Called.

=ERROR REPORT==== 23-Feb-2008::17:39:37 ===
Error in process <0.2389.0> with exit value:
{{badmatch,{error,enoent}},[{target
_system,copy_file,3},{target_system,create,1},{erl_eval,do_apply,5},{shell,e
xprs
,6},{shell,eval_loop,3}]}

** exited: {{badmatch,{error,enoent}},
            [{target_system,copy_file,3},
             {target_system,create,1},
             {erl_eval,do_apply,5},
             {shell,exprs,6},
             {shell,eval_loop,3}]} **

Kindly suggest me a workaround for this.

Thanks in advance...!

Sumith


_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem in file:rawopen/2

Matthias Lang
Sumith Gamage writes:

 > I use following code segment to copy directory in Linux.

 >     {ok, OutFd} = file:rawopen(Dest, {binary, write}),

 > I used the same code in Windows it fails with the same even with the same
 > Erlang version (I tried with 5.5.5 & 5.6 both).

 > Kindly suggest me a workaround for this.

file:rawopen/2 isn't documented.

Looking at the library source, there's a comment above rawopen:

%% Obsolete, undocumented, local node only, don't use!

Matt

_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem in file:rawopen/2

Kostis Sagonas-2
Matthias Lang wrote:

> Sumith Gamage writes:
>
>  > I use following code segment to copy directory in Linux.
>
>  >     {ok, OutFd} = file:rawopen(Dest, {binary, write}),
>
>  > I used the same code in Windows it fails with the same even with the same
>  > Erlang version (I tried with 5.5.5 & 5.6 both).
>
>  > Kindly suggest me a workaround for this.
>
> file:rawopen/2 isn't documented.
>
> Looking at the library source, there's a comment above rawopen:
>
> %% Obsolete, undocumented, local node only, don't use!

Matt is of course right in pointing out these, but this really begs the
questions:

  - Since this function is obsolete, why does it survive across multiple
    OTP releases?

  - Assuming the previous question has a good answer, why aren't such
    functions marked as obsolete so that at least the compiler warns
    for its uses?

Kostis
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem in file:rawopen/2

Matthias Lang

 > > Sumith Gamage writes:
 > >
 > >  > I use following code segment to copy directory in Linux.
 > >
 > >  >     {ok, OutFd} = file:rawopen(Dest, {binary, write}),
 > >
 > >  > I used the same code in Windows it fails with the same even
 > >  > with the same Erlang version (I tried with 5.5.5 & 5.6 both).
 > >
 > >  > Kindly suggest me a workaround for this.

 Matthias>> file:rawopen/2 isn't documented.

 Matthias>> Looking at the library source, there's a comment above
 Matthias>> rawopen:

 Matthias>> %% Obsolete, undocumented, local node only, don't use!

    Kostis> Matt is of course right in pointing out these, but this
    Kostis> really begs the questions:

    Kostis> - Since this function is obsolete, why does it survive
    Kostis> across multiple OTP releases?

    Kostis> - Assuming the previous question has a good answer, why
    Kostis> aren't such functions marked as obsolete so that at least
    Kostis> the compiler warns for its uses?

Taking a few steps back at this problem, Sumith is trying to copy a
directory by opening it as a file and copying the contents. This is a
trick, i.e. it exploits knowledge about how directories are
implemented in some operating systems. He's then combining that first
trick with a second trick, namely using an undocumented function. That
has "will probably blow up in your face" written all over it.

Taking a guess, on Windows directories are implemented such that the
'a directory is a file' abstraction doesn't hold. Maybe someone who
actually knows something about windows can confirm.

Back to Kostis, who already knows the answer to "why is it still
there", but I'll bite anyway:

  ic/src/icenum.erl:    case file:rawopen(FName, {binary, write}) of
  ic/src/icstruct.erl:    case file:rawopen(FName, {binary, write}) of
  ic/src/icstruct.erl:        case file:rawopen(FName, {binary, write}) of
  ic/src/icstruct.erl:    case file:rawopen(FName, {binary, write}) of
  ic/src/icunion.erl:    case file:rawopen(FName, {binary, write}) of
  mnesia/src/mnesia_lib.erl:    case file:rawopen(Fname, read) of
  mnesia/src/mnesia_lib.erl:    case file:rawopen(From, {binary, read}) of
  mnesia/src/mnesia_lib.erl:          case file:rawopen(To, {binary, write}) of

A warning wouldn't hurt, though compiler-administered electric
shocks would be more effective.

Matt
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem in file:rawopen/2

Matthias Lang

 >  > > Sumith Gamage writes:

 >  > >  > I use following code segment to copy directory in Linux.

 >  > >  >     {ok, OutFd} = file:rawopen(Dest, {binary, write}),

 >  > >  > It works fine

If I ignore your claim that the posted code copies a directory,
i.e. assume that nothing clever is going on, then everything gets much
less unlikely*. You probably just want to use

  file:open(Src, [binary, read, raw])

and

  file:open(Dest, [binary, write, raw])call, with the options [raw,

Matt

 * I say that because attempting to open a directory doesn't even
   work on linux. It probably hasn't worked on any unix for about 15
   years.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem in file:rawopen/2

Bjorn Gustavsson
In reply to this post by Kostis Sagonas-2
Kostis Sagonas <[hidden email]> writes:

> Matt is of course right in pointing out these, but this really begs the
> questions:
>
>   - Since this function is obsolete, why does it survive across multiple
>     OTP releases?
>
>   - Assuming the previous question has a good answer, why aren't such
>     functions marked as obsolete so that at least the compiler warns
>     for its uses?

Oversight, I suppose. file:rawopen/2 was kept when the 'raw' flag for file:open/2
was introduced, because of some hairy start-up problem. I think that the need
for file:rawopen/2 disappeared when the prim_file module was introduced.

We will deprecate file:rawopen/2 in R12B-2.

/Bjorn
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem in file:rawopen/2

Bjorn Gustavsson
In reply to this post by Matthias Lang
Matthias Lang <[hidden email]> writes:

>
> Taking a few steps back at this problem, Sumith is trying to copy a
> directory by opening it as a file and copying the contents. This is a
> trick, i.e. it exploits knowledge about how directories are
> implemented in some operating systems. He's then combining that first
> trick with a second trick, namely using an undocumented function. That
> has "will probably blow up in your face" written all over it.

I don't think he actually opens the directory itself. That is supposed
to give an {error,eisdir} on any platform. (Erlang has never allowed
reading directories as files, even on Unix systems.)

> A warning wouldn't hurt, though compiler-administered electric
> shocks would be more effective.

Good idea. :-)

/Bjorn
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Loading...