-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]> <!-- ps-id: 7c730484-ecf9-4dae-9718-9bbd0bd480f5 -->
- Loading branch information
Showing
5 changed files
with
92 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,51 @@ | ||
#include <caml/fail.h> | ||
#include <caml/memory.h> | ||
#include <caml/mlvalues.h> | ||
|
||
#if defined(__APPLE__) | ||
#define _DARWIN_C_SOURCE | ||
#include <caml/threads.h> | ||
#include <caml/unixsupport.h> | ||
#include <copyfile.h> | ||
#include <stdlib.h> | ||
|
||
CAMLprim value stdune_copyfile(value v_from, value v_to) { | ||
CAMLparam2(v_from, v_to); | ||
char *from = caml_stat_strdup(String_val(v_from)); | ||
char *to = caml_stat_strdup(String_val(v_to)); | ||
caml_release_runtime_system(); | ||
/* clonefile doesn't follow symlinks automatically */ | ||
char *real_from = realpath(from, NULL); | ||
caml_stat_free(from); | ||
if (real_from == NULL) { | ||
caml_acquire_runtime_system(); | ||
caml_stat_free(to); | ||
uerror("realpath", Nothing); | ||
} | ||
int ret = copyfile(real_from, to, NULL, COPYFILE_CLONE); | ||
caml_stat_free(to); | ||
free(real_from); | ||
caml_acquire_runtime_system(); | ||
if (ret < 0) { | ||
uerror("copyfile", Nothing); | ||
} | ||
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