From dd36baa5173a0bcccdedd82b1e40c3ebbf854c2d Mon Sep 17 00:00:00 2001 From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> Date: Wed, 16 Jun 2021 13:09:23 -0400 Subject: [PATCH] fileutils: remove symlink creation on windows Requires developer mode at best on windows 10 Change-Id: I0861c3f87c51f0de9de3f3860b371ced0d5ccb2d --- src/data_transfer.cpp | 8 ++++---- src/fileutils.cpp | 27 ++++++++++++++++++--------- src/fileutils.h | 2 +- src/jamidht/jamiaccount.cpp | 17 +++++++++-------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/data_transfer.cpp b/src/data_transfer.cpp index c977b542ba..696a7f293d 100644 --- a/src/data_transfer.cpp +++ b/src/data_transfer.cpp @@ -1200,20 +1200,20 @@ TransferManager::onIncomingFileTransfer(const std::string& fileId, return; } - auto symlinkPath = path(fileId); + auto filelinkPath = path(fileId); DRing::DataTransferInfo info; info.accountId = pimpl_->accountId_; info.conversationId = pimpl_->to_; info.path = itW->second.path; if (info.path.empty()) - info.path = symlinkPath; + info.path = filelinkPath; info.totalSize = itW->second.totalSize; info.bytesProgress = 0; // Create symlink for future transfers - if (info.path != symlinkPath && !fileutils::isSymLink(symlinkPath)) - fileutils::createSymLink(symlinkPath, info.path); + if (info.path != filelinkPath && !fileutils::isSymLink(filelinkPath)) + fileutils::createFileLink(filelinkPath, info.path); auto ifile = std::make_shared<IncomingFile>(std::move(channel), info, diff --git a/src/fileutils.cpp b/src/fileutils.cpp index c802152103..d7f5786195 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -317,16 +317,25 @@ writeTime(const std::string& path) } void -createSymLink(const std::string& linkFile, const std::string& target) +createFileLink(const std::string& linkFile, const std::string& target, bool hard) { - auto sep = target.find_last_of('/'); - if (sep != std::string::npos) - check_dir(target.substr(0, sep).c_str()); #ifndef _WIN32 - symlink(target.c_str(), linkFile.c_str()); + int status; + if (hard) + status = link(target.c_str(), linkFile.c_str()); + else + status = symlink(target.c_str(), linkFile.c_str()); + if (status != 0) + JAMI_ERR("Couldn't create %s link: ", (hard ? "hard" : "soft"), strerror(errno)); #else - std::error_code ec; - std::filesystem::create_symlink(target, linkFile, ec); + try { + if (hard) + std::filesystem::create_hard_link(target, linkFile); + else + std::filesystem::create_symlink(target, linkFile); + } catch (const std::exception& e) { + JAMI_ERR("Couldn't create %s link: ", (hard ? "hard" : "soft"), e.what()); + } #endif } @@ -458,7 +467,7 @@ dirent_buf_size(UNUSED DIR* dirp) #if defined(NAME_MAX) name_max = (NAME_MAX > 255) ? NAME_MAX : 255; #else - return (size_t)(-1); + return (size_t) (-1); #endif #else #if defined(NAME_MAX) @@ -479,7 +488,7 @@ readDirectory(const std::string& dir) return {}; size_t size = dirent_buf_size(dp); - if (size == (size_t)(-1)) + if (size == (size_t) (-1)) return {}; std::vector<uint8_t> buf(size); dirent* entry; diff --git a/src/fileutils.h b/src/fileutils.h index 677ea18fed..31c134d304 100644 --- a/src/fileutils.h +++ b/src/fileutils.h @@ -85,7 +85,7 @@ bool isSymLink(const std::string& path); std::chrono::system_clock::time_point writeTime(const std::string& path); -void createSymLink(const std::string& src, const std::string& dest); +void createFileLink(const std::string& src, const std::string& dest, bool hard = false); std::string getFileExtension(const std::string& filename); diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index 82e6b962a5..cc1a520dcb 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -5344,16 +5344,17 @@ JamiAccount::sendFile(const std::string& conversationId, tid, path](bool, const std::string& commitId) { // Create a symlink to answer to re-ask - auto symlinkPath = fileutils::get_data_dir() + DIR_SEPARATOR_STR - + accId + DIR_SEPARATOR_STR - + "conversation_data" + DIR_SEPARATOR_STR - + conversationId + DIR_SEPARATOR_STR - + commitId + "_" + std::to_string(tid); + auto filelinkPath = fileutils::get_data_dir() + + DIR_SEPARATOR_STR + accId + + DIR_SEPARATOR_STR + "conversation_data" + + DIR_SEPARATOR_STR + conversationId + + DIR_SEPARATOR_STR + commitId + "_" + + std::to_string(tid); auto extension = fileutils::getFileExtension(path); if (!extension.empty()) - symlinkPath += "." + extension; - if (path != symlinkPath && !fileutils::isSymLink(symlinkPath)) - fileutils::createSymLink(symlinkPath, path); + filelinkPath += "." + extension; + if (path != filelinkPath && !fileutils::isSymLink(filelinkPath)) + fileutils::createFileLink(filelinkPath, path, true); }); } }); -- GitLab