Skip to content

Commit 658e59c

Browse files
committed
Use the same code path for local and remote syncs
In the Copy module, local syncs do a direct copy and that's it. Remote syncs meanwhile check for already transferred files, check for partially transferred resumable files, can use the rsync algorithm and an external copyprog. Make local and remote syncs use the same code path. The functionality for both cases is now the same, but since the code was optimized for the remote case then there could be some optimization opportunities for local syncs. This is something this patch does not include.
1 parent 5773955 commit 658e59c

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

src/copy.ml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ let rec fingerprintPrefix fspath path offset len accu =
144144
end
145145

146146
let fingerprintPrefixRemotely =
147-
Remote.registerServerCmd
147+
Remote.registerServerCmd'
148148
"fingerprintSubfile"
149149
(fun _ (fspath, path, len) ->
150150
Lwt.return (fingerprintPrefix fspath path 0L len []))
@@ -461,7 +461,7 @@ let compress conn
461461
Util.convertUnixErrorsToTransient "transferring file contents"
462462
(fun () -> raise e))
463463

464-
let compressRemotely = Remote.registerServerCmd "compress" compress
464+
let compressRemotely = Remote.registerServerCmd' "compress" compress
465465

466466
let close_all infd outfd =
467467
Util.convertUnixErrorsToTransient
@@ -957,17 +957,10 @@ let file rootFrom pathFrom rootTo fspathTo pathTo realPathTo
957957
(Fspath.toDebugString fspathTo) (Path.toString pathTo)
958958
(Props.toString desc));
959959
let timer = Trace.startTimer "Transmitting file" in
960-
begin match rootFrom, rootTo with
961-
(Common.Local, fspathFrom), (Common.Local, realFspathTo) ->
962-
localFile
963-
fspathFrom pathFrom fspathTo pathTo realPathTo
964-
update desc (Osx.ressLength ress) (Some id);
965-
paranoidCheck fspathTo pathTo realPathTo desc fp ress
966-
| _ ->
967-
transferFile
968-
rootFrom pathFrom rootTo fspathTo pathTo realPathTo
969-
update desc fp ress id
970-
end >>= fun status ->
960+
transferFile
961+
rootFrom pathFrom rootTo fspathTo pathTo realPathTo
962+
update desc fp ress id
963+
>>= fun status ->
971964
Trace.showTimer timer;
972965
match status with
973966
TransferSucceeded info ->

src/remote.ml

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ type servercmd =
602602
let serverCmds = ref (Util.StringMap.empty : servercmd Util.StringMap.t)
603603

604604
type serverstream =
605-
connection -> Bytearray.t -> unit
605+
connection option -> Bytearray.t -> unit
606606
let serverStreams = ref (Util.StringMap.empty : serverstream Util.StringMap.t)
607607

608608
type header =
@@ -660,7 +660,7 @@ let processStream conn id cmdName buf =
660660
try Util.StringMap.find cmdName !serverStreams
661661
with Not_found -> raise (Util.Fatal (cmdName ^ " not registered!"))
662662
in
663-
cmd conn buf;
663+
cmd (Some conn) buf;
664664
Lwt.return ()
665665
with e ->
666666
Hashtbl.add streamError id e;
@@ -789,6 +789,17 @@ let registerServerCmd name f =
789789
registerSpecialServerCmd
790790
name defaultMarshalingFunctions defaultMarshalingFunctions f
791791

