-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: use clonefile on macos (#7210)
Use mac's [clonefile] instead of manually copying. [clonefile] is like hardlink but it will copy-on-write when edited. Signed-off-by: Rudi Grinberg <[email protected]>
- Loading branch information
Showing
5 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
open Stdune | ||
|
||
let dir = Temp.create Dir ~prefix:"copyfile" ~suffix:"bench" | ||
|
||
let contents = String.make 50_000 '0' | ||
|
||
let () = | ||
let src = Path.relative dir "initial" in | ||
Io.write_file (Path.relative dir "initial") contents; | ||
let chmod _ = 444 in | ||
for i = 1 to 10_000 do | ||
let dst = Path.relative dir (sprintf "dst-%d" i) in | ||
Io.copy_file ~chmod ~src ~dst () | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
(executable | ||
(name copyfile) | ||
(modules copyfile) | ||
(libraries stdune)) | ||
|
||
(executable | ||
(name main) | ||
(modules main) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <caml/fail.h> | ||
#include <caml/memory.h> | ||
#include <caml/mlvalues.h> | ||
|
||
#if defined(__APPLE__) | ||
#define _DARWIN_C_SOURCE | ||
|
||
#include <caml/alloc.h> | ||
#include <caml/threads.h> | ||
#include <caml/unixsupport.h> | ||
|
||
#include <copyfile.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/syslimits.h> | ||
|
||
CAMLprim value stdune_copyfile(value v_from, value v_to) { | ||
CAMLparam2(v_from, v_to); | ||
caml_unix_check_path(v_from, "copyfile"); | ||
caml_unix_check_path(v_to, "copyfile"); | ||
char from[PATH_MAX]; | ||
char to[PATH_MAX]; | ||
char real_from[PATH_MAX]; | ||
int from_len = caml_string_length(v_from); | ||
int to_len = caml_string_length(v_to); | ||
memcpy(from, String_val(v_from), from_len); | ||
memcpy(to, String_val(v_to), to_len); | ||
from[from_len] = '\0'; | ||
to[to_len] = '\0'; | ||
|
||
caml_release_runtime_system(); | ||
/* clonefile doesn't follow symlinks automatically */ | ||
char *realpath_result = realpath(from, real_from); | ||
if (realpath_result == NULL) { | ||
caml_acquire_runtime_system(); | ||
uerror("realpath", v_from); | ||
} | ||
/* nor does it automatically overwrite the target */ | ||
int ret = unlink(to); | ||
if (ret < 0 && errno != ENOENT) { | ||
caml_acquire_runtime_system(); | ||
uerror("unlink", v_to); | ||
} | ||
ret = copyfile(real_from, to, NULL, COPYFILE_CLONE); | ||
caml_acquire_runtime_system(); | ||
if (ret < 0) { | ||
uerror("copyfile", v_to); | ||
} | ||
CAMLreturn(Val_unit); | ||
} | ||
|
||
CAMLprim value stdune_is_darwin(value v_unit) { | ||
CAMLparam1(v_unit); | ||
CAMLreturn(Val_true); | ||
} | ||
|
||
#else | ||
|
||
CAMLprim value stdune_copyfile(value v_from, value v_to) { | ||
caml_failwith("copyfile: only on macos"); | ||
} | ||
|
||
CAMLprim value stdune_is_darwin(value v_unit) { | ||
CAMLparam1(v_unit); | ||
CAMLreturn(Val_false); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters