From 022a92e5139e12f1eea1ded36e5f9f20f6be8ef2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Thu, 2 Jan 2025 16:04:10 -0500
Subject: [PATCH] ConversationRepository: unlock on startup

Change-Id: I019a385b43d78ce35691dd0da3599749e0442c83
---
 src/jamidht/conversationrepository.cpp | 43 ++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/src/jamidht/conversationrepository.cpp b/src/jamidht/conversationrepository.cpp
index ac98a2c92..741fe093e 100644
--- a/src/jamidht/conversationrepository.cpp
+++ b/src/jamidht/conversationrepository.cpp
@@ -70,12 +70,55 @@ public:
         conversationDataPath_ = fileutils::get_data_dir() / accountId_
                                 / "conversation_data" / id_;
         membersCache_ = conversationDataPath_ / "members";
+        checkLocks();
         loadMembers();
         if (members_.empty()) {
             initMembers();
         }
     }
 
+    void checkLocks()
+    {
+        auto repo = repository();
+        if (!repo)
+            throw std::logic_error("Invalid git repository");
+
+        std::filesystem::path repoPath = git_repository_path(repo.get());
+        std::error_code ec;
+
+        auto indexPath = std::filesystem::path(repoPath / "index.lock");
+        if (std::filesystem::exists(indexPath, ec)) {
+            JAMI_WARNING("[conv {}] Conversation is locked, removing lock {}", id_, indexPath);
+            std::filesystem::remove(indexPath, ec);
+            if (ec)
+                JAMI_ERROR("[conv {}] Unable to remove lock {}: {}", id_, indexPath, ec.message());
+        }
+
+        auto refPath = std::filesystem::path(repoPath / "refs" / "heads" / "main.lock");
+        if (std::filesystem::exists(refPath)) {
+            JAMI_WARNING("[conv {}] Conversation is locked, removing lock {}", id_, refPath);
+            std::filesystem::remove(refPath, ec);
+            if (ec)
+                JAMI_ERROR("[conv {}] Unable to remove lock {}: {}", id_, refPath, ec.message());
+        }
+
+        auto remotePath = std::filesystem::path(repoPath / "refs" / "remotes");
+        for (const auto& fileIt : std::filesystem::directory_iterator(remotePath, ec)) {
+            auto refPath = fileIt.path() / "main.lock";
+            if (std::filesystem::exists(refPath, ec)) {
+                JAMI_WARNING("[conv {}] Conversation is locked for remote {}, removing lock", id_, fileIt.path().filename());
+                std::filesystem::remove(refPath, ec);
+                if (ec)
+                    JAMI_ERROR("[conv {}] Unable to remove lock {}: {}", id_, refPath, ec.message());
+            }
+        }
+
+        auto err = git_repository_state_cleanup(repo.get());
+        if (err < 0) {
+            JAMI_ERROR("[conv {}] Unable to cleanup repository: {}", id_, git_error_last()->message);
+        }
+    }
+
     void loadMembers()
     {
         try {
-- 
GitLab