12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020-2021 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)typet={protocol_store_dir:[`Protocol_dir]Naming.directory;mutableprotocols:Protocol_hash.Set.t;}letmemtprotocol_hash=Protocol_hash.Set.memprotocol_hasht.protocolsletall{protocols;_}=protocolsletraw_storestoreprotocol_hashbytes=letopenLwt_syntaxinifmemstoreprotocol_hashthenLwt.return_noneelseletprotocol_file=Naming.protocol_filestore.protocol_store_dirprotocol_hashinlet*fd=Lwt_unix.openfile(Naming.file_pathprotocol_file)[Unix.O_CREAT;O_WRONLY;O_CLOEXEC]0o644inlet*()=Lwt_utils_unix.write_bytesfdbytesinlet*_=Lwt_utils_unix.safe_closefdinstore.protocols<-Protocol_hash.Set.addprotocol_hashstore.protocols;Lwt.return_someprotocol_hashletstorestoreprotocol_hashprotocol=raw_storestoreprotocol_hash(Protocol.to_bytesprotocol)letreadstoreprotocol_hash=letopenLwt_syntaxinOption.catch_os(fun()->letprotocol_file=Naming.protocol_filestore.protocol_store_dirprotocol_hashinlet*content=Lwt_utils_unix.read_file(Naming.file_pathprotocol_file)inLwt.return(Protocol.of_stringcontent))letinitstore_dir=letopenLwt_syntaxinletprotocol_store_dir=Naming.protocol_store_dirstore_dirinletprotocol_store_dir_path=Naming.dir_pathprotocol_store_dirinlet*file_exists=Lwt_unix.file_existsprotocol_store_dir_pathinlet*()=ifnotfile_existsthenLwt_utils_unix.create_dirprotocol_store_dir_pathelseLwt.return_unitinlet*dir=Lwt_unix.opendirprotocol_store_dir_pathinletrecloopset=Lwt.try_bind(fun()->Lwt_unix.readdirdir)(funfile->matchProtocol_hash.of_b58check_optfilewith|Someprotocol_hash->loop(Protocol_hash.Set.addprotocol_hashset)|None->loopset)(functionEnd_of_file->Lwt.returnset|_->loopset)inlet*protocols=loopProtocol_hash.Set.emptyinlet*()=Lwt_unix.closedirdirinLwt.return{protocol_store_dir;protocols}