sl@0
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "FDESC.H"
|
sl@0
|
17 |
#include "LPOSIX.H"
|
sl@0
|
18 |
#include "LTIME.H"
|
sl@0
|
19 |
#include <string.h>
|
sl@0
|
20 |
#include <stdio_r.h>
|
sl@0
|
21 |
#include <fcntl.h> // for struct stat
|
sl@0
|
22 |
#include <sys/errno.h> // for ENOTSOCK
|
sl@0
|
23 |
#include <sys/ioctl.h>
|
sl@0
|
24 |
#include <c32comm.h>
|
sl@0
|
25 |
|
sl@0
|
26 |
#include "POSIXIF.H" // for details of CPosixRequest::iLink
|
sl@0
|
27 |
|
sl@0
|
28 |
// Cleanup support
|
sl@0
|
29 |
|
sl@0
|
30 |
void CFileDescBase::Cleanup(TAny *aPtr)
|
sl@0
|
31 |
{
|
sl@0
|
32 |
((CFileDescBase*)aPtr)->Close();
|
sl@0
|
33 |
}
|
sl@0
|
34 |
|
sl@0
|
35 |
void CFileDescBase::PushLC()
|
sl@0
|
36 |
{
|
sl@0
|
37 |
CleanupStack::PushL(TCleanupItem(Cleanup,this));
|
sl@0
|
38 |
}
|
sl@0
|
39 |
|
sl@0
|
40 |
// Private constructor
|
sl@0
|
41 |
|
sl@0
|
42 |
inline TPosixRequestQueue::TPosixRequestQueue()
|
sl@0
|
43 |
: TSglQue<CPosixRequest>(_FOFF(CPosixRequest,iLink))
|
sl@0
|
44 |
{}
|
sl@0
|
45 |
|
sl@0
|
46 |
CFileDescBase::CFileDescBase() : iReadTimeout(-1)
|
sl@0
|
47 |
{
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
// A CFileDescBase factory function, for "named" file-like objects
|
sl@0
|
51 |
|
sl@0
|
52 |
//CFileDescBase* CFileDescBase::Open(RFs& aSession, const char* name, int mode, int perms, TInt& err)
|
sl@0
|
53 |
//CFileDescBase* CFileDescBase::Open(RFs& aSession, const wchar_t* name, int mode, int perms, TInt& err)
|
sl@0
|
54 |
CFileDescBase* CFileDescBase::Open(RSessionBase& aSession, const wchar_t* name, int mode, int perms, TInt& err)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
CFileDescBase* ret=0;
|
sl@0
|
57 |
|
sl@0
|
58 |
if (wcscmp(name,L"CON:")==0)
|
sl@0
|
59 |
ret= new CTtyDesc; // NB. This won't be the default stdin/stdout/stderr console
|
sl@0
|
60 |
else
|
sl@0
|
61 |
if (wcscmp(name,L"NUL:")==0)
|
sl@0
|
62 |
ret= new CFileDescBase;
|
sl@0
|
63 |
else
|
sl@0
|
64 |
if (wcscmp(name,L"TMP:")==0)
|
sl@0
|
65 |
{
|
sl@0
|
66 |
RFs& rfs = static_cast<RFs&>(aSession);
|
sl@0
|
67 |
TParse path;
|
sl@0
|
68 |
err=GetFullPath(path,(const TText16*)WIDEP_tmpdir, rfs,NULL);
|
sl@0
|
69 |
if (err)
|
sl@0
|
70 |
return 0;
|
sl@0
|
71 |
CTempFileDesc* tmp= new CTempFileDesc;
|
sl@0
|
72 |
if (tmp)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
err=tmp->Open(rfs,path.DriveAndPath());
|
sl@0
|
75 |
if (err)
|
sl@0
|
76 |
{
|
sl@0
|
77 |
delete tmp;
|
sl@0
|
78 |
return 0;
|
sl@0
|
79 |
}
|
sl@0
|
80 |
}
|
sl@0
|
81 |
ret=tmp;
|
sl@0
|
82 |
}
|
sl@0
|
83 |
else if ((L'C' == name[0]) && (L'O' == name[1]) && (L'M' == name[2]) && (L':' == name[4]) && ((name[3] >= L'1') && (name[3] <= L'9')) ||
|
sl@0
|
84 |
(L'I' == name[0]) && (L'R' == name[1]) && (L'C' == name[2]) && (L'O' == name[3]) && (L'M' == name[4]) && (L':' == name[6]) && ((name[5] >= L'1') && (name[5] <= L'9')))
|
sl@0
|
85 |
{
|
sl@0
|
86 |
|
sl@0
|
87 |
RCommServ& rcs = static_cast<RCommServ&>(aSession);
|
sl@0
|
88 |
if (!rcs.Handle())
|
sl@0
|
89 |
{
|
sl@0
|
90 |
err=rcs.Connect(); //connect to the server
|
sl@0
|
91 |
if (err)
|
sl@0
|
92 |
return 0;
|
sl@0
|
93 |
}
|
sl@0
|
94 |
CSerialDesc * tmp = new CSerialDesc;
|
sl@0
|
95 |
if (tmp)
|
sl@0
|
96 |
{
|
sl@0
|
97 |
RCommServ& rcs = static_cast<RCommServ&>(aSession);
|
sl@0
|
98 |
CleanupStack::PushL(tmp);
|
sl@0
|
99 |
err = tmp->Open(rcs, name, mode, perms);
|
sl@0
|
100 |
CleanupStack::Pop(tmp);
|
sl@0
|
101 |
if (err)
|
sl@0
|
102 |
{
|
sl@0
|
103 |
delete tmp;
|
sl@0
|
104 |
return 0;
|
sl@0
|
105 |
}
|
sl@0
|
106 |
}
|
sl@0
|
107 |
ret = tmp;
|
sl@0
|
108 |
}
|
sl@0
|
109 |
else
|
sl@0
|
110 |
{
|
sl@0
|
111 |
TFullName fullName;
|
sl@0
|
112 |
RFs& rfs = static_cast<RFs&>(aSession);
|
sl@0
|
113 |
err=GetFullFile(fullName,(const TText16*)name,rfs);
|
sl@0
|
114 |
if (err)
|
sl@0
|
115 |
return 0;
|
sl@0
|
116 |
CFileDesc* f= new CFileDesc;
|
sl@0
|
117 |
if (f)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
err=f->Open(rfs,fullName,mode,perms);
|
sl@0
|
120 |
if (err)
|
sl@0
|
121 |
{
|
sl@0
|
122 |
delete f;
|
sl@0
|
123 |
return 0;
|
sl@0
|
124 |
}
|
sl@0
|
125 |
}
|
sl@0
|
126 |
ret=f;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
err=(ret==0)? KErrNoMemory:KErrNone;
|
sl@0
|
129 |
return ret;
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
// Useful default implementations for CFileDescBase virtual functions.
|
sl@0
|
133 |
|
sl@0
|
134 |
TInt CFileDescBase::LSeek (int& offset, int)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
// minimal implementation for devices which can't seek
|
sl@0
|
137 |
offset=0;
|
sl@0
|
138 |
return KErrNone;
|
sl@0
|
139 |
}
|
sl@0
|
140 |
|
sl@0
|
141 |
void CFileDescBase::Read (TDes8& aBuf, TRequestStatus& aStatus)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
// minimal implementation for /dev/null
|
sl@0
|
144 |
aBuf.Zero(); // set length to zero
|
sl@0
|
145 |
TRequestStatus* sp=&aStatus;
|
sl@0
|
146 |
User::RequestComplete(sp,KErrNone);
|
sl@0
|
147 |
}
|
sl@0
|
148 |
|
sl@0
|
149 |
void CFileDescBase::ReadCancel() {}
|
sl@0
|
150 |
|
sl@0
|
151 |
TInt CFileDescBase::ReadCompletion (TDes8& /*aBuf*/, TInt aStatus)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
return aStatus;
|
sl@0
|
154 |
}
|
sl@0
|
155 |
|
sl@0
|
156 |
|
sl@0
|
157 |
TInt CFileDescBase::FStat (struct stat *st)
|
sl@0
|
158 |
{
|
sl@0
|
159 |
// minimal implementation:
|
sl@0
|
160 |
// I am a character device about which little is known
|
sl@0
|
161 |
st->st_mode = S_IFCHR;
|
sl@0
|
162 |
st->st_blksize=0;
|
sl@0
|
163 |
return KErrNone;
|
sl@0
|
164 |
}
|
sl@0
|
165 |
|
sl@0
|
166 |
void CFileDescBase::Complete (TRequestStatus& aStatus, TInt aResult)
|
sl@0
|
167 |
{
|
sl@0
|
168 |
TRequestStatus* sp=&aStatus;
|
sl@0
|
169 |
User::RequestComplete(sp,aResult);
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
void CFileDescBase::Write (TDes8& /*aBuf*/, TRequestStatus& aStatus)
|
sl@0
|
173 |
{
|
sl@0
|
174 |
// minimal implementation for /dev/null
|
sl@0
|
175 |
// we will claim to have written all of the data
|
sl@0
|
176 |
Complete(aStatus,KErrNone);
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
void CFileDescBase::WriteCancel() {}
|
sl@0
|
180 |
|
sl@0
|
181 |
TInt CFileDescBase::WriteCompletion (TDes8& /*aBuf*/, TInt aStatus)
|
sl@0
|
182 |
{
|
sl@0
|
183 |
return aStatus;
|
sl@0
|
184 |
}
|
sl@0
|
185 |
|
sl@0
|
186 |
void CFileDescBase::Sync (TRequestStatus& aStatus)
|
sl@0
|
187 |
{
|
sl@0
|
188 |
// minimal implementation for /dev/null
|
sl@0
|
189 |
Complete(aStatus,KErrNone);
|
sl@0
|
190 |
}
|
sl@0
|
191 |
|
sl@0
|
192 |
void CFileDescBase::SyncCancel() {}
|
sl@0
|
193 |
|
sl@0
|
194 |
void CFileDescBase::Ioctl(int /*aCmd*/, void* /*aParam*/, TRequestStatus& aStatus)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
// minimal implementation for /dev/null and other synchronous devices
|
sl@0
|
197 |
Complete(aStatus,KErrNone);
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
void CFileDescBase::IoctlCancel()
|
sl@0
|
201 |
{
|
sl@0
|
202 |
return; // suitable for all synchronous ioctls
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
TInt CFileDescBase::IoctlCompletion(int aCmd, void* aParam, TInt aStatus)
|
sl@0
|
206 |
{
|
sl@0
|
207 |
TInt ret=aStatus;
|
sl@0
|
208 |
if (ret!=KErrNone)
|
sl@0
|
209 |
return ret;
|
sl@0
|
210 |
int *param=REINTERPRET_CAST(int*,aParam);
|
sl@0
|
211 |
switch (aCmd)
|
sl@0
|
212 |
{
|
sl@0
|
213 |
case E32IONREAD:
|
sl@0
|
214 |
*param=0; // claim that no data is available
|
sl@0
|
215 |
break;
|
sl@0
|
216 |
case E32IOSELECT:
|
sl@0
|
217 |
*param=(*param)&(E32SELECT_READ|E32SELECT_WRITE); // but don't block
|
sl@0
|
218 |
break;
|
sl@0
|
219 |
default:
|
sl@0
|
220 |
ret=KErrNotSupported;
|
sl@0
|
221 |
break;
|
sl@0
|
222 |
}
|
sl@0
|
223 |
return ret;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
|
sl@0
|
226 |
// A CFileDescBase factory function, for socket objects
|
sl@0
|
227 |
|
sl@0
|
228 |
CFileDescBase* CFileDescBase::Socket(RSocketServ& aSs, int family, int style, int protocol, TInt& err)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
// connect to the Socket Server if necessary
|
sl@0
|
231 |
if (aSs.Handle()==0)
|
sl@0
|
232 |
{
|
sl@0
|
233 |
err=aSs.Connect(TUint(-1)); // allow arbitrary number of requests
|
sl@0
|
234 |
if (err)
|
sl@0
|
235 |
return 0;
|
sl@0
|
236 |
}
|
sl@0
|
237 |
CSocketDesc* s= new CSocketDesc;
|
sl@0
|
238 |
if (s==0)
|
sl@0
|
239 |
{
|
sl@0
|
240 |
err=KErrNoMemory;
|
sl@0
|
241 |
return 0;
|
sl@0
|
242 |
}
|
sl@0
|
243 |
err=s->Socket(aSs,family,style,protocol);
|
sl@0
|
244 |
if (err)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
delete s;
|
sl@0
|
247 |
return 0;
|
sl@0
|
248 |
}
|
sl@0
|
249 |
return s;
|
sl@0
|
250 |
}
|
sl@0
|
251 |
|
sl@0
|
252 |
// minimal implementation of sockets, useful for all non-socket descriptors
|
sl@0
|
253 |
|
sl@0
|
254 |
void CFileDescBase::RecvFrom(TDes8& /*aDesc*/, TSockAddr& /*from*/, int /*flags*/, TRequestStatus& aStatus)
|
sl@0
|
255 |
{
|
sl@0
|
256 |
// minimal implementation
|
sl@0
|
257 |
Complete(aStatus,ENOTSOCK);
|
sl@0
|
258 |
}
|
sl@0
|
259 |
|
sl@0
|
260 |
void CFileDescBase::RecvFromCancel () {}
|
sl@0
|
261 |
|
sl@0
|
262 |
TInt CFileDescBase::RecvFromCompletion(TInt& /*aLength*/, TInt aStatus)
|
sl@0
|
263 |
{
|
sl@0
|
264 |
return aStatus;
|
sl@0
|
265 |
}
|
sl@0
|
266 |
|
sl@0
|
267 |
void CFileDescBase::SendTo(TDes8& /*aDesc*/, TSockAddr& /*to*/, int /*flags*/, TRequestStatus& aStatus)
|
sl@0
|
268 |
{
|
sl@0
|
269 |
// minimal implementation
|
sl@0
|
270 |
Complete(aStatus,ENOTSOCK);
|
sl@0
|
271 |
}
|
sl@0
|
272 |
|
sl@0
|
273 |
void CFileDescBase::SendToCancel () {}
|
sl@0
|
274 |
|
sl@0
|
275 |
TInt CFileDescBase::SendToCompletion(TDes8& /*aDesc*/, TInt aStatus)
|
sl@0
|
276 |
{
|
sl@0
|
277 |
return aStatus;
|
sl@0
|
278 |
}
|
sl@0
|
279 |
|
sl@0
|
280 |
void CFileDescBase::Shutdown(TUint /*aHow*/,TRequestStatus& aStatus)
|
sl@0
|
281 |
{
|
sl@0
|
282 |
// minimal implementation
|
sl@0
|
283 |
Complete(aStatus,ENOTSOCK);
|
sl@0
|
284 |
}
|
sl@0
|
285 |
|
sl@0
|
286 |
void CFileDescBase::ShutdownCancel () {}
|
sl@0
|
287 |
|
sl@0
|
288 |
TInt CFileDescBase::Bind(TSockAddr& /*anAddr*/)
|
sl@0
|
289 |
{
|
sl@0
|
290 |
return ENOTSOCK;
|
sl@0
|
291 |
}
|
sl@0
|
292 |
|
sl@0
|
293 |
TInt CFileDescBase::Listen(TUint /*qSize*/)
|
sl@0
|
294 |
{
|
sl@0
|
295 |
return ENOTSOCK;
|
sl@0
|
296 |
}
|
sl@0
|
297 |
|
sl@0
|
298 |
void CFileDescBase::Accept(CSocketDesc*& /*aNewSocket*/, TRequestStatus& aStatus, RSocketServ& /*aSs*/)
|
sl@0
|
299 |
{
|
sl@0
|
300 |
// minimal implementation
|
sl@0
|
301 |
Complete(aStatus,ENOTSOCK);
|
sl@0
|
302 |
}
|
sl@0
|
303 |
|
sl@0
|
304 |
void CFileDescBase::AcceptCancel () {}
|
sl@0
|
305 |
|
sl@0
|
306 |
void CFileDescBase::Connect(TSockAddr& /*anAddr*/,TRequestStatus& aStatus)
|
sl@0
|
307 |
{
|
sl@0
|
308 |
// minimal implementation
|
sl@0
|
309 |
Complete(aStatus,ENOTSOCK);
|
sl@0
|
310 |
}
|
sl@0
|
311 |
|
sl@0
|
312 |
void CFileDescBase::ConnectCancel () {}
|
sl@0
|
313 |
|
sl@0
|
314 |
TInt CFileDescBase::SockName(int /*anEnd*/, TSockAddr& /*anAddr*/)
|
sl@0
|
315 |
{
|
sl@0
|
316 |
return ENOTSOCK;
|
sl@0
|
317 |
}
|
sl@0
|
318 |
|
sl@0
|
319 |
TInt CFileDescBase::GetSockOpt(TUint /*anOptionName*/,TUint /*anOptionLevel*/,TDes8& /*anOption*/)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
return ENOTSOCK;
|
sl@0
|
322 |
}
|
sl@0
|
323 |
|
sl@0
|
324 |
TInt CFileDescBase::SetSockOpt(TUint /*anOptionName*/,TUint /*anOptionLevel*/,TDesC8& /*anOption*/)
|
sl@0
|
325 |
{
|
sl@0
|
326 |
return ENOTSOCK;
|
sl@0
|
327 |
}
|
sl@0
|
328 |
|
sl@0
|
329 |
// Queue handling
|
sl@0
|
330 |
|
sl@0
|
331 |
void CFileDescBase::AddLast(CPosixRequest& aRequest, IOQueues aQueue)
|
sl@0
|
332 |
{
|
sl@0
|
333 |
TPosixRequestQueue& queue = iQueues[aQueue];
|
sl@0
|
334 |
queue.AddLast(aRequest);
|
sl@0
|
335 |
if (queue.IsFirst(&aRequest))
|
sl@0
|
336 |
aRequest.StartAsynch(); // queue was empty, so start straight away
|
sl@0
|
337 |
}
|
sl@0
|
338 |
|
sl@0
|
339 |
void CFileDescBase::Remove(CPosixRequest& aRequest, IOQueues aQueue)
|
sl@0
|
340 |
{
|
sl@0
|
341 |
TPosixRequestQueue& queue = iQueues[aQueue];
|
sl@0
|
342 |
TBool wasFirst = queue.IsFirst(&aRequest);
|
sl@0
|
343 |
queue.Remove(aRequest);
|
sl@0
|
344 |
if (wasFirst)
|
sl@0
|
345 |
{
|
sl@0
|
346 |
if (!queue.IsEmpty())
|
sl@0
|
347 |
queue.First()->StartAsynch(); // start the next outstanding request
|
sl@0
|
348 |
}
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
|
sl@0
|
352 |
// Generic (non-virtual) handling for Close
|
sl@0
|
353 |
|
sl@0
|
354 |
TInt CFileDescBase::Close()
|
sl@0
|
355 |
{
|
sl@0
|
356 |
TInt err=KErrNone;
|
sl@0
|
357 |
if (--iDupCount < 0)
|
sl@0
|
358 |
{
|
sl@0
|
359 |
err=FinalClose();
|
sl@0
|
360 |
delete this;
|
sl@0
|
361 |
}
|
sl@0
|
362 |
return err;
|
sl@0
|
363 |
}
|
sl@0
|
364 |
|
sl@0
|
365 |
TInt CFileDescBase::FinalClose()
|
sl@0
|
366 |
{
|
sl@0
|
367 |
return KErrNone;
|
sl@0
|
368 |
}
|
sl@0
|
369 |
|
sl@0
|
370 |
|
sl@0
|
371 |
// Simple implementation of File handling
|
sl@0
|
372 |
|
sl@0
|
373 |
static int MapMode(int aMode, TUint& fMode)
|
sl@0
|
374 |
{
|
sl@0
|
375 |
// EPOC32 doesn't support Write-Only
|
sl@0
|
376 |
|
sl@0
|
377 |
if (aMode & (O_WRONLY|O_RDWR))
|
sl@0
|
378 |
{
|
sl@0
|
379 |
fMode = EFileWrite;
|
sl@0
|
380 |
fMode |= (aMode & O_EXCL) ? EFileShareExclusive : EFileShareAny;
|
sl@0
|
381 |
}
|
sl@0
|
382 |
else
|
sl@0
|
383 |
{
|
sl@0
|
384 |
fMode = EFileRead;
|
sl@0
|
385 |
fMode |= (aMode & O_EXCL) ? EFileShareExclusive : EFileShareReadersOnly;
|
sl@0
|
386 |
}
|
sl@0
|
387 |
|
sl@0
|
388 |
fMode |= (aMode & O_TEXT) ? EFileStreamText : EFileStream;
|
sl@0
|
389 |
|
sl@0
|
390 |
return aMode & (O_CREAT|O_TRUNC|O_APPEND|O_EXCL);
|
sl@0
|
391 |
}
|
sl@0
|
392 |
|
sl@0
|
393 |
CFileDesc::CFileDesc()
|
sl@0
|
394 |
:CFileDescBase(), iSize(EBufferSize), iExt(-1)
|
sl@0
|
395 |
{}
|
sl@0
|
396 |
|
sl@0
|
397 |
CFileDesc::~CFileDesc()
|
sl@0
|
398 |
{
|
sl@0
|
399 |
iFile.Close();
|
sl@0
|
400 |
delete [] iBuffer;
|
sl@0
|
401 |
}
|
sl@0
|
402 |
|
sl@0
|
403 |
TInt CFileDesc::FinalClose()
|
sl@0
|
404 |
{
|
sl@0
|
405 |
return DoSync();
|
sl@0
|
406 |
}
|
sl@0
|
407 |
|
sl@0
|
408 |
TInt CFileDesc::Open(RFs& aSession, const TDesC& aName, int mode, int /*perms*/)
|
sl@0
|
409 |
{
|
sl@0
|
410 |
TInt err;
|
sl@0
|
411 |
TUint fMode;
|
sl@0
|
412 |
|
sl@0
|
413 |
iDrive=(TInt16)TDriveUnit(aName);
|
sl@0
|
414 |
|
sl@0
|
415 |
// Create = make new file, can return KErrAlreadyExists
|
sl@0
|
416 |
// Open = open an existing file, can return KErrPathNotFound or KErrNotFound
|
sl@0
|
417 |
// Replace = open a new file, zapping the existing one if necessary
|
sl@0
|
418 |
|
sl@0
|
419 |
int mapped=MapMode(mode, fMode);
|
sl@0
|
420 |
switch (mapped)
|
sl@0
|
421 |
{
|
sl@0
|
422 |
case O_CREAT|O_EXCL:
|
sl@0
|
423 |
err = iFile.Create(aSession, aName, fMode);
|
sl@0
|
424 |
break;
|
sl@0
|
425 |
case O_CREAT|O_TRUNC:
|
sl@0
|
426 |
err = iFile.Replace(aSession, aName, fMode);
|
sl@0
|
427 |
break;
|
sl@0
|
428 |
case O_TRUNC:
|
sl@0
|
429 |
err = iFile.Open(aSession, aName, fMode);
|
sl@0
|
430 |
if (err == KErrPathNotFound)
|
sl@0
|
431 |
{
|
sl@0
|
432 |
// missing directories etc, so fail directly
|
sl@0
|
433 |
}
|
sl@0
|
434 |
else
|
sl@0
|
435 |
{
|
sl@0
|
436 |
iFile.Close();
|
sl@0
|
437 |
err = iFile.Replace(aSession, aName, fMode);
|
sl@0
|
438 |
}
|
sl@0
|
439 |
break;
|
sl@0
|
440 |
|
sl@0
|
441 |
// Everything else is assumed to mean open existing file,
|
sl@0
|
442 |
// If the file isn't there, O_CREAT implies that we should make it
|
sl@0
|
443 |
default:
|
sl@0
|
444 |
err = iFile.Open(aSession, aName, fMode);
|
sl@0
|
445 |
if (err == KErrNotFound && (mapped & O_CREAT))
|
sl@0
|
446 |
err = iFile.Create(aSession, aName, fMode);
|
sl@0
|
447 |
if (err == KErrNone && (mapped & O_APPEND))
|
sl@0
|
448 |
{
|
sl@0
|
449 |
iPos = Ext();
|
sl@0
|
450 |
if (iPos < 0)
|
sl@0
|
451 |
err = iPos;
|
sl@0
|
452 |
}
|
sl@0
|
453 |
break;
|
sl@0
|
454 |
}
|
sl@0
|
455 |
if ((mode & O_BUFFERED) == 0)
|
sl@0
|
456 |
iSize = 0;
|
sl@0
|
457 |
return err;
|
sl@0
|
458 |
}
|
sl@0
|
459 |
|
sl@0
|
460 |
TInt CFileDesc::LSeek (int& offset, int whence)
|
sl@0
|
461 |
{
|
sl@0
|
462 |
|
sl@0
|
463 |
TInt pos=offset;
|
sl@0
|
464 |
TInt ext=Ext();
|
sl@0
|
465 |
if (ext < 0)
|
sl@0
|
466 |
return ext;
|
sl@0
|
467 |
|
sl@0
|
468 |
switch (whence)
|
sl@0
|
469 |
{
|
sl@0
|
470 |
case SEEK_SET:
|
sl@0
|
471 |
break;
|
sl@0
|
472 |
case SEEK_CUR:
|
sl@0
|
473 |
pos += Pos();
|
sl@0
|
474 |
break;
|
sl@0
|
475 |
case SEEK_END:
|
sl@0
|
476 |
pos += ext;
|
sl@0
|
477 |
break;
|
sl@0
|
478 |
default:
|
sl@0
|
479 |
return KErrArgument;
|
sl@0
|
480 |
}
|
sl@0
|
481 |
TInt ret = KErrNone;
|
sl@0
|
482 |
if (pos < 0)
|
sl@0
|
483 |
{
|
sl@0
|
484 |
pos = 0;
|
sl@0
|
485 |
ret = KErrEof;
|
sl@0
|
486 |
}
|
sl@0
|
487 |
else if (pos > ext)
|
sl@0
|
488 |
{
|
sl@0
|
489 |
pos = ext;
|
sl@0
|
490 |
ret = KErrEof;
|
sl@0
|
491 |
}
|
sl@0
|
492 |
|
sl@0
|
493 |
switch (iState)
|
sl@0
|
494 |
{
|
sl@0
|
495 |
case EAlloc:
|
sl@0
|
496 |
iPos = pos;
|
sl@0
|
497 |
break;
|
sl@0
|
498 |
case EReading:
|
sl@0
|
499 |
{
|
sl@0
|
500 |
TInt lag = iPos - pos;
|
sl@0
|
501 |
if (lag >= 0 && lag <= (iEnd - iBuffer))
|
sl@0
|
502 |
iPtr = iEnd - lag;
|
sl@0
|
503 |
else
|
sl@0
|
504 |
{
|
sl@0
|
505 |
iPtr = iEnd;
|
sl@0
|
506 |
iPos = pos;
|
sl@0
|
507 |
}
|
sl@0
|
508 |
}
|
sl@0
|
509 |
break;
|
sl@0
|
510 |
case EWriting:
|
sl@0
|
511 |
if (pos != Pos())
|
sl@0
|
512 |
{
|
sl@0
|
513 |
ret = Flush();
|
sl@0
|
514 |
if (ret == KErrNone)
|
sl@0
|
515 |
iPos = pos;
|
sl@0
|
516 |
}
|
sl@0
|
517 |
break;
|
sl@0
|
518 |
}
|
sl@0
|
519 |
offset = pos;
|
sl@0
|
520 |
return ret;
|
sl@0
|
521 |
}
|
sl@0
|
522 |
|
sl@0
|
523 |
void CFileDesc::MapStat(struct stat& st, const TTime& aModTime, TUint& aAttr)
|
sl@0
|
524 |
{
|
sl@0
|
525 |
st.st_mode = (aAttr&KEntryAttDir) ? S_IFDIR:S_IFREG;
|
sl@0
|
526 |
if ((aAttr&KEntryAttReadOnly)==0)
|
sl@0
|
527 |
st.st_mode |= S_IWUSR;
|
sl@0
|
528 |
st.st_nlink = 1;
|
sl@0
|
529 |
st.st_mtime = as_time_t(aModTime);
|
sl@0
|
530 |
st.st_blksize=512;
|
sl@0
|
531 |
}
|
sl@0
|
532 |
|
sl@0
|
533 |
TInt CFileDesc::FStat (struct stat* st)
|
sl@0
|
534 |
{
|
sl@0
|
535 |
TInt err;
|
sl@0
|
536 |
TUint att;
|
sl@0
|
537 |
TTime modtime;
|
sl@0
|
538 |
|
sl@0
|
539 |
err = iFile.Att(att);
|
sl@0
|
540 |
if (!err)
|
sl@0
|
541 |
{
|
sl@0
|
542 |
err = iFile.Modified(modtime);
|
sl@0
|
543 |
if (!err)
|
sl@0
|
544 |
{
|
sl@0
|
545 |
err=Ext();
|
sl@0
|
546 |
if (err >= 0)
|
sl@0
|
547 |
{
|
sl@0
|
548 |
st->st_size = err;
|
sl@0
|
549 |
st->st_dev = st->st_rdev = iDrive;
|
sl@0
|
550 |
MapStat(*st, modtime, att);
|
sl@0
|
551 |
return 0;
|
sl@0
|
552 |
}
|
sl@0
|
553 |
}
|
sl@0
|
554 |
}
|
sl@0
|
555 |
return err;
|
sl@0
|
556 |
}
|
sl@0
|
557 |
|
sl@0
|
558 |
TInt CFileDesc::Alloc()
|
sl@0
|
559 |
{
|
sl@0
|
560 |
if (iSize)
|
sl@0
|
561 |
{
|
sl@0
|
562 |
iBuffer = new TUint8[iSize];
|
sl@0
|
563 |
if (iBuffer == 0)
|
sl@0
|
564 |
return KErrNoMemory;
|
sl@0
|
565 |
}
|
sl@0
|
566 |
return KErrNone;
|
sl@0
|
567 |
}
|
sl@0
|
568 |
|
sl@0
|
569 |
TInt CFileDesc::FileRead(TUint8* aPtr,TInt aLength)
|
sl@0
|
570 |
{
|
sl@0
|
571 |
TPtr8 ptr(aPtr,aLength);
|
sl@0
|
572 |
TInt r=iFile.Read(iPos,ptr);
|
sl@0
|
573 |
if (r == KErrNone)
|
sl@0
|
574 |
{
|
sl@0
|
575 |
r = ptr.Length();
|
sl@0
|
576 |
iPos += r;
|
sl@0
|
577 |
if (r < aLength)
|
sl@0
|
578 |
iExt = iPos;
|
sl@0
|
579 |
}
|
sl@0
|
580 |
return r;
|
sl@0
|
581 |
}
|
sl@0
|
582 |
|
sl@0
|
583 |
TInt CFileDesc::FileWrite(TUint8* aPtr,TInt aLength)
|
sl@0
|
584 |
{
|
sl@0
|
585 |
TPtrC8 ptr(aPtr,aLength);
|
sl@0
|
586 |
TInt r = iFile.Write(iPos,ptr);
|
sl@0
|
587 |
if (r == KErrNone)
|
sl@0
|
588 |
{
|
sl@0
|
589 |
iPos += aLength;
|
sl@0
|
590 |
if (iPos > iExt && iExt >= 0)
|
sl@0
|
591 |
iExt = iPos;
|
sl@0
|
592 |
}
|
sl@0
|
593 |
return r;
|
sl@0
|
594 |
}
|
sl@0
|
595 |
|
sl@0
|
596 |
TInt CFileDesc::Flush()
|
sl@0
|
597 |
{
|
sl@0
|
598 |
if (iPtr > iBuffer)
|
sl@0
|
599 |
{
|
sl@0
|
600 |
TInt r = FileWrite(iBuffer, iPtr-iBuffer);
|
sl@0
|
601 |
if (r < 0)
|
sl@0
|
602 |
return r;
|
sl@0
|
603 |
iPtr = iBuffer;
|
sl@0
|
604 |
}
|
sl@0
|
605 |
return KErrNone;
|
sl@0
|
606 |
}
|
sl@0
|
607 |
|
sl@0
|
608 |
TInt CFileDesc::DoRead (TDes8& aDesc)
|
sl@0
|
609 |
{
|
sl@0
|
610 |
if (iState != EReading)
|
sl@0
|
611 |
{
|
sl@0
|
612 |
TInt ret = (iState == EAlloc) ? Alloc() : Flush();
|
sl@0
|
613 |
if (ret != KErrNone)
|
sl@0
|
614 |
return ret;
|
sl@0
|
615 |
iState = EReading;
|
sl@0
|
616 |
iPtr = iEnd = iBuffer;
|
sl@0
|
617 |
}
|
sl@0
|
618 |
|
sl@0
|
619 |
TUint8* p = (TUint8*) aDesc.Ptr();
|
sl@0
|
620 |
TInt max = aDesc.MaxLength();
|
sl@0
|
621 |
TInt avail = iEnd - iPtr;
|
sl@0
|
622 |
TInt len = Min(max, avail);
|
sl@0
|
623 |
if (len > 0)
|
sl@0
|
624 |
{
|
sl@0
|
625 |
p = Mem::Copy(p, iPtr, len);
|
sl@0
|
626 |
iPtr += len;
|
sl@0
|
627 |
max -= len;
|
sl@0
|
628 |
}
|
sl@0
|
629 |
if (max >= iSize)
|
sl@0
|
630 |
{
|
sl@0
|
631 |
TInt ret = FileRead(p, max);
|
sl@0
|
632 |
if (ret < 0)
|
sl@0
|
633 |
return ret;
|
sl@0
|
634 |
p += ret;
|
sl@0
|
635 |
}
|
sl@0
|
636 |
else if (max > 0)
|
sl@0
|
637 |
{
|
sl@0
|
638 |
TInt ret = FileRead(iBuffer, Min(max + EReadAhead, iSize));
|
sl@0
|
639 |
if (ret < 0)
|
sl@0
|
640 |
return ret;
|
sl@0
|
641 |
len = Min(max, ret);
|
sl@0
|
642 |
p = Mem::Copy(p, iBuffer, len);
|
sl@0
|
643 |
iPtr = iBuffer + len;
|
sl@0
|
644 |
iEnd = iBuffer + ret;
|
sl@0
|
645 |
}
|
sl@0
|
646 |
aDesc.SetLength(p-aDesc.Ptr());
|
sl@0
|
647 |
return KErrNone;
|
sl@0
|
648 |
}
|
sl@0
|
649 |
|
sl@0
|
650 |
void CFileDesc::Read (TDes8& aDesc, TRequestStatus& aStatus)
|
sl@0
|
651 |
{
|
sl@0
|
652 |
Complete(aStatus,DoRead(aDesc));
|
sl@0
|
653 |
}
|
sl@0
|
654 |
|
sl@0
|
655 |
TInt CFileDesc::DoWrite (TDes8& aDesc)
|
sl@0
|
656 |
{
|
sl@0
|
657 |
if (iState != EWriting)
|
sl@0
|
658 |
{
|
sl@0
|
659 |
if (iState == EAlloc)
|
sl@0
|
660 |
{
|
sl@0
|
661 |
TInt ret = Alloc();
|
sl@0
|
662 |
if (ret != KErrNone)
|
sl@0
|
663 |
return ret;
|
sl@0
|
664 |
}
|
sl@0
|
665 |
else
|
sl@0
|
666 |
iPos -= iEnd - iPtr;
|
sl@0
|
667 |
|
sl@0
|
668 |
iState = EWriting;
|
sl@0
|
669 |
iPtr = iBuffer;
|
sl@0
|
670 |
iEnd = iBuffer + iSize;
|
sl@0
|
671 |
}
|
sl@0
|
672 |
|
sl@0
|
673 |
TUint8* p = (TUint8*) aDesc.Ptr();
|
sl@0
|
674 |
TInt max = aDesc.Length();
|
sl@0
|
675 |
TInt avail = iEnd - iPtr;
|
sl@0
|
676 |
TInt len = Min(max, avail);
|
sl@0
|
677 |
if (len > 0)
|
sl@0
|
678 |
{
|
sl@0
|
679 |
iPtr = Mem::Copy(iPtr, p, len);
|
sl@0
|
680 |
p += len;
|
sl@0
|
681 |
max -= len;
|
sl@0
|
682 |
}
|
sl@0
|
683 |
if (max == 0)
|
sl@0
|
684 |
return KErrNone;
|
sl@0
|
685 |
TInt r=Flush();
|
sl@0
|
686 |
if (r < 0)
|
sl@0
|
687 |
return r;
|
sl@0
|
688 |
if (max >= iSize)
|
sl@0
|
689 |
return FileWrite(p, max);
|
sl@0
|
690 |
iPtr = Mem::Copy(iPtr, p, max);
|
sl@0
|
691 |
return KErrNone;
|
sl@0
|
692 |
}
|
sl@0
|
693 |
|
sl@0
|
694 |
void CFileDesc::Write(TDes8& aDesc, TRequestStatus& aStatus)
|
sl@0
|
695 |
{
|
sl@0
|
696 |
Complete(aStatus,DoWrite(aDesc));
|
sl@0
|
697 |
}
|
sl@0
|
698 |
|
sl@0
|
699 |
TInt CFileDesc::DoSync()
|
sl@0
|
700 |
{
|
sl@0
|
701 |
if (iState == EWriting)
|
sl@0
|
702 |
{
|
sl@0
|
703 |
TInt ret = Flush();
|
sl@0
|
704 |
if (ret < 0)
|
sl@0
|
705 |
return ret;
|
sl@0
|
706 |
}
|
sl@0
|
707 |
return iFile.Flush();
|
sl@0
|
708 |
}
|
sl@0
|
709 |
|
sl@0
|
710 |
void CFileDesc::Sync(TRequestStatus& aStatus)
|
sl@0
|
711 |
{
|
sl@0
|
712 |
Complete(aStatus,DoSync());
|
sl@0
|
713 |
}
|
sl@0
|
714 |
|
sl@0
|
715 |
TInt CFileDesc::Pos()
|
sl@0
|
716 |
{
|
sl@0
|
717 |
TInt pos = iPos;
|
sl@0
|
718 |
if (iState == EReading)
|
sl@0
|
719 |
pos -= (iEnd - iPtr);
|
sl@0
|
720 |
else if (iState == EWriting)
|
sl@0
|
721 |
pos += (iPtr - iBuffer);
|
sl@0
|
722 |
return pos;
|
sl@0
|
723 |
}
|
sl@0
|
724 |
|
sl@0
|
725 |
TInt CFileDesc::Ext()
|
sl@0
|
726 |
{
|
sl@0
|
727 |
if (iExt < 0)
|
sl@0
|
728 |
{
|
sl@0
|
729 |
TInt r = iFile.Size(iExt);
|
sl@0
|
730 |
if (r < 0)
|
sl@0
|
731 |
return r;
|
sl@0
|
732 |
}
|
sl@0
|
733 |
return Max(iExt, Pos());
|
sl@0
|
734 |
}
|
sl@0
|
735 |
|
sl@0
|
736 |
TInt CFileDesc::IoctlCompletion(int aCmd, void* aParam, TInt aStatus)
|
sl@0
|
737 |
{
|
sl@0
|
738 |
TInt ret=aStatus;
|
sl@0
|
739 |
if (ret!=KErrNone)
|
sl@0
|
740 |
return ret;
|
sl@0
|
741 |
// some useful sums about the current state of the file
|
sl@0
|
742 |
TInt curoff = Pos();
|
sl@0
|
743 |
TInt size = Ext();
|
sl@0
|
744 |
if (size < 0)
|
sl@0
|
745 |
ret = size;
|
sl@0
|
746 |
int *param=REINTERPRET_CAST(int*,aParam);
|
sl@0
|
747 |
switch (aCmd)
|
sl@0
|
748 |
{
|
sl@0
|
749 |
case E32IONREAD:
|
sl@0
|
750 |
if (ret==KErrNone)
|
sl@0
|
751 |
*param=(size-curoff);
|
sl@0
|
752 |
break;
|
sl@0
|
753 |
case E32IOSELECT:
|
sl@0
|
754 |
{
|
sl@0
|
755 |
int mask=E32SELECT_WRITE;
|
sl@0
|
756 |
if ((size-curoff)>0)
|
sl@0
|
757 |
mask |= E32SELECT_READ;
|
sl@0
|
758 |
*param=(*param)&mask; // but don't block
|
sl@0
|
759 |
}
|
sl@0
|
760 |
break;
|
sl@0
|
761 |
default:
|
sl@0
|
762 |
ret=KErrNotSupported;
|
sl@0
|
763 |
break;
|
sl@0
|
764 |
}
|
sl@0
|
765 |
return ret;
|
sl@0
|
766 |
}
|
sl@0
|
767 |
|
sl@0
|
768 |
// Extra support for temporary files
|
sl@0
|
769 |
|
sl@0
|
770 |
TInt CTempFileDesc::Open(RFs& aSession, const TDesC& aPath)
|
sl@0
|
771 |
{
|
sl@0
|
772 |
iSession=aSession;
|
sl@0
|
773 |
iDrive=(TInt16)TDriveUnit(aPath);
|
sl@0
|
774 |
TInt err=iFile.Temp(aSession, aPath, iName, EFileShareAny);
|
sl@0
|
775 |
return err;
|
sl@0
|
776 |
}
|
sl@0
|
777 |
|
sl@0
|
778 |
TInt CTempFileDesc::FinalClose()
|
sl@0
|
779 |
{
|
sl@0
|
780 |
iFile.Close();
|
sl@0
|
781 |
TInt err=iSession.Delete(iName);
|
sl@0
|
782 |
return err;
|
sl@0
|
783 |
}
|