Skip to content

Commit

Permalink
fix(sendfile): Flush the out_channel used in the fallback path (#8288)
Browse files Browse the repository at this point in the history
Fixes #8284

Signed-off-by: Alan Hu <alanh@ccs.neu.edu>
  • Loading branch information
alan-j-hu authored Jul 28, 2023
1 parent a358079 commit 22c63df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
----------

- Fix flushing when using `sendfile` fallback (#8288, @alan-j-hu)

- Add `dune show rules` as alias of the `dune rules` command. (#8000, @Alizter)

- Fix `%{deps}` to expand properly in `(cat ...)` when containing 2 or more
Expand Down
28 changes: 17 additions & 11 deletions otherlibs/stdune/src/io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,23 @@ module Copyfile = struct
| Error `Src_missing ->
let message = Printf.sprintf "%s: No such file or directory" src in
raise (Sys_error message)
| Ok (src, dst, src_size) ->
Exn.protect
~f:(fun () ->
try sendfile ~src ~dst src_size
with Unix.Unix_error (EINVAL, "sendfile", _) ->
let ic = Unix.in_channel_of_descr src in
let oc = Unix.out_channel_of_descr dst in
copy_channels ic oc)
~finally:(fun () ->
Unix.close src;
Unix.close dst)
| Ok (src, dst, src_size) -> (
let close_fds () =
Unix.close src;
Unix.close dst
in
match sendfile ~src ~dst src_size with
| exception Unix.Unix_error (EINVAL, "sendfile", _) ->
let ic = Unix.in_channel_of_descr src in
let oc = Unix.out_channel_of_descr dst in
Exn.protect
~f:(fun () -> copy_channels ic oc)
~finally:(fun () ->
(* we make sure to close the fd's with the channel api to make
sure everything has been flushed *)
close_both (ic, oc))
| () -> close_fds ()
| exception _ -> close_fds ())

let copyfile ?chmod ~src ~dst () =
let src_stats =
Expand Down
2 changes: 1 addition & 1 deletion test/blackbox-tests/test-cases/github8041.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ larger files.
> else
> echo "File copied incorrectly"
> fi
File copied incorrectly
File copied correctly

0 comments on commit 22c63df

Please sign in to comment.