MCPcopy Create free account
hub / github.com/Icinga/icinga2 / AtomicFile

Method AtomicFile

lib/base/atomic-file.cpp:26–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24}
25
26AtomicFile::AtomicFile(String path, int mode) : m_Path(std::move(path))
27{
28 m_TempFilename = m_Path + ".tmp.XXXXXX";
29
30#ifdef _WIN32
31 m_Fd = Utility::MksTemp(&m_TempFilename[0]);
32#else /* _WIN32 */
33 m_Fd = mkstemp(&m_TempFilename[0]);
34#endif /* _WIN32 */
35
36 if (m_Fd < 0) {
37 auto error (errno);
38
39 BOOST_THROW_EXCEPTION(posix_error()
40 << boost::errinfo_api_function("mkstemp")
41 << boost::errinfo_errno(error)
42 << boost::errinfo_file_name(m_TempFilename));
43 }
44
45 try {
46 exceptions(failbit | badbit);
47
48 open(boost::iostreams::file_descriptor(
49 m_Fd,
50 // Rationale: https://github.com/boostorg/iostreams/issues/152
51 boost::iostreams::never_close_handle
52 ));
53
54 if (chmod(m_TempFilename.CStr(), mode) < 0) {
55 auto error (errno);
56
57 BOOST_THROW_EXCEPTION(posix_error()
58 << boost::errinfo_api_function("chmod")
59 << boost::errinfo_errno(error)
60 << boost::errinfo_file_name(m_TempFilename));
61 }
62 } catch (...) {
63 if (is_open()) {
64 close();
65 }
66
67 (void)::close(m_Fd);
68 (void)unlink(m_TempFilename.CStr());
69 throw;
70 }
71}
72
73AtomicFile::~AtomicFile()
74{

Callers

nothing calls this directly

Calls 2

posix_errorClass · 0.85
CStrMethod · 0.80

Tested by

no test coverage detected