From bd3f4f05bb6dafdedbae5671ee50c769a3560021 Mon Sep 17 00:00:00 2001
From: Andre Nathan
Date: Thu, 18 Apr 2019 17:45:04 -0300
Subject: file: allow open/2 to work on directories
This is useful mainly to ensure that a new file has been persisted
to disk by calling file:sync/1 or file:datasync/1 on file's parent
directory.
---
lib/kernel/doc/src/file.xml | 10 ++++++++--
lib/kernel/src/file.erl | 4 ++--
lib/kernel/test/file_SUITE.erl | 11 +++++++++++
3 files changed, 21 insertions(+), 4 deletions(-)
(limited to 'lib')
diff --git a/lib/kernel/doc/src/file.xml b/lib/kernel/doc/src/file.xml
index fc25e83d40..b3e8149cc2 100644
--- a/lib/kernel/doc/src/file.xml
+++ b/lib/kernel/doc/src/file.xml
@@ -939,6 +939,10 @@ f.txt: {person, "kalle", 25}.
support for POSIX O_SYNC or equivalent, use of the sync
flag causes open to return {error, enotsup}.
+ directory
+ -
+
Allows open to work on directories.
+
Returns:
@@ -985,8 +989,10 @@ f.txt: {person, "kalle", 25}.
enotdir
-
-
A component of the filename is not a directory. On some
- platforms, enoent is returned instead.
+ A component of the filename is not a directory, or the
+ filename itself is not a directory if directory
+ mode was specified. On some platforms, enoent is
+ returned instead.
enospc
-
diff --git a/lib/kernel/src/file.erl b/lib/kernel/src/file.erl
index 1d4e37196c..a0616da670 100644
--- a/lib/kernel/src/file.erl
+++ b/lib/kernel/src/file.erl
@@ -460,7 +460,7 @@ raw_write_file_info(Name, #file_info{} = Info) ->
-spec open(File, Modes) -> {ok, IoDevice} | {error, Reason} when
File :: Filename | iodata(),
Filename :: name_all(),
- Modes :: [mode() | ram],
+ Modes :: [mode() | ram | directory],
IoDevice :: io_device(),
Reason :: posix() | badarg | system_limit.
@@ -1143,7 +1143,7 @@ path_script(Path, File, Bs) ->
{ok, IoDevice, FullName} | {error, Reason} when
Path :: [Dir :: name_all()],
Filename :: name_all(),
- Modes :: [mode()],
+ Modes :: [mode() | directory],
IoDevice :: io_device(),
FullName :: filename_all(),
Reason :: posix() | badarg | system_limit.
diff --git a/lib/kernel/test/file_SUITE.erl b/lib/kernel/test/file_SUITE.erl
index 3bc8e6e828..21aaefa654 100644
--- a/lib/kernel/test/file_SUITE.erl
+++ b/lib/kernel/test/file_SUITE.erl
@@ -987,6 +987,14 @@ new_modes(Config) when is_list(Config) ->
ok
end,
+ % open directory
+ {ok, Fd9} = ?FILE_MODULE:open(NewDir, [directory]),
+ ok = ?FILE_MODULE:close(Fd9),
+
+ % open raw directory
+ {ok, Fd10} = ?FILE_MODULE:open(NewDir, [raw, directory]),
+ ok = ?FILE_MODULE:close(Fd10),
+
[] = flush(),
ok.
@@ -1236,6 +1244,9 @@ open_errors(Config) when is_list(Config) ->
{error, E4} = ?FILE_MODULE:open(DataDirSlash, [write]),
{eisdir,eisdir,eisdir,eisdir} = {E1,E2,E3,E4},
+ Real = filename:join(DataDir, "realmen.html"),
+ {error, enotdir} = ?FILE_MODULE:open(Real, [directory]),
+
[] = flush(),
ok.
--
cgit v1.2.3