Locking a Large File

How to lock a section of a large file.

The RFile64::Lock() function has a TInt64 parameter that specifies the position to lock from. By using a TInt64 rather than a TInt, the function can lock a section of file that is greater than 2GB–1.

See RFile64::Lock(TInt64 aPos, TInt64 aLength).

  1. Use 64-bit RFile64 rather than RFile.
    // RFile file;
    RFile64 file;
  2. Replace TInt variables with TInt64 variables. Use a TInt64 variable if a lock position in a file that is greater than 2GB–1 is required.
    // TInt lockPosition, lockLength;
    TInt64 lockPosition, lockLength;

Lock example

The example below shows a variable of the type TInt64 being used to specify the lock position and the lock length being used together with the RFile64 class.

//    RFile file;
    RFile64 file;
    TInt err = file.Open(TheFs, _L(“BIGFILE.BIG”), EFileWrite);
    if(err != KErrNone)
     {
        // handle error
        return err;
     }

    // Large file access is now supported!
    // TInt lockPosition, lockLength;
    TInt64 lockPosition, lockLength;
    err = file.Lock(lockPosition,lockLength);
        if(err != KErrNone)
            {
            //could return with KErrArgument if lockPosition+lockLength>63Bytes   
            return err;
            }

You must unlock the section of file using RFile64::Unlock(), as explained in Unlocking a Large File.