remotestoragefw/remotefileengine/src/rsfwsession.cpp
branchRCL_3
changeset 19 88ee4cf65e19
parent 16 87c71b25c937
child 20 1aa8c82cb4cb
equal deleted inserted replaced
16:87c71b25c937 19:88ee4cf65e19
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Client side of Remote Storage FW API access functions.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "rsfwsession.h"
       
    21 #include "rsfwinterface.h"
       
    22 
       
    23 #ifdef __WINS__
       
    24 #include <e32math.h>
       
    25 #endif
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 // Number of message slots to reserve for this client server session.
       
    30 // Since we only communicate synchronously here, we never have any
       
    31 // outstanding asynchronous requests.
       
    32 const TUint KDefaultMessageSlots = 4;
       
    33 
       
    34 #ifdef __WINS__
       
    35 const TUint KServerMinHeapSize =   0x1000;  //  4K
       
    36 const TUint KServerMaxHeapSize = 0x100000;  // 64K
       
    37 #endif
       
    38 
       
    39 // ============================ MEMBER FUNCTIONS ==============================
       
    40 
       
    41 // ----------------------------------------------------------------------------
       
    42 // RRsfwSession::RRsfwSession
       
    43 // C++ default constructor can NOT contain any code, that
       
    44 // might leave.
       
    45 // ----------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C RRsfwSession::RRsfwSession() : RSessionBase()
       
    48     {
       
    49     }
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // RRsfwSession::Connect
       
    53 // Connects to the framework by starting the server if neccessary and creating
       
    54 // a session.
       
    55 // (other items were commented in a header).
       
    56 // ----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C TInt RRsfwSession::Connect()
       
    59     {
       
    60     const TInt KTryCount = 3;
       
    61     
       
    62     TInt err;
       
    63     TBool retry;
       
    64     TInt i = KTryCount;
       
    65     do
       
    66         {
       
    67         err = StartServer(KRfeServerName);
       
    68         if (err == KErrNone)
       
    69             {
       
    70             err = CreateSession(KRfeServerName,
       
    71                                 Version(),
       
    72                                 KDefaultMessageSlots);
       
    73             }
       
    74         retry = ((err == KErrNotFound) || (err == KErrServerTerminated));
       
    75         } while (retry && (++i <= KTryCount));
       
    76 
       
    77     return err;
       
    78     }
       
    79     
       
    80 // ----------------------------------------------------------------------------
       
    81 // RRsfwSession::Close
       
    82 // ----------------------------------------------------------------------------
       
    83 //
       
    84 EXPORT_C void RRsfwSession::Close()
       
    85     {
       
    86     }
       
    87     
       
    88 // ----------------------------------------------------------------------------
       
    89 // RRsfwSession::Version
       
    90 // Returns the version of Remote File Engine
       
    91 // (other items were commented in a header).
       
    92 // ----------------------------------------------------------------------------
       
    93 //
       
    94 EXPORT_C TVersion RRsfwSession::Version() const
       
    95     {
       
    96     return(TVersion(KRfeMajorVersionNumber,
       
    97                     KRfeMinorVersionNumber,
       
    98                     KRfeBuildVersionNumber));
       
    99     }
       
   100     
       
   101 // ----------------------------------------------------------------------------
       
   102 // RRsfwSession::MoveFids
       
   103 // Sends the rename operation to Remote File Engine by putting the parameters
       
   104 // into the shared memory chunk, sending the request and reading the result.
       
   105 // (other items were commented in a header).
       
   106 // ----------------------------------------------------------------------------
       
   107 //
       
   108 EXPORT_C TInt RRsfwSession::MoveFids(
       
   109     TFid aSourceFid,
       
   110     const TDesC& aSourceName,
       
   111     TFid aDestFid,
       
   112     const TDesC& aDestName,
       
   113     TBool aOverWrite) 
       
   114     {
       
   115 
       
   116     TRfeRenameInArgs* renameArgs = new TRfeRenameInArgs();
       
   117     if (!renameArgs) 
       
   118         {
       
   119         return KErrNoMemory;
       
   120         }
       
   121     
       
   122     //  TRfeRenameInArgs* writePtr = static_cast<TRfeRenameInArgs*>(iWritePtr);
       
   123     renameArgs->iOpCode = ERenameReplace;
       
   124 
       
   125     renameArgs->iFid = aSourceFid;
       
   126     renameArgs->iSrcName.Copy(aSourceName); 
       
   127     renameArgs->iDstFid = aDestFid;
       
   128     renameArgs->iDstName.Copy(aDestName);
       
   129     renameArgs->iOverWrite = aOverWrite;
       
   130        
       
   131     TPckg<TRfeRenameInArgs> pckgInArgs(*renameArgs);
       
   132     
       
   133     TInt result = SendRequest(ERenameReplace, aSourceFid.iVolumeId, 
       
   134                               TIpcArgs(&pckgInArgs));
       
   135     delete renameArgs;
       
   136     return result;
       
   137     }
       
   138     
       
   139 // ----------------------------------------------------------------------------
       
   140 // RRsfwSession::SetEntry
       
   141 // (other items were commented in a header).
       
   142 // ----------------------------------------------------------------------------
       
   143 //    
       
   144 EXPORT_C TInt RRsfwSession::SetEntry(
       
   145     const TFid aFid,
       
   146     const TTime& aTime,
       
   147     TUint aSetAttMask,
       
   148     TUint aClearAttMask)
       
   149     {  
       
   150     TRfeSetAttrInArgs* setEntryArgs = new TRfeSetAttrInArgs();
       
   151     if (!setEntryArgs) 
       
   152         {
       
   153         return KErrNoMemory;
       
   154         }
       
   155         
       
   156     setEntryArgs->iOpCode = ESetAttr;
       
   157     setEntryArgs->iFid = aFid;
       
   158 
       
   159     // default: no change
       
   160     setEntryArgs->iAttr.iAtt = 0; 
       
   161     setEntryArgs->iMask.iAtt = 0; 
       
   162 
       
   163     if (aSetAttMask & KEntryAttReadOnly)
       
   164         {
       
   165         // Set read-only
       
   166         setEntryArgs->iAttr.iAtt |= KEntryAttReadOnly;
       
   167         setEntryArgs->iMask.iAtt |= KEntryAttReadOnly;
       
   168         }
       
   169     if (aClearAttMask & KEntryAttReadOnly)
       
   170         {
       
   171         // Reset read-only
       
   172         setEntryArgs->iMask.iAtt |= KEntryAttReadOnly;
       
   173         }
       
   174          
       
   175     // Setting time
       
   176     setEntryArgs->iAttr.iModified = aTime;
       
   177 
       
   178     // Let's see - we will not want to do anything unless
       
   179     // the attributes convey something significant.
       
   180     TInt result = KErrNone;
       
   181     if (setEntryArgs->iMask.iAtt)
       
   182         {
       
   183         TPckg<TRfeSetAttrInArgs> pckgInArgs(*setEntryArgs);
       
   184         result = SendRequest(ESetAttr, aFid.iVolumeId, TIpcArgs(&pckgInArgs));
       
   185         }       
       
   186     delete setEntryArgs;   
       
   187     return result;
       
   188     }
       
   189     
       
   190 // ----------------------------------------------------------------------------
       
   191 // RRsfwSession::FlushCache
       
   192 // Flushes the directory cache of a file or directory by putting the parameters
       
   193 // into the shared memory chunk and sending the request.
       
   194 // (other items were commented in a header).
       
   195 // ----------------------------------------------------------------------------
       
   196 //
       
   197 EXPORT_C TInt RRsfwSession::FlushCache(
       
   198     TFid& aFid)
       
   199     {
       
   200     TRfeIoctlInArgs* ioctlArgs = new TRfeIoctlInArgs();
       
   201     if (!ioctlArgs) 
       
   202         {
       
   203         return KErrNoMemory;
       
   204         }
       
   205 
       
   206     ioctlArgs->iOpCode = EFsIoctl;
       
   207     ioctlArgs->iFid = aFid;
       
   208     ioctlArgs->iCmd = ERemoteFsIoctlRefresh;
       
   209     ioctlArgs->iLen = 0;
       
   210 
       
   211     TPckg<TRfeIoctlInArgs> pckgInArgs(*ioctlArgs);
       
   212     TInt result = SendRequest(EFsIoctl, aFid.iVolumeId, TIpcArgs(&pckgInArgs));
       
   213     delete ioctlArgs;
       
   214     return result;
       
   215     }
       
   216  
       
   217 // ----------------------------------------------------------------------------
       
   218 // RRsfwSession::SetHighCachePriority
       
   219 // Sets higher cache priority for a file by putting the parameters
       
   220 // into the shared memory chunk and sending the request.
       
   221 // This feature is not supported currently.
       
   222 // Perhaps will be implemented in near future.
       
   223 // (other items were commented in a header).
       
   224 // ----------------------------------------------------------------------------
       
   225 //
       
   226 EXPORT_C TInt RRsfwSession::SetHighCachePriority(TFid& /* aFid */)
       
   227     {
       
   228     return KErrNotSupported;
       
   229     } 
       
   230 
       
   231 // ----------------------------------------------------------------------------
       
   232 // RRsfwSession::GetAttributes
       
   233 // Sends GetAttr operation to Remote File Engine by putting the parameters
       
   234 // into the shared memory chunk, sending the request and reading the result. 
       
   235 // (other items were commented in a header).
       
   236 // ----------------------------------------------------------------------------
       
   237 //
       
   238 EXPORT_C TInt RRsfwSession::GetAttributes(
       
   239     TFid aFileFid, 
       
   240     TEntry& aAttributes)
       
   241     {
       
   242     TRfeGetAttrInArgs* getattrArgs = new TRfeGetAttrInArgs();
       
   243     if (!getattrArgs) 
       
   244         {
       
   245         return KErrNoMemory;
       
   246         }
       
   247     TRfeGetAttrOutArgs* getattrOutArgs = new TRfeGetAttrOutArgs();
       
   248     if (!getattrOutArgs) 
       
   249         {
       
   250         return KErrNoMemory;
       
   251         }   
       
   252         
       
   253     getattrArgs->iOpCode = EGetAttr;
       
   254     getattrArgs->iFid = aFileFid;
       
   255     
       
   256     TPckg<TRfeGetAttrInArgs> pckgInArgs(*getattrArgs);
       
   257     TPckg<TRfeGetAttrOutArgs> pckgOutArgs(*getattrOutArgs);
       
   258     
       
   259     TInt result = SendRequest(EGetAttr, aFileFid.iVolumeId, 
       
   260                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   261     
       
   262     if (result == KErrNone) 
       
   263         {
       
   264         // Note that aAttributes.iType (the entry UID)
       
   265         // should only be set for a file whose
       
   266         // size is greater than or equal to sizeof(TCheckedUid).
       
   267         aAttributes.iAtt = getattrOutArgs->iAttr.iAtt;
       
   268         aAttributes.iSize = getattrOutArgs->iAttr.iSize;
       
   269         aAttributes.iModified = getattrOutArgs->iAttr.iModified;
       
   270         aAttributes.iType = KNullUid;       
       
   271         }
       
   272 
       
   273     delete getattrArgs;
       
   274     delete getattrOutArgs;
       
   275     return result;
       
   276     }
       
   277     
       
   278 // ----------------------------------------------------------------------------
       
   279 // RRsfwSession::OpenByPathL
       
   280 // Sends OpenByPath operation to Remote File Engine by putting the parameters
       
   281 // into the shared memory chunk, sending the request and reading the result. 
       
   282 // Remote File Engine returns the path of the cache container file for this fid
       
   283 // (other items were commented in a header).
       
   284 // ----------------------------------------------------------------------------
       
   285 //
       
   286 EXPORT_C TInt RRsfwSession::OpenByPath(
       
   287     TFid aFid,
       
   288     TDes& aContainerPath,
       
   289     TDirEntAttr* aAttributes,
       
   290     TBool aTrueOpen)
       
   291     {
       
   292     TRfeOpenByPathInArgs* openbypathArgs = new TRfeOpenByPathInArgs();
       
   293     if (!openbypathArgs) 
       
   294         {
       
   295         return KErrNoMemory;
       
   296         }
       
   297     TRfeOpenByPathOutArgs* openbypathOutArgs = new TRfeOpenByPathOutArgs();
       
   298     if (!openbypathOutArgs) 
       
   299         {
       
   300         return KErrNoMemory;
       
   301         }
       
   302         
       
   303     openbypathArgs->iOpCode = EOpenByPath;
       
   304     openbypathArgs->iFid = aFid;
       
   305 
       
   306     // Flag field is used to pass attributes,
       
   307     // which indicate which lock should be obtained for the file
       
   308     if (aAttributes)
       
   309         {
       
   310         openbypathArgs->iFlags = aAttributes->iAtt;
       
   311         }
       
   312     else
       
   313         {
       
   314         openbypathArgs->iFlags = 0;
       
   315         }
       
   316 
       
   317     // tells whether the file is really opened or do we need it because 
       
   318     // ReadSection() was called... (in the latter case Symbian File Server 
       
   319     // does not open the file...) */
       
   320     openbypathArgs->iTrueOpen = aTrueOpen;
       
   321     
       
   322     TPckg<TRfeOpenByPathInArgs> pckgInArgs(*openbypathArgs);
       
   323     TPckg<TRfeOpenByPathOutArgs> pckgOutArgs(*openbypathOutArgs);
       
   324     
       
   325     TInt result = SendRequest(EOpenByPath, aFid.iVolumeId, TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   326     
       
   327     if (result == KErrNone) 
       
   328         {
       
   329         if (aAttributes)
       
   330             {
       
   331             *aAttributes = openbypathOutArgs->iAttr;
       
   332             }
       
   333         // Processing the response
       
   334         _LIT(KPathRedundancy, "\\.\\");
       
   335         TInt j = openbypathOutArgs->iPath.Find(KPathRedundancy);
       
   336         if (j != KErrNotFound)
       
   337             {
       
   338             TInt i = openbypathOutArgs->iPath.Length();
       
   339             TInt k;
       
   340             for (k = j; k + 2 < i; k++)
       
   341                 {
       
   342                 openbypathOutArgs->iPath[k] =
       
   343                     openbypathOutArgs->iPath[k + 2];
       
   344                 }
       
   345             openbypathOutArgs->iPath.SetLength(i - 2);
       
   346             }
       
   347     
       
   348         const TInt maximumLength = aContainerPath.MaxLength();
       
   349         if (maximumLength >= openbypathOutArgs->iPath.Length())
       
   350             {
       
   351             aContainerPath.Copy(openbypathOutArgs->iPath);
       
   352             }
       
   353         else
       
   354             {
       
   355             aContainerPath.Copy(
       
   356                 openbypathOutArgs->iPath.Left(maximumLength));
       
   357             }
       
   358         }
       
   359     delete openbypathArgs;
       
   360     delete openbypathOutArgs;
       
   361     return result;
       
   362     }
       
   363 
       
   364 // ----------------------------------------------------------------------------
       
   365 // RRsfwSession::RfeInit
       
   366 // Gets the fid of the root of the mount for Remote File Engine by putting the 
       
   367 // parameters into the shared memory chunk, sending the request and reading 
       
   368 // the result.  
       
   369 // This allows us to get other fids by lookup(). 
       
   370 // (other items were commented in a header).
       
   371 // ----------------------------------------------------------------------------
       
   372 //
       
   373 EXPORT_C TInt RRsfwSession::RfeInit(TFid& aRootFid)
       
   374     {
       
   375     TRfeRootInArgs* rootArgs = new TRfeRootInArgs();
       
   376     if (!rootArgs) 
       
   377         {
       
   378         return KErrNoMemory;
       
   379         }
       
   380     
       
   381     TRfeRootOutArgs* rootOutArgs = new TRfeRootOutArgs();
       
   382     if (!rootOutArgs) 
       
   383         {
       
   384         return KErrNoMemory;
       
   385         }
       
   386         
       
   387     rootArgs->iOpCode = EFsRoot;
       
   388     rootArgs->iFid.iVolumeId = 0;
       
   389     rootArgs->iFid.iNodeId = 0;
       
   390     
       
   391     TPckg<TRfeRootInArgs> pckgInArgs(*rootArgs);
       
   392     TPckg<TRfeRootOutArgs> pckgOutArgs(*rootOutArgs);
       
   393     
       
   394     TInt result = SendRequest(EFsRoot, aRootFid.iVolumeId, 
       
   395                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   396    
       
   397     if (result == KErrNone) 
       
   398         {
       
   399         aRootFid  = rootOutArgs->iFid;
       
   400         }
       
   401  
       
   402     delete rootArgs;
       
   403     delete rootOutArgs;
       
   404     return result;    
       
   405     }
       
   406     
       
   407 // ----------------------------------------------------------------------------
       
   408 // RRsfwSession::MakeDirectoryL
       
   409 // Sends MkDir operation to Remote File Engine by putting the parameters
       
   410 // into the shared memory chunk, sending the request and reading the result. 
       
   411 // (other items were commented in a header).
       
   412 // ----------------------------------------------------------------------------
       
   413 //
       
   414 EXPORT_C TInt RRsfwSession::MakeDirectory(
       
   415     TFid aParentFid, 
       
   416     const TDesC& aDirName)
       
   417     {
       
   418     TRfeMkdirInArgs* mkdirArgs = new TRfeMkdirInArgs();
       
   419     if (!mkdirArgs) 
       
   420         {
       
   421         return KErrNoMemory;
       
   422         }   
       
   423     
       
   424     mkdirArgs->iOpCode = EMkDir;
       
   425     mkdirArgs->iFid = aParentFid;
       
   426 
       
   427     mkdirArgs->iEntry.iName.Copy(aDirName);
       
   428     mkdirArgs->iEntry.iAttr.iAtt = 0; // not read only
       
   429     
       
   430     TPckg<TRfeMkdirInArgs> pckgInArgs(*mkdirArgs);
       
   431     TInt result = SendRequest(EMkDir,
       
   432                               aParentFid.iVolumeId,
       
   433                               TIpcArgs(&pckgInArgs));
       
   434     delete mkdirArgs;
       
   435     return result;
       
   436     }
       
   437 
       
   438 // ----------------------------------------------------------------------------
       
   439 // RRsfwSession::RemoveDirectoryL
       
   440 // Sends Remove Directory operation to Remote File Engine by putting the 
       
   441 // parameters into the shared memory chunk, sending the request and reading 
       
   442 // the result. 
       
   443 // (other items were commented in a header).
       
   444 // ----------------------------------------------------------------------------
       
   445 //
       
   446 EXPORT_C TInt RRsfwSession::RemoveDirectory(
       
   447     TFid aParentFid, 
       
   448     const TDesC& aDirName)
       
   449     {
       
   450     TRfeRmdirInArgs* rmdirArgs = new TRfeRmdirInArgs();
       
   451     if (!rmdirArgs) 
       
   452         {
       
   453         return KErrNoMemory;
       
   454         }
       
   455        
       
   456     rmdirArgs->iOpCode = ERemoveDir;
       
   457     rmdirArgs->iFid = aParentFid;
       
   458 
       
   459     rmdirArgs->iName.Copy(aDirName);
       
   460     
       
   461     TPckg<TRfeRmdirInArgs> pckgInArgs(*rmdirArgs);
       
   462     TInt result = SendRequest(ERemoveDir,
       
   463                               aParentFid.iVolumeId,
       
   464                               TIpcArgs(&pckgInArgs));
       
   465     
       
   466     delete rmdirArgs;
       
   467     return result;
       
   468     }
       
   469 
       
   470 // ----------------------------------------------------------------------------
       
   471 // RRsfwSession::CreateFileL
       
   472 // Sends Create File operation to Remote File Engine by putting the parameters
       
   473 // into the shared memory chunk, sending the request and reading the result.
       
   474 // (other items were commented in a header).
       
   475 // ----------------------------------------------------------------------------
       
   476 //
       
   477 EXPORT_C TInt RRsfwSession::CreateFile(
       
   478     TFid aParentFid,
       
   479     const TDesC& aFileName,
       
   480     TUint aMode,
       
   481     TUint aExcl,
       
   482     TFid& aNewFid)
       
   483     {
       
   484     TRfeCreateInArgs* createArgs = new TRfeCreateInArgs();
       
   485     if (!createArgs) 
       
   486         {
       
   487         return KErrNoMemory;
       
   488         }
       
   489     
       
   490     TRfeCreateOutArgs* createOutArgs = new TRfeCreateOutArgs();
       
   491     if (!createOutArgs) 
       
   492         {
       
   493         return KErrNoMemory;
       
   494         }
       
   495     
       
   496     createArgs->iOpCode = ECreateFile;
       
   497     createArgs->iFid = aParentFid;
       
   498 
       
   499     createArgs->iEntry.iName.Copy(aFileName);
       
   500 
       
   501     if (aMode & EFileWrite)
       
   502         {
       
   503         createArgs->iEntry.iAttr.iAtt = 0;
       
   504         }
       
   505     else
       
   506         {
       
   507         createArgs->iEntry.iAttr.iAtt = KEntryAttReadOnly;
       
   508         }
       
   509     createArgs->iEntry.iAttr.iSize = 0;
       
   510     createArgs->iEntry.iAttr.iModified = 0;
       
   511 
       
   512     createArgs->iExcl = aExcl;
       
   513 
       
   514     TPckg<TRfeCreateInArgs> pckgInArgs(*createArgs);
       
   515     TPckg<TRfeCreateOutArgs> pckgOutArgs(*createOutArgs);
       
   516     TInt result = SendRequest(ECreateFile,
       
   517                               aParentFid.iVolumeId,
       
   518                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   519     
       
   520     if (result == KErrNone) 
       
   521         {
       
   522         aNewFid = createOutArgs->iFid;
       
   523         }
       
   524         
       
   525     delete createArgs;
       
   526     delete createOutArgs;   
       
   527     return result;
       
   528     }
       
   529 
       
   530 // ----------------------------------------------------------------------------
       
   531 // RRsfwSession::RemoveFileL
       
   532 // Sends Remove File operation to Remote File Engine by putting the parameters
       
   533 // into the shared memory chunk, sending the request and reading the result.
       
   534 // (other items were commented in a header).
       
   535 // ----------------------------------------------------------------------------
       
   536 //
       
   537 EXPORT_C TInt RRsfwSession::RemoveFile(
       
   538     TFid aParentFid, 
       
   539     const TDesC& aFileName)
       
   540     {
       
   541     TRfeRemoveInArgs* removeArgs = new TRfeRemoveInArgs();
       
   542     if (!removeArgs) 
       
   543         {
       
   544         return KErrNoMemory;
       
   545         }
       
   546     
       
   547     removeArgs->iOpCode = ERemove;
       
   548     removeArgs->iFid = aParentFid;
       
   549 
       
   550     removeArgs->iName.Copy(aFileName);
       
   551 
       
   552     TPckg<TRfeRemoveInArgs> pckgInArgs(*removeArgs);
       
   553     TInt result = SendRequest(ERemove,
       
   554                               aParentFid.iVolumeId,
       
   555                               TIpcArgs(&pckgInArgs));
       
   556     
       
   557     delete removeArgs;
       
   558     return result;
       
   559     }
       
   560  
       
   561 // ----------------------------------------------------------------------------
       
   562 // RRsfwSession::LookupL
       
   563 // Sends Lookup operation to Remote File Engine by putting the parameters
       
   564 // into the shared memory chunk, sending the request and reading the result.
       
   565 // (other items were commented in a header).
       
   566 // ----------------------------------------------------------------------------
       
   567 //
       
   568 EXPORT_C TInt RRsfwSession::Lookup(
       
   569     TFid aParentFid, 
       
   570     const TDesC& aName, 
       
   571     TUint aNodeType,
       
   572     TFid& aFid)
       
   573     {
       
   574     TBool directory = EFalse;
       
   575 
       
   576     TPtrC peek;
       
   577     peek.Set(aName.Right(1)); // the last char
       
   578     if ((peek.Length() > 0) && (peek[0] == '\\'))
       
   579         {
       
   580         directory = ETrue;
       
   581         }
       
   582         
       
   583     TRfeLookupInArgs* lookupArgs = new TRfeLookupInArgs();
       
   584     if (!lookupArgs) 
       
   585         {
       
   586         return KErrNoMemory;
       
   587         }
       
   588     
       
   589     TRfeLookupOutArgs* lookupOutArgs = new TRfeLookupOutArgs();
       
   590     if (!lookupOutArgs) 
       
   591         {
       
   592         return KErrNoMemory;
       
   593         }   
       
   594         
       
   595     lookupArgs->iOpCode = ELookUp;
       
   596     lookupArgs->iFid = aParentFid;
       
   597     lookupArgs->iNodeType = aNodeType;
       
   598 
       
   599     lookupArgs->iName.Copy(aName);
       
   600 
       
   601     if (directory) 
       
   602         {
       
   603         // We don't want to copy the trailing backslash
       
   604         TInt len = lookupArgs->iName.Length();
       
   605         lookupArgs->iName.SetLength(len - 1);
       
   606         }
       
   607         
       
   608     TPckg<TRfeLookupInArgs> pckgInArgs(*lookupArgs);
       
   609     TPckg<TRfeLookupOutArgs> pckgOutArgs(*lookupOutArgs);
       
   610     TInt result = SendRequest(ELookUp,
       
   611                               aParentFid.iVolumeId,
       
   612                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   613     
       
   614     if (result == KErrNone) 
       
   615         {
       
   616         aFid = lookupOutArgs->iFid;
       
   617         }
       
   618 
       
   619     if (result == KErrNotFound)
       
   620         {
       
   621         if (directory)
       
   622             {
       
   623             return KErrPathNotFound;
       
   624             }
       
   625         else
       
   626             {
       
   627             return KErrNotFound;
       
   628             }
       
   629         }
       
   630         
       
   631     delete lookupArgs;
       
   632     delete lookupOutArgs;
       
   633     return result;
       
   634     }
       
   635 
       
   636 // ----------------------------------------------------------------------------
       
   637 // RRsfwSession::CloseFile
       
   638 // Sends Close operation to Remote File Engine by putting the parameters
       
   639 // into the shared memory chunk, sending the request and reading the result.
       
   640 // (other items were commented in a header).
       
   641 // ----------------------------------------------------------------------------
       
   642 //
       
   643 EXPORT_C void RRsfwSession::CloseFile(
       
   644     const TFid aFid, 
       
   645     const TUint aFlags)
       
   646     {
       
   647     // close cannot be called asynchronously in the file server API
       
   648     // and cannot return an error code
       
   649     // so we make a blind request to the server 
       
   650     SendReceive(EClose, TIpcArgs(aFid.iVolumeId, aFid.iNodeId, aFlags));
       
   651     }
       
   652 
       
   653 // ----------------------------------------------------------------------------
       
   654 // RRsfwSession::FlushL
       
   655 // This "abuses" close operation code.
       
   656 // The file is not really closed by the File Server,
       
   657 // and Remote File Engine only writes the (changed) file back to the 
       
   658 // remote server, but does not release a possible write lock.
       
   659 // (other items were commented in a header).
       
   660 // ----------------------------------------------------------------------------
       
   661 //
       
   662 EXPORT_C TInt RRsfwSession::Flush(
       
   663     const TFid aFid,
       
   664     TInt aFirstByte,
       
   665     TInt aDataLength,
       
   666     TInt aTotalSize)
       
   667     {
       
   668     TRfeFlushInArgs* flushArgs = new TRfeFlushInArgs();
       
   669     if (!flushArgs) 
       
   670         {
       
   671         return KErrNoMemory;
       
   672         }
       
   673     
       
   674     flushArgs->iOpCode = EFlush;
       
   675     flushArgs->iFid = aFid;
       
   676     flushArgs->iFirstByte = aFirstByte;
       
   677     flushArgs->iDataLength = aDataLength;
       
   678     flushArgs->iTotalSize = aTotalSize; 
       
   679             
       
   680     TPckg<TRfeFlushInArgs> pckgInArgs(*flushArgs);
       
   681     
       
   682     TInt result = SendRequest(EFlush, aFid.iVolumeId, 
       
   683                     TIpcArgs(&pckgInArgs));
       
   684 
       
   685     delete flushArgs;
       
   686     return result;
       
   687     }
       
   688 
       
   689 // ----------------------------------------------------------------------------
       
   690 // RRsfwSession::Fetch
       
   691 // Sends Fetch operation to Remote File Engine by putting the parameters
       
   692 // into the shared memory chunk, sending the request and reading the result.
       
   693 // Remote File Engine will write data into cache file, which path it knows by
       
   694 // the fid.
       
   695 // (other items were commented in a header).
       
   696 // ----------------------------------------------------------------------------
       
   697 //
       
   698 EXPORT_C TInt RRsfwSession::Fetch(
       
   699     TFid aFileFid, 
       
   700     TInt aFirstByte, 
       
   701     TInt aLastByte,
       
   702     TInt& aCachedBytes)
       
   703     {
       
   704     TRfeFetchInArgs* fetchArgs = new TRfeFetchInArgs();
       
   705     if (!fetchArgs) 
       
   706         {
       
   707         return KErrNoMemory;
       
   708         }
       
   709     TRfeFetchOutArgs* fetchOutArgs = new TRfeFetchOutArgs();
       
   710     if (!fetchOutArgs) 
       
   711         {
       
   712         return KErrNoMemory;
       
   713         }
       
   714     
       
   715     fetchArgs->iOpCode = EFetch;
       
   716     fetchArgs->iFid = aFileFid;
       
   717     fetchArgs->iFirstByte = aFirstByte;
       
   718     fetchArgs->iLastByte = aLastByte;
       
   719     
       
   720     TPckg<TRfeFetchInArgs> pckgInArgs(*fetchArgs);
       
   721     TPckg<TRfeFetchOutArgs> pckgOutArgs(*fetchOutArgs);
       
   722     TInt result = SendRequest(EFetch, aFileFid.iVolumeId, 
       
   723                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   724 
       
   725     if (result == KErrNone)
       
   726         {
       
   727         aCachedBytes = fetchOutArgs->iLastByte;
       
   728         }
       
   729    
       
   730     delete fetchArgs;
       
   731     delete fetchOutArgs;
       
   732     return result;
       
   733     } 
       
   734  
       
   735 // ----------------------------------------------------------------------------
       
   736 // RRsfwSession::FetchData
       
   737 // Sends Fetch operation to Remote File Engine by putting the parameters
       
   738 // into the shared memory chunk, sending the request and reading the result.
       
   739 // Remote File Engine will write data into a temporary cache file,
       
   740 // valid only for the duration of this request.
       
   741 // Note that in this case Remote File Engine will read exactly the requested
       
   742 // amount, so aLastByte is not reset.
       
   743 // (other items were commented in a header).
       
   744 // ----------------------------------------------------------------------------
       
   745 //
       
   746 EXPORT_C TInt RRsfwSession::FetchData(
       
   747     TFid aFileFid, 
       
   748     TInt aFirstByte, 
       
   749     TInt aLastByte,
       
   750     TDes& aTempFileName,
       
   751     TBool& aUseTempPath)
       
   752     {
       
   753     TRfeFetchDataInArgs* fetchDataArgs = new TRfeFetchDataInArgs();
       
   754     if (!fetchDataArgs) 
       
   755         {
       
   756         return KErrNoMemory;
       
   757         }
       
   758     
       
   759     TRfeFetchDataOutArgs* fetchDataOutArgs = new TRfeFetchDataOutArgs();
       
   760     if (!fetchDataOutArgs) 
       
   761         {
       
   762         return KErrNoMemory;
       
   763         }
       
   764 
       
   765     fetchDataArgs->iOpCode = EFetchData;
       
   766     fetchDataArgs->iFid = aFileFid;
       
   767     fetchDataArgs->iFirstByte = aFirstByte;
       
   768     fetchDataArgs->iLastByte = aLastByte;
       
   769     
       
   770     TPckg<TRfeFetchDataInArgs> pckgInArgs(*fetchDataArgs);
       
   771     TPckg<TRfeFetchDataOutArgs> pckgOutArgs(*fetchDataOutArgs);
       
   772     TInt result = SendRequest(EFetchData, aFileFid.iVolumeId, 
       
   773                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   774     
       
   775     if (result == KErrNone) 
       
   776         {
       
   777         // Processing the response
       
   778         _LIT(KPathRedundancy, "\\.\\");
       
   779         TInt j = fetchDataOutArgs->iTempPath.Find(KPathRedundancy);
       
   780         if (j != KErrNotFound)
       
   781             {
       
   782             TInt i = fetchDataOutArgs->iTempPath.Length();
       
   783             TInt k;
       
   784             for (k = j; k + 2 < i; k++)
       
   785                 {
       
   786                 fetchDataOutArgs->iTempPath[k] =
       
   787                     fetchDataOutArgs->iTempPath[k + 2];
       
   788                 }
       
   789             fetchDataOutArgs->iTempPath.SetLength(i - 2);
       
   790             }
       
   791     
       
   792         const TInt maximumLength = aTempFileName.MaxLength();
       
   793         if (maximumLength >= fetchDataOutArgs->iTempPath.Length())
       
   794             {
       
   795             aTempFileName.Copy(fetchDataOutArgs->iTempPath);
       
   796             }
       
   797         else
       
   798             {
       
   799             aTempFileName.Copy(
       
   800                 fetchDataOutArgs->iTempPath.Left(maximumLength));
       
   801             }
       
   802         aUseTempPath = fetchDataOutArgs->iUseTempPath;
       
   803         }
       
   804     delete fetchDataArgs;
       
   805     delete fetchDataOutArgs;
       
   806     return result;
       
   807     } 
       
   808  
       
   809 // ----------------------------------------------------------------------------
       
   810 // RRsfwSession::OkToWriteL
       
   811 // (other items were commented in a header).
       
   812 // ----------------------------------------------------------------------------
       
   813 //
       
   814 EXPORT_C TInt RRsfwSession::OkToWrite(
       
   815     TFid aFid, 
       
   816     TUint aBytes,
       
   817     TBool& aOkToWrite)
       
   818     {
       
   819     TRfeWriteDataInArgs* writedataArgs = new TRfeWriteDataInArgs();
       
   820     if (!writedataArgs) 
       
   821         {
       
   822         return KErrNoMemory;
       
   823         }
       
   824     
       
   825     TRfeWriteDataOutArgs* writedataOutArgs = new TRfeWriteDataOutArgs();
       
   826     if (!writedataOutArgs) 
       
   827         {
       
   828         return KErrNoMemory;
       
   829         }   
       
   830         
       
   831     writedataArgs->iOpCode = EOkToWrite;
       
   832     writedataArgs->iFid = aFid;
       
   833     writedataArgs->iBytes = aBytes;
       
   834     
       
   835     TPckg<TRfeWriteDataInArgs> pckgInArgs(*writedataArgs);
       
   836     TPckg<TRfeWriteDataOutArgs> pckgOutArgs(*writedataOutArgs);
       
   837     TInt result = SendRequest(EOkToWrite, aFid.iVolumeId, 
       
   838                               TIpcArgs(&pckgInArgs, &pckgOutArgs));
       
   839     
       
   840     if (result == KErrNone) 
       
   841         {
       
   842         aOkToWrite = writedataOutArgs->iOkToWrite;
       
   843         }
       
   844     
       
   845     delete writedataArgs;
       
   846     delete writedataOutArgs;
       
   847     return result;
       
   848     }
       
   849 
       
   850 // ----------------------------------------------------------------------------
       
   851 // RRsfwSession::StartServer
       
   852 // Starts the Remote File Engine if it is not running, uses semaphore to 
       
   853 // synchronize startup.
       
   854 // (other items were commented in a header).
       
   855 // ----------------------------------------------------------------------------
       
   856 //
       
   857 TInt RRsfwSession::StartServer(const TDesC& aServerName)
       
   858     {
       
   859     TFindServer findRfe(aServerName);
       
   860     TFullName name;
       
   861 
       
   862     TInt result = findRfe.Next(name);
       
   863     if (result == KErrNone)
       
   864         {
       
   865         // Server already running
       
   866         return KErrNone;
       
   867         }
       
   868 
       
   869     RSemaphore semaphore;       
       
   870     result = semaphore.CreateGlobal(KRfeSemaphoreName, 0);
       
   871     if (result != KErrNone)
       
   872         {
       
   873         return  result;
       
   874         }
       
   875 
       
   876     result = CreateServerProcess(aServerName);
       
   877     if (result != KErrNone)
       
   878         {
       
   879         semaphore.Close();  
       
   880         return  result;
       
   881         }
       
   882     semaphore.Wait();
       
   883     semaphore.Close();       
       
   884 
       
   885     return  KErrNone;
       
   886     }
       
   887 
       
   888 // ----------------------------------------------------------------------------
       
   889 // RRsfwSession::CreateServerProcess
       
   890 // Starts the Remote File Engine using name to find the binary
       
   891 // (other items were commented in a header).
       
   892 // ----------------------------------------------------------------------------
       
   893 //
       
   894 TInt RRsfwSession::CreateServerProcess(const TDesC& aServerName)
       
   895     {
       
   896     // Just load anything that matches with the name
       
   897     const TUidType serverUid(KNullUid, KNullUid, KNullUid);
       
   898 
       
   899     RProcess server;
       
   900 
       
   901     _LIT(KStartCommand, "");
       
   902     TInt result = server.Create(aServerName, KStartCommand, serverUid);
       
   903     if (result != KErrNone)
       
   904         {
       
   905         return  result;
       
   906         }
       
   907     server.Resume();
       
   908     server.Close();
       
   909 
       
   910     return  KErrNone;
       
   911     }
       
   912 
       
   913 // ----------------------------------------------------------------------------
       
   914 // RRsfwSession::SendRequest
       
   915 // ----------------------------------------------------------------------------
       
   916 //    
       
   917 TInt RRsfwSession::SendRequest(TInt aOpCode, TInt aDrive, TIpcArgs aArgs) 
       
   918     {
       
   919     TInt result = SendReceive(aOpCode, aArgs);
       
   920     if (result == KErrServerTerminated)
       
   921         {
       
   922         // try to restart the server
       
   923         result = Connect();
       
   924         if (result == KErrNone) 
       
   925             {
       
   926             result = SendReceive(aOpCode, aArgs);
       
   927             }   
       
   928         }
       
   929 
       
   930     // Disable following codes will fix auto connection of rsfw
       
   931     /*
       
   932     if (result == KErrNotReady) 
       
   933         {
       
   934         // try to restore the mount
       
   935         result = SendReceive(EMountByDriveLetter, TIpcArgs(aDrive));
       
   936         if (result == KErrNone) 
       
   937             {
       
   938             result = SendReceive(aOpCode, aArgs);
       
   939             }   
       
   940         }
       
   941     */
       
   942     
       
   943     return result;
       
   944     }
       
   945 
       
   946 //  End of File