@@ -162,41 +162,60 @@ let get_cache t name =
162162 Hashtbl. add t.caches name c;
163163 c
164164
165- let cache ~user t name : (string * (unit -> unit Lwt.t)) Lwt.t =
165+ let cache ?( shared = false ) ~user t name : (string * (unit -> unit Lwt.t)) Lwt.t =
166166 let cache = get_cache t name in
167- Lwt_mutex. with_lock cache.lock @@ fun () ->
168- let tmp = Cache. cache_tmp t.next name in
169- t.next < - t.next + 1 ;
170167 let snapshot = Cache. cache name in
171- (* Create cache if it doesn't already exist. *)
172- let * () =
173- let * exists = Cache. exists snapshot in
174- if not exists then Cache. create snapshot
175- else Lwt. return_unit
176- in
177- (* Create writeable clone. *)
178- let gen = cache.gen in
179- let * () = Cache. snapshot ~src: snapshot tmp in
180- let + () = match user with
181- | `Unix { Obuilder_spec. uid; gid } ->
182- let * tmp = Docker.Cmd. mount_point tmp in
183- Os. sudo [" chown" ; strf " %d:%d" uid gid; tmp]
184- | `Windows _ -> Lwt. return_unit (* FIXME: does Windows need special treatment? *)
185- in
186- let release () =
168+ if shared then
169+ (* Shared mode: return the actual cache volume, no copy-on-write *)
187170 Lwt_mutex. with_lock cache.lock @@ fun () ->
171+ (* Create cache if it doesn't already exist. *)
188172 let * () =
189- if cache.gen = gen then (
190- (* The cache hasn't changed since we cloned it. Update it. *)
191- (* todo: check if it has actually changed. *)
192- cache.gen < - cache.gen + 1 ;
193- let * () = Cache. delete snapshot in
194- Cache. snapshot ~src: tmp snapshot
195- ) else Lwt. return_unit
173+ let * exists = Cache. exists snapshot in
174+ if not exists then Cache. create snapshot
175+ else Lwt. return_unit
196176 in
197- Cache. delete tmp
198- in
199- Cache. name tmp, release
177+ let + () = match user with
178+ | `Unix { Obuilder_spec. uid; gid } ->
179+ let * mp = Docker.Cmd. mount_point snapshot in
180+ Os. sudo [" chown" ; strf " %d:%d" uid gid; mp]
181+ | `Windows _ -> Lwt. return_unit (* FIXME: does Windows need special treatment? *)
182+ in
183+ let release () = Lwt. return_unit in (* No-op for shared caches *)
184+ Cache. name snapshot, release
185+ else
186+ (* Non-shared mode: existing snapshot behavior *)
187+ Lwt_mutex. with_lock cache.lock @@ fun () ->
188+ let tmp = Cache. cache_tmp t.next name in
189+ t.next < - t.next + 1 ;
190+ (* Create cache if it doesn't already exist. *)
191+ let * () =
192+ let * exists = Cache. exists snapshot in
193+ if not exists then Cache. create snapshot
194+ else Lwt. return_unit
195+ in
196+ (* Create writeable clone. *)
197+ let gen = cache.gen in
198+ let * () = Cache. snapshot ~src: snapshot tmp in
199+ let + () = match user with
200+ | `Unix { Obuilder_spec. uid; gid } ->
201+ let * tmp = Docker.Cmd. mount_point tmp in
202+ Os. sudo [" chown" ; strf " %d:%d" uid gid; tmp]
203+ | `Windows _ -> Lwt. return_unit (* FIXME: does Windows need special treatment? *)
204+ in
205+ let release () =
206+ Lwt_mutex. with_lock cache.lock @@ fun () ->
207+ let * () =
208+ if cache.gen = gen then (
209+ (* The cache hasn't changed since we cloned it. Update it. *)
210+ (* todo: check if it has actually changed. *)
211+ cache.gen < - cache.gen + 1 ;
212+ let * () = Cache. delete snapshot in
213+ Cache. snapshot ~src: tmp snapshot
214+ ) else Lwt. return_unit
215+ in
216+ Cache. delete tmp
217+ in
218+ Cache. name tmp, release
200219
201220let delete_cache t name =
202221 let cache = get_cache t name in
0 commit comments