From ff8118c8e11e3fbc05a0f95057f3322a0f1e08bb Mon Sep 17 00:00:00 2001 From: ketmar Date: Wed, 25 Apr 2018 11:05:38 +0000 Subject: [PATCH] create lockfile, so we won't run two instances on the same acc FossilOrigin-Name: efd0837e020af7a01fabdf3dbeee28b39cb3f2dce7531eb79fb77e73ccbc5c1a --- accobj.d | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/accobj.d b/accobj.d index d9666ea..6177fda 100644 --- a/accobj.d +++ b/accobj.d @@ -25,6 +25,7 @@ import arsd.simpledisplay; import iv.cmdcon; import iv.cmdcon.gl; import iv.strex; +import iv.lockfile; import iv.tox; import iv.txtser; import iv.unarray; @@ -934,6 +935,19 @@ private: private: @property string srvalias () const pure nothrow @safe @nogc => info.nick; + LockFile lockf; + + // false: get lost + bool tryLockIt () { + if (toxDataDiskName.length == 0) return false; + lockf = LockFile(toxDataDiskName~".lock"); + if (!lockf.tryLock) { + lockf.close(); + return false; + } + return true; + } + void createWithBaseDir (string aBaseDir) { import std.algorithm : sort; import std.file : DirEntry, SpanMode, dirEntries; @@ -941,6 +955,9 @@ private: basePath = normalizeBaseDir(aBaseDir); toxDataDiskName = basePath~"toxdata.tox"; + + if (!tryLockIt) throw new Exception("cannot activate already active account"); + protoOpts.txtunser(VFile(basePath~"proto.rc")); info.txtunser(VFile(basePath~"config.rc")); @@ -1154,6 +1171,11 @@ private: public: static Account CreateNew (string aBaseDir, string aAccName) { aBaseDir = normalizeBaseDir(aBaseDir); + auto lockf = LockFile(aBaseDir~"toxdata.tox.lock"); + if (!lockf.tryLock) { + lockf.close(); + throw new Exception("cannot create locked account"); + } mkdirRec(aBaseDir~"contacts"); // write protocol options { -- 2.11.4.GIT