| 971 | } |
| 972 | |
| 973 | ChangeLog::Segment* ChangeLog::reuseSegment(ChangeLog::Segment* segment) |
| 974 | { |
| 975 | // Remove segment from the list |
| 976 | |
| 977 | FB_SIZE_T pos = 0; |
| 978 | if (m_segments.find(segment, pos)) |
| 979 | m_segments.remove(pos); |
| 980 | else |
| 981 | fb_assert(false); |
| 982 | |
| 983 | // Save its original filename |
| 984 | |
| 985 | const PathName orgname = segment->getPathName(); |
| 986 | |
| 987 | // Release the reference (thus destroying the segment) |
| 988 | |
| 989 | segment->release(); |
| 990 | |
| 991 | // Increase the sequence |
| 992 | |
| 993 | const auto state = m_sharedMemory->getHeader(); |
| 994 | const auto sequence = state->sequence + 1; |
| 995 | |
| 996 | // Attempt to rename the backing file |
| 997 | |
| 998 | PathName newname; |
| 999 | newname.printf(FILENAME_PATTERN, m_config->filePrefix.c_str(), sequence); |
| 1000 | newname = m_config->journalDirectory + newname; |
| 1001 | |
| 1002 | // If renaming fails, then we just create a new file. |
| 1003 | // The old segment will be reused later in this case. |
| 1004 | if (::rename(orgname.c_str(), newname.c_str()) < 0) |
| 1005 | { |
| 1006 | #ifdef DEV_BUILD |
| 1007 | const auto err = ERRNO; |
| 1008 | |
| 1009 | string warn; |
| 1010 | warn.printf("Journal file %s rename to %s failed (error %d)", |
| 1011 | orgname.c_str(), newname.c_str(), err); |
| 1012 | |
| 1013 | logPrimaryWarning(m_config->dbName, warn); |
| 1014 | #endif |
| 1015 | return createSegment(); |
| 1016 | } |
| 1017 | |
| 1018 | // Re-open the segment using a new name and initialize it |
| 1019 | |
| 1020 | const auto fd = os_utils::openCreateSharedFile(newname.c_str(), O_BINARY); |
| 1021 | |
| 1022 | segment = FB_NEW_POOL(getPool()) Segment(getPool(), newname, fd); |
| 1023 | |
| 1024 | segment->init(sequence, m_guid); |
| 1025 | segment->addRef(); |
| 1026 | |
| 1027 | m_segments.add(segment); |
| 1028 | state->generation++; |
| 1029 | state->sequence++; |
| 1030 |
nothing calls this directly
no test coverage detected