bool MojDbPutHandler::matchWildcard(const MojString& expr, const MojChar* val, MojSize len) { LOG_TRACE("Entering function %s", __FUNCTION__); if (expr.last() == _T('*') && len >= (expr.length() - 1) && MojStrNCmp(expr, val, expr.length() - 1) == 0) { return true; } return false; }
MojErr MojDbTextUtils::strToUnicode(const MojString& src, UnicodeVec& destOut) { MojErr err = destOut.resize(src.length() * 2); MojErrCheck(err); MojInt32 destCapacity = 0; MojInt32 destLength = 0; do { UChar* dest = NULL; err = destOut.begin(dest); MojErrCheck(err); destCapacity = (MojInt32) destOut.size(); UErrorCode status = U_ZERO_ERROR; u_strFromUTF8(dest, destCapacity, &destLength, src.data(), (MojInt32) src.length(), &status); if (status != U_BUFFER_OVERFLOW_ERROR) MojUnicodeErrCheck(status); err = destOut.resize(destLength); MojErrCheck(err); } while (destLength > destCapacity); return MojErrNone; }
MojErr MojDbIndex::validateName(const MojString& name) { if (name.length() > MaxIndexNameLen) { MojErrThrowMsg(MojErrDbInvalidIndexName, _T("db: index name '%s' invalid: length is %zd chars, max is %zd"), name.data(), name.length(), MaxIndexNameLen); } for (MojString::ConstIterator i = name.begin(); i < name.end(); ++i) { if (!MojIsAlNum(*i) && *i != _T('_')) { MojErrThrowMsg(MojErrDbInvalidIndexName, _T("db: index name '%s' invalid: char at position %zd not allowed"), name.data(), (MojSize) (i - name.begin())); } } return MojErrNone; }
MojErr MojDbPutHandler::validateWildcard(const MojString& val, MojErr errToThrow) { LOG_TRACE("Entering function %s", __FUNCTION__); if (val.empty()) MojErrThrow(errToThrow); MojSize wildcardPos = val.find(_T('*')); if (wildcardPos != MojInvalidSize) { if (wildcardPos != val.length() - 1 || (wildcardPos != 0 && val.at(wildcardPos - 1) != _T('.'))) { MojErrThrowMsg(errToThrow, _T("db: invalid wildcard in - '%s'"), val.data()); } } return MojErrNone; }
MojErr MojDbKind::init(const MojString& id) { MojLogTrace(s_log); // parse name and version out of id if (id.length() > KindIdLenMax) MojErrThrowMsg(MojErrDbMalformedId, _T("db: kind id too long")); MojSize sepIdx = id.rfind(VersionSeparator); if (sepIdx == MojInvalidIndex) MojErrThrow(MojErrDbMalformedId); MojErr err = id.substring(0, sepIdx, m_name); MojErrCheck(err); MojString str; err = id.substring(sepIdx + 1, id.length() - sepIdx - 1, str); MojErrCheck(err); const MojChar* end = NULL; MojInt64 ver = MojStrToInt64(str.data(), &end, 0); if (*end != '\0' || ver < 0 || ver > MojUInt32Max) MojErrThrow(MojErrDbMalformedId); m_version = (MojUInt32) ver; m_id = id; return MojErrNone; }
void BusClient::ScanDir(const MojString& _id, Configurator::RunType scanType, const std::string &baseDir, ScanTypes bitmask, Configurator::ConfigType configType, AdditionalFileTypes types) { const std::string id(_id.data(), _id.length()); if (m_shuttingDown) { MojLogDebug(m_log, "Aborting shutdown - request received"); assert(m_timerTimeout != 0); g_source_remove(m_timerTimeout); m_timerTimeout = 0; assert(m_shuttingDown); m_shuttingDown = false; } if (bitmask & DBKINDS) { if (types & DeprecatedDbKind) { // deprecated MojLogWarning(m_log, "Scanning deprecated mojodb config directory under %s", baseDir.c_str()); ConfiguratorPtr oldDbKindConfigurator(new DbKindConfigurator(id, configType, scanType, *this, m_dbClient, baseDir + OLD_DB_KIND_DIR)); m_configurators.push_back(oldDbKindConfigurator); } ConfiguratorPtr dbKindConfigurator(new DbKindConfigurator(id, configType, scanType, *this, m_dbClient, baseDir + DB_KIND_DIR)); m_configurators.push_back(dbKindConfigurator); } if (bitmask & DBPERMISSIONS) { ConfiguratorPtr dbPermsConfigurator(new DbPermissionsConfigurator(id, configType, scanType, *this, m_dbClient, baseDir + DB_PERMISSIONS_DIR)); m_configurators.push_back(dbPermsConfigurator); } if (bitmask & FILECACHE) { ConfiguratorPtr fileCacheConfigurator(new FileCacheConfigurator(id, configType, scanType, *this, baseDir + FILE_CACHE_CONFIG_DIR)); m_configurators.push_back(fileCacheConfigurator); } if (bitmask & ACTIVITIES) { ActivityConfigurator *activityConfigurator = new ActivityConfigurator(id, configType, scanType, *this, baseDir + ACTIVITY_CONFIG_DIR); m_configurators.push_back(activityConfigurator); } }
void SaveEmailCommand::WriteToFileCache(MojObject &part, const MojString &pathName) { MojErr err; MojString content; err = part.getRequired("content", content); ErrorToException(err); // FIXME: make this async FILE *fp = fopen(pathName.data(), "w"); if(fp) { fwrite(content.data(), sizeof(MojChar), content.length(), fp); fclose(fp); } else { throw MailException("error opening file cache path", __FILE__, __LINE__); } // Cancel subscription to signal that we're done writing m_fileCacheInsertedSlot.cancel(); MojString type; err = part.getRequired(EmailSchema::Part::TYPE, type); ErrorToException(err); if (type == EmailSchema::Part::Type::BODY) { std::string previewText = PreviewTextGenerator::GeneratePreviewText(content.data(), MAX_SUMMARY_LENGTH, true); err = m_email.putString(EmailSchema::SUMMARY, previewText.c_str()); ErrorToException(err); } // Delete content field so it doesn't get written to the database bool wasDeleted; part.del("content", wasDeleted); // Next step if(m_partsIt == m_partsArray.arrayEnd()) PersistToDatabase(); // done else CreateNextCacheObject(); // more parts remaining return; }
void SaveEmailCommand::CreateNextCacheObject() { MojErr err; for(; m_partsIt != m_partsArray.arrayEnd(); ++m_partsIt) { MojObject& part = *m_partsIt; bool hasContent; MojString content; err = part.get("content", content, hasContent); ErrorToException(err); if(hasContent) { MojString cacheType; cacheType.assign("email"); bool hasDisplayName; MojString displayName; err = part.get("displayName", displayName, hasDisplayName); ErrorToException(err); MojString cacheFileName; if(hasDisplayName && displayName.length() > 0) { cacheFileName.assign(displayName); } else { // FIXME only for HTML parts cacheFileName.assign("body.html"); } MojLogDebug(m_log, "creating a file cache entry"); m_client.GetFileCacheClient().InsertCacheObject(m_fileCacheInsertedSlot, cacheType, cacheFileName, content.length(), EMAIL_FILECACHE_COST, EMAIL_NO_LIFE_TIME); // Exit method and wait for response return; } } // If this is the last part with content, persist to database now PersistToDatabase(); }
MojErr MojDb::dumpObj(MojFile& file, MojObject obj, MojSize& bytesWrittenOut, MojUInt32 maxBytes) { // remove the rev key before dumping the object bool found = false; MojErr err = obj.del(RevKey, found); MojErrCheck(err); MojString str; err = obj.toJson(str); MojErrCheck(err); err = str.append(_T("\n")); MojErrCheck(err); MojSize len = str.length() * sizeof(MojChar); // if writing this object will put us over the max length, throw an error if (maxBytes && bytesWrittenOut + len > maxBytes) { MojErrThrow(MojErrDbBackupFull); } err = file.writeString(str, bytesWrittenOut); MojErrCheck(err); return MojErrNone; }
MojErr MojDbTextUtils::unicodeToStr(const UChar* src, MojSize len, MojString& destOut) { MojAssert(src || len == 0); MojErr err = destOut.resize(len * 2); MojErrCheck(err); MojInt32 destCapacity = 0; MojInt32 destLength = 0; do { MojChar* dest = NULL; err = destOut.begin(dest); MojErrCheck(err); destCapacity = (MojInt32) destOut.length(); UErrorCode status = U_ZERO_ERROR; u_strToUTF8(dest, destCapacity, &destLength, src, (MojInt32) len, &status); if (status != U_BUFFER_OVERFLOW_ERROR) MojUnicodeErrCheck(status); err = destOut.resize(destLength); MojErrCheck(err); } while (destLength > destCapacity); return MojErrNone; }