return errorStr;
}
-static std::string getSystemStrerror(void) {
- std::stringstream ss;
-#ifdef WIN32
- char* win_msg = NULL;
- DWORD err = GetLastError();
- FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, err,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &win_msg,
- 0, NULL);
- ss << "errno = " << err << ": '" << win_msg << "'";
- LocalFree(win_msg);
-#else
- ss << "errno = " << errno << ": '" << strerror(errno) << "'";
-#endif
-
- return ss.str();
-}
-
static uint8_t determine_datatype(const unsigned char* value,
size_t length) {
if (checkUTF8JSON(value, length)) {
LOG(EXTENSION_LOG_WARNING,
"Warning: failed to rename '%s' to '%s': %s",
compact_file.c_str(), new_file.c_str(),
- getSystemStrerror().c_str());
+ getStringErrno().c_str());
removeCompactFile(compact_file);
return false;
if (remove(new_file.c_str()) != 0) {
LOG(EXTENSION_LOG_WARNING,
"Warning: Failed to remove '%s': %s",
- new_file.c_str(), getSystemStrerror().c_str());
+ new_file.c_str(), getStringErrno().c_str());
}
return false;
}
dbFileName.c_str(), options,
((newRevNum > fileRev) ? newRevNum : fileRev),
couchstore_strerror(errorCode),
- getSystemStrerror().c_str());
+ getStringErrno().c_str());
} else {
if (newRevNum > fileRev) {
// new revision number found, update it
LOG(EXTENSION_LOG_INFO, "INFO: couchstore_open_db failed, name=%s "
"options=%" PRIX64 " error=%s [%s], try it again!",
dbfile.c_str(), options, couchstore_strerror(errCode),
- getSystemStrerror().c_str());
+ getStringErrno().c_str());
*newFileRev = checkNewRevNum(dbfile);
++retry;
if (retry == MAX_OPEN_DB_RETRY - 1 && options == 0 &&
} else {
LOG(EXTENSION_LOG_WARNING,
"Warning: Failed to remove the stale file '%s': %s",
- old_file.str().c_str(), getSystemStrerror().c_str());
+ old_file.str().c_str(), getStringErrno().c_str());
}
} else {
LOG(EXTENSION_LOG_WARNING,
else {
LOG(EXTENSION_LOG_WARNING,
"Warning: Failed to remove compact file '%s': %s",
- filename.c_str(), getSystemStrerror().c_str());
+ filename.c_str(), getStringErrno().c_str());
if (errno != ENOENT) {
pendingFileDeletions.push(const_cast<std::string &>(filename));
"new", "del", "del_all", "commit1", "commit2", NULL
};
-
#ifdef WIN32
ssize_t pread(file_handle_t fd, void *buf, size_t nbyte, uint64_t offset)
{
size_t nbytes) {
DWORD byteswritten;
if (!WriteFile(fd, buf, nbytes, &byteswritten, NULL)) {
- /* luckily we don't check errno so we don't need to care about that */
+ const std::string& err_str = getStringErrno();
+ LOG(EXTENSION_LOG_WARNING,
+ "Failed to write to mutation log with error: %s",
+ err_str.c_str());
return -1;
}
- cb_assert(GetLastError() != ERROR_IO_PENDING);
return byteswritten;
}
}
}
-static inline std::string getErrorString(void) {
- std::string ret;
- char* win_msg = NULL;
- DWORD err = GetLastError();
- FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, err,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR)&win_msg,
- 0, NULL);
- ret.assign(win_msg);
- LocalFree(win_msg);
- return ret;
-}
-
static int64_t SeekFile(file_handle_t fd, const std::string &fname,
uint64_t offset, bool end)
{
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
std::stringstream ss;
ss << "FATAL: SetFilePointer failed " << fname << ": " <<
- getErrorString();
+ getStringErrno();
LOG(EXTENSION_LOG_WARNING, ss.str().c_str());
li.QuadPart = -1;
}
}
if (fd == INVALID_FILE_VALUE) {
- error.assign(getErrorString());
+ error.assign(getStringErrno());
}
return fd;
#endif
-static void writeFully(file_handle_t fd, const uint8_t *buf, size_t nbytes) {
+static bool writeFully(file_handle_t fd, const uint8_t *buf, size_t nbytes) {
while (nbytes > 0) {
ssize_t written = doWrite(fd, buf, nbytes);
- cb_assert(written >= 0);
-
- nbytes -= written;
- buf += written;
+ if (written >= 0) {
+ nbytes -= written;
+ buf += written;
+ } else {
+ return false;
+ }
}
+
+ return true;
}
uint64_t MutationLogEntry::rowid() const {
0, ML_COMMIT1, 0,
"");
writeEntry(mle);
+
if ((getSyncConfig() & FLUSH_COMMIT_1) != 0) {
flush();
}
0, ML_COMMIT2, 0,
"");
writeEntry(mle);
+
if ((getSyncConfig() & FLUSH_COMMIT_2) != 0) {
flush();
}
cb_assert(isOpen());
headerBlock.set(blockSize);
- writeFully(file, (uint8_t*)&headerBlock, sizeof(headerBlock));
+ if (!writeFully(file, (uint8_t*)&headerBlock, sizeof(headerBlock))) {
+ return false;
+ }
int64_t seek_result = SeekFile(file, getLogFile(),
std::max(
getLogFile().c_str(), strerror(errno));
return false;
}
+
uint8_t zero(0);
- writeFully(file, &zero, sizeof(zero));
+ if (!writeFully(file, &zero, sizeof(zero))) {
+ return false;
+ }
return true;
}
cb_assert(mlog.isEnabled());
cb_assert(isEnabled());
- mlog.flush();
+ if (!mlog.flush()) {
+ return false;
+ }
mlog.close();
- flush();
+
+ if (!flush()) {
+ return false;
+ }
close();
for (int i(0); i < MUTATION_LOG_TYPES; ++i) {
return true;
}
-void MutationLog::flush() {
+bool MutationLog::flush() {
if (isEnabled() && blockPos > HEADER_RESERVED) {
cb_assert(isOpen());
needWriteAccess();
uint16_t crc16(htons(crc32 & 0xffff));
memcpy(blockBuffer, &crc16, sizeof(crc16));
- writeFully(file, blockBuffer, blockSize);
- logSize.fetch_add(blockSize);
-
- blockPos = HEADER_RESERVED;
- entries = 0;
+ if (writeFully(file, blockBuffer, blockSize)) {
+ logSize.fetch_add(blockSize);
+ blockPos = HEADER_RESERVED;
+ entries = 0;
+ } else {
+ /* write to the mutation log failed. Disable the log */
+ disabled = true;
+ LOG(EXTENSION_LOG_WARNING,
+ "Disabling access log due to write failures");
+ return false;
+ }
}
+
+ return true;
}
void MutationLog::writeEntry(MutationLogEntry *mle) {