From b2937e7a60da94e31d501b37e9240a7fc88a8b71 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Tue, 29 Nov 2022 14:44:59 -0500
Subject: [PATCH] misc: ensure directory exists before storing vcard

This fixes testProfileReceivedMultiDevice

Change-Id: I9fadebe2f353cdacd5a06fd860195ba7a50813dd
---
 src/data_transfer.cpp                     | 11 ++++++-----
 test/unitTest/syncHistory/syncHistory.cpp |  9 ++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/data_transfer.cpp b/src/data_transfer.cpp
index 489aa8e9b0..b72252738a 100644
--- a/src/data_transfer.cpp
+++ b/src/data_transfer.cpp
@@ -204,9 +204,9 @@ IncomingFile::process()
         if (!shared)
             return;
         auto correct = shared->sha3Sum_.empty();
+        if (shared->stream_ && shared->stream_.is_open())
+            shared->stream_.close();
         if (!correct) {
-            if (shared->stream_ && shared->stream_.is_open())
-                shared->stream_.close();
             // Verify shaSum
             auto sha3Sum = fileutils::sha3File(shared->info_.path);
             if (shared->sha3Sum_ == sha3Sum) {
@@ -505,9 +505,10 @@ TransferManager::onIncomingProfile(const std::shared_ptr<ChannelSocket>& channel
     libjami::DataTransferInfo info;
     info.accountId = pimpl_->accountId_;
     info.conversationId = pimpl_->to_;
-    info.path = fileutils::get_cache_dir() + DIR_SEPARATOR_STR + pimpl_->accountId_
-                + DIR_SEPARATOR_STR + "vcard" + DIR_SEPARATOR_STR + deviceId + "_" + uri + "_"
-                + std::to_string(tid);
+
+    auto recvDir = fmt::format("{:s}/{:s}/vcard/", fileutils::get_cache_dir(), pimpl_->accountId_);
+    fileutils::recursive_mkdir(recvDir);
+    info.path =  fmt::format("{:s}/{:s}_{:s}_{}", recvDir, deviceId, uri, tid);
 
     auto ifile = std::make_shared<IncomingFile>(std::move(channel), info, "profile.vcf", "");
     auto res = pimpl_->vcards_.emplace(idx, std::move(ifile));
diff --git a/test/unitTest/syncHistory/syncHistory.cpp b/test/unitTest/syncHistory/syncHistory.cpp
index 2aacb3b644..0a5a09f8c2 100644
--- a/test/unitTest/syncHistory/syncHistory.cpp
+++ b/test/unitTest/syncHistory/syncHistory.cpp
@@ -969,7 +969,7 @@ END:VCARD";
     std::condition_variable cv;
     std::map<std::string, std::shared_ptr<libjami::CallbackWrapperBase>> confHandlers;
     bool conversationReady = false, requestReceived = false, bobProfileReceived = false,
-         aliceProfileReceived = false;
+         aliceProfileReceived = false, bobProfileReceivedAlice2 = false;
     std::string convId = "";
     std::string bobDest = aliceAccount->dataTransfer()->profilePath(bobUri);
     confHandlers.insert(libjami::exportable_callback<libjami::ConfigurationSignal::IncomingTrustRequest>(
@@ -996,12 +996,11 @@ END:VCARD";
             if (accountId == aliceId && peerId == bobUri) {
                 bobProfileReceived = true;
                 auto p = std::filesystem::path(bobDest);
-                fileutils::recursive_mkdir(p.parent_path());
-                std::rename(path.c_str(), bobDest.c_str());
+                auto res = std::rename(path.c_str(), bobDest.c_str());
             } else if (accountId == bobId && peerId == aliceUri) {
                 aliceProfileReceived = true;
             } else if (accountId == alice2Id && peerId == bobUri) {
-                bobProfileReceived = true;
+                bobProfileReceivedAlice2 = true;
             } else if (accountId == alice2Id && peerId == aliceUri) {
                 aliceProfileReceived = true;
             }
@@ -1030,7 +1029,7 @@ END:VCARD";
     bobProfileReceived = false, aliceProfileReceived = false;
     alice2Id = Manager::instance().addAccount(details);
 
-    CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return aliceProfileReceived && bobProfileReceived; }));
+    CPPUNIT_ASSERT(cv.wait_for(lk, 60s, [&] { return aliceProfileReceived && bobProfileReceivedAlice2; }));
     libjami::unregisterSignalHandlers();
 }
 
-- 
GitLab