From 5e2d9e027b141c91b0e72c51bfd5c489fac7a8e7 Mon Sep 17 00:00:00 2001 From: Andreas Traczyk Date: Wed, 26 Feb 2025 16:41:08 -0500 Subject: [PATCH] fileutils: improve Windows hard link file handling Fix file access issues when reading hard links on Windows by: - Adding explicit share mode (_SH_DENYNO) to allow reading files opened elsewhere - Using binary mode to ensure consistent behavior with hard links This resolves issues with reading hard linked files on Windows while maintaining behavior on other platforms. https://git.jami.net/savoirfairelinux/jami-client-qt/-/issues/1899 Change-Id: I2adc0f29db46d5bf5b69762018e7a5e677ec3d04 --- src/fileutils.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/fileutils.cpp b/src/fileutils.cpp index 9d619da02..177f24c0e 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -275,9 +275,18 @@ std::string loadTextFile(const std::filesystem::path& path, const std::filesystem::path& default_dir) { std::string buffer; - std::ifstream file(getFullPath(default_dir, path)); + auto fullPath = getFullPath(default_dir, path); + + // Open with explicit share mode to allow reading even if file is opened elsewhere +#ifdef _WIN32 + std::ifstream file(fullPath, std::ios::in | std::ios::binary, _SH_DENYNO); +#else + std::ifstream file(fullPath); +#endif + if (!file) throw std::runtime_error("Unable to read file: " + path.string()); + file.seekg(0, std::ios::end); auto size = file.tellg(); if (size > std::numeric_limits::max())