| 150 | |
| 151 | public: |
| 152 | ControlFile(const PathName& directory, |
| 153 | const Guid& guid, FB_UINT64 sequence, |
| 154 | TransactionList& transactions) |
| 155 | : AutoFile(init(directory, guid)) |
| 156 | { |
| 157 | char guidStr[GUID_BUFF_SIZE]; |
| 158 | GuidToString(guidStr, &guid); |
| 159 | |
| 160 | const PathName filename = directory + guidStr; |
| 161 | |
| 162 | #ifdef WIN_NT |
| 163 | string name; |
| 164 | name.printf("firebird_replctl_%s", guidStr); |
| 165 | m_mutex = CreateMutex(ISC_get_security_desc(), FALSE, name.c_str()); |
| 166 | if (WaitForSingleObject(m_mutex, INFINITE) != WAIT_OBJECT_0) |
| 167 | #else // POSIX |
| 168 | #ifdef HAVE_FLOCK |
| 169 | if (flock(m_handle, LOCK_EX)) |
| 170 | #else |
| 171 | if (lockf(m_handle, F_LOCK, 0)) |
| 172 | #endif |
| 173 | #endif |
| 174 | { |
| 175 | raiseError("Control file %s lock failed (error: %d)", filename.c_str(), ERRNO); |
| 176 | } |
| 177 | |
| 178 | memset(&m_data, 0, sizeof(Data)); |
| 179 | strcpy(m_data.signature, CTL_SIGNATURE); |
| 180 | m_data.version = CTL_CURRENT_VERSION; |
| 181 | |
| 182 | const size_t length = (size_t) lseek(m_handle, 0, SEEK_END); |
| 183 | |
| 184 | if (!length) |
| 185 | { |
| 186 | m_data.sequence = sequence ? sequence - 1 : 0; |
| 187 | m_data.offset = 0; |
| 188 | m_data.db_sequence = 0; |
| 189 | |
| 190 | lseek(m_handle, 0, SEEK_SET); |
| 191 | if (write(m_handle, &m_data, sizeof(Data)) != sizeof(Data)) |
| 192 | raiseError("Control file %s cannot be written", filename.c_str()); |
| 193 | } |
| 194 | else if (length >= sizeof(DataV1)) |
| 195 | { |
| 196 | lseek(m_handle, 0, SEEK_SET); |
| 197 | if (read(m_handle, &m_data, sizeof(DataV1)) != sizeof(DataV1)) |
| 198 | raiseError("Control file %s appears corrupted", filename.c_str()); |
| 199 | |
| 200 | if (strcmp(m_data.signature, CTL_SIGNATURE) || |
| 201 | (m_data.version != CTL_VERSION1)) |
| 202 | { |
| 203 | raiseError("Control file %s appears corrupted", filename.c_str()); |
| 204 | } |
| 205 | |
| 206 | ActiveTransaction* const ptr = |
| 207 | m_data.txn_count ? transactions.getBuffer(m_data.txn_count) : NULL; |
| 208 | const ULONG txn_size = m_data.txn_count * sizeof(ActiveTransaction); |
| 209 |
nothing calls this directly
no test coverage detected