195 const std::vector<unsigned char>&
data,
198 namespace fs = std::filesystem;
202 std::ofstream dataofs;
206 throw armarx::LocalException(
"Could not write data to filesystem file '" +
207 p.string() +
"'. Skipping this file.");
209 dataofs.write(
reinterpret_cast<const char*
>(
data.data()),
data.size());
214 const fs::path dir = p.parent_path().empty() ? fs::current_path() : p.parent_path();
215 const std::string filename = p.filename().string();
218 fs::path tmpl = dir / (filename +
".tmpXXXXXX");
219 std::string tmpl_str = tmpl.string();
222 int fd = ::mkstemp(tmpl_str.data());
225 throw std::system_error(errno, std::generic_category(),
"mkstemp failed");
229 const unsigned char* buf =
data.data();
230 size_t left =
data.size();
233 ssize_t n = ::write(fd, buf, left);
238 ::unlink(tmpl_str.c_str());
239 throw std::system_error(e, std::generic_category(),
"write failed");
242 left -=
static_cast<size_t>(n);
246 if (::fdatasync(fd) != 0)
250 ::unlink(tmpl_str.c_str());
251 throw std::system_error(e, std::generic_category(),
"fdatasync failed");
254 if (::close(fd) != 0)
257 ::unlink(tmpl_str.c_str());
258 throw std::system_error(e, std::generic_category(),
"close failed");
262 if (::rename(tmpl_str.c_str(), p.c_str()) != 0)
265 ::unlink(tmpl_str.c_str());
266 throw std::system_error(e, std::generic_category(),
"rename failed");
270 int dfd = ::open(dir.c_str(), O_DIRECTORY | O_RDONLY);
282 if (!std::filesystem::exists(p))
284 throw std::runtime_error(
"File not found: " + p.string());
287 std::ifstream dataifs(p);
291 throw std::runtime_error(
"Could not open file: " + p.string());
294 std::vector<unsigned char> datafilecontent((std::istreambuf_iterator<char>(dataifs)),
295 (std::istreambuf_iterator<char>()));
300 throw std::runtime_error(
"Error reading file: " + p.string());
304 return datafilecontent;
310 std::vector<std::filesystem::path> ret;
311 for (
const auto& subdir : std::filesystem::directory_iterator(p))
313 std::filesystem::path subdirPath = subdir.path();
314 if (std::filesystem::is_directory(subdirPath))
316 ret.push_back(subdirPath);
319 std::sort(ret.begin(),
321 [](
const std::filesystem::path& a,
const std::filesystem::path& b) ->
bool
322 { return a.string() < b.string(); });
329 std::vector<std::filesystem::path> ret;
330 for (
const auto& subdir : std::filesystem::directory_iterator(p))
332 std::filesystem::path subdirPath = subdir.path();
333 if (std::filesystem::is_regular_file(subdirPath))
335 ret.push_back(subdirPath);
338 std::sort(ret.begin(),
340 [](
const std::filesystem::path& a,
const std::filesystem::path& b) ->
bool
341 { return a.string() > b.string(); });
378 namespace fs = std::filesystem;
380 if (!fs::exists(dir) || !fs::is_directory(dir))
386 fs::path testFile = dir /
".ltm_write_test_XXXXXX";
387 std::string testPath = testFile.string();
389 int fd = ::mkstemp(testPath.data());
396 ::unlink(testPath.c_str());