Update contrib.
1 // Copyright (c) 1998-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_disk.cpp
20 #if defined(_LOCKABLE_MEDIA)
22 LOCAL_C TInt DelayedWriter(TAny *aPtr);
23 LOCAL_C void DelayedWriterL(const TDelayedWriter *aDW);
26 EXPORT_C void WriteToDisk(const TDesC& aFileName, const TDesC8& aBuf)
28 // Launches as separate thread that writes the contents of the pbus pswd
29 // store to disk. It is possible that this function will be called again
30 // before the thread has finished writing. In that case, a new thread
31 // will be created and it will wait on the global DelayedWriteSem semaphore.
34 static TInt32 ctr = 0x00000000; // ctr to create unique thd names
36 __PRINT(_L("WriteToDisk"));
37 __PRINT1(_L("wtd:afn%S"), &aFileName);
39 TDelayedWriterInit dwi;
40 dwi.iFileName = &aFileName;
43 // Create local semaphore this thread can wait on until the child thread has
44 // made copies of the file name and the store data.
46 __PRINT(_L("wtd:cr sem"));
48 semName.Format(_L("dws%08x"), ctr++);
49 dwi.iSemName = &semName;
51 if (svrSem.CreateGlobal(semName, 0) != KErrNone)
54 // Spin off a thread with a unique name.
56 __PRINT(_L("wtd:cr thd"));
58 nm.Format(_L("dw%08x"), ctr);
60 TInt hminsz = Max(KHeapMinSize, aFileName.Length() + aBuf.Length() + 1024);
62 nm, DelayedWriter, KDefaultStackSize,
63 hminsz /* aHeapMinSize */, hminsz /* aHeapMaxSize */, &dwi) == KErrNone)
65 __PRINT(_L("wtd:set pri"));
66 t.SetPriority(EPriorityMuchLess); // run as low priority task
67 __PRINT(_L("wtd:res"));
69 __PRINT(_L("wtd:wait"));
71 __PRINT(_L("wtd:cls thd"));
72 t.Close(); // get rid of our handle
75 __PRINT(_L("wtd:cls sem"));
80 LOCAL_D TInt DelayedWriter(TAny *aPtr)
82 // Main thread function for thread that is spun off from WriteToDisk().
83 // After local copies of the data have been allocated (or failed), tell
84 // the server to continue.
87 __PRINT(_L("DelayedWriter"));
89 User::SetCritical(User::ESystemCritical);
93 TDelayedWriterInit *dwi = (TDelayedWriterInit *) aPtr;
94 RSemaphore svrSem; // signal svr when data copied
95 CTrapCleanup *th = NULL; // thread trap handler
96 TDelayedWriter *dw = NULL; // thread copy of data
97 RSemaphore queueSem; // queued delayed write threads
99 // Allocate a trap handler.
100 __PRINT(_L("dlw:alc tp"));
101 if ((th = CTrapCleanup::New()) == NULL)
107 // Make copies of the filename and store data.
108 __PRINT(_L("dlw:cp dat"));
109 TRAP(r, dw = TDelayedWriter::NewL(dwi));
113 // Tell file server made local copies of data and so can continue.
114 __PRINT(_L("dlw:sg cp dat"));
115 if ((r = svrSem.OpenGlobal(*dwi->iSemName)) != KErrNone)
119 // Wait for the other store threads to finish.
120 __PRINT(_L("dlw:wait"));
121 if ((r = queueSem.OpenGlobal(_L("dwsem"))) != KErrNone)
125 // Write the data and signal the global semaphore so follow up threads can run.
126 __PRINT(_L("dlw:wrt"));
127 TRAP(r, DelayedWriterL(dw));
128 __PRINT1(_L("dlw:wrt r = %d"), r);
131 cleanup: // free any opened resources
132 __PRINT(_L("dlw:cln"));
142 LOCAL_D void DelayedWriterL(const TDelayedWriter *aDW)
144 // Replace any existing store file; write data and set file as hidden and system.
147 __PRINT(_L("DelayedWriterL"));
149 RFs fs; // connect to the file server
150 CleanupClosePushL(fs);
151 User::LeaveIfError(fs.Connect());
153 RFile f; // replace any existing file
154 CleanupClosePushL(f);
156 __PRINT(_L("dlw: opn"));
158 // Create the directory if it doesn't already exist
160 r = fs.MkDirAll(*aDW->iFileName);
161 if (r != KErrNone && r != KErrAlreadyExists)
163 __PRINT(_L("dlw: MkDirAll err"));
167 User::LeaveIfError(f.Replace(fs, *aDW->iFileName, EFileShareExclusive | EFileStream | EFileWrite));
168 __PRINT(_L("dlw: wrt"));
169 User::LeaveIfError(f.Write(*aDW->iData));
170 __PRINT(_L("dlw: sat"));
171 #ifndef __WINS__ // cannot replace hidden | system file in WINS.
172 User::LeaveIfError(f.SetAtt(KEntryAttHidden | KEntryAttSystem, 0x00000000));
174 __PRINT(_L("dlw: dst"));
175 CleanupStack::PopAndDestroy(2); // f, fs
182 TDelayedWriter *TDelayedWriter::NewL(const TDelayedWriterInit *dwi)
185 // Allocates a TDelayedWriter structure on the thread's heap that is used to
186 // persist the data that the writer thread will need during its lifetime.
189 __PRINT(_L("TDelayedWriter::NewL"));
191 TDelayedWriter *self = new(ELeave) TDelayedWriter();
192 CleanupStack::PushL(self);
193 self->ConstructL(dwi);
194 CleanupStack::Pop(); // self
199 TDelayedWriter::TDelayedWriter()
201 __PRINT(_L("TDelayedWriter::TDelayedWriter"));
207 void TDelayedWriter::ConstructL(const TDelayedWriterInit *dwi)
209 // 2y initialisation. Makes own copy of filename and data.
210 // Fields are not popped onto CleanupStack because will be deleted in dtor
213 __PRINT(_L("TDelayedWriter::ConstructL"));
215 iFileName = dwi->iFileName->AllocL();
216 iData = dwi->iData->AllocL();
219 TDelayedWriter::~TDelayedWriter()
221 // dtor - frees filename and store data that was allocated in ConstructL().
224 __PRINT(_L("TDelayedWriter::~TDelayedWriter"));
226 delete iFileName; // alloc in ConstructL()
227 delete iData; // alloc in server
232 EXPORT_C void WriteToDisk(const TDesC& /*aFileName*/, const TDesC8& /*aBuf*/)