792+
(* Same as [registerServerCmd] but returns a function that runs either
793+
the proxy or the local version, depending on whether the call is to
794+
the local host (in this case [conn] is None) or a remote one. *)
795+
let registerServerCmd' name f =
796+
let serverSide = (fun conn args -> f (Some conn) args) in
797+
let client0 = registerServerCmd name serverSide in
798+
fun conn args ->
799+
match conn with
800+
| None -> f None args
801+
| Some conn -> client0 conn args
802+
792803
(* RegisterHostCmd is a simpler version of registerClientServer [registerServerCmd?].
793804
It is used to create remote procedure calls: the only communication
794805
between the client and server is the sending of arguments from
@@ -826,16 +837,16 @@ let registerRootCmd (cmdName : string) (cmd : (Fspath.t * 'a) -> 'b) =
826837
fun root args -> r (hostOfRoot root) ((snd root), args)
827838

828839
let registerRootCmdWithConnection
829-
(cmdName : string) (cmd : connection -> 'a -> 'b) =
830-
let client0 = registerServerCmd cmdName cmd in
840+
(cmdName : string) (cmd : connection option -> 'a -> 'b) =
841+
let serverSide = (fun conn args -> cmd (Some conn) args) in
842+
let client0 = registerServerCmd cmdName serverSide in
831843
(* Return a function that runs either the proxy or the local version,
832844
depending on whether the call is to the local host or a remote one *)
833845
fun localRoot remoteRoot args ->
834-
match (hostOfRoot localRoot) with
835-
"" -> let conn = hostConnection (hostOfRoot remoteRoot) in
836-
cmd conn args
837-
| _ -> let conn = hostConnection (hostOfRoot localRoot) in
838-
client0 conn args
846+
match hostOfRoot localRoot, hostOfRoot remoteRoot with
847+
| "", "" -> cmd None args
848+
| "", _ -> cmd (Some (connectionToRoot remoteRoot)) args
849+
| _ -> client0 (connectionToRoot localRoot) args
839850

840851
let streamReg = Lwt_util.make_region 1
841852

@@ -849,12 +860,12 @@ let streamingActivated =
849860
let registerStreamCmd
850861
(cmdName : string)
851862
marshalingFunctionsArgs
852-
(serverSide : connection -> 'a -> unit)
863+
(serverSide : connection option -> 'a -> unit)
853864
=
854865
let cmd =
855866
registerSpecialServerCmd
856867
cmdName marshalingFunctionsArgs defaultMarshalingFunctions
857-
(fun conn v -> serverSide conn v; Lwt.return ())
868+
(fun conn v -> serverSide (Some conn) v; Lwt.return ())
858869
in
859870
let ping =
860871
registerServerCmd (cmdName ^ "Ping")
@@ -889,7 +900,7 @@ let registerStreamCmd
889900
in
890901
dumpIdle conn request
891902
in
892-
fun conn sender ->
903+
let proxy conn sender =
893904
if not (Prefs.read streamingActivated) then
894905
sender (fun v -> cmd conn v)
895906
else begin
@@ -905,6 +916,11 @@ let registerStreamCmd
905916
Util.msg "Pinging remote end after streaming error\n");
906917
ping conn id >>= fun () -> Lwt.fail e)
907918
end
919+
in
920+
fun conn sender ->
921+
match conn with
922+
| None -> sender (fun v -> Lwt.return (serverSide conn v))
923+
| Some conn -> proxy conn sender
908924

909925
let commandAvailable =
910926
registerRootCmd "commandAvailable"

src/remote.mli

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ val connectionToRoot : Common.root -> connection
9191

9292
val registerServerCmd :
9393
string -> (connection -> 'a -> 'b Lwt.t) -> connection -> 'a -> 'b Lwt.t
94+
val registerServerCmd' :
95+
string -> (connection option -> 'a -> 'b Lwt.t) -> connection option -> 'a -> 'b Lwt.t
9496
val registerSpecialServerCmd :
9597
string ->
9698
('a ->
@@ -109,7 +111,7 @@ val encodeInt : int -> Bytearray.t * int * int
109111
val decodeInt : Bytearray.t -> int -> int
110112
val registerRootCmdWithConnection :
111113
string (* command name *)
112-
-> (connection -> 'a -> 'b Lwt.t) (* local command *)
114+
-> (connection option -> 'a -> 'b Lwt.t) (* local command *)
113115
-> Common.root (* root on which the command is executed *)
114116
-> Common.root (* other root *)
115117
-> 'a (* additional arguments *)
@@ -122,5 +124,5 @@ val registerStreamCmd :
122124
('a ->
123125
(Bytearray.t * int * int) list -> (Bytearray.t * int * int) list * int) *
124126
(Bytearray.t -> int -> 'a) ->
125-
(connection -> 'a -> unit) ->
126-
connection -> (('a -> unit Lwt.t) -> 'b Lwt.t) -> 'b Lwt.t
127+
(connection option -> 'a -> unit) ->
128+
connection option -> (('a -> unit Lwt.t) -> 'b Lwt.t) -> 'b Lwt.t

0 commit comments

Comments
 (0)