Update contrib.
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32\sfile\sf_main.cpp
19 #include "sf_plugin.h"
20 #include "sf_file_cache.h" // for TClosedFileUtils
21 #include "sf_memory_man.h"
27 #include "d32btrace.h"
29 // define this macro to enable tracing very early on in the boot sequence
30 //#define __ENABLE_TRACE__
33 _LIT(KStartupExeSysBinName,"Z:\\Sys\\Bin\\ESTART.EXE");
35 _LIT(KStartupExeName,"E32STRT.EXE");
36 _LIT(KStartupExeSysBinName,"E32STRT.EXE");
39 //const TInt KSessionNotifyListGranularity=16; //-- not used anywhere
41 // patch ldds should specify this as their third uid
42 //const TInt KPatchLddUidValue=0x100000cc; //-- not used anywhere
44 _LIT(KFileServerName,"!FileServer");
48 // Create a new CServerFs.
51 TheFileServer=new CServerFs(EPriority);
52 __ASSERT_ALWAYS(TheFileServer!=NULL,Fault(EMainCreateServer));
53 TInt r = TheFileServer->iSessionQueueLock.CreateLocal();
54 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateServer));
55 r=TheFileServer->Start(KFileServerName);
56 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainStartServer));
60 CServerFs::CServerFs(TInt aPriority)
64 : CServer2(aPriority,EGlobalSharableSessions)
68 CServerFs::~CServerFs()
73 iSessionQueueLock.Close();
76 void CServerFs::RunL()
78 // calls CServer2::RunL
81 TInt fn = Message().Function();
83 // CServer2::DoConnectL() manipulates iSessionQ & so does CSession2::~CSession2().
84 // Unfortunately the session is deleted from a seperate thread (the disconnect
85 // thread) so we need a lock to protect it.
86 if (fn == RMessage2::EConnect)
88 SessionQueueLockWait(); // lock
90 SessionQueueLockSignal(); // unlock
98 CSessionFs* CServerFs::operator[](TInt anIndex)
100 // Indexing operator used by DoFsListOpenFiles
103 __ASSERT_DEBUG(anIndex>=0,Fault(ESvrBadSessionIndex));
104 iSessionIter.SetToFirst();
107 CSessionFs* ses=(CSessionFs*)&(*iSessionIter);
111 _LIT(KPrivatePath,"?:\\Private\\");
112 CSession2* CServerFs::NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const
114 // Create a new client session for this server.
117 TVersion v(KF32MajorVersionNumber,KF32MinorVersionNumber,KF32BuildVersionNumber);
118 TBool r=User::QueryVersionSupported(v,aVersion);
120 User::Leave(KErrNotSupported);
121 __CALL(if(UserHeapAllocFailCount>=0){__UHEAP_FAILNEXT(10);}); // Create session must succeed
122 CSessionFs* ses=CSessionFs::NewL();
123 CleanupStack::PushL(ses);
124 TUid aUid = aMessage.SecureId();
125 TBuf<30> thePath = KPrivatePath();
126 thePath[0] = (TUint8) RFs::GetSystemDriveChar();
127 thePath.AppendNumFixedWidth(aUid.iUid, EHex, 8);
128 thePath.Append(KSlash);
129 HBufC* pP=thePath.AllocL();
131 __CALL(if (UserHeapAllocFailCount>=0){__UHEAP_FAILNEXT(UserHeapAllocFailCount);});
135 User::LeaveIfError(aMessage.Client(idClient, EOwnerThread));
136 ses->SetThreadId(idClient.Id());
139 CleanupStack::Pop(); //ses
144 void CSessionFs::ServiceL(const RMessage2& aMessage)
146 // Service this message for the server
149 __CALL( if (SimulateError(&aMessage)) { aMessage.Complete(ErrorCondition); return; } );
150 const TInt ipcFunction = aMessage.Function() & KIpcFunctionMask;
152 if((ipcFunction) >= EMaxClientOperations)
154 __THRD_PRINT1(_L("CSessionFs::DoServiceL() - func 0x%x KErrNotSupported"), ipcFunction);
155 aMessage.Complete(KErrNotSupported);
159 const TOperation& oP = OperationArray[ipcFunction];
160 CFsClientMessageRequest* pR = NULL;
161 TInt r = RequestAllocator::GetMessageRequest(oP, aMessage, pR);
164 if(r == KErrBadHandle)
166 _LIT(KPanic,"Panic");
167 aMessage.Panic(KPanic, r);
170 aMessage.Complete(r);
173 pR->Set(aMessage, oP, this);
174 __PRINT4TEMP(_L("***** Received Message sess %08x req %08x func 0x%x - %S"), this, pR, ipcFunction, GetFunctionName(ipcFunction));
178 void CActiveSchedulerFs::New()
180 // Create and install the active scheduler.
184 CActiveSchedulerFs* pA=new CActiveSchedulerFs;
185 __ASSERT_ALWAYS(pA!=NULL,Fault(EMainCreateScheduler));
186 CActiveScheduler::Install(pA);
189 void CActiveSchedulerFs::Error(TInt anError) const
191 // Called if any Run() method leaves, which should never happen.
195 __PRINT1(_L("FileSystemActiveScheduler Error %d"),anError);
196 User::Panic(_L("FSRV-ERR"),anError);
202 // Create the initial objects
206 // First we need to create all the containers.
208 TheContainer=CFsObjectConIx::NewL();
209 FileSystems=TheContainer->CreateL();
210 Extensions=TheContainer->CreateL();
211 ProxyDrives=TheContainer->CreateL();
212 Files=TheContainer->CreateL();
213 FileShares=TheContainer->CreateL();
214 Dirs=TheContainer->CreateL();
215 Formats=TheContainer->CreateL();
216 RawDisks=TheContainer->CreateL();
217 TClosedFileUtils::InitL();
220 // Initialize the drives
222 for (TInt i=0;i<KMaxDrives;i++)
223 TheDrives[i].CreateL(i);
226 // Next we need to create the ROM file system.
228 #if defined(__EPOC32__)
229 InstallRomFileSystemL();
230 CFileSystem* romFs=GetFileSystem(_L("Rom"));
231 //#ifndef __DATA_CAGING__
232 TheDefaultPath=_L("Z:\\"); // Temporarily set the default path to the ROM
234 TheDrives[EDriveZ].SetAtt(KDriveAttRom|KDriveAttInternal);
235 TheDrives[EDriveZ].GetFSys()=romFs;
236 TInt r=FsThreadManager::InitDrive(EDriveZ,ETrue);
237 User::LeaveIfError(r);
242 TInt InitializeLocalFileSystem(const TDesC& aName)
244 // Initialize the local file system
248 __PRINT(_L("InitializeLocalFileSystem"));
249 CFileSystem* localFileSystem=GetFileSystem(aName);
250 __ASSERT_DEBUG(localFileSystem!=NULL,Fault(EMainGetLocalFileSystem));
251 if(localFileSystem == NULL)
253 #if defined(__WINS__)
254 TheDrives[EDriveZ].GetFSys()=localFileSystem;
255 TheDrives[EDriveZ].SetAtt(KDriveAttRom|KDriveAttInternal);
256 TInt r=FsThreadManager::InitDrive(EDriveZ,ETrue);
257 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainInitialiseRomFs));
261 // Initialize the default path
263 //#ifndef __DATA_CAGING__
264 #if !defined(__WINS__)
267 r=localFileSystem->DefaultPath(TheDefaultPath);
268 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainGetLocalDefaultPath));
271 LocalFileSystemInitialized=ETrue;
277 _LIT(KMediaLddName, "ELOCD");
279 TInt StartupThread(TAny*)
281 // The startup thread.
285 __PRINT(_L("StartupThread"));
286 User::SetCritical(User::ESystemCritical);
289 #ifdef SYMBIAN_FTRACE_ENABLE
290 r = User::LoadLogicalDevice(_L("D_FTRACE"));
291 __PRINT1(_L("User::LoadLogicalDevice(D_FTRACE) returns %d"),r);
292 __ASSERT_ALWAYS(r==KErrNone || r==KErrAlreadyExists,Fault(ETraceLddLoadFailure));
294 r = TheFtrace.Open(EOwnerProcess);
295 __ASSERT_ALWAYS(r==KErrNone || r==KErrAlreadyExists,Fault(ETraceLddLoadFailure));
298 #if defined (__ENABLE_TRACE__)
304 // trace.SetMode(RBTrace::EEnable + RBTrace::EFreeRunning);
305 trace.SetFilter(BTrace::EThreadIdentification,1);
307 trace.SetFilter(UTF::EBorder,1);
308 trace.SetFilter(UTF::EError,1);
310 trace.SetFilter2(EF32TraceUidEfsrv,1);
311 // trace.SetFilter2(EF32TraceUidFileSys,1);
312 // trace.SetFilter2(EF32TraceUidProxyDrive,1);
320 // Load the file system's device driver
322 r=User::LoadLogicalDevice(KMediaLddName);
323 __PRINT1(_L("User::LoadLogicalDevice(KMediaLddName) returns %d"),r);
325 __ASSERT_ALWAYS(r==KErrNone || r==KErrAlreadyExists || r==KErrNotFound,Fault(EMainCreateResources6));
327 // Load media drivers using Win32 functions. It is not possible to directly
328 // read the \epoc32\release\wins\udeb directory, and ELOCAL must be mounted
329 // to access Win32 anyway.
331 _LIT(KMDW1, "MED*.PDD");
332 TBuf<9> KMDW(KMDW1); // reserve space for \0
334 TFileName *pfn = new TFileName;
335 __ASSERT_ALWAYS(pfn != NULL, Fault(EMainScanMediaDriversMem1));
336 TFileName &fn = *pfn;
338 MapEmulatedFileName(fn, KMDW);
339 __ASSERT_ALWAYS(fn.Length() < KMaxFileName, Fault(EMainScanMediaDriversLocation));
342 HANDLE h = Emulator::FindFirstFile((LPCTSTR)fn.PtrZ(), &ffd); //SL: added cast
343 BOOL fF = (h != INVALID_HANDLE_VALUE);
346 TPtrC mdNm((const TUint16 *)ffd.cFileName); // null terminated wchar_t array SL: added cast
348 // NB: parse Win32 file path with EPOC32 functionality.
349 TParse *pprs = new TParse;
350 __ASSERT_ALWAYS(pprs != NULL, Fault(EMainScanMediaDriversMem2));
352 prs.Set(mdNm, NULL, NULL);
353 r = User::LoadPhysicalDevice(prs.NameAndExt());
354 __ASSERT_ALWAYS(r==KErrNone || r==KErrAlreadyExists || r==KErrNotFound,Fault(EMainLoadMediaDriver));
355 fF = Emulator::FindNextFile(h, &ffd);
358 FindClose(h); // Win32 direct
362 // Load media drivers for EPOC32 using built-in rom file system.
366 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainScanMediaDriverConnect));
369 _LIT(KMDSysBinHome, "Z:\\Sys\\Bin\\med*.pdd");
371 // _LIT(KMDHome, "med*.pdd");
374 r = d.Open(fsM, KMDSysBinHome, KEntryAttMaskSupported);
375 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainScanMediaDriverDirOpen));
382 __ASSERT_ALWAYS(r == KErrNone || r == KErrEof, Fault(EMainScanMediaDriverDirRead));
383 done = (r == KErrEof);
385 for (TInt i = 0; i < ea.Count(); ++i)
387 const TEntry &e = ea[i];
390 TParse *pprs = new TParse;
391 __ASSERT_ALWAYS(pprs != NULL, Fault(EMainScanMediaDriversMem1));
393 prs.Set(e.iName, NULL, NULL);
394 TPtrC mdName(prs.NameAndExt());
395 r = User::LoadPhysicalDevice(mdName);
396 __PRINT1(_L("User::LoadPhysicalDevice(mdName) returns %d"),r);
397 __ASSERT_ALWAYS(r==KErrNone || r==KErrAlreadyExists || r==KErrNotFound,Fault(EMainLoadMediaDriver));
406 #endif // else __WINS__
408 #if defined(__WINS__)
409 //#ifndef __DATA_CAGING__
410 TheDefaultPath=_L("?:\\");
411 TheDefaultPath[0] = (TUint8) RFs::GetSystemDriveChar();
415 #if defined(__EPOC32__)
416 TMachineStartupType reason;
417 UserHal::StartupReason(reason);
418 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
419 PrintStartUpReason(reason);
421 OpenOnDriveZOnly = (reason==EStartupSafeReset);
425 // Now we must load estart from z:
428 #if defined(__WINS__)
429 const char* eStartPath = NULL;
430 UserSvr::HalFunction(EHalGroupEmulator,EEmulatorHalStringProperty,(TAny*)"EStart",&eStartPath);
431 if (eStartPath == NULL)
433 r=eStart.Create(KStartupExeSysBinName,KNullDesC);
437 TPtrC8 temp((unsigned char *)eStartPath);
438 TBuf16<KMaxFileName> buf;
440 r=eStart.Create(buf,KNullDesC);
443 r=eStart.Create(KStartupExeSysBinName,KNullDesC);
446 if (r!=KErrNone) // Whoops!
447 Fault(EMainStartupNoEStart);
448 eStart.Resume(); // Start the process going
449 eStart.Close(); // Get rid of our handle
450 #if defined(_LOCKABLE_MEDIA)
451 // Create a global semaphore for the asynchronous WriteToDisk() threads.
453 r = s.CreateGlobal(_L("dwsem"), 1); // only supp 1 thd at a time
454 __ASSERT_ALWAYS(r == KErrNone, Fault(EMainStartupWriteToDiskSemaphore));
458 // Now we can just exit the startup thread as its no longer needed.
463 void commonInitialize()
465 // Initialization common to all platforms.
469 __PRINT(_L("commonInitialize"));
470 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
471 ErrorCondition=KErrNone;
473 UserHeapAllocFailCount=-1;
474 KernHeapAllocFailCount=-1;
477 TInt r= RequestAllocator::iCacheLock.CreateLocal();
478 __ASSERT_ALWAYS(r==KErrNone,Fault(EFsCacheLockFailure));
480 // initialise the TParse pool lock object
481 r = TParsePool::Init();
482 __ASSERT_ALWAYS(r==KErrNone,Fault(EFsParsePoolLockFailure));
484 // Get local copies of capability sets
486 caps.SetAllSupported();
487 AllCapabilities=*(SCapabilitySet*)∩︀
489 DisabledCapabilities=*(SCapabilitySet*)∩︀
491 FsThreadManager::SetMainThreadId();
492 r=FsThreadManager::CreateDisconnectThread();
493 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainDisconnectThread));
495 RequestAllocator::Initialise();
498 // Install a trap handler
500 CTrapCleanup* trapHandler=CTrapCleanup::New();
501 __ASSERT_ALWAYS(trapHandler!=NULL,Fault(EMainCreateResources1));
504 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateResources1));
505 CActiveSchedulerFs::New();
507 TheKernEventNotifier = CKernEventNotifier::New();
508 __ASSERT_ALWAYS(TheKernEventNotifier,Fault(EMainCreateResources5));
509 CActiveSchedulerFs::Add(TheKernEventNotifier);
510 TheKernEventNotifier->Start();
512 __ASSERT_ALWAYS(InitLoader()==KErrNone,Fault(ELdrRestartInit));
514 LocalFileSystemInitialized=EFalse;
515 StartupInitCompleted=EFalse;
516 RefreshZDriveCache=EFalse;
517 CompFsMounted=EFalse;
520 // initialise notification information
521 FsNotify::Initialise();
522 // initialise local drive specific information
523 LocalDrives::Initialise();
525 TRAP(r, FsPluginManager::InitialiseL());
526 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateStartupThread0));
529 r=t.Create(_L("StartupThread"),StartupThread,KDefaultStackSize,KHeapMinSize,KHeapMinSize,NULL);
530 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateStartupThread1));
531 t.SetPriority(EPriorityLess);
534 TRAP(r,pL=CLogon::NewL());
535 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateStartupThread2));
537 // NOTE: This function only returns after the startup thread has exited
540 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateStartupThread3));
543 // Make a proper process relative handle to the server
544 r=TheServerThread.Duplicate(TheServerThread);
545 __ASSERT_ALWAYS(r==KErrNone,Fault(EMainCreateStartupThread4));
546 ServerThreadAllocator = &User::Heap();
549 TBool ServerIsRunning()
551 // Check whether or not the server already exists
555 TFullName serverName;
556 TFindServer fileServer(KFileServerName);
557 TInt result=fileServer.Next(serverName);
558 if (result!=KErrNotFound)
568 if (ServerIsRunning())
571 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
572 if (UserSvr::DebugMask() & 0x402) // KBOOT | KDLL
574 //DebugReg=KFSERV|KFLDR;
575 DebugReg=KFSERV|KFLDR|KLFFS|KTHRD|KROFS;
577 // DebugReg=KFSYS|KFSERV|KFLDR|KLFFS|KTHRD|KCACHE|KROFS|KCOMPFS|KCACHE;
578 // User::SetDebugMask(0x80004000);
581 __PRINT(_L("FileServer E32Main"));
583 UserSvr::FsRegisterThread();
584 RThread().SetPriority(EPriorityMore);
586 CActiveSchedulerFs::Start();