Current section

Files

Jump to
gladvent src ffi@file.erl
Raw

src/ffi@file.erl

-module(ffi@file).
-compile(no_auto_import).
-export([open_file/2, open_file_exclusive/1, write_file/2, open_and_write_exclusive/2, ensure_dir/1]).
-export_type([io_device/0, file_mode/0, write_result/0]).
-type io_device() :: any().
-type file_mode() :: read | write | append | exclusive.
-type write_result() :: ok | {error, gleam@erlang@file:reason()}.
-spec open_file(binary(), list(file_mode())) -> {ok, io_device()} |
{error, gleam@erlang@file:reason()}.
open_file(A, B) ->
file:open(A, B).
-spec open_file_exclusive(binary()) -> {ok, io_device()} |
{error, gleam@erlang@file:reason()}.
open_file_exclusive(S) ->
file:open(S, [exclusive]).
-spec write_file(io_device(), binary()) -> {ok, nil} |
{error, gleam@erlang@file:reason()}.
write_file(Iod, S) ->
case file:write(Iod, gleam@erlang@charlist:from_string(S)) of
ok ->
{ok, nil};
{error, Reason} ->
{error, Reason}
end.
-spec open_and_write_exclusive(binary(), binary()) -> {ok, nil} |
{error, gleam@erlang@file:reason()}.
open_and_write_exclusive(Path, Contents) ->
case open_file_exclusive(Path) of
{error, _try} -> {error, _try};
{ok, Iod} ->
write_file(Iod, Contents)
end.
-spec ensure_dir(binary()) -> {ok, nil} | {error, gleam@erlang@file:reason()}.
ensure_dir(Dir) ->
case filelib:ensure_dir(Dir) of
ok ->
{ok, nil};
{error, Reason} ->
{error, Reason}
end.