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
This commit is contained in:
Andreas Traczyk
2025-02-26 16:41:08 -05:00
parent d5ce21e39b
commit 5e2d9e027b

View File

@ -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<unsigned>::max())