persistentstorage/sql/OsLayer/FileBuf64.cpp
changeset 31 ba1c4f4a893f
parent 29 cce6680bbf1c
equal deleted inserted replaced
29:cce6680bbf1c 31:ba1c4f4a893f
   254 
   254 
   255 @panic FBuf64 1 In _DEBUG mode - aMinCapacity is 0 or negative.
   255 @panic FBuf64 1 In _DEBUG mode - aMinCapacity is 0 or negative.
   256 */
   256 */
   257 RFileBuf64::RFileBuf64(TInt aMinCapacity) :
   257 RFileBuf64::RFileBuf64(TInt aMinCapacity) :
   258 	iCapacity(aMinCapacity),
   258 	iCapacity(aMinCapacity),
       
   259 	iBase(NULL),
   259 	iReadAheadSize(RFileBuf64::KDefaultReadAheadSize),
   260 	iReadAheadSize(RFileBuf64::KDefaultReadAheadSize),
   260 	iOptimized(EFalse)
   261 	iOptimized(EFalse)
   261 	{
   262 	{
   262 	__FBUF64_ASSERT(aMinCapacity > 0, EFBufPanicCapacity);
   263 	__FBUF64_ASSERT(aMinCapacity > 0, EFBufPanicCapacity);
   263 	}
   264 	}
   655 					iLength = aFilePos - iFilePos;
   656 					iLength = aFilePos - iFilePos;
   656 					iFileSize = Max(iFileSize, (iFilePos + iLength));
   657 					iFileSize = Max(iFileSize, (iFilePos + iLength));
   657 					iDirty = ETrue;	
   658 					iDirty = ETrue;	
   658 					}
   659 					}
   659 				else									
   660 				else									
   660 				//Beyond the end of the file and not in the buffer - set file size.
   661 				//Beyond the end of the file and not in the buffer - write the buffer to the file.
   661 					{
   662 					{
   662 					err = DoSetFileSize(aFilePos);
   663                     err = DoFileWrite2(aFilePos);
   663 					}
   664 					}
   664 				}
   665 				}
   665 			else										
   666 			else										
   666 			//Within the file, not in the buffer - write the buffer and associate the new file pos with the buffer
   667 			//Within the file, not in the buffer - write the buffer and associate the new file pos with the buffer
   667 				{
   668 				{
   671 		//3. The new write pos is in the buffer, but the data length is too big
   672 		//3. The new write pos is in the buffer, but the data length is too big
   672 		//   (For SQLite is OK, otherwise the whole block must be written to the file)
   673 		//   (For SQLite is OK, otherwise the whole block must be written to the file)
   673 		//4. The new write pos is in the buffer, the data entirely fits in the buffer
   674 		//4. The new write pos is in the buffer, the data entirely fits in the buffer
   674 		else
   675 		else
   675 			{
   676 			{
   676 			if(iCapacity == iLength)			//The buffer is full. Write the buffer and associate the new file pos
   677             if (iFilePos+iCapacity == aFilePos)	//The buffer is full. The new position to write is the end of the buffer.
   677 				{
   678                 {
   678 				err = DoFileWrite2(aFilePos);
   679                 err = DoFileWrite2(aFilePos);
   679 				}
   680                 }
   680 			if(err == KErrNone)
   681             if(err == KErrNone)
   681 				{
   682                 {
   682 				TInt amount = Min(len, (iCapacity - (aFilePos - iFilePos)));
   683                 TInt amount = Min(len, (iCapacity - (aFilePos - iFilePos)));
   683 				const TUint8* end = Mem::Copy(iBase + (aFilePos - iFilePos), data, amount);
   684                 const TUint8* end = Mem::Copy(iBase + (aFilePos - iFilePos), data, amount);
   684 				iLength = Max(iLength, (end - iBase));
   685                 iLength = Max(iLength, (end - iBase));
   685 				iFileSize = Max(iFileSize, (iFilePos + iLength));
   686                 iFileSize = Max(iFileSize, (iFilePos + iLength));
   686 				len -= amount;
   687                 len -= amount;
   687 				data += amount;
   688                 data += amount;
   688 				aFilePos += amount;
   689                 aFilePos += amount;
   689 				iDirty = ETrue;	
   690                 iDirty = ETrue;	
   690 				}
   691                 }
   691 			}
   692            }
   692 		}
   693 		}
   693 	__FILEBUF64_INVARIANT();
   694 	__FILEBUF64_INVARIANT();
   694 	return err;
   695 	return err;
   695 	}
   696 	}
   696 	
   697 	
   798 TInt RFileBuf64::DoPreInit()
   799 TInt RFileBuf64::DoPreInit()
   799     {
   800     {
   800     DoDiscard();
   801     DoDiscard();
   801     iReadAheadSize = RFileBuf64::KDefaultReadAheadSize;
   802     iReadAheadSize = RFileBuf64::KDefaultReadAheadSize;
   802     iBase = static_cast <TUint8*> (User::Alloc(iCapacity));
   803     iBase = static_cast <TUint8*> (User::Alloc(iCapacity));
   803     return iBase ? KErrNone : KErrNoMemory; 
   804     return iBase ? KErrNone : KErrNoMemory;   
   804     }
   805     }
   805 
   806 
   806 /**
   807 /**
   807 Performs post-initialization of the RFileBuf64 object.   
   808 Performs post-initialization of the RFileBuf64 object.   
   808 If aInitErr is not KErrNone, then the buffer memory will be released.
   809 If aInitErr is not KErrNone, then the buffer memory will be released.
   858 	PROFILE_SIZE(iFileSize, err);
   859 	PROFILE_SIZE(iFileSize, err);
   859 	if(err != KErrNone)
   860 	if(err != KErrNone)
   860 		{
   861 		{
   861 		DoDiscard();
   862 		DoDiscard();
   862 		}
   863 		}
       
   864 	else
       
   865 	    {
       
   866         iRealFileSize = iFileSize;
       
   867 	    }
   863 	__FILEBUF64_INVARIANT();
   868 	__FILEBUF64_INVARIANT();
   864 	return err;
   869 	return err;
   865 	}
   870 	}
   866 
   871 
   867 /**
   872 /**
   899 		DoDiscard();
   904 		DoDiscard();
   900 		}
   905 		}
   901 	else
   906 	else
   902 		{
   907 		{
   903 		iFileSize = aFileSize;
   908 		iFileSize = aFileSize;
       
   909 		iRealFileSize = aFileSize;
   904 		}
   910 		}
   905 	__FILEBUF64_INVARIANT();
   911 	__FILEBUF64_INVARIANT();
   906 	return err;
   912 	return err;
   907 	}
   913 	}
   908 
   914 
   954 	if(iLength == 0)
   960 	if(iLength == 0)
   955 		{
   961 		{
   956 		__FILEBUF64_INVARIANT();
   962 		__FILEBUF64_INVARIANT();
   957 		return KErrNone;	
   963 		return KErrNone;	
   958 		}
   964 		}
   959 	TPtrC8 data(iBase, iLength);		
   965 	TPtrC8 data(iBase, iLength);
   960 	TInt err = iFile.Write(iFilePos, data);
   966 	TInt err = KErrNone;
       
   967 	if(iFilePos > iRealFileSize )
       
   968 	    {
       
   969         err = DoSetFileSize(iFileSize);
       
   970  	    }
       
   971 	if(err == KErrNone)
       
   972 	    {
       
   973         err = iFile.Write(iFilePos, data);
       
   974 	    }
   961 	PROFILE_WRITE(iFilePos, iLength, err);
   975 	PROFILE_WRITE(iFilePos, iLength, err);
   962 	if(err == KErrNone)
   976 	if(err == KErrNone)
   963 		{
   977 		{
   964 		iFileSize = Max(iFileSize, (iFilePos + iLength));
   978 		iRealFileSize = iFileSize;
   965 		}
   979 		}
   966 	else
   980 	else
   967 		{
   981 		{
   968 		DoDiscard();
   982 		DoDiscard();
   969 		}
   983 		}