intrusive_ptr<const Value> AccumulatorAvg::getValue() const { if (!pCtx->getInShard()) { double avg = 0; if (count) { if (totalType != NumberDouble) avg = static_cast<double>(longTotal / count); else avg = doubleTotal / count; } return Value::createDouble(avg); } intrusive_ptr<Document> pDocument(Document::create()); intrusive_ptr<const Value> pSubTotal; if (totalType == NumberInt) pSubTotal = Value::createInt((int)longTotal); else if (totalType == NumberLong) pSubTotal = Value::createLong(longTotal); else pSubTotal = Value::createDouble(doubleTotal); pDocument->addField(subTotalName, pSubTotal); intrusive_ptr<const Value> pCount(Value::createLong(count)); pDocument->addField(countName, pCount); return Value::createDocument(pDocument); }
intrusive_ptr<const Value> AccumulatorAvg::evaluate( const intrusive_ptr<Document> &pDocument) const { if (!pCtx->getInRouter()) { Super::evaluate(pDocument); ++count; } else { /* If we're in the router, we expect an object that contains both a subtotal and a count. This is what getValue() produced below. */ intrusive_ptr<const Value> prhs( vpOperand[0]->evaluate(pDocument)); verify(prhs->getType() == Object); intrusive_ptr<Document> pShardDoc(prhs->getDocument()); intrusive_ptr<const Value> pSubTotal( pShardDoc->getValue(subTotalName)); verify(pSubTotal.get()); BSONType subTotalType = pSubTotal->getType(); if ((totalType == NumberLong) || (subTotalType == NumberLong)) totalType = NumberLong; if ((totalType == NumberDouble) || (subTotalType == NumberDouble)) totalType = NumberDouble; if (subTotalType == NumberInt) { int v = pSubTotal->getInt(); longTotal += v; doubleTotal += v; } else if (subTotalType == NumberLong) { long long v = pSubTotal->getLong(); longTotal += v; doubleTotal += v; } else { double v = pSubTotal->getDouble(); doubleTotal += v; } intrusive_ptr<const Value> pCount(pShardDoc->getValue(countName)); count += pCount->getLong(); } return Value::getZero(); }
/** Open a drive for formatting. */ TInt FsFormatOpen(CFsRequest* aRequest) { TDrive& drive = *aRequest->Drive(); __PRINT1(_L("FsFormatOpen() drv:%d"), drive.DriveNumber()); TInt nMountRes = drive.CheckMount(); //-- KErrNotReady means that there is no file system mounted on this drive //-- KErrInUse means that there are some "disk access" objects, like RFormat or RRawDisk opened on the mount. if(nMountRes == KErrNotReady || nMountRes == KErrInUse) { __PRINT1(_L("FsFormatOpen() ChkMount:%d"), nMountRes); return nMountRes; } const TFormatMode fmtMode = (TFormatMode)aRequest->Message().Int1(); TName buf; TUint32 currFsNameHash = 0; //-- current file system name hash, 0 means "not set"; used during forced FS dismounting if((nMountRes == KErrNone) && drive.CurrentMount().LockStatus() < 0) {//-- the mount is locked, it has normal objects (files, directories) opened on it. //-- if someone is interested in the list of opened files and number of opened directories, compile this code in. #ifdef DUMP_OPENED_OBJECTS DumpOpenedObjects(drive); #endif //DUMP_OPENED_OBJECTS if(!(fmtMode & EForceFormat)) { __PRINT(_L("FsFormatOpen() The mount is in use")); return KErrInUse; } //-- there is a special flag that tells to force media dismounting even if it has files or dirs opened. __PRINT(_L("FsFormatOpen() The mount is in use, forcing dismounting!")); //-- record currently mounted FS name hash, it may be used after forced dismounting drive.CurrentMount().FileSystemName(buf); //-- the iCurrentMount is alive currFsNameHash = TVolFormatParam::CalcFSNameHash(buf); //-- kill the current mount FsThreadManager::LockDrive(drive.DriveNumber()); TInt nRes = drive.ForceUnmountFileSystemForFormatting(); FsThreadManager::UnlockDrive(drive.DriveNumber()); switch(nRes) { case KErrInUse: __PRINT(_L("FsFormatOpen() The mount has clamps! Can't force dismount")); return KErrInUse; //-- there are clamps on this drive - can't dismount case KErrNone: break; default: ASSERT(0); //-- unexpected error code return nRes; }; if(fmtMode & EQuickFormat) {//-- quick format may require the normally mounted FS, make the best effrot to mount it nMountRes = drive.CheckMount(); } else {//-- this will make the FS mounted by force; for full format it will be quicker nMountRes = KErrCorrupt; } } //-- if True, we will need mount (probably specific) file system by force because normal mounting has failed TBool bNeedForceMount = (nMountRes != KErrNone); //-- find out if we have optional data structure that describes format parameter TUint32 newFsNameHash = 0; //-- file system name hash, may be used for selecting which file system to put onto the volume. 0 means "not specified" const TLDFormatInfo* pLDFormatInfo = NULL; const TVolFormatParam* pVolFormatParam = NULL; __ASSERT_COMPILE(sizeof(TVolFormatParam) >= sizeof(TLDFormatInfo)); TBuf8<sizeof(TVolFormatParam)> paramBuf; if(fmtMode & ESpecialFormat) { //-- the user has provided format parameters structure. //-- IPC argument #2 contains a structure: <TUint32>[optional package descriptor] //-- where 1st mandatory TUint32 is a pointer to format counter and the optional additional package is a data structure passed to the filesystem by the client of RFormat const TInt desLen = aRequest->GetDesLength(KMsgPtr2); ASSERT((TUint32)desLen >= sizeof(TUint32)); const TInt dataPckgLen = desLen - sizeof(TUint32); if((TUint32)dataPckgLen > sizeof(TUint32)) { aRequest->ReadL(KMsgPtr2, paramBuf); } if(dataPckgLen == sizeof(TLDFormatInfo)) {//-- the user has provided formatting parameters via TLDFormatInfo structure. pLDFormatInfo = (const TLDFormatInfo*)(paramBuf.Ptr() + sizeof(TUint32)); } else if(dataPckgLen == sizeof(TVolFormatParam)) {//-- it's likely to be TVolFormatParam, need to check UId to be sure. pVolFormatParam = (const TVolFormatParam*)(const TVolFormatParam*)(paramBuf.Ptr() + sizeof(TUint32)); if(pVolFormatParam->iUId == TVolFormatParam::KUId) //-- check the class UID {//-- this is the real TVolFormatParam object passed newFsNameHash = pVolFormatParam->FSNameHash(); } } else if(dataPckgLen >0) {//-- parameters data structure has strange length return KErrArgument; } } //------------------- if(!newFsNameHash && currFsNameHash) {//-- new file system name isn't specified (default formatting), but the volume had been forcedly dismounted. //-- restore the original file system newFsNameHash = currFsNameHash; } if(newFsNameHash) {//-- check if the specified FS is already mounted on the volume if(!bNeedForceMount) { drive.CurrentMount().FileSystemName(buf); //-- the iCurrentMount is alive } else { //-- the iCurrentMount can be NULL, use the iFsys - the real file system associated with this drive buf = drive.GetFSys()->Name(); } const TUint32 currFSNameHash = TVolFormatParam::CalcFSNameHash(buf); if(currFSNameHash == newFsNameHash) {//-- no need to do anything, the required FS is already mounted newFsNameHash = 0; } } if(newFsNameHash) { //-- the user has specified some filesystem to be mounted on the volume. Check if this FS is supported at all. //-- if it is supported, but some other FS is currently mounted, it will be dismounted and the new one will be forced. TInt nRes; for(TInt cntFS=0; ;++cntFS) { nRes = drive.FSys().GetSupportedFileSystemName(cntFS, buf); //-- enumerate possible child file systems if(nRes != KErrNone) return KErrNotSupported; //-- the filesystem with the given name (fsNameHash) is not supported. if(newFsNameHash == TVolFormatParam::CalcFSNameHash(buf)) {//-- the filesystem with the given name (fsNameHash) is supported, but some other filesystem can be already mounted drive.Dismount(); bNeedForceMount = ETrue; //-- this will force the desired FS to be mounted break; } } }//if(fsNameHash) //-- try force mounting the desired file system if it is required if(bNeedForceMount) { const TInt KMaxRetries = 3; for(TInt cnt=0; ; ++cnt) { drive.MountFileSystem(ETrue, newFsNameHash); nMountRes = drive.GetReason(); if(nMountRes == KErrNone || nMountRes == KErrLocked) break; drive.Dismount(); //-- will reset mount retries counter if(cnt >= KMaxRetries) { __PRINT1(_L("FsFormatOpen() can't mount FS! res:%d"), nMountRes); return nMountRes; } } } ASSERT(nMountRes == KErrNone || nMountRes == KErrLocked); __ASSERT_DEBUG(drive.CurrentMount().LockStatus()==0, Fault(ESvrFormatOpenFailed)); TDriveInfo dInfo; drive.DriveInfo(dInfo); const TInt mediaAtt = dInfo.iMediaAtt; #if defined(_LOCKABLE_MEDIA) if (!(fmtMode & EForceErase) && (mediaAtt & KMediaAttLocked)) { // if attempting to format a locked drive, dismount otherwise subsequent // requests will operate on a mount that has been forcibly mounted (a few lines above) CMountCB* pM = &drive.CurrentMount(); if(pM) pM->Close(); drive.MountFileSystem(EFalse); // clear iCurrentMount return KErrLocked; } #endif if (!(mediaAtt & KMediaAttFormattable) || (mediaAtt & KMediaAttWriteProtected)) { CMountCB* pM = &drive.CurrentMount(); if(pM) pM->Close(); drive.MountFileSystem(EFalse); return KErrAccessDenied; } //-- instantinate and open CFormatCB object for this drive CFormatCB* formatCB=NULL; TInt fmtHandle; TRAPD(ret, formatCB = drive.FormatOpenL(aRequest, fmtHandle, fmtMode, pLDFormatInfo, pVolFormatParam )); if (ret!=KErrNone) { if(formatCB) formatCB->Close(); return ret; } TPtrC8 pH((TUint8*)&fmtHandle,sizeof(TInt)); aRequest->WriteL(KMsgPtr3,pH); TInt count=100; TPtrC8 pCount((TUint8*)&count,sizeof(TInt)); aRequest->WriteL(KMsgPtr2,pCount); aRequest->Session()->IncResourceCount(); return KErrNone; }