123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105(**************************************************************************)(* *)(* Copyright 2012-2020 OCamlPro *)(* Copyright 2012 INRIA *)(* *)(* All rights reserved. This file is distributed under the terms of the *)(* GNU Lesser General Public License version 2.1, with the special *)(* exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)openOpamStd.OpmoduletypeARG=sigtypetvalname:stringendmoduleMake(X:ARG):sigtypet=X.tvalsave:OpamFilename.t->t->unitvalload:OpamFilename.t->toptionvalremove:OpamFilename.t->unitend=structletlogfmt=OpamConsole.log(Printf.sprintf"CACHE(%s)"X.name)fmtletslog=OpamConsole.slogtypet=X.tletcheck_marshaled_filefd=tryletic=Unix.in_channel_of_descrfdinletthis_magic=OpamVersion.magic()inletmagic_len=String.lengththis_magicinletfile_magic=letb=Bytes.createmagic_leninreally_inputicb0magic_len;Bytes.to_stringbinifnotOpamCoreConfig.developer&&file_magic<>this_magicthen(log"Bad %s cache: incompatible magic string %S (expected %S)."X.namefile_magicthis_magic;None)elseSomeicwithe->OpamStd.Exn.fatale;log"Bad %s cache: %s"X.name(Printexc.to_stringe);Noneletmarshal_from_filefilefd=letchrono=OpamConsole.timer()inletfic=trylet(cache:t)=Marshal.from_channelicinlog"Loaded %a in %.3fs"(slogOpamFilename.to_string)file(chrono());SomecachewithEnd_of_file|Failure_->log"Bad %s cache: likely a truncated file, ignoring."X.name;NoneinOpamStd.Option.Op.(check_marshaled_filefd>>=f)letloadcache_file=matchOpamFilename.opt_filecache_filewith|Somefile->letr=OpamFilename.with_flock`Lock_readfile@@funfd->marshal_from_filefilefdinifr=Nonethenbeginlog"Invalid %s cache, removing"X.name;OpamFilename.removefileend;r|None->Noneletsavecache_filet=ifOpamCoreConfig.(!r.safe_mode)thenlog"Running in safe mode, not upgrading the %s cache"X.nameelsetryletchrono=OpamConsole.timer()inOpamFilename.with_flock`Lock_writecache_file@@funfd->log"Writing the %s cache to %s ..."X.name(OpamFilename.prettifycache_file);letoc=Unix.out_channel_of_descrfdinoutput_stringoc(OpamVersion.magic());Marshal.to_channeloct[];flushoc;log"%a written in %.3fs"(slogOpamFilename.prettify)cache_file(chrono())withUnix.Unix_error_->log"Could not acquire lock for writing %s, skipping %s cache update"(OpamFilename.prettifycache_file)X.nameletremovecache_file=OpamFilename.removecache_fileend