Source file tar_transfer.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
open Lwt.Infix
let ( / ) = Filename.concat
let level = Tar.Header.GNU
module Tar_lwt_unix = struct
include Tar_lwt_unix
module Writer = struct
type out_channel = Lwt_unix.file_descr
type 'a t = 'a Lwt.t
let really_write fd = Lwt_cstruct.(complete (write fd))
end
module HW = Tar.HeaderWriter(Lwt)(Writer)
let write_block ?level (: Tar.Header.t) (body: Lwt_unix.file_descr -> unit Lwt.t) (fd : Lwt_unix.file_descr) =
HW.write ?level header fd
>>= fun () ->
body fd >>= fun () ->
Writer.really_write fd (Tar.Header.zero_padding header)
let write_end (fd: Lwt_unix.file_descr) =
Writer.really_write fd Tar.Header.zero_block >>= fun () ->
Writer.really_write fd Tar.Header.zero_block
end
let copy_to ~dst src =
let len = 4096 in
let buf = Bytes.create len in
let rec aux () =
Lwt_io.read_into src buf 0 len >>= function
| 0 -> Lwt.return_unit
| n -> Os.write_all dst buf 0 n >>= aux
in
aux ()
let copy_file ~src ~dst ~to_untar ~user =
Lwt_unix.LargeFile.lstat src >>= fun stat ->
let hdr = Tar.Header.make
~file_mode:(if stat.Lwt_unix.LargeFile.st_perm land 0o111 <> 0 then 0o755 else 0o644)
~mod_time:(Int64.of_float stat.Lwt_unix.LargeFile.st_mtime)
~user_id:user.Obuilder_spec.uid
~group_id:user.Obuilder_spec.gid
dst stat.Lwt_unix.LargeFile.st_size
in
Tar_lwt_unix.write_block ~level hdr (fun ofd ->
Lwt_io.(with_file ~mode:input) src (copy_to ~dst:ofd)
) to_untar
let copy_symlink ~src ~target ~dst ~to_untar ~user =
Lwt_unix.LargeFile.lstat src >>= fun stat ->
let hdr = Tar.Header.make
~file_mode:0o777
~mod_time:(Int64.of_float stat.Lwt_unix.LargeFile.st_mtime)
~link_indicator:Tar.Header.Link.Symbolic
~link_name:target
~user_id:user.Obuilder_spec.uid
~group_id:user.Obuilder_spec.gid
dst 0L
in
Tar_lwt_unix.write_block ~level hdr (fun _ -> Lwt.return_unit) to_untar
let rec copy_dir ~src_dir ~src ~dst ~(items:(Manifest.t list)) ~to_untar ~user =
Log.debug(fun f -> f "Copy dir %S -> %S@." src dst);
Lwt_unix.LargeFile.lstat (src_dir / src) >>= fun stat ->
begin
let hdr = Tar.Header.make
~file_mode:0o755
~mod_time:(Int64.of_float stat.Lwt_unix.LargeFile.st_mtime)
~user_id:user.Obuilder_spec.uid
~group_id:user.Obuilder_spec.gid
(dst ^ "/") 0L
in
Tar_lwt_unix.write_block ~level hdr (fun _ -> Lwt.return_unit) to_untar
end >>= fun () -> send_dir ~src_dir ~dst ~to_untar ~user items
and send_dir ~src_dir ~dst ~to_untar ~user items =
items |> Lwt_list.iter_s (function
| `File (src, _) ->
let src = src_dir / src in
let dst = dst / Filename.basename src in
copy_file ~src ~dst ~to_untar ~user
| `Symlink (src, target) ->
let src = src_dir / src in
let dst = dst / Filename.basename src in
copy_symlink ~src ~target ~dst ~to_untar ~user
| `Dir (src, items) ->
let dst = dst / Filename.basename src in
copy_dir ~src_dir ~src ~dst ~items ~to_untar ~user
)
let remove_leading_slashes = Astring.String.drop ~sat:((=) '/')
let send_files ~src_dir ~src_manifest ~dst_dir ~user ~to_untar =
let dst = remove_leading_slashes dst_dir in
send_dir ~src_dir ~dst ~to_untar ~user src_manifest >>= fun () ->
Tar_lwt_unix.write_end to_untar
let send_file ~src_dir ~src_manifest ~dst ~user ~to_untar =
let dst = remove_leading_slashes dst in
begin
match src_manifest with
| `File (path, _) ->
let src = src_dir / path in
copy_file ~src ~dst ~to_untar ~user
| `Symlink (src, target) ->
let src = src_dir / src in
copy_symlink ~src ~target ~dst ~to_untar ~user
| `Dir (src, items) ->
copy_dir ~src_dir ~src ~dst ~items ~to_untar ~user
end >>= fun () ->
Tar_lwt_unix.write_end to_untar