remotestoragefw/remotefileengine/src/rsfwopenbypathstatemachine.cpp
branchRCL_3
changeset 19 88ee4cf65e19
parent 16 87c71b25c937
child 20 1aa8c82cb4cb
equal deleted inserted replaced
16:87c71b25c937 19:88ee4cf65e19
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  State machine for opening a file or directory
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "rsfwopenbypathstatemachine.h"
       
    20 #include "rsfwfileentry.h"
       
    21 #include "rsfwfiletable.h"
       
    22 #include "rsfwvolumetable.h"
       
    23 #include "rsfwvolume.h"
       
    24 #include "rsfwinterface.h"
       
    25 #include "rsfwfileengine.h"
       
    26 #include "rsfwlockmanager.h"
       
    27 #include "mdebug.h"
       
    28 
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // CRsfwOpenByPathStateMachine::CRsfwOpenByPathStateMachine
       
    32 // ----------------------------------------------------------------------------
       
    33 //
       
    34 CRsfwOpenByPathStateMachine::CRsfwOpenByPathStateMachine()
       
    35     {
       
    36     }
       
    37 
       
    38 // ----------------------------------------------------------------------------
       
    39 // CRsfwOpenByPathStateMachine::~CRsfwOpenByPathStateMachine
       
    40 // ----------------------------------------------------------------------------
       
    41 //
       
    42 CRsfwOpenByPathStateMachine::~CRsfwOpenByPathStateMachine()
       
    43     {
       
    44     delete iLockToken;
       
    45     }
       
    46 
       
    47 // ----------------------------------------------------------------------------
       
    48 // CRsfwOpenByPathStateMachine::CompleteRequestL
       
    49 // ----------------------------------------------------------------------------
       
    50 //
       
    51 CRsfwRfeStateMachine::TState*
       
    52 CRsfwOpenByPathStateMachine::CompleteRequestL(TInt aError)
       
    53     {
       
    54     DEBUGSTRING(("CRsfwOpenByPathStateMachine::CompleteRequestL %d", aError));
       
    55     TRfeOpenByPathOutArgs* outArgs =
       
    56         static_cast<TRfeOpenByPathOutArgs*>(iOutArgs);
       
    57     if(!aError)
       
    58         {
       
    59         if ((Node()->Type() == KNodeTypeFile) &&
       
    60             iRealOpen) 
       
    61             {
       
    62             // file opened successfully and was not already opened in create
       
    63             Node()->iFileTable->UpdateOpenFileCount(1);
       
    64             }
       
    65   
       
    66         outArgs->iPath.Copy(*iCacheName);
       
    67         iAttrp->iAtt = Node()->Att();
       
    68         iAttrp->iSize = Node()->Size();
       
    69         iAttrp->iModified = Node()->Modified();
       
    70         }
       
    71 
       
    72     CompleteAndDestroyState()->SetErrorCode(aError);
       
    73     return CompleteAndDestroyState();
       
    74     }
       
    75 
       
    76 // ----------------------------------------------------------------------------
       
    77 // CRsfwOpenByPathStateMachine::TRequestOpenModeState::TRequestOpenModeState
       
    78 // ----------------------------------------------------------------------------
       
    79 //
       
    80 CRsfwOpenByPathStateMachine::TRequestOpenModeState::TRequestOpenModeState(
       
    81     CRsfwOpenByPathStateMachine* aParent)
       
    82     : iOperation(aParent)
       
    83     {
       
    84     iRequestedLock = EFalse;
       
    85     }
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 // CRsfwOpenByPathStateMachine::TRequestOpenModeState::EnterL
       
    89 // ----------------------------------------------------------------------------
       
    90 //
       
    91 void CRsfwOpenByPathStateMachine::TRequestOpenModeState::EnterL()
       
    92     {
       
    93     
       
    94     DEBUGSTRING(("CRsfwOpenByPathStateMachine::TRequestOpenModeState::EnterL"));
       
    95     
       
    96     TRfeOpenByPathInArgs* inArgs =
       
    97         static_cast<TRfeOpenByPathInArgs*>(iOperation->iInArgs);
       
    98     TRfeOpenByPathOutArgs* outArgs =
       
    99         static_cast<TRfeOpenByPathOutArgs*>(iOperation->iOutArgs);
       
   100     iOperation->iRealOpen = inArgs->iTrueOpen;
       
   101     // Use inArgs->iFlags to pass desired locking information
       
   102     iOperation->iFlags = inArgs->iFlags;
       
   103     iOperation->iAttrp = &(outArgs->iAttr);
       
   104 
       
   105     if (!iOperation->Node())
       
   106         {
       
   107         User::Leave(KErrNotFound);
       
   108         }
       
   109 
       
   110     if (iOperation->Node()->Size() > iOperation->Volumes()->iMaxCacheSize)
       
   111         {
       
   112         DEBUGSTRING(("OPENBYPATH failed: file too large: file size %d, max cache size %d",
       
   113                      iOperation->Node()->Size(),
       
   114                      iOperation->Volumes()->iMaxCacheSize));
       
   115         User::Leave(KErrTooBig);
       
   116         }
       
   117 
       
   118     DEBUGSTRING(("opening fid %d with flags 0x%x",
       
   119                  iOperation->Node()->Fid().iNodeId,
       
   120                  iOperation->iFlags));
       
   121 
       
   122     if (!iOperation->FileEngine()->WriteDisconnected())
       
   123         {
       
   124         if (!(iOperation->Node()->IsLocked()) &&
       
   125             (iOperation->Node()->Type() != KNodeTypeDir) &&
       
   126             (iOperation->iFlags & EFileWrite))
       
   127             {
       
   128             DEBUGSTRING(("requesting lock"));
       
   129             // For the time being
       
   130             // if flag is 0 we are not interested in getting a lock
       
   131             iOperation->FileEngine()->LockManager()->ObtainLockL(
       
   132                 iOperation->Node(),
       
   133                 iOperation->iFlags,
       
   134                 iOperation->iLockToken,
       
   135                 iOperation);
       
   136             iRequestedLock = ETrue;
       
   137             }
       
   138         else
       
   139             {
       
   140             // IsLocked() || !(iOperation->iFlags & EFileWrite)
       
   141             iOperation->HandleRemoteAccessResponse(0, KErrNone);
       
   142             }
       
   143         }
       
   144     else
       
   145         {
       
   146         // if WriteDisconnected()
       
   147         iOperation->HandleRemoteAccessResponse(0, KErrNone);
       
   148         }
       
   149     }
       
   150 
       
   151 // ----------------------------------------------------------------------------
       
   152 // CRsfwOpenByPathStateMachine::TRequestOpenModeState::CompleteL
       
   153 // ----------------------------------------------------------------------------
       
   154 //
       
   155 CRsfwOpenByPathStateMachine::TState*
       
   156 CRsfwOpenByPathStateMachine::TRequestOpenModeState::CompleteL()
       
   157     {
       
   158      DEBUGSTRING(("CRsfwOpenByPathStateMachine::TRequestOpenModeState::CompleteL"));
       
   159     if (iRequestedLock)
       
   160         {
       
   161         //from CRsfwLockManager::ObtainLock()
       
   162 #ifdef _DEBUG
       
   163         TPtrC p = iOperation->Node()->FullNameLC()->Des();
       
   164         DEBUGSTRING16(("ObtainLockL(): flags %d returned %d for file '%S'",
       
   165                        iOperation->iFlags,
       
   166                        KErrNone,
       
   167                        &p));
       
   168         CleanupStack::PopAndDestroy();
       
   169 #endif // DEBUG
       
   170         iOperation->Node()->SetLockedL(
       
   171             iOperation->FileEngine()->LockManager(),
       
   172             iOperation->iLockToken);
       
   173         iOperation->iLockToken = NULL;
       
   174         }
       
   175 
       
   176     iOperation->iCacheName = iOperation->Node()->CacheFileName();
       
   177 
       
   178     // make sure that the relevant cache file is on place
       
   179     // (i.e. it has not been removed e.g. by other apps)
       
   180     iOperation->Node()->ValidateCacheFile();
       
   181 
       
   182     if (!iOperation->Node()->IsCached())
       
   183         {
       
   184         if (iOperation->FileEngine()->Disconnected())
       
   185             {
       
   186             if ((iOperation->Node()->Type() != KNodeTypeDir) &&
       
   187                 !(iOperation->iFlags & EFileWrite))
       
   188                 {
       
   189                 // While disconnected,
       
   190                 // write access of files returns "not found"
       
   191                 return iOperation->CompleteRequestL(KErrNotFound);
       
   192                 }
       
   193             }
       
   194         iOperation->FileEngine()->CreateContainerFileL(*(iOperation->Node()));
       
   195         iOperation->iCacheName = iOperation->Node()->CacheFileName();
       
   196         }
       
   197     else // is cached
       
   198         {
       
   199         if (iOperation->Node()->IsLocallyDirty())
       
   200             {
       
   201             // This is a directory which has at least one kid
       
   202             // that has been cached or flushed since the last opening
       
   203             // of the directory
       
   204             iOperation->FileEngine()->UpdateDirectoryContainerL(
       
   205                 *(iOperation->Node()));
       
   206             }
       
   207 
       
   208         // If this is a cached file,
       
   209         // we must remove it from LRUlist as it is now open
       
   210         if (iOperation->iRealOpen)
       
   211             {
       
   212             iOperation->Node()->iFileTable->Volume()->iVolumeTable->
       
   213                 RemoveFromLRUPriorityList(iOperation->Node());
       
   214             }
       
   215         }
       
   216 
       
   217     // if file or dir has just been opened, remove it from metadata LRU list
       
   218     iOperation->Node()->iFileTable->Volume()->iVolumeTable->
       
   219         RemoveFromMetadataLRUPriorityList(iOperation->Node());
       
   220 
       
   221     return iOperation->CompleteRequestL(KErrNone);
       
   222     }
       
   223 
       
   224 // ----------------------------------------------------------------------------
       
   225 // CRsfwOpenByPathStateMachine::TRequestOpenModeState::ErrorL
       
   226 // fileEngine->RequestConnectionStateL() has returned with error
       
   227 // ----------------------------------------------------------------------------
       
   228 //
       
   229 CRsfwOpenByPathStateMachine::TState*
       
   230 CRsfwOpenByPathStateMachine::TRequestOpenModeState::ErrorL(TInt aCode)
       
   231     {
       
   232     DEBUGSTRING(("CRsfwOpenByPathStateMachine::TRequestOpenModeState::ErrorL %d", aCode));
       
   233     // from CRsfwFileEngine::DoOpenByPath()
       
   234     if (aCode == KErrNotSupported)
       
   235         {
       
   236         iRequestedLock = EFalse;
       
   237         // locks not supported is not an error state
       
   238         // instead we run the success state...
       
   239         return CompleteL();
       
   240         }
       
   241 
       
   242     //from CRsfwLockManager::ObtainLock()
       
   243 #ifdef _DEBUG
       
   244     TPtrC p = iOperation->Node()->FullNameLC()->Des();
       
   245     DEBUGSTRING16(("ObtainLockL(): flags %d returned %d for file '%S'",
       
   246                    iOperation->iFlags,
       
   247                    aCode,
       
   248                    &p));
       
   249     CleanupStack::PopAndDestroy();
       
   250 #endif
       
   251 
       
   252     return iOperation->CompleteRequestL(aCode);
       
   253     }
       
   254