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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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 get_ids = function
| `Unix user -> Some user.Obuilder_spec.uid, Some user.gid, None, None
| `Windows user when user.Obuilder_spec.name = "ContainerAdministrator" ->
let x = 93 and rid = 1 in
Some (0x1000 * x + rid), Some (0x1000 * x + rid), Some user.name, Some user.name
| `Windows _ -> None, None, None, None
let copy_file ~src ~dst ~to_untar ~user =
Lwt_unix.LargeFile.lstat src >>= fun stat ->
let user_id, group_id, uname, gname = get_ids user in
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 ?group_id ?uname ?gname
dst stat.Lwt_unix.LargeFile.st_size
in
Tar_lwt_unix.write_block ~level hdr (fun ofd ->
let flags = [Unix.O_RDONLY; Unix.O_NONBLOCK; Unix.O_CLOEXEC] in
Lwt_io.(with_file ~mode:input ~flags) src (copy_to ~dst:ofd)
) to_untar
let copy_symlink ~src ~target ~dst ~to_untar ~user =
Lwt_unix.LargeFile.lstat src >>= fun stat ->
let user_id, group_id, uname, gname = get_ids user in
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 ?group_id ?uname ?gname
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 user_id, group_id, uname, gname = get_ids user in
let hdr = Tar.Header.make
~file_mode:0o755
~mod_time:(Int64.of_float stat.Lwt_unix.LargeFile.st_mtime)
?user_id ?group_id ?uname ?gname
(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
let transform ~user fname hdr =
let hdr' = Tar.Header.(make ~file_mode:hdr.file_mode ~mod_time:hdr.mod_time hdr.file_name hdr.file_size) in
let hdr' = match user with
| `Unix user ->
{ hdr' with Tar.Header.user_id = user.Obuilder_spec.uid; group_id = user.gid; }
| `Windows user when user.Obuilder_spec.name = "ContainerAdministrator" ->
let id = let x = 93 and rid = 1 in 0x1000 * x + rid in
{ hdr' with user_id = id; group_id = id; uname = user.name; gname = user.name; }
| `Windows _ -> hdr'
in
match hdr.Tar.Header.link_indicator with
| Normal ->
{ hdr' with
file_mode = if hdr.file_mode land 0o111 <> 0 then 0o755 else 0o644;
file_name = fname hdr.file_name; }
| Symbolic ->
{ hdr' with
file_mode = 0o777;
file_name = fname hdr.file_name;
link_indicator = hdr.link_indicator;
link_name = hdr.link_name; }
| Directory ->
{ hdr' with
file_mode = 0o755;
file_name = fname hdr.file_name ^ "/"; }
| _ -> Fmt.invalid_arg "Unsupported file type"
let rec map_transform ~dst transformations = function
| `File (src, _) ->
let dst = dst / Filename.basename src in
Hashtbl.add transformations src dst
| `Symlink (src, _) ->
let dst = dst / Filename.basename src in
Hashtbl.add transformations src dst
| `Dir (src, items) ->
let dst = dst / Filename.basename src in
Hashtbl.add transformations src dst;
Log.debug(fun f -> f "Copy dir %S -> %S" src dst);
List.iter (map_transform ~dst transformations) items
and transform_files ~from_tar ~src_manifest ~dst_dir ~user ~to_untar =
let dst = remove_leading_slashes dst_dir in
let transformations = Hashtbl.create ~random:true 64 in
List.iter (map_transform ~dst transformations) src_manifest;
let fname file_name =
match Hashtbl.find transformations file_name with
| exception Not_found -> Fmt.failwith "Could not find mapping for %s" file_name
| file_name -> file_name
in
Tar_lwt_unix.Archive.transform ~level (transform ~user fname) from_tar to_untar
let transform_file ~from_tar ~src_manifest ~dst ~user ~to_untar =
let dst = remove_leading_slashes dst in
let transformations = Hashtbl.create ~random:true 1 in
let map_transform = function
| `File (src, _) -> Hashtbl.add transformations src dst
| `Symlink (src, _) -> Hashtbl.add transformations src dst
| `Dir (src, items) ->
Hashtbl.add transformations src dst;
Log.debug(fun f -> f "Copy dir %S -> %S" src dst);
List.iter (map_transform ~dst transformations) items
in
map_transform src_manifest;
let fname file_name =
match Hashtbl.find transformations file_name with
| exception Not_found -> Fmt.failwith "Could not find mapping for %s" file_name
| file_name -> file_name
in
Tar_lwt_unix.Archive.transform ~level (fun hdr ->
let hdr' = transform ~user fname hdr in
Log.debug (fun f -> f "Copying %s -> %s" hdr.Tar.Header.file_name hdr'.Tar.Header.file_name);
hdr')
from_tar to_untar