MCPcopy Create free account
hub / github.com/ByConity/ByConity / StatusFile

Method StatusFile

src/Common/StatusFile.cpp:45–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43
44
45StatusFile::StatusFile(std::string path_, FillFunction fill_)
46 : path(std::move(path_)), fill(std::move(fill_))
47{
48 /// If file already exists. NOTE Minor race condition.
49 if (fs::exists(path))
50 {
51 std::string contents;
52 {
53 ReadBufferFromFile in(path, 1024);
54 LimitReadBuffer limit_in(in, 1024, false);
55 readStringUntilEOF(contents, limit_in);
56 }
57
58 if (!contents.empty())
59 LOG_INFO(&Poco::Logger::get("StatusFile"), "Status file {} already exists - unclean restart. Contents:\n{}", path, contents);
60 else
61 LOG_INFO(&Poco::Logger::get("StatusFile"), "Status file {} already exists and is empty - probably unclean hardware restart.", path);
62 }
63
64 fd = ::open(path.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0666);
65
66 if (-1 == fd)
67 throwFromErrnoWithPath("Cannot open file " + path, path, ErrorCodes::CANNOT_OPEN_FILE);
68
69 try
70 {
71 int flock_ret = flock(fd, LOCK_EX | LOCK_NB);
72 if (-1 == flock_ret)
73 {
74 if (errno == EWOULDBLOCK)
75 throw Exception("Cannot lock file " + path + ". Another server instance in same directory is already running.", ErrorCodes::CANNOT_OPEN_FILE);
76 else
77 throwFromErrnoWithPath("Cannot lock file " + path, path, ErrorCodes::CANNOT_OPEN_FILE);
78 }
79
80 if (0 != ftruncate(fd, 0))
81 throwFromErrnoWithPath("Cannot ftruncate " + path, path, ErrorCodes::CANNOT_TRUNCATE_FILE);
82
83 if (0 != lseek(fd, 0, SEEK_SET))
84 throwFromErrnoWithPath("Cannot lseek " + path, path, ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
85
86 /// Write information about current server instance to the file.
87 WriteBufferFromFileDescriptor out(fd, 1024);
88 try
89 {
90 fill(out);
91 /// Finalize here to avoid throwing exceptions in destructor.
92 out.finalize();
93 }
94 catch (...)
95 {
96 /// Finalize in case of exception to avoid throwing exceptions in destructor
97 out.finalize();
98 throw;
99 }
100 }
101 catch (...)
102 {

Callers

nothing calls this directly

Calls 9

readStringUntilEOFFunction · 0.85
openFunction · 0.85
throwFromErrnoWithPathFunction · 0.85
closeFunction · 0.85
existsFunction · 0.70
ExceptionClass · 0.70
getFunction · 0.50
emptyMethod · 0.45
finalizeMethod · 0.45

Tested by

no test coverage detected