persistentstorage/sql/OsLayer/FileBuf64.cpp
changeset 31 ba1c4f4a893f
parent 29 cce6680bbf1c
--- a/persistentstorage/sql/OsLayer/FileBuf64.cpp	Thu Jul 01 17:02:22 2010 +0100
+++ b/persistentstorage/sql/OsLayer/FileBuf64.cpp	Tue Jul 06 11:54:49 2010 +0100
@@ -256,6 +256,7 @@
 */
 RFileBuf64::RFileBuf64(TInt aMinCapacity) :
 	iCapacity(aMinCapacity),
+	iBase(NULL),
 	iReadAheadSize(RFileBuf64::KDefaultReadAheadSize),
 	iOptimized(EFalse)
 	{
@@ -657,9 +658,9 @@
 					iDirty = ETrue;	
 					}
 				else									
-				//Beyond the end of the file and not in the buffer - set file size.
+				//Beyond the end of the file and not in the buffer - write the buffer to the file.
 					{
-					err = DoSetFileSize(aFilePos);
+                    err = DoFileWrite2(aFilePos);
 					}
 				}
 			else										
@@ -673,22 +674,22 @@
 		//4. The new write pos is in the buffer, the data entirely fits in the buffer
 		else
 			{
-			if(iCapacity == iLength)			//The buffer is full. Write the buffer and associate the new file pos
-				{
-				err = DoFileWrite2(aFilePos);
-				}
-			if(err == KErrNone)
-				{
-				TInt amount = Min(len, (iCapacity - (aFilePos - iFilePos)));
-				const TUint8* end = Mem::Copy(iBase + (aFilePos - iFilePos), data, amount);
-				iLength = Max(iLength, (end - iBase));
-				iFileSize = Max(iFileSize, (iFilePos + iLength));
-				len -= amount;
-				data += amount;
-				aFilePos += amount;
-				iDirty = ETrue;	
-				}
-			}
+            if (iFilePos+iCapacity == aFilePos)	//The buffer is full. The new position to write is the end of the buffer.
+                {
+                err = DoFileWrite2(aFilePos);
+                }
+            if(err == KErrNone)
+                {
+                TInt amount = Min(len, (iCapacity - (aFilePos - iFilePos)));
+                const TUint8* end = Mem::Copy(iBase + (aFilePos - iFilePos), data, amount);
+                iLength = Max(iLength, (end - iBase));
+                iFileSize = Max(iFileSize, (iFilePos + iLength));
+                len -= amount;
+                data += amount;
+                aFilePos += amount;
+                iDirty = ETrue;	
+                }
+           }
 		}
 	__FILEBUF64_INVARIANT();
 	return err;
@@ -800,7 +801,7 @@
     DoDiscard();
     iReadAheadSize = RFileBuf64::KDefaultReadAheadSize;
     iBase = static_cast <TUint8*> (User::Alloc(iCapacity));
-    return iBase ? KErrNone : KErrNoMemory; 
+    return iBase ? KErrNone : KErrNoMemory;   
     }
 
 /**
@@ -860,6 +861,10 @@
 		{
 		DoDiscard();
 		}
+	else
+	    {
+        iRealFileSize = iFileSize;
+	    }
 	__FILEBUF64_INVARIANT();
 	return err;
 	}
@@ -901,6 +906,7 @@
 	else
 		{
 		iFileSize = aFileSize;
+		iRealFileSize = aFileSize;
 		}
 	__FILEBUF64_INVARIANT();
 	return err;
@@ -956,12 +962,20 @@
 		__FILEBUF64_INVARIANT();
 		return KErrNone;	
 		}
-	TPtrC8 data(iBase, iLength);		
-	TInt err = iFile.Write(iFilePos, data);
+	TPtrC8 data(iBase, iLength);
+	TInt err = KErrNone;
+	if(iFilePos > iRealFileSize )
+	    {
+        err = DoSetFileSize(iFileSize);
+ 	    }
+	if(err == KErrNone)
+	    {
+        err = iFile.Write(iFilePos, data);
+	    }
 	PROFILE_WRITE(iFilePos, iLength, err);
 	if(err == KErrNone)
 		{
-		iFileSize = Max(iFileSize, (iFilePos + iLength));
+		iRealFileSize = iFileSize;
 		}
 	else
 		{