remotestoragefw/remotefileengine/src/rsfwlookupstatemachine.cpp
changeset 13 6b4fc789785b
parent 2 c32dc0be5eb4
equal deleted inserted replaced
2:c32dc0be5eb4 13:6b4fc789785b
     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 file lookup
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "rsfwlookupstatemachine.h"
       
    20 #include "rsfwfileentry.h"
       
    21 #include "rsfwfiletable.h"
       
    22 #include "rsfwinterface.h"
       
    23 #include "rsfwfileengine.h"
       
    24 #include "mdebug.h"
       
    25 #include "rsfwdirentattr.h"
       
    26 #include "rsfwvolumetable.h"
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // CRsfwLookupStateMachine::CRsfwLookupStateMachine
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 CRsfwLookupStateMachine::CRsfwLookupStateMachine()
       
    33     {
       
    34     iKidCreated = ETrue;
       
    35     }
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // CRsfwLookupStateMachine::~CRsfwLookupStateMachine
       
    39 // ----------------------------------------------------------------------------
       
    40 //
       
    41 CRsfwLookupStateMachine::~CRsfwLookupStateMachine()
       
    42     {
       
    43     delete iDirEntAttr;
       
    44     if (iPath)
       
    45     	{
       
    46     	delete iPath;
       
    47     	}
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // CRsfwLookupStateMachine::CompleteRequestL
       
    52 // ----------------------------------------------------------------------------
       
    53 //
       
    54 CRsfwRfeStateMachine::TState* CRsfwLookupStateMachine::CompleteRequestL(TInt aError)
       
    55     {
       
    56     TRfeLookupOutArgs* outArgs = static_cast<TRfeLookupOutArgs*>(iOutArgs);
       
    57     if (aError == KUpdateNotRequired)
       
    58         {   // discard
       
    59         aError = KErrNone;
       
    60         }
       
    61 
       
    62     if (!aError)
       
    63         {
       
    64         outArgs->iFid = iKidFep->Fid();
       
    65         }
       
    66     CompleteAndDestroyState()->SetErrorCode(aError);
       
    67     return CompleteAndDestroyState();
       
    68     }
       
    69 
       
    70 // ----------------------------------------------------------------------------
       
    71 // CRsfwLookupStateMachine::CompleteL
       
    72 // ----------------------------------------------------------------------------
       
    73 //
       
    74 CRsfwLookupStateMachine::TState* CRsfwLookupStateMachine::CompleteL()
       
    75     {
       
    76     // from CRsfwFileEngine::UpdateFileAttributes/UpdateDirAttributes
       
    77 #ifdef _DEBUG
       
    78     if (iDirEntAttr)
       
    79         {
       
    80         DEBUGSTRING(("Kid attributes: attr=0x%x, size=%d, time=",
       
    81                      iDirEntAttr->Att(),
       
    82                      iDirEntAttr->Size()));
       
    83         DEBUGTIME((iDirEntAttr->Modified()));
       
    84         }
       
    85 #endif
       
    86 
       
    87     if (iKidCreated)
       
    88         {
       
    89         /* from CRsfwFileEngine::DoLookupL */
       
    90         // Create a file entry for this kid
       
    91         if (!Volumes()->EnsureMetadataCanBeAddedL(Node()))
       
    92             {
       
    93             User::Leave(KErrNoMemory);
       
    94             }
       
    95         iKidFep = CRsfwFileEntry::NewL(iKidName, Node());
       
    96         if (iDirEntAttr->Att() & KEntryAttDir)
       
    97             {
       
    98             iKidFep->SetType(KNodeTypeDir);
       
    99             }
       
   100         else
       
   101             {
       
   102             iKidFep->SetType(KNodeTypeFile);
       
   103             }
       
   104 
       
   105         iKidFep->SetAttributesL(*iDirEntAttr, ETrue);
       
   106 
       
   107         // Remember that we have this kid
       
   108         FileEngine()->iFileTable->AddL(iKidFep);
       
   109         Node()->AddKid(*iKidFep);
       
   110         // If we really find a new kid that we were not aware of
       
   111         // we must set the parent as locally dirty
       
   112         // (this should not happen too often)
       
   113         Node()->SetLocallyDirty();
       
   114         }
       
   115 
       
   116     // We now have a valid kid entry
       
   117     return CompleteRequestL(KErrNone);
       
   118     }
       
   119 
       
   120 // ----------------------------------------------------------------------------
       
   121 // CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::TUpdateKidAttributesTryFirstTypeState
       
   122 // ----------------------------------------------------------------------------
       
   123 //
       
   124 CRsfwLookupStateMachine::
       
   125 TUpdateKidAttributesTryFirstTypeState::
       
   126 TUpdateKidAttributesTryFirstTypeState(CRsfwLookupStateMachine* aParent)
       
   127     : iOperation(aParent)
       
   128     {
       
   129     }
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::EnterL
       
   133 // ----------------------------------------------------------------------------
       
   134 //
       
   135 void CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::EnterL()
       
   136     {
       
   137     DEBUGSTRING(("CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::EnterL"));
       
   138     TRfeLookupInArgs* inArgs =
       
   139         static_cast<TRfeLookupInArgs*>(iOperation->iInArgs);
       
   140     TRfeLookupOutArgs* outArgs =
       
   141         static_cast<TRfeLookupOutArgs*>(iOperation->iOutArgs);
       
   142     iOperation->iNodeType = inArgs->iNodeType;
       
   143     iOperation->iKidName.Set(inArgs->iName);
       
   144 
       
   145     TInt err = KErrNone;
       
   146     iOperation->iKidFep = NULL;
       
   147 
       
   148     if (!iOperation->Node())
       
   149         {
       
   150         User::Leave(KErrNotFound);
       
   151         }
       
   152 
       
   153     DEBUGSTRING16(("looking up '%S' in fid=%d",
       
   154                    &(iOperation->iKidName),
       
   155                    iOperation->Node()->Fid().iNodeId));
       
   156 
       
   157     // We'd better be looking up in a directory
       
   158     if (iOperation->Node()->Type() != KNodeTypeDir)
       
   159         {
       
   160         User::Leave(KErrNotFound);
       
   161         }
       
   162 
       
   163     // Try to get the entry from the parent directory
       
   164     iOperation->iKidFep =
       
   165         iOperation->Node()->FindKidByName(iOperation->iKidName);
       
   166     if (!iOperation->iKidFep)
       
   167         {
       
   168         DEBUGSTRING(("no such kid!"));
       
   169         // Didn't find it
       
   170         // if the parent directory's cache entry is still valid
       
   171         // we return "not found"
       
   172         if ((iOperation->FileEngine()->UseCachedAttributes(*iOperation->Node())) &&
       
   173          	(iOperation->FileEngine()->UseCachedData(*iOperation->Node())))
       
   174         	{
       
   175 			User::Leave(KErrNotFound);
       
   176         	}
       
   177 
       
   178         iOperation->iPath =
       
   179             iOperation->FileEngine()->FullNameL(*iOperation->Node());
       
   180         if (iOperation->iNodeType == KNodeTypeUnknown)
       
   181             {
       
   182             iOperation->FileEngine()->
       
   183                 UpdateAttributesL(*iOperation->iPath,
       
   184                                   iOperation->iKidName,
       
   185                                   iOperation->iDirEntAttr,
       
   186                                   KNodeTypeFile,
       
   187                                   iOperation);
       
   188             }
       
   189         else
       
   190             {
       
   191             iOperation->FileEngine()->
       
   192                 UpdateAttributesL(*iOperation->iPath,
       
   193                                   iOperation->iKidName,
       
   194                                   iOperation->iDirEntAttr,
       
   195                                   iOperation->iNodeType,
       
   196                                   iOperation);
       
   197             }
       
   198 
       
   199         if (err)
       
   200             {
       
   201             // communication to the server failed,
       
   202             //e.g. we are in disconnected mode
       
   203             User::Leave(err);
       
   204             }
       
   205         }
       
   206     else
       
   207         {
       
   208         // We now have a valid kid entry
       
   209         outArgs->iFid = iOperation->iKidFep->Fid();
       
   210         iOperation->HandleRemoteAccessResponse(0, KUpdateNotRequired);
       
   211         }
       
   212     }
       
   213 
       
   214 // ----------------------------------------------------------------------------
       
   215 // CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::CompleteL
       
   216 // ----------------------------------------------------------------------------
       
   217 //
       
   218 CRsfwLookupStateMachine::TState*
       
   219 CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::CompleteL()
       
   220     {
       
   221     DEBUGSTRING(("CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::CompleteL"));
       
   222     return iOperation->CompleteL();
       
   223     }
       
   224 
       
   225 // ----------------------------------------------------------------------------
       
   226 // CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::ErrorL
       
   227 // If we were looking for a file return KErrNotFound
       
   228 // if for directory KErrPathNotFound.
       
   229 // File Server seems to always call Entry() (-->lookup()) before other file
       
   230 // operations so as long as we return the right variant of "not found"
       
   231 // error here other places do not matter.
       
   232 // ----------------------------------------------------------------------------
       
   233 //
       
   234 CRsfwLookupStateMachine::TState*
       
   235 CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::ErrorL(TInt aCode)
       
   236     {
       
   237     DEBUGSTRING(("CRsfwLookupStateMachine::TUpdateKidAttributesTryFirstTypeState::ErrorL %d", aCode));
       
   238     if (aCode == KUpdateNotRequired)
       
   239         {
       
   240         iOperation->iKidCreated = EFalse;
       
   241         return iOperation->CompleteL();
       
   242         }
       
   243     else if (iOperation->iNodeType == KNodeTypeUnknown)
       
   244         {
       
   245         return new CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState(
       
   246             iOperation);
       
   247         }
       
   248     else if ((aCode != KErrNotFound) && (aCode != KErrPathNotFound))
       
   249    	 	{
       
   250     	return iOperation->CompleteRequestL(aCode);
       
   251     	}
       
   252     else if (iOperation->iNodeType == KNodeTypeDir)
       
   253         {
       
   254         return iOperation->CompleteRequestL(KErrPathNotFound);
       
   255         }
       
   256     else
       
   257         {
       
   258         return iOperation->CompleteRequestL(KErrNotFound);
       
   259         }
       
   260     }
       
   261 
       
   262 // ----------------------------------------------------------------------------
       
   263 // CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::TUpdateKidAttributesTrySecondTypeState
       
   264 // ----------------------------------------------------------------------------
       
   265 //
       
   266 CRsfwLookupStateMachine::
       
   267 TUpdateKidAttributesTrySecondTypeState::
       
   268 TUpdateKidAttributesTrySecondTypeState(CRsfwLookupStateMachine* aParent)
       
   269     : iOperation(aParent)
       
   270     {
       
   271     }
       
   272 
       
   273 // ----------------------------------------------------------------------------
       
   274 // CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::EnterL
       
   275 // ----------------------------------------------------------------------------
       
   276 //
       
   277 void CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::EnterL()
       
   278     {
       
   279     DEBUGSTRING(("CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::EnterL"));
       
   280     // We only come here if nodetype is unknown and
       
   281     // we already tried to lookup as a file
       
   282     iOperation->FileEngine()->UpdateAttributesL(*iOperation->iPath,
       
   283                                                 iOperation->iKidName,
       
   284                                                 iOperation->iDirEntAttr,
       
   285                                                 KNodeTypeDir, iOperation);
       
   286     }
       
   287 
       
   288 // ----------------------------------------------------------------------------
       
   289 // CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::CompleteL
       
   290 // ----------------------------------------------------------------------------
       
   291 //
       
   292 CRsfwLookupStateMachine::TState*
       
   293 CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::CompleteL()
       
   294     {
       
   295     DEBUGSTRING(("CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::CompleteL"));   
       
   296     // from CRsfwFileEngine::UpdateFileAttributes/UpdateDirAttributes
       
   297     return iOperation->CompleteL();
       
   298     }
       
   299 
       
   300 
       
   301 // ----------------------------------------------------------------------------
       
   302 // CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::ErrorL
       
   303 // If we were looking for a file return KErrNotFound,
       
   304 // if for directory KErrPathNotFound.
       
   305 // File Server seems to always call Entry() (-->lookup()) before other file
       
   306 // operations so as long as we return the right variant of "not found"
       
   307 // error here other places do not matter.
       
   308 // ----------------------------------------------------------------------------
       
   309 //
       
   310 CRsfwLookupStateMachine::TState*
       
   311 CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::ErrorL(TInt aCode)
       
   312     {
       
   313     DEBUGSTRING(("CRsfwLookupStateMachine::TUpdateKidAttributesTrySecondTypeState::ErrorL %d", aCode));    
       
   314     // from CRsfwFileEngine::Lookup()
       
   315     // No such kid
       
   316 
       
   317     // cache the last failed lookup results
       
   318    	iOperation->FileEngine()->SetFailedLookup(*iOperation->iPath,
       
   319    											iOperation->iKidName);
       
   320 
       
   321     if (iOperation->iNodeType == KNodeTypeDir)
       
   322         {
       
   323         return iOperation->CompleteRequestL(KErrPathNotFound);
       
   324         }
       
   325     else
       
   326         {
       
   327         return iOperation->CompleteRequestL(KErrNotFound);
       
   328         }
       
   329     }
       
   330