util: Fix multiple use of LockDirectory
util: Fix multiple use of LockDirectory

This commit fixes problems with calling LockDirectory multiple times on
the same directory, or from multiple threads. It also fixes the build on
OpenBSD.

- Wrap the boost::interprocess::file_lock in a std::unique_ptr inside
  the map that keeps track of per-directory locks. This fixes a build
  issue with the clang 4.0.0+boost-1.58.0p8 version combo on OpenBSD
  6.2, and should have no observable effect otherwise.

- Protect the locks map using a mutex.

- Make sure that only locks that are successfully acquired are inserted
  in the map.

- Open the lock file for appending only if we know we don't have the
  lock yet - The `FILE* file = fsbridge::fopen(pathLockFile, "a");`
  wipes the 'we own this lock' administration, likely because it opens
  a new fd for the locked file then closes it.

Github-Pull: #12422
Rebased-From: fc888bfcacb875c45bc8f9d7ca1357ab70a30490
Tree-SHA512: c8b8942fad9ea9d9e55f8e5e360f7cf3a8b00cd17e6bc5ec5895e1e6ddcbca796e62e82856e82f0562869a624d75ad510e108077461bb47a87b2b52be0aba866

deeponion/deeponion,viacoin/viacoin,vertcoin/vertcoin,GroestlCoin/GroestlCoin,MazaCoin/maza,particl/particl-core,digibyte/digibyte,vertcoin/vertcoin,vertcoin/vertcoin,1185/starwels,qtumproject/qtum,tecnovert/particl-core,particl/particl-core,peercoin/peercoin,digibyte/digibyte,myriadcoin/myriadcoin,viacoin/viacoin,GroestlCoin/bitcoin,peercoin/peercoin,ppcoin/ppcoin,1185/starwels,deeponion/deeponion,gjhiggins/vcoincore,1185/starwels,myriadteam/myriadcoin,starwels/starwels,qtumproject/qtum,starwels/starwels,viacoin/viacoin,ppcoin/ppcoin,deeponion/deeponion,myriadcoin/myriadcoin,GroestlCoin/bitcoin,GroestlCoin/GroestlCoin,peercoin/peercoin,GroestlCoin/GroestlCoin,GroestlCoin/bitcoin,tecnovert/particl-core,ahmedbodi/vertcoin,GroestlCoin/GroestlCoin,myriadcoin/myriadcoin,gjhiggins/vcoincore,particl/particl-core,particl/particl-core,deeponion/deeponion,myriadteam/myriadcoin,peercoin/peercoin,DigitalPandacoin/pandacoin,myriadteam/myriadcoin,starwels/starwels,GroestlCoin/GroestlCoin,ahmedbodi/vertcoin,tecnovert/particl-core,DigitalPandacoin/pandacoin,gjhiggins/vcoincore,GroestlCoin/bitcoin,peercoin/peercoin,digibyte/digibyte,starwels/starwels,digibyte/digibyte,starwels/starwels,viacoin/viacoin,myriadcoin/myriadcoin,1185/starwels,vertcoin/vertcoin,1185/starwels,digibyte/digibyte,ahmedbodi/vertcoin,MazaCoin/maza,ppcoin/ppcoin,qtumproject/qtum,vertcoin/vertcoin,tecnovert/particl-core,ahmedbodi/vertcoin,myriadcoin/myriadcoin,MazaCoin/maza,ahmedbodi/vertcoin,qtumproject/qtum,deeponion/deeponion,GroestlCoin/bitcoin,qtumproject/qtum,gjhiggins/vcoincore,qtumproject/qtum,MazaCoin/maza,GroestlCoin/GroestlCoin,gjhiggins/vcoincore,myriadteam/myriadcoin,myriadcoin/myriadcoin,DigitalPandacoin/pandacoin,deeponion/deeponion,MazaCoin/maza,DigitalPandacoin/pandacoin,tecnovert/particl-core,viacoin/viacoin,1185/starwels,myriadteam/myriadcoin,gjhiggins/vcoincore,myriadteam/myriadcoin,tecnovert/particl-core,ahmedbodi/vertcoin,particl/particl-core,digibyte/digibyte,DigitalPandacoin/pandacoin,vertcoin/vertcoin,particl/particl-core,viacoin/viacoin,qtumproject/qtum,MazaCoin/maza,peercoin/peercoin,GroestlCoin/bitcoin,ppcoin/ppcoin,DigitalPandacoin/pandacoin,starwels/starwels