Skip to content

Commit 2684f6a

Browse files
authored
Set file permissions correctly (#338)
* adding all permissions * removing exec permissions * fixing race condition
1 parent 94a2fd9 commit 2684f6a

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

co_sim_io/sources/communication/communication.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@
2323
namespace CoSimIO {
2424
namespace Internals {
2525

26+
void AddFilePermissions(const fs::path& rPath)
27+
{
28+
CO_SIM_IO_TRY
29+
30+
fs::permissions(rPath,
31+
fs::perms::owner_read | fs::perms::owner_write |
32+
fs::perms::group_read | fs::perms::group_write |
33+
fs::perms::others_read | fs::perms::others_write,
34+
fs::perm_options::add);
35+
36+
CO_SIM_IO_CATCH
37+
}
38+
2639
Communication::Communication(
2740
const Info& I_Settings,
2841
std::shared_ptr<DataCommunicator> I_DataComm)
@@ -139,6 +152,7 @@ void Communication::BaseConnectDetail(const Info& I_Info)
139152
CO_SIM_IO_INFO_IF("CoSimIO", ec) << "Warning, communication directory (" << mCommFolder << ") could not be deleted!\nError code: " << ec.message() << std::endl;
140153
if (!fs::exists(mCommFolder)) {
141154
fs::create_directory(mCommFolder);
155+
AddFilePermissions(mCommFolder); // otherwise the process with lower rights cannot delete files in it
142156
}
143157
}
144158

@@ -356,10 +370,13 @@ void Communication::MakeFileVisible(
356370
CO_SIM_IO_TRY
357371

358372
if (!UseAuxFileForFileAvailability) {
373+
const fs::path tmp_file_name = GetTempFileName(rPath, UseAuxFileForFileAvailability);
374+
AddFilePermissions(tmp_file_name);
359375
std::error_code ec;
360-
fs::rename(GetTempFileName(rPath, UseAuxFileForFileAvailability), rPath, ec);
376+
fs::rename(tmp_file_name, rPath, ec);
361377
CO_SIM_IO_ERROR_IF(ec) << rPath << " could not be made visible!\nError code: " << ec.message() << std::endl;
362378
} else {
379+
AddFilePermissions(rPath);
363380
std::ofstream avail_file;
364381
avail_file.open(rPath.string() + ".avail");
365382
avail_file.close();

0 commit comments

Comments
 (0)