From 9cee703e73b1c5fc00411b406dfe04bbe7189ca7 Mon Sep 17 00:00:00 2001 From: Miguel Molina Date: Mon, 8 Jan 2018 18:08:57 +0100 Subject: [PATCH] wip: implement writeFile and writeFileSync Signed-off-by: Miguel Molina --- src/fs.re | 36 +++++++++++++++++++++++++++++++++--- src/fs.rei | 38 +++++++++++++++++--------------------- src/renodeUtils.re | 5 +++++ 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/fs.re b/src/fs.re index 76ef09b..e179ad6 100644 --- a/src/fs.re +++ b/src/fs.re @@ -168,6 +168,36 @@ let writeString = (~fd, ~string, ~offset, ~length) => let writeStringSync = (~fd, ~string, ~offset, ~length) => Unix.write_substring(fd, string, offset, length); -let writeFile = (~file, ~data, ~options) => (); - -let writeFileSync = (~file, ~data, ~options) => (); \ No newline at end of file +type writeFileOptions = { + encoding: option(string), + mode: option(asyncFilePerm), + flags: list(asyncOpenFlag) +}; + +let defaultWriteFileFlags = [Unix.O_CREAT, Unix.O_TRUNC]; + +let writeFile = (~options={encoding: None, mode: None, flags: []}, ~file, ~data) => { + let flags = List.length(options.flags) < 1 ? defaultWriteFileFlags : options.flags; + let mode = RenodeUtils.withDefault(options.mode, ~default=438); + Lwt.bind( + Lwt_unix.openfile(file, flags, mode), + fd => Lwt.bind( + write(fd, data, 0, Bytes.length(data)), + (_) => close(fd) + ) + ) +}; + +type writeFileSyncOptions = { + encoding: option(string), + mode: option(syncFilePerm), + flags: list(syncOpenFlag) +}; + +let writeFileSync = (~options={encoding: None, mode: None, flags: []}, ~file, ~data) => { + let flags = List.length(options.flags) < 1 ? defaultWriteFileFlags : options.flags; + let mode = RenodeUtils.withDefault(options.mode, ~default=438); + let fd = Unix.openfile(file, flags, mode); + let _ = writeSync(fd, data, 0, Bytes.length(data)); + closeSync(fd); +}; \ No newline at end of file diff --git a/src/fs.rei b/src/fs.rei index 6cce62f..f3b368b 100644 --- a/src/fs.rei +++ b/src/fs.rei @@ -363,24 +363,20 @@ let writeString: let writeStringSync: (~fd: syncFileDescr, ~string: string, ~offset: int, ~length: int) => int; -[@ocaml.deprecated - {| - Fs.writeFile has yet to be implemented. - Please open a Work-In-Progress pull request if you are interested in contributing. - We will help answer questions and push you in the right direction. - - Repo URL: https://github.com/kennetpostigo/lwt-node - |} -] -let writeFile: (~file: 'a, ~data: 'b, ~options: 'c) => unit; - -[@ocaml.deprecated - {| - Fs.writeFileSync has yet to be implemented. - Please open a Work-In-Progress pull request if you are interested in contributing. - We will help answer questions and push you in the right direction. - - Repo URL: https://github.com/kennetpostigo/lwt-node - |} -] -let writeFileSync: (~file: 'a, ~data: 'b, ~options: 'c) => unit; \ No newline at end of file +type writeFileOptions = { + encoding: option(string), + mode: option(asyncFilePerm), + flags: list(asyncOpenFlag) +}; + +let writeFile: + (~options: writeFileOptions=?, ~file: string, ~data: bytes) => Node.t(unit); + +type writeFileSyncOptions = { + encoding: option(string), + mode: option(syncFilePerm), + flags: list(syncOpenFlag) +}; + +let writeFileSync: + (~options: writeFileSyncOptions=?, ~file: string, ~data: bytes) => unit; \ No newline at end of file diff --git a/src/renodeUtils.re b/src/renodeUtils.re index 6a744a5..e300dd4 100644 --- a/src/renodeUtils.re +++ b/src/renodeUtils.re @@ -157,4 +157,9 @@ let iterPathUntil = (~condition, ~list, ~f) => { f(arr[position^], position, ! (position^ > (-1))); position := position^ - 1; }; +}; + +let withDefault = (opt, ~default) => switch opt { +| Some(v) => v +| None => default }; \ No newline at end of file