|
1 /* |
|
2 * Copyright (c) 1997-2009 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <string.h> |
|
20 #include <stdio.h> |
|
21 #include <fcntl.h> // for struct stat |
|
22 #include <wchar.h> |
|
23 #include <sys/errno.h> // for ENOTSOCK |
|
24 #include <sys/ioctl.h> |
|
25 #include <c32comm.h> |
|
26 #include <sys/stat.h> |
|
27 #include <limits.h> //for LLONG_MAX and INT_MAX |
|
28 #ifdef SYMBIAN_OE_LIBRT |
|
29 #include <sys/shm.h> |
|
30 #endif// SYMBIAN_OE_LIBRT |
|
31 #include "sysreent.h" |
|
32 #include "systemspecialfilercg.h" |
|
33 #include "fdesc.h" |
|
34 #include "lposix.h" |
|
35 #include "ltime.h" |
|
36 |
|
37 |
|
38 |
|
39 /* |
|
40 Temp directory, this should be checked later |
|
41 */ |
|
42 #ifdef WIDEP_tmpdir |
|
43 #undef WIDEP_tmpdir |
|
44 #define WIDEP_tmpdir L"/System/temp/" |
|
45 #endif //WIDEP_tmpdir |
|
46 |
|
47 |
|
48 |
|
49 CFileDescBase::CFileDescBase( TUint aFcntl, TUint32 aFdAttrib ) : |
|
50 iReadTimeout(-1), iFcntlFlag(aFcntl), iFdAttrib(aFdAttrib), iPollErr(KErrNone) |
|
51 { |
|
52 } |
|
53 TInt CFileDescBase::SetAtt(TUint /*aSetAttMask*/, TUint /*aClearAttMask*/) |
|
54 { |
|
55 return 0; |
|
56 } |
|
57 |
|
58 // A CFileDescBase factory function, for "named" file-like objects |
|
59 CFileDescBase* CFileDescBase::Open(const wchar_t* name, int mode, int perms, TInt& err) |
|
60 { |
|
61 err = KErrNone; |
|
62 CFileDescBase* ret = NULL; |
|
63 |
|
64 if(WcsCmp(name, L"CON:") == 0) |
|
65 { |
|
66 ret = new CTtyDesc; // NB. This won't be the default stdin/stdout/stderr console |
|
67 } |
|
68 else if(WcsCmp(name, L"NUL:") == 0) |
|
69 { |
|
70 ret = new CFileDescBase; |
|
71 } |
|
72 else if((WcsCmp(name, L"TMP:") == 0) || (mode & O_TMPFILE)) |
|
73 { |
|
74 CTempFileDesc* tmp = new CTempFileDesc; |
|
75 if(tmp != NULL) |
|
76 { |
|
77 err = tmp->Open(name, mode); |
|
78 if(err != KErrNone) |
|
79 { |
|
80 delete tmp; |
|
81 tmp = NULL; |
|
82 } |
|
83 } |
|
84 ret = tmp; |
|
85 } |
|
86 #ifdef SYMBIAN_OE_LIBRT |
|
87 else if(mode & O_SHMFLG) |
|
88 { |
|
89 CSharedMemDesc* shm= new CSharedMemDesc; |
|
90 if(shm != NULL) |
|
91 { |
|
92 err = shm->Open(name, mode, perms); |
|
93 if(err != KErrNone) |
|
94 { |
|
95 delete shm; |
|
96 shm = NULL; |
|
97 } |
|
98 } |
|
99 ret = shm; |
|
100 } |
|
101 #endif //SYMBIAN_OE_LIBRT |
|
102 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')) || |
|
103 (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'))) |
|
104 { |
|
105 CSerialDesc* com = new CSerialDesc; |
|
106 if (com != NULL) |
|
107 { |
|
108 err = com->Open(name, mode, perms); |
|
109 if(err != KErrNone) |
|
110 { |
|
111 delete com; |
|
112 com = NULL; |
|
113 } |
|
114 } |
|
115 ret = com; |
|
116 } |
|
117 else //It's a special file, directory or normal file |
|
118 { |
|
119 RFs& rfs = Backend()->FileSession(); |
|
120 TUint attribval; |
|
121 TFullName fullName; |
|
122 err = GetFullFile(fullName, (const TText16*)name, rfs); |
|
123 if(err != KErrNone) |
|
124 return NULL; |
|
125 int ret1 = rfs.Att(fullName, attribval); |
|
126 if (ret1 == 0 && ((attribval & (KEntryAttHidden | KEntryAttSystem))== (KEntryAttHidden | KEntryAttSystem))) |
|
127 { |
|
128 TSpecialFileType fileType = _SystemSpecialFileBasedFilePath(name, err, rfs); |
|
129 if(fileType == EFileTypeMkFifo) //special file, FIFO |
|
130 { |
|
131 ret = CFileTable::FifoOpen(name, mode, perms, err); |
|
132 } |
|
133 else if(fileType == EFileTypeSymLink) //special file, symlink |
|
134 { |
|
135 ret = CFileTable::LinkOpen(name, mode, perms, err, rfs); |
|
136 } |
|
137 } |
|
138 else //normal file or directory |
|
139 { |
|
140 //Try opening as a file |
|
141 CFileDesc* file = new CFileDesc; |
|
142 if(file != NULL) |
|
143 { |
|
144 err = file->Open(rfs, fullName, mode, perms); |
|
145 if(err != KErrNone) |
|
146 { |
|
147 delete file; |
|
148 file = NULL; |
|
149 } |
|
150 } |
|
151 ret = file; |
|
152 |
|
153 //Check if it is directory, if the open as file failed |
|
154 if(err != KErrNone && CheckIfDirectory(fullName, rfs)) |
|
155 { |
|
156 CDirectoryDesc* dir = new CDirectoryDesc; |
|
157 if(dir != NULL) |
|
158 { |
|
159 err = dir->Open(rfs, fullName, mode, perms); |
|
160 if(err != KErrNone) |
|
161 { |
|
162 delete dir; |
|
163 dir = NULL; |
|
164 } |
|
165 } |
|
166 ret = dir; |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 if(err == KErrNone && ret == NULL) |
|
172 { |
|
173 err = KErrNoMemory; |
|
174 } |
|
175 |
|
176 return ret; |
|
177 } |
|
178 |
|
179 //Check if a path corresponds to a directory. This is a helper function |
|
180 //for CFileDescBase::Open() |
|
181 TBool CFileDescBase::CheckIfDirectory(const TDesC& aPath, RFs& aFs) |
|
182 { |
|
183 //Check whether its a Drive Name |
|
184 if(KMaxDriveName+1 == aPath.Length()) |
|
185 { |
|
186 if(RFs::IsValidDrive(TDriveUnit(aPath))) |
|
187 { |
|
188 return ETrue; |
|
189 } |
|
190 } |
|
191 //Now check whether it is a directory |
|
192 else |
|
193 { |
|
194 TEntry entry; |
|
195 if(aFs.Entry(aPath, entry) == KErrNone) |
|
196 { |
|
197 if(entry.IsDir()) |
|
198 { |
|
199 return ETrue; |
|
200 } |
|
201 } |
|
202 } |
|
203 |
|
204 return EFalse; |
|
205 } |
|
206 |
|
207 // Useful default implementations for CFileDescBase virtual functions. |
|
208 // Function to be re-implemented in a child class which can seek. |
|
209 TInt CFileDescBase::LSeek (off_t& offset, int) |
|
210 { |
|
211 // minimal implementation for devices which can't seek |
|
212 offset=-1; |
|
213 return ESPIPE; |
|
214 } |
|
215 |
|
216 void CFileDescBase::Read (TDes8& aBuf, TRequestStatus& aStatus) |
|
217 { |
|
218 // minimal implementation for /dev/null |
|
219 aBuf.Zero(); // set length to zero |
|
220 TRequestStatus* sp=&aStatus; |
|
221 User::RequestComplete(sp,KErrNone); |
|
222 } |
|
223 |
|
224 void CFileDescBase::ReadCancel() {} |
|
225 |
|
226 TInt CFileDescBase::ReadCompletion (TDes8& /*aBuf*/, TInt aStatus) |
|
227 { |
|
228 return aStatus; |
|
229 } |
|
230 |
|
231 |
|
232 TInt CFileDescBase::FStat (struct stat *st) |
|
233 { |
|
234 // minimal implementation: |
|
235 // I am a character device about which little is known |
|
236 st->st_mode = S_IFCHR; |
|
237 st->st_blksize=0; |
|
238 return KErrNone; |
|
239 } |
|
240 |
|
241 void CFileDescBase::Complete (TRequestStatus& aStatus, TInt aResult) |
|
242 { |
|
243 TRequestStatus* sp=&aStatus; |
|
244 User::RequestComplete(sp,aResult); |
|
245 } |
|
246 |
|
247 void CFileDescBase::Write (TDes8& /*aBuf*/, TRequestStatus& aStatus) |
|
248 { |
|
249 // minimal implementation for /dev/null |
|
250 // we will claim to have written all of the data |
|
251 Complete(aStatus,KErrNone); |
|
252 } |
|
253 |
|
254 void CFileDescBase::WriteCancel() {} |
|
255 |
|
256 TInt CFileDescBase::WriteCompletion (TDes8& /*aBuf*/, TInt aStatus) |
|
257 { |
|
258 return aStatus; |
|
259 } |
|
260 |
|
261 void CFileDescBase::Sync(TRequestStatus& aStatus) |
|
262 { |
|
263 // minimal implementation for /dev/null |
|
264 Complete(aStatus,KErrNone); |
|
265 } |
|
266 |
|
267 void CFileDescBase::SyncCancel() {} |
|
268 |
|
269 void CFileDescBase::Ioctl(int /*aCmd*/, void* /*aParam*/, TRequestStatus& aStatus) |
|
270 { |
|
271 // minimal implementation for /dev/null and other synchronous devices |
|
272 Complete(aStatus,KErrNone); |
|
273 } |
|
274 |
|
275 void CFileDescBase::IoctlCancel() |
|
276 { |
|
277 return; // suitable for all synchronous ioctls |
|
278 } |
|
279 |
|
280 TInt CFileDescBase::IoctlCompletion(int aCmd, void* aParam, TInt aStatus) |
|
281 { |
|
282 TInt ret=aStatus; |
|
283 if (ret!=KErrNone) |
|
284 return ret; |
|
285 int *param = reinterpret_cast<int*> (aParam); |
|
286 switch ((unsigned)aCmd) |
|
287 { |
|
288 case FIONREAD: |
|
289 case E32IONREAD: |
|
290 *param=0; // claim that no data is available |
|
291 break; |
|
292 case E32IOSELECT: |
|
293 *param=(*param)&(E32SELECT_READ|E32SELECT_WRITE); // but don't block |
|
294 break; |
|
295 default: |
|
296 ret=KErrNotSupported; |
|
297 break; |
|
298 } |
|
299 return ret; |
|
300 } |
|
301 |
|
302 // A CFileDescBase factory function, for socket objects |
|
303 |
|
304 CFileDescBase* CFileDescBase::Socket(RSocketServ& aSs, int family, int style, int protocol, TInt& err) |
|
305 { |
|
306 // connect to the Socket Server if necessary |
|
307 if (aSs.Handle()==0) |
|
308 { |
|
309 err=aSs.Connect(TUint(-1)); // allow arbitrary number of requests |
|
310 if (err) |
|
311 return 0; |
|
312 else |
|
313 { |
|
314 err = aSs.ShareAuto(); |
|
315 if (err) |
|
316 return 0; |
|
317 } |
|
318 } |
|
319 CSockDescBase *socketBase = NULL; |
|
320 if (family == AF_LOCAL || family == PF_LOCAL || family == AF_UNIX) |
|
321 { |
|
322 |
|
323 //coverity[alloc_fn] |
|
324 //coverity[assign] |
|
325 |
|
326 socketBase = new CFileSocketDesc; |
|
327 } |
|
328 else |
|
329 { |
|
330 socketBase = new CSocketDesc; |
|
331 } |
|
332 |
|
333 if (!socketBase) |
|
334 { |
|
335 err=KErrNoMemory; |
|
336 return 0; |
|
337 } |
|
338 err = socketBase->Socket(aSs,family,style,protocol); |
|
339 |
|
340 if (err) |
|
341 { |
|
342 delete socketBase; |
|
343 if (err == KErrBadName) // Some mismatch in family-style-protocol |
|
344 { |
|
345 err = EPROTONOSUPPORT; |
|
346 } |
|
347 //coverity[memory_leak] |
|
348 return 0; |
|
349 } |
|
350 return socketBase; |
|
351 } |
|
352 |
|
353 // minimal implementation of sockets, useful for all non-socket descriptors |
|
354 |
|
355 void CFileDescBase::RecvFrom(TDes8& /*aDesc*/, TSockAddr& /*from*/, int /*flags*/, TRequestStatus& aStatus) |
|
356 { |
|
357 // minimal implementation |
|
358 Complete(aStatus,ENOTSOCK); |
|
359 } |
|
360 |
|
361 void CFileDescBase::RecvFromCancel() {} |
|
362 |
|
363 TInt CFileDescBase::CompletionStatus (TInt& /*aLength*/, TInt aStatus) |
|
364 { |
|
365 return aStatus; |
|
366 } |
|
367 |
|
368 void CFileDescBase::SendTo(TDes8& /*aDesc*/, const struct sockaddr* /* anAddr*/, unsigned long /*aAddrLen*/, int /*flags*/, TRequestStatus& aStatus) |
|
369 { |
|
370 // minimal implementation |
|
371 Complete(aStatus,ENOTSOCK); |
|
372 } |
|
373 |
|
374 void CFileDescBase::SendToCancel() {} |
|
375 |
|
376 void CFileDescBase::Shutdown(TUint /*aHow*/,TRequestStatus& aStatus) |
|
377 { |
|
378 // minimal implementation |
|
379 Complete(aStatus,ENOTSOCK); |
|
380 } |
|
381 |
|
382 void CFileDescBase::ShutdownCancel() {} |
|
383 |
|
384 TInt CFileDescBase::Bind(const struct sockaddr* /*addr*/, unsigned long /*size*/) |
|
385 { |
|
386 return ENOTSOCK; |
|
387 } |
|
388 |
|
389 TInt CFileDescBase::Listen(TUint /*qSize*/) |
|
390 { |
|
391 return ENOTSOCK; |
|
392 } |
|
393 |
|
394 void CFileDescBase::Accept(CFileDescBase*& /*aNewSocket*/, TRequestStatus& aStatus, RSocketServ& /*aSs*/, TSockAddr * /*aAddr*/) |
|
395 { |
|
396 // minimal implementation |
|
397 Complete(aStatus,ENOTSOCK); |
|
398 } |
|
399 |
|
400 void CFileDescBase::AcceptCancel() {} |
|
401 |
|
402 void CFileDescBase::Connect(const struct sockaddr* /*anAddr*/,unsigned long /*aSize*/,TRequestStatus& aStatus) |
|
403 { |
|
404 // minimal implementation |
|
405 Complete(aStatus,ENOTSOCK); |
|
406 User::WaitForRequest(aStatus); |
|
407 } |
|
408 |
|
409 void CFileDescBase::ConnectCancel() {} |
|
410 |
|
411 TBool CFileDescBase::GetConnectionProgress() |
|
412 { |
|
413 return EFalse; |
|
414 } |
|
415 |
|
416 void CFileDescBase::SetConnectionProgress( TBool /* aInProgress */ ) {} |
|
417 |
|
418 TInt CFileDescBase::SockName(int /*anEnd*/, struct sockaddr* /*anAddr*/,unsigned long* /*aSize*/) |
|
419 { |
|
420 return ENOTSOCK; |
|
421 } |
|
422 |
|
423 TInt CFileDescBase::GetSockOpt(TInt /*anOptionName*/,TInt /*anOptionLevel*/,TDes8& /*anOption*/) |
|
424 { |
|
425 return ENOTSOCK; |
|
426 } |
|
427 |
|
428 TInt CFileDescBase::SetSockOpt(TInt /*anOptionName*/,TInt /*anOptionLevel*/,TDesC8& /*anOption*/) |
|
429 { |
|
430 return ENOTSOCK; |
|
431 } |
|
432 |
|
433 // ----------------------------------------------------------------------------- |
|
434 // CFileDescBase::Fcntl |
|
435 // Minimal Implementation for fcntl |
|
436 // ----------------------------------------------------------------------------- |
|
437 // |
|
438 TInt CFileDescBase::Fcntl(TUint /*anArg*/, TUint aCmd) |
|
439 { |
|
440 // minimal implementation: |
|
441 if(aCmd == F_GETFL) |
|
442 { |
|
443 return KErrNotFound; |
|
444 } |
|
445 return KErrNotSupported; |
|
446 } |
|
447 |
|
448 // ----------------------------------------------------------------------------- |
|
449 // CFileDescBase::Poll |
|
450 // Checks the file for ready to write/read and exception status |
|
451 // ----------------------------------------------------------------------------- |
|
452 // |
|
453 TInt CFileDescBase::Poll(TPollMode /*aMode*/,TBool& aStatus,TInt& /*aErrno*/) |
|
454 { |
|
455 // always return True, let the derived filedescriptors override |
|
456 aStatus = ETrue; |
|
457 return 0; |
|
458 } |
|
459 |
|
460 // ----------------------------------------------------------------------------- |
|
461 // CFileDescBase::Poll - Overloaded |
|
462 // Synchronous non-blocking call that returns current state of the file descriptor |
|
463 // ----------------------------------------------------------------------------- |
|
464 // |
|
465 TInt CFileDescBase::Poll(TUint aEvents) |
|
466 { |
|
467 // We return that the file descriptor is select true for ready to read, |
|
468 // ready to write and error conditions. |
|
469 // Let derived file descriptors override as approp. |
|
470 TInt readyEvents = 0; |
|
471 |
|
472 if (aEvents & EReadyForReading) |
|
473 { |
|
474 readyEvents = EReadyForReading; |
|
475 } |
|
476 if (aEvents & EReadyForWriting) |
|
477 { |
|
478 readyEvents |= EReadyForWriting; |
|
479 } |
|
480 if (aEvents & EAnyException) |
|
481 { |
|
482 readyEvents |= EAnyException; |
|
483 } |
|
484 |
|
485 return readyEvents; |
|
486 } |
|
487 |
|
488 // ----------------------------------------------------------------------------- |
|
489 // CFileDescBase::NotifyActivity |
|
490 // Registers with the approp. underlying subsystem for read/write/except notifications |
|
491 // ----------------------------------------------------------------------------- |
|
492 // |
|
493 TInt CFileDescBase::NotifyActivity(TUint /*aEvents*/, TRequestStatus& /*aRequest*/, TTimeIntervalMicroSeconds32 /*timeout*/) |
|
494 { |
|
495 // We return KErrCompletion indicating that this activity is complete for |
|
496 // this descriptor. |
|
497 // Let derived file descriptors override as approp. |
|
498 return KErrCompletion; |
|
499 } |
|
500 |
|
501 // ----------------------------------------------------------------------------- |
|
502 // CFileDescBase::TweakWatchedEvents |
|
503 // Requests for fd behaviour specific additional events |
|
504 // ----------------------------------------------------------------------------- |
|
505 // |
|
506 void CFileDescBase::TweakWatchedEvents(TUint& /*events*/) |
|
507 { |
|
508 // No implementation required |
|
509 // This version is used for both Redir and pipe desc, it is overridden |
|
510 // by socket |
|
511 } |
|
512 |
|
513 // ----------------------------------------------------------------------------- |
|
514 // CFileDescBase::TweakReadyEvents |
|
515 // Prepares the fd behaviours specific output events |
|
516 // ----------------------------------------------------------------------------- |
|
517 // |
|
518 TInt CFileDescBase::TweakReadyEvents(TInt /*errval*/) |
|
519 { |
|
520 return KErrNone; |
|
521 } |
|
522 |
|
523 // CFileDescBase::CancelNotify |
|
524 // Cancel an outstanding request for read/write/except notifications |
|
525 // ----------------------------------------------------------------------------- |
|
526 // |
|
527 void CFileDescBase::CancelNotify() |
|
528 { |
|
529 // do nothing |
|
530 return; |
|
531 } |
|
532 |
|
533 |
|
534 // Generic (non-virtual) handling for Close |
|
535 EXPORT_C TInt CFileDescBase::Close() |
|
536 { |
|
537 TInt err = KErrNone; |
|
538 //Decrement the dup count and mark this FD as invalid if dupcount is 0 |
|
539 if ( --iDupCount < 0 ) |
|
540 { |
|
541 iFdAttrib |= KInvalidFd; |
|
542 //delete this FD only if its not mmaped and being used |
|
543 if(!(iFdAttrib & KMMapedFd)) |
|
544 { |
|
545 err=FinalClose(); |
|
546 //Release the Lock now |
|
547 delete this; |
|
548 } |
|
549 } |
|
550 return err; |
|
551 } |
|
552 |
|
553 TInt CFileDescBase::FinalClose() |
|
554 { |
|
555 return KErrNone; |
|
556 } |
|
557 |
|
558 void CFileDescBase::SetFids(void *) |
|
559 { |
|
560 // nada (virtual) |
|
561 } |
|
562 |
|
563 // Simple implementation of File handling |
|
564 static int MapMode(int aMode, TUint& fMode) |
|
565 { |
|
566 // EPOC32 doesn't support Write-Only |
|
567 if (aMode & (O_WRONLY|O_RDWR)) |
|
568 { |
|
569 fMode = EFileWrite; |
|
570 } |
|
571 else |
|
572 { |
|
573 fMode = EFileRead; |
|
574 } |
|
575 |
|
576 fMode |= (aMode & O_TEXT) ? EFileStreamText : EFileStream; |
|
577 fMode |= EFileShareReadersOrWriters; |
|
578 |
|
579 return aMode & (O_CREAT|O_TRUNC|O_APPEND|O_EXCL|O_EXLOCK); |
|
580 } |
|
581 |
|
582 CFileDesc::CFileDesc() |
|
583 :CFileDescBase(), iSize(EBufferSize), iExt(-1) |
|
584 {} |
|
585 |
|
586 void CFileDesc::SetState(const TDes& params) |
|
587 { |
|
588 TFileName name; |
|
589 iFile.FullName(name); |
|
590 iDrive = (TInt16)TDriveUnit(name); |
|
591 |
|
592 TLex lexr(params); |
|
593 lexr.Val(iPos); |
|
594 lexr.Inc(); |
|
595 lexr.Val(iExt); |
|
596 lexr.Inc(); |
|
597 lexr.Val(iFcntlFlag); |
|
598 lexr.Inc(); |
|
599 lexr.Val(iFdAttrib, EDecimal); |
|
600 lexr.Inc(); |
|
601 lexr.Val(iPos); |
|
602 lexr.Inc(); |
|
603 lexr.Val(iExt); |
|
604 lexr.Inc(); |
|
605 lexr.Val(iSize); |
|
606 |
|
607 } |
|
608 |
|
609 CFileDesc::~CFileDesc() |
|
610 { |
|
611 iFile.Close(); |
|
612 if(iBuffer) |
|
613 { |
|
614 RHeap* oldHeap = User::SwitchHeap(Backend()->Heap()); |
|
615 delete [] iBuffer; |
|
616 User::SwitchHeap(oldHeap); |
|
617 } |
|
618 } |
|
619 |
|
620 TInt CFileDesc::FinalClose() |
|
621 { |
|
622 TInt err = DoSync(); |
|
623 iLock.Close(); |
|
624 return err; |
|
625 } |
|
626 |
|
627 TInt CFileDesc::Open(RFs& aSession, const TDesC& aName, int mode, int perms) |
|
628 { |
|
629 TInt err; |
|
630 TUint fMode; |
|
631 |
|
632 iDrive=(TInt16)TDriveUnit(aName); |
|
633 |
|
634 // Create = make new file, can return KErrAlreadyExists |
|
635 // Open = open an existing file, can return KErrPathNotFound or KErrNotFound |
|
636 // Replace = open a new file, zapping the existing one if necessary |
|
637 |
|
638 int mapped=MapMode(mode, fMode); |
|
639 // if the file is in \sys, use EFileShareReadersOnly not EFileShareReadersOrWriters |
|
640 TParsePtrC pars(aName); |
|
641 if (pars.Path().FindF(_L("\\SYS\\")) == 0) |
|
642 { |
|
643 fMode &= ~EFileShareReadersOrWriters; |
|
644 fMode |= EFileShareReadersOnly; |
|
645 } |
|
646 |
|
647 // if the file is in \resource, dont use EFileShareReadersOrWriters |
|
648 if (pars.Path().FindF(_L("\\resource\\")) == 0) |
|
649 { |
|
650 fMode &= ~EFileShareReadersOrWriters; |
|
651 fMode |= EFileShareReadersOnly; |
|
652 } |
|
653 |
|
654 //Set iFcntlFlag flag |
|
655 iFcntlFlag = mode & O_ACCMODE; |
|
656 |
|
657 if(mode & O_APPEND) |
|
658 { |
|
659 iFcntlFlag |= O_APPEND; |
|
660 } |
|
661 |
|
662 TInt fileMode = 0; |
|
663 TBool created = EFalse; |
|
664 //store the open modes in the file descriptor object |
|
665 |
|
666 switch(perms & (S_IRUSR | S_IWUSR )) |
|
667 { |
|
668 case S_IRUSR | S_IWUSR: //owner read write |
|
669 case S_IWUSR : //owner write |
|
670 { |
|
671 break; |
|
672 } |
|
673 case S_IRUSR: //owner read |
|
674 { |
|
675 fileMode = KEntryAttReadOnly; |
|
676 break; |
|
677 } |
|
678 default: |
|
679 { |
|
680 break; |
|
681 } |
|
682 } |
|
683 |
|
684 switch (mapped) |
|
685 { |
|
686 case O_CREAT|O_EXCL: //For O_EXCL if file exists then return error |
|
687 err = iFile.Create(aSession, aName, fMode); |
|
688 if(err) |
|
689 { |
|
690 return err; |
|
691 } |
|
692 created = ETrue ; |
|
693 break; |
|
694 case O_CREAT|O_TRUNC: |
|
695 err = iFile.Replace(aSession, aName, fMode); |
|
696 if(!err) |
|
697 { |
|
698 created = ETrue; |
|
699 } |
|
700 break; |
|
701 case O_TRUNC: |
|
702 err = iFile.Open(aSession, aName, fMode); |
|
703 if(!err) |
|
704 err = iFile.SetSize(0); |
|
705 break; |
|
706 case O_CREAT|O_EXCL|O_APPEND: //if file exists then return error |
|
707 err = iFile.Create(aSession, aName, fMode); |
|
708 if(err) |
|
709 { |
|
710 return err; |
|
711 } |
|
712 created = ETrue ; |
|
713 break; |
|
714 case O_CREAT|O_EXCL|O_TRUNC: //if file exists then return error |
|
715 err = iFile.Create(aSession, aName, fMode); |
|
716 if(!err) |
|
717 { |
|
718 err = iFile.SetSize(0); |
|
719 created = ETrue ; |
|
720 } |
|
721 else |
|
722 { |
|
723 return err; |
|
724 } |
|
725 break; |
|
726 case O_EXLOCK: //emulating exclusive locks |
|
727 err = iFile.Open(aSession, aName, fMode); |
|
728 if(err == KErrNone) |
|
729 { |
|
730 FSIZE fileSize = 0; |
|
731 err = iFile.Size(fileSize); |
|
732 if(fileSize <= 0 ) |
|
733 return KErrNotSupported; |
|
734 |
|
735 if(err == KErrNone) |
|
736 { |
|
737 err = iFile.Lock(0, fileSize); |
|
738 } |
|
739 } |
|
740 |
|
741 if(err != KErrNone) |
|
742 { |
|
743 return err; |
|
744 } |
|
745 break; |
|
746 // Everything else is assumed to mean open existing file, |
|
747 // If the file isn't there, O_CREAT implies that we should make it |
|
748 default: |
|
749 err = iFile.Open(aSession, aName, fMode); |
|
750 if (err == KErrNotFound && (mapped & O_CREAT)) |
|
751 { |
|
752 err = iFile.Create(aSession, aName, fMode); |
|
753 if(!err) |
|
754 { |
|
755 created = ETrue; |
|
756 } |
|
757 |
|
758 } |
|
759 if((err == KErrArgument) &&(mode == (O_CREAT | O_RDONLY))) |
|
760 { |
|
761 TUint CrMode = EFileShareReadersOrWriters | EFileWrite ; |
|
762 CrMode |= (mode & O_TEXT) ? EFileStreamText : EFileStream; |
|
763 err = iFile.Create(aSession , aName , CrMode); |
|
764 |
|
765 if(!err) |
|
766 { |
|
767 created = ETrue; |
|
768 iFile.Close() ; |
|
769 err = iFile.Open(aSession, aName , fMode); |
|
770 } |
|
771 } |
|
772 |
|
773 if (err == KErrNone && (mapped & O_APPEND)) |
|
774 { |
|
775 iPos = Ext(); |
|
776 if (iPos < 0) |
|
777 err = iPos; |
|
778 } |
|
779 break; |
|
780 |
|
781 } |
|
782 if ((mode & O_BUFFERED) == 0) |
|
783 iSize = 0; |
|
784 |
|
785 //If File is to be opened for O_CREAT | O_RDONLY, set the corresponding attributes |
|
786 if(created && fileMode) |
|
787 { |
|
788 err = iFile.SetAtt(fileMode, 0) ; |
|
789 } |
|
790 |
|
791 if (!err) |
|
792 { |
|
793 err = Alloc(); |
|
794 } |
|
795 |
|
796 if(err == KErrNone) |
|
797 { |
|
798 err = CreateLock(); |
|
799 } |
|
800 |
|
801 return err; |
|
802 } |
|
803 |
|
804 TInt CFileDesc::LSeek (off_t& offset, int whence) |
|
805 { |
|
806 //return if its an invalid fd |
|
807 //This scenario comes when the file is closed, but mmap still exists |
|
808 if(iFdAttrib & KInvalidFd) |
|
809 { |
|
810 return KErrBadHandle; |
|
811 } |
|
812 iLock.Wait(); |
|
813 FSIZE pos=offset; |
|
814 FSIZE ext=Ext(); |
|
815 if (ext < 0) |
|
816 { |
|
817 iLock.Signal(); |
|
818 return ext; |
|
819 } |
|
820 switch (whence) |
|
821 { |
|
822 case SEEK_SET: |
|
823 break; |
|
824 case SEEK_CUR: |
|
825 pos += Pos(); |
|
826 break; |
|
827 case SEEK_END: |
|
828 pos += ext; |
|
829 break; |
|
830 default: |
|
831 { |
|
832 iLock.Signal(); |
|
833 return KErrArgument; |
|
834 } |
|
835 } |
|
836 |
|
837 TInt ret = KErrNone; |
|
838 if (pos < 0) |
|
839 { |
|
840 iLock.Signal(); |
|
841 return KErrArgument; |
|
842 } |
|
843 else if (pos >= ext) |
|
844 { |
|
845 ret = Flush(); |
|
846 if (!ret) |
|
847 { |
|
848 iState = EAlloc; |
|
849 } |
|
850 } |
|
851 |
|
852 switch (iState) |
|
853 { |
|
854 case EAlloc: |
|
855 iPos = pos; |
|
856 break; |
|
857 case EReading: |
|
858 { |
|
859 FSIZE lag = iPos - pos; |
|
860 if (lag >= 0 && lag <= (iEnd - iBuffer)) |
|
861 iPtr = iEnd - lag; |
|
862 else |
|
863 { |
|
864 iPtr = iEnd; |
|
865 iPos = pos; |
|
866 } |
|
867 } |
|
868 break; |
|
869 case EWriting: |
|
870 if (pos != Pos()) |
|
871 { |
|
872 ret = Flush(); |
|
873 if (ret != KErrNone) |
|
874 { |
|
875 iLock.Signal(); |
|
876 return ret; |
|
877 } |
|
878 iPos = pos; |
|
879 } |
|
880 break; |
|
881 default: |
|
882 { |
|
883 //Do nothing |
|
884 } |
|
885 } |
|
886 offset = pos; |
|
887 iLock.Signal(); |
|
888 return ret; |
|
889 } |
|
890 |
|
891 void CFileDesc::MapStat(struct stat& st, const TTime& aModTime, TUint& aAttr, const mode_t aMode) |
|
892 { |
|
893 if ( aMode == S_IFREG) |
|
894 { |
|
895 st.st_mode = (aAttr&KEntryAttDir) ? S_IFDIR: S_IFREG; |
|
896 } |
|
897 else |
|
898 { |
|
899 st.st_mode = aMode; |
|
900 } |
|
901 |
|
902 if ((aAttr&KEntryAttReadOnly)) |
|
903 { |
|
904 st.st_mode |= S_IRUSR; |
|
905 } |
|
906 else |
|
907 { |
|
908 st.st_mode = st.st_mode | S_IWUSR | S_IRUSR; |
|
909 } |
|
910 |
|
911 st.st_nlink = (S_IFLNK == st.st_mode) ? 2 : 1; |
|
912 TTimeIntervalSeconds res; |
|
913 const TTime KEpocTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000)); // 00:00, Jan 1st 1970 |
|
914 if(aModTime.SecondsFrom(KEpocTime, res)) |
|
915 { |
|
916 st.st_mtime = -1; |
|
917 st.st_atime = -1 ; |
|
918 st.st_ctime = -1; |
|
919 } |
|
920 else |
|
921 { |
|
922 st.st_mtime = res.Int(); |
|
923 st.st_ctime = st.st_atime = st.st_mtime ; //here modification and access time are same |
|
924 } |
|
925 st.st_blksize=512; |
|
926 } |
|
927 |
|
928 TInt CFileDesc::FStat (struct stat* st) |
|
929 { |
|
930 //return if its an invalid fd |
|
931 if(iFdAttrib & KInvalidFd) |
|
932 { |
|
933 return KErrBadHandle; |
|
934 } |
|
935 TInt err; |
|
936 TUint att; |
|
937 TTime modtime; |
|
938 |
|
939 err = iFile.Att(att); |
|
940 if (!err) |
|
941 { |
|
942 err = iFile.Modified(modtime); |
|
943 if (!err) |
|
944 { |
|
945 FSIZE ext=Ext(); |
|
946 if (ext >= 0) |
|
947 { |
|
948 st->st_size = ext; |
|
949 st->st_dev = st->st_rdev = iDrive; |
|
950 MapStat(*st, modtime, att); |
|
951 return 0; |
|
952 } |
|
953 else |
|
954 err = ext; |
|
955 } |
|
956 } |
|
957 return err; |
|
958 } |
|
959 |
|
960 TInt CFileDesc::SetAtt(TUint aSetAttMask, TUint aClearAttMask) |
|
961 { |
|
962 return iFile.SetAtt(aSetAttMask , aClearAttMask) ; |
|
963 } |
|
964 |
|
965 TInt CFileDesc::Alloc() |
|
966 { |
|
967 if (iSize) |
|
968 { |
|
969 //Delete if iBuffer is holding some memory already |
|
970 if (iBuffer) |
|
971 { |
|
972 delete [] iBuffer; |
|
973 } |
|
974 iBuffer = new TUint8[iSize]; |
|
975 if (iBuffer == 0) |
|
976 return KErrNoMemory; |
|
977 } |
|
978 return KErrNone; |
|
979 } |
|
980 |
|
981 TInt CFileDesc::FileRead(TUint8* aPtr,TInt aLength) |
|
982 { |
|
983 TPtr8 ptr(aPtr,aLength); |
|
984 TInt r=iFile.Read(iPos,ptr); |
|
985 if (r == KErrNone) |
|
986 { |
|
987 r = ptr.Length(); |
|
988 iPos += r; |
|
989 if (r < aLength) |
|
990 iExt = iPos; |
|
991 } |
|
992 return r; |
|
993 } |
|
994 |
|
995 TInt CFileDesc::FileWrite(TUint8* aPtr,TInt aLength) |
|
996 { |
|
997 TPtrC8 ptr(aPtr,aLength); |
|
998 if (iExt >= 0) |
|
999 { |
|
1000 if (iPos > iExt) |
|
1001 { |
|
1002 iFile.SetSize(iPos); |
|
1003 } |
|
1004 } |
|
1005 |
|
1006 // Check if the specified file is opened in O_APPEND |
|
1007 // mode and set right the file write position |
|
1008 if(iFcntlFlag & O_APPEND) |
|
1009 { |
|
1010 FSIZE bytes=0; |
|
1011 TInt ret = iFile.Size(bytes); |
|
1012 if (ret < 0) |
|
1013 return ret; |
|
1014 iPos = Max(bytes, Pos()); |
|
1015 } |
|
1016 |
|
1017 TInt r = iFile.Write(iPos,ptr); |
|
1018 if (r == KErrNone) |
|
1019 { |
|
1020 r = ptr.Length(); |
|
1021 iPos += aLength; |
|
1022 if (iPos > iExt && iExt >= 0) |
|
1023 iExt = iPos; |
|
1024 } |
|
1025 return r; |
|
1026 } |
|
1027 |
|
1028 TInt CFileDesc::Flush() |
|
1029 { |
|
1030 if (iPtr > iBuffer) |
|
1031 { |
|
1032 TInt r = FileWrite(iBuffer, iPtr-iBuffer); |
|
1033 if (r < 0) |
|
1034 return r; |
|
1035 iPtr = iBuffer; |
|
1036 } |
|
1037 return KErrNone; |
|
1038 } |
|
1039 |
|
1040 TInt CFileDesc::DoRead (TDes8& aDesc) |
|
1041 { |
|
1042 if (iState != EReading) |
|
1043 { |
|
1044 if (iState != EAlloc) |
|
1045 { |
|
1046 TInt ret = Flush(); |
|
1047 if (ret != KErrNone) |
|
1048 { |
|
1049 return ret; |
|
1050 } |
|
1051 } |
|
1052 iState = EReading; |
|
1053 iPtr = iEnd = iBuffer; |
|
1054 } |
|
1055 |
|
1056 TUint8* p = (TUint8*) aDesc.Ptr(); |
|
1057 TInt max = aDesc.MaxLength(); |
|
1058 TInt avail = iEnd - iPtr; |
|
1059 TInt len = Min(max, avail); |
|
1060 if (len > 0) |
|
1061 { |
|
1062 p = Mem::Copy(p, iPtr, len); |
|
1063 iPtr += len; |
|
1064 max -= len; |
|
1065 } |
|
1066 if (max >= iSize) |
|
1067 { |
|
1068 TInt ret = FileRead(p, max); |
|
1069 if (ret < 0) |
|
1070 return ret; |
|
1071 p += ret; |
|
1072 } |
|
1073 else if (max > 0) |
|
1074 { |
|
1075 TInt ret = FileRead(iBuffer, Min(max + EReadAhead, iSize)); |
|
1076 if (ret < 0) |
|
1077 return ret; |
|
1078 len = Min(max, ret); |
|
1079 p = Mem::Copy(p, iBuffer, len); |
|
1080 iPtr = iBuffer + len; |
|
1081 iEnd = iBuffer + ret; |
|
1082 } |
|
1083 aDesc.SetLength(p-aDesc.Ptr()); |
|
1084 return aDesc.Length(); |
|
1085 } |
|
1086 |
|
1087 void CFileDesc::Read (TDes8& aDesc, TRequestStatus& aStatus) |
|
1088 { |
|
1089 //return if its an invalid fd |
|
1090 if(iFdAttrib & KInvalidFd) |
|
1091 { |
|
1092 Complete(aStatus, KErrBadHandle); |
|
1093 return; |
|
1094 } |
|
1095 //Acquire the Lock before read and release it later |
|
1096 iLock.Wait(); |
|
1097 Complete(aStatus,DoRead(aDesc)); |
|
1098 iLock.Signal(); |
|
1099 } |
|
1100 |
|
1101 TInt CFileDesc::DoWrite (TDes8& aDesc) |
|
1102 { |
|
1103 if (iState != EWriting) |
|
1104 { |
|
1105 if (iState != EAlloc) |
|
1106 { |
|
1107 iPos -= iEnd - iPtr; |
|
1108 } |
|
1109 |
|
1110 iState = EWriting; |
|
1111 iPtr = iBuffer; |
|
1112 iEnd = iBuffer + iSize; |
|
1113 } |
|
1114 |
|
1115 TUint8* p = (TUint8*) aDesc.Ptr(); |
|
1116 TInt max = aDesc.Length(); |
|
1117 TInt avail = iEnd - iPtr; |
|
1118 TInt len = Min(max, avail); |
|
1119 if (len > 0) |
|
1120 { |
|
1121 iPtr = Mem::Copy(iPtr, p, len); |
|
1122 p += len; |
|
1123 max -= len; |
|
1124 } |
|
1125 if (max == 0) |
|
1126 return len; |
|
1127 TInt r=Flush(); |
|
1128 if (r < 0) |
|
1129 return r; |
|
1130 if (max >= iSize) |
|
1131 return len + FileWrite(p, max); |
|
1132 iPtr = Mem::Copy(iPtr, p, max); |
|
1133 return len + max; |
|
1134 } |
|
1135 |
|
1136 void CFileDesc::Write(TDes8& aDesc, TRequestStatus& aStatus) |
|
1137 { |
|
1138 //return if its an invalid fd |
|
1139 if(iFdAttrib & KInvalidFd) |
|
1140 { |
|
1141 Complete(aStatus, KErrBadHandle); |
|
1142 return; |
|
1143 } |
|
1144 //Acquire the Lock before write and release it later |
|
1145 iLock.Wait(); |
|
1146 Complete(aStatus,DoWrite(aDesc)); |
|
1147 iLock.Signal(); |
|
1148 } |
|
1149 |
|
1150 TInt CFileDesc::DoSync() |
|
1151 { |
|
1152 if (iState == EWriting) |
|
1153 { |
|
1154 TInt ret = Flush(); |
|
1155 if (ret < 0) |
|
1156 return ret; |
|
1157 } |
|
1158 return iFile.Flush(); |
|
1159 } |
|
1160 |
|
1161 void CFileDesc::Sync(TRequestStatus& aStatus) |
|
1162 { |
|
1163 iLock.Wait(); |
|
1164 Complete(aStatus,DoSync()); |
|
1165 iLock.Signal(); |
|
1166 } |
|
1167 |
|
1168 FSIZE CFileDesc::Pos() |
|
1169 { |
|
1170 FSIZE pos = iPos; |
|
1171 if (iState == EReading) |
|
1172 pos -= (iEnd - iPtr); |
|
1173 else if (iState == EWriting) |
|
1174 pos += (iPtr - iBuffer); |
|
1175 return pos; |
|
1176 } |
|
1177 |
|
1178 FSIZE CFileDesc::Ext() |
|
1179 { |
|
1180 TInt r = iFile.Size(iExt); |
|
1181 if (r < 0) |
|
1182 return r; |
|
1183 return iExt; |
|
1184 } |
|
1185 |
|
1186 TInt CFileDesc::IoctlCompletion(int aCmd, void* aParam, TInt aStatus) |
|
1187 { |
|
1188 TInt ret=aStatus; |
|
1189 if (ret!=KErrNone) |
|
1190 return ret; |
|
1191 // some useful sums about the current state of the file |
|
1192 FSIZE curoff = Pos(); |
|
1193 FSIZE size = Ext(); |
|
1194 if (size < 0) |
|
1195 ret = size; |
|
1196 int *param=reinterpret_cast<int*> (aParam); |
|
1197 switch ((unsigned)aCmd) |
|
1198 { |
|
1199 case FIONREAD: |
|
1200 case E32IONREAD: |
|
1201 if (ret==KErrNone) |
|
1202 *param=(size-curoff); |
|
1203 break; |
|
1204 case E32IOSELECT: |
|
1205 { |
|
1206 int mask=E32SELECT_WRITE; |
|
1207 if ((size-curoff)>0) |
|
1208 mask |= E32SELECT_READ; |
|
1209 *param=(*param)&mask; // but don't block |
|
1210 } |
|
1211 break; |
|
1212 default: |
|
1213 ret=KErrNotSupported; |
|
1214 break; |
|
1215 } |
|
1216 return ret; |
|
1217 } |
|
1218 |
|
1219 |
|
1220 #if defined(SYMBIAN_OE_LARGE_FILE_SUPPORT) && !defined(SYMBIAN_OE_NO_LFS) |
|
1221 // this needs to be corrected once the api which gives the large file size is available. |
|
1222 //#define MAX_SIZE LLONG_MAX /* 2^63-1 - MAX file size */ |
|
1223 #define MAX_SIZE INT_MAX /* 2^31-1 - MAX file size */ |
|
1224 #else |
|
1225 #define MAX_SIZE INT_MAX /* 2^31-1 - MAX file size */ |
|
1226 #endif //SYMBIAN_OE_LARGE_FILE_SUPPORT && !SYMBIAN_OE_NO_LFS |
|
1227 |
|
1228 TInt CFileDesc::ProcessLockParams(FSIZE& pos, FSIZE& lock_len, TInt& lock_type, struct flock* lock) |
|
1229 { |
|
1230 TInt retVal=0; |
|
1231 lock_type = lock->l_type; |
|
1232 if(!lock->l_len) //if len = 0 then lock must be extended to the system limit for max file size |
|
1233 { |
|
1234 lock_len=MAX_SIZE; |
|
1235 } |
|
1236 else //len argument is non-zero |
|
1237 { |
|
1238 lock_len = lock->l_len; |
|
1239 } |
|
1240 pos =lock->l_start; //offset |
|
1241 switch( lock->l_whence ) |
|
1242 { |
|
1243 case SEEK_SET: |
|
1244 |
|
1245 if(pos<0) |
|
1246 { |
|
1247 retVal = KErrArgument; |
|
1248 return retVal; |
|
1249 } |
|
1250 else |
|
1251 break; |
|
1252 case SEEK_CUR: |
|
1253 pos += iPos; //current position + offset |
|
1254 break; |
|
1255 case SEEK_END: |
|
1256 retVal = iFile.Size(pos); |
|
1257 switch( retVal) |
|
1258 { |
|
1259 case KErrNone: |
|
1260 pos += lock->l_start; //file size + ofset |
|
1261 break; |
|
1262 default: |
|
1263 return retVal; |
|
1264 } |
|
1265 |
|
1266 if(pos<0) |
|
1267 { |
|
1268 retVal = KErrArgument; |
|
1269 return retVal; |
|
1270 } |
|
1271 break; |
|
1272 default: |
|
1273 retVal = KErrArgument; //invalid argument |
|
1274 return retVal; |
|
1275 } |
|
1276 |
|
1277 if( lock_len < 0) |
|
1278 { |
|
1279 pos += lock_len; |
|
1280 lock_len = -lock_len; |
|
1281 } |
|
1282 return retVal; |
|
1283 } |
|
1284 // ----------------------------------------------------------------------------- |
|
1285 // CFileDesc::Fcntl |
|
1286 // Symbian File Specific Implementation for fcntl |
|
1287 // There is no mapping from Symbian RFile for File fcntl. |
|
1288 // This implementation supports fcntl with F_SETFL and F_GETFL as cmd |
|
1289 // We can set the Flag as Blocking or Non-Blocking. |
|
1290 // ----------------------------------------------------------------------------- |
|
1291 // |
|
1292 TInt CFileDesc::Fcntl(TUint anArg, TUint aCmd) |
|
1293 { |
|
1294 struct flock *lock = NULL; |
|
1295 FSIZE pos = 0,lock_len = -1; |
|
1296 TInt lock_type = -1; |
|
1297 //return if its an invalid fd |
|
1298 if(iFdAttrib & KInvalidFd) |
|
1299 { |
|
1300 return KErrBadHandle; |
|
1301 } |
|
1302 TInt retVal = KErrNone; |
|
1303 //fcntl supports only F_SETFL and F_GETFL for Non-Blocking I/O |
|
1304 //If aCmd and anArg does not match these, return with Error |
|
1305 switch( aCmd ) |
|
1306 { |
|
1307 case F_SETFL: |
|
1308 { |
|
1309 //Set the fcntl Flag |
|
1310 if((anArg == O_APPEND) || (anArg == O_NONBLOCK)) |
|
1311 { |
|
1312 iFcntlFlag |= anArg ; |
|
1313 } |
|
1314 retVal = 0 ; |
|
1315 break; |
|
1316 } |
|
1317 case F_GETFL: |
|
1318 { |
|
1319 //Return fcntl flag |
|
1320 retVal = iFcntlFlag; |
|
1321 break; |
|
1322 } |
|
1323 case F_GETFD: |
|
1324 { |
|
1325 if(iFdAttrib & KCloseonExec) |
|
1326 { |
|
1327 retVal = 1; |
|
1328 } |
|
1329 break; |
|
1330 } |
|
1331 case F_SETFD: |
|
1332 { |
|
1333 if(anArg == 1) |
|
1334 { |
|
1335 iFdAttrib |= SET_CLOSE_ON_EXEC_FLG; // (1 << 2) ; |
|
1336 retVal = anArg; |
|
1337 } |
|
1338 else if(anArg == 0 ) |
|
1339 { |
|
1340 iFdAttrib &= ~SET_CLOSE_ON_EXEC_FLG; //(1 << 2) ; |
|
1341 retVal = anArg; |
|
1342 } |
|
1343 else |
|
1344 { |
|
1345 retVal = KErrArgument; //return invalid argument |
|
1346 } |
|
1347 break; |
|
1348 } |
|
1349 case F_GETLK: |
|
1350 { |
|
1351 lock = (struct flock*)anArg; |
|
1352 retVal=ProcessLockParams(pos, lock_len, lock_type, lock); |
|
1353 if(retVal) |
|
1354 return retVal; |
|
1355 switch( lock_type) |
|
1356 { |
|
1357 case F_WRLCK: |
|
1358 retVal = iFile.Lock(pos ,lock_len); //try to lock the region. |
|
1359 if (retVal == KErrNone) |
|
1360 { |
|
1361 retVal = iFile.UnLock(pos ,lock_len); //if previous lock() is successful then unlock the region |
|
1362 lock->l_type = F_UNLCK; //indication that region can be locked |
|
1363 } |
|
1364 else |
|
1365 { |
|
1366 retVal = KErrNone; //region cant be lock. return success, but don't change lock->l_type |
|
1367 } |
|
1368 break; |
|
1369 default: |
|
1370 retVal = KErrArgument; |
|
1371 } |
|
1372 break; |
|
1373 } |
|
1374 case F_SETLK: |
|
1375 { |
|
1376 lock = (struct flock*)anArg; |
|
1377 retVal=ProcessLockParams(pos, lock_len, lock_type, lock); |
|
1378 if(retVal) |
|
1379 return retVal; |
|
1380 switch( lock_type) |
|
1381 { |
|
1382 case F_WRLCK: |
|
1383 retVal = iFile.Lock(pos ,lock_len); //try to lock without waiting |
|
1384 break; |
|
1385 case F_UNLCK: |
|
1386 retVal = iFile.UnLock(pos ,lock_len); //try to unlock without waiting |
|
1387 break; |
|
1388 case F_RDLCK: |
|
1389 retVal = KErrNotSupported; |
|
1390 |
|
1391 default: |
|
1392 retVal = KErrArgument; //return invalid argument |
|
1393 } |
|
1394 break; |
|
1395 } |
|
1396 default: |
|
1397 retVal = KErrNotSupported; |
|
1398 } |
|
1399 return retVal; |
|
1400 } |
|
1401 |
|
1402 // ----------------------------------------------------------------------------- |
|
1403 // CFileDesc::Truncate |
|
1404 // Symbian File Specific Implementation for Truncate |
|
1405 // This will truncate the file which is opened in write mode |
|
1406 // ----------------------------------------------------------------------------- |
|
1407 // |
|
1408 TInt CFileDesc::Truncate(off_t anOffset) |
|
1409 { |
|
1410 TInt retVal; |
|
1411 //return if its an invalid fd |
|
1412 if(iFdAttrib & KInvalidFd) |
|
1413 { |
|
1414 return KErrBadHandle; |
|
1415 } |
|
1416 iExt = anOffset; |
|
1417 retVal = iFile.SetSize(anOffset); |
|
1418 if (retVal == KErrNotSupported) |
|
1419 { |
|
1420 return KErrArgument;//to work around RFile64::SetSize() return value |
|
1421 } |
|
1422 return retVal; |
|
1423 } |
|
1424 |
|
1425 // Extra support for temporary files |
|
1426 TInt CTempFileDesc::Open(const wchar_t* aName, TInt mode) |
|
1427 { |
|
1428 iSession = Backend()->FileSession(); |
|
1429 |
|
1430 TFileName filePath; |
|
1431 if (mode & O_TMPFILE) //from tmpfile() |
|
1432 { |
|
1433 //obtain original mode for tmpfile() |
|
1434 mode &= (~O_TMPFILE); |
|
1435 filePath.Copy((const TText*)aName); |
|
1436 } |
|
1437 else //for "TMP:" |
|
1438 { |
|
1439 TParse path; |
|
1440 TInt err = GetFullPath(path, (const TText16*)WIDEP_tmpdir, NULL); |
|
1441 if (err != KErrNone) |
|
1442 { |
|
1443 return err; |
|
1444 } |
|
1445 filePath.Copy(path.DriveAndPath()); |
|
1446 } |
|
1447 |
|
1448 if((mode & O_BUFFERED) == 0) |
|
1449 { |
|
1450 setSize(0); |
|
1451 } |
|
1452 iFcntlFlag = mode & O_ACCMODE;//setting the fcntl flag |
|
1453 TInt ret = Alloc(); |
|
1454 if (ret != KErrNone) |
|
1455 { |
|
1456 return ret; |
|
1457 } |
|
1458 TFileName aPathName; |
|
1459 aPathName.Append(iSession.GetSystemDriveChar()); |
|
1460 aPathName.Append(TChar(KDriveDelimiter)); |
|
1461 aPathName.Append(_L("\\System\\temp")); |
|
1462 if (filePath.Find(aPathName) == 0) |
|
1463 { |
|
1464 ret = iSession.MkDir(filePath); |
|
1465 if (ret != KErrNone && ret != KErrAlreadyExists) |
|
1466 { |
|
1467 return ret; |
|
1468 } |
|
1469 else |
|
1470 { |
|
1471 ret = iFile.Temp(iSession, filePath, iName, EFileShareReadersOrWriters); |
|
1472 } |
|
1473 } |
|
1474 else |
|
1475 { |
|
1476 ret = iFile.Create(iSession, filePath, EFileShareReadersOrWriters); |
|
1477 } |
|
1478 return ret; |
|
1479 } |
|
1480 |
|
1481 TInt CTempFileDesc::FinalClose() |
|
1482 { |
|
1483 TInt ret = iFile.FullName(iName); |
|
1484 if (ret != KErrNone) |
|
1485 { |
|
1486 return ret; |
|
1487 } |
|
1488 iFile.Close(); |
|
1489 return iSession.Delete(iName); |
|
1490 } |
|
1491 |
|
1492 #ifdef SYMBIAN_OE_LIBRT |
|
1493 // ----------------------------------------------------------------------------- |
|
1494 // CSharedMemDesc::CSharedMemDesc : Constructor |
|
1495 // Constructs sharedmemory Descriptor |
|
1496 // ----------------------------------------------------------------------------- |
|
1497 CSharedMemDesc::CSharedMemDesc() |
|
1498 { |
|
1499 iLock.CreateLocal(); |
|
1500 } |
|
1501 |
|
1502 // ----------------------------------------------------------------------------- |
|
1503 // CSharedMemDesc::CSharedMemDesc : Destructor |
|
1504 // Destructor for sharedmemory Descriptor |
|
1505 // ----------------------------------------------------------------------------- |
|
1506 CSharedMemDesc::~CSharedMemDesc() |
|
1507 { |
|
1508 iLock.Close(); |
|
1509 } |
|
1510 |
|
1511 // ----------------------------------------------------------------------------- |
|
1512 // CSharedMemDesc::Open |
|
1513 // Gives sharedmemory Descriptor |
|
1514 // ----------------------------------------------------------------------------- |
|
1515 TInt CSharedMemDesc::Open(const wchar_t* aName, int mode, int perms) |
|
1516 { |
|
1517 int aerr = 0, err =0; |
|
1518 void* ptr = NULL; |
|
1519 TInt shmkey = 0; |
|
1520 TInt shmid = 0; |
|
1521 struct shmid_ds *buf = NULL; |
|
1522 |
|
1523 mode &= (~O_SHMFLG); |
|
1524 if(mode == O_WRONLY) |
|
1525 { |
|
1526 return KErrNone; |
|
1527 } |
|
1528 |
|
1529 TFileName shrdmemname; |
|
1530 shrdmemname.Copy((const TText*)aName); |
|
1531 |
|
1532 iPerms = perms; |
|
1533 iFcntlFlag = mode & O_ACCMODE; |
|
1534 shmkey = GeneratePathKey(shrdmemname); |
|
1535 switch (mode) |
|
1536 { |
|
1537 case O_CREAT: |
|
1538 case O_CREAT|O_RDWR: |
|
1539 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, IPC_CREAT|perms, aerr); |
|
1540 if (shmid == -1) |
|
1541 { |
|
1542 err = aerr; |
|
1543 return err; |
|
1544 } |
|
1545 break; |
|
1546 case O_TRUNC: |
|
1547 case O_TRUNC|O_RDWR: |
|
1548 case O_CREAT|O_TRUNC: |
|
1549 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, IPC_CREAT|perms, aerr); |
|
1550 if (shmid == -1) |
|
1551 { |
|
1552 err = aerr; |
|
1553 return err; |
|
1554 } |
|
1555 if (Backend()->iIpcS.shmctl(shmid, IPC_RMID, buf, aerr) == -1) |
|
1556 { |
|
1557 err = aerr; |
|
1558 return err; |
|
1559 } |
|
1560 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, IPC_CREAT|perms, aerr); |
|
1561 if (shmid == -1) |
|
1562 { |
|
1563 err = aerr; |
|
1564 return err; |
|
1565 } |
|
1566 break; |
|
1567 case O_CREAT|O_EXCL: |
|
1568 case O_CREAT|O_EXCL|O_RDWR: |
|
1569 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, IPC_CREAT|IPC_EXCL|perms, aerr); |
|
1570 if (shmid == -1) |
|
1571 { |
|
1572 err = aerr; |
|
1573 return err; |
|
1574 } |
|
1575 break; |
|
1576 case O_CREAT|O_EXCL|O_TRUNC: |
|
1577 case O_CREAT|O_EXCL|O_TRUNC|O_RDWR: |
|
1578 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, IPC_CREAT|IPC_EXCL|perms, aerr); |
|
1579 if (shmid == -1) |
|
1580 { |
|
1581 err = aerr; |
|
1582 return err; |
|
1583 } |
|
1584 if (Backend()->iIpcS.shmctl(shmid, IPC_RMID, buf, aerr) == -1) |
|
1585 { |
|
1586 err = aerr; |
|
1587 return err; |
|
1588 } |
|
1589 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, IPC_CREAT|IPC_EXCL|perms, aerr); |
|
1590 if (shmid == -1) |
|
1591 { |
|
1592 err = aerr; |
|
1593 return err; |
|
1594 } |
|
1595 break; |
|
1596 default: |
|
1597 shmid = Backend()->iIpcS.shmget(shmkey, SHM_CHUNKSIZE, perms, aerr); |
|
1598 if (shmid == -1) |
|
1599 { |
|
1600 err = aerr; |
|
1601 return err; |
|
1602 } |
|
1603 break; |
|
1604 } |
|
1605 ptr = Backend()->iIpcS.shmat(shmid, (void *)NULL, 0, aerr); |
|
1606 if (ptr == (void*)-1) |
|
1607 { |
|
1608 err = aerr; |
|
1609 return err; |
|
1610 } |
|
1611 iPtr = ptr; |
|
1612 iKey = shmkey; |
|
1613 return err; |
|
1614 } |
|
1615 |
|
1616 TInt CSharedMemDesc::ShmRead(TUint8* aPtr,TInt aLength) |
|
1617 { |
|
1618 TInt err = KErrNone; |
|
1619 iSize = Backend()->iIpcS.GetShmSize(iKey, err); |
|
1620 TInt diff = iSize - iPos; |
|
1621 if (aLength >= diff) |
|
1622 aLength = diff; |
|
1623 TPtr8 ptr(aPtr, aLength); |
|
1624 ptr.Copy((TUint8*)(iPtr)+iPos, aLength); |
|
1625 TInt r = ptr.Length(); |
|
1626 iPos += r; |
|
1627 return r; |
|
1628 } |
|
1629 |
|
1630 TInt CSharedMemDesc::DoShmRead (TDes8& aDesc) |
|
1631 { |
|
1632 TUint8* p = (TUint8*) aDesc.Ptr(); |
|
1633 TInt max = aDesc.MaxLength(); |
|
1634 TInt ret = ShmRead(p, max); |
|
1635 p += ret; |
|
1636 aDesc.SetLength(p-aDesc.Ptr()); |
|
1637 return aDesc.Length(); |
|
1638 } |
|
1639 |
|
1640 |
|
1641 // ----------------------------------------------------------------------------- |
|
1642 // CSharedMemDesc::Read |
|
1643 // Reading from a sharedmemory |
|
1644 // ----------------------------------------------------------------------------- |
|
1645 void CSharedMemDesc::Read(TDes8& aDesc, TRequestStatus& aStatus) |
|
1646 { |
|
1647 //return if its an invalid fd |
|
1648 if(iFdAttrib & KInvalidFd) |
|
1649 { |
|
1650 Complete(aStatus, KErrBadHandle); |
|
1651 return; |
|
1652 } |
|
1653 //Acquire the Lock before read and release it later |
|
1654 iLock.Wait(); |
|
1655 Complete(aStatus,DoShmRead(aDesc)); |
|
1656 iLock.Signal(); |
|
1657 } |
|
1658 |
|
1659 TInt CSharedMemDesc::ShmWrite(TUint8* aPtr,TInt aLength) |
|
1660 { |
|
1661 TInt err = KErrNone; |
|
1662 TInt size = Backend()->iIpcS.GetShmSize(iKey, err); |
|
1663 TPtrC8 ptr(aPtr,aLength); |
|
1664 TUint8* bufPtr = const_cast<TUint8*>(ptr.Ptr()); |
|
1665 TInt len = ptr.Length(); |
|
1666 TInt r = Min(aLength, len); |
|
1667 Mem::Copy((TUint8*)(iPtr)+iPos, bufPtr, r); |
|
1668 iPos += r; |
|
1669 if (iPos > iExt && iExt >= 0) |
|
1670 iExt = iPos; |
|
1671 if (iExt <= size) |
|
1672 iSize = iExt = size; |
|
1673 else |
|
1674 iSize = iExt; |
|
1675 Backend()->iIpcS.SetShmSize(iKey, iSize, err); |
|
1676 return r; |
|
1677 } |
|
1678 |
|
1679 TInt CSharedMemDesc::DoShmWrite (TDes8& aDesc) |
|
1680 { |
|
1681 TUint8* p = (TUint8*) aDesc.Ptr(); |
|
1682 TInt max = aDesc.Length(); |
|
1683 return ShmWrite(p, max); |
|
1684 } |
|
1685 |
|
1686 // ----------------------------------------------------------------------------- |
|
1687 // CSharedMemDesc::Write |
|
1688 // Writing to sharedmemory |
|
1689 // ----------------------------------------------------------------------------- |
|
1690 void CSharedMemDesc::Write(TDes8& aDesc, TRequestStatus& aStatus) |
|
1691 { |
|
1692 //return if its an invalid fd |
|
1693 if(iFdAttrib & KInvalidFd) |
|
1694 { |
|
1695 Complete(aStatus, KErrBadHandle); |
|
1696 return; |
|
1697 } |
|
1698 //Acquire the Lock before write and release it later |
|
1699 iLock.Wait(); |
|
1700 Complete(aStatus,DoShmWrite(aDesc)); |
|
1701 iLock.Signal(); |
|
1702 } |
|
1703 |
|
1704 // ----------------------------------------------------------------------------- |
|
1705 // CSharedMemDesc::Fcntl |
|
1706 // fcntl Implementation for sharedmemory |
|
1707 // ----------------------------------------------------------------------------- |
|
1708 // |
|
1709 TInt CSharedMemDesc::Fcntl(TUint anArg, TUint aCmd) |
|
1710 { |
|
1711 //return if its an invalid fd |
|
1712 if(iFdAttrib & KInvalidFd) |
|
1713 { |
|
1714 return KErrBadHandle; |
|
1715 } |
|
1716 TInt retVal = KErrNotSupported; |
|
1717 //fcntl supports only F_SETFL and F_GETFL for Non-Blocking I/O |
|
1718 //If aCmd and anArg does not match these, return with Error |
|
1719 switch(aCmd) |
|
1720 { |
|
1721 case F_SETFL: |
|
1722 { |
|
1723 //Set the fcntl Flag |
|
1724 if(anArg == O_NONBLOCK) |
|
1725 { |
|
1726 iFcntlFlag |= anArg ; |
|
1727 } |
|
1728 retVal = 0 ; |
|
1729 break; |
|
1730 } |
|
1731 case F_GETFL: |
|
1732 { |
|
1733 //Return fcntl flag |
|
1734 retVal = iFcntlFlag; |
|
1735 break; |
|
1736 } |
|
1737 case F_GETFD: |
|
1738 { |
|
1739 if(iFdAttrib & KCloseonExec) |
|
1740 { |
|
1741 retVal = 1; |
|
1742 } |
|
1743 break; |
|
1744 } |
|
1745 case F_SETFD: |
|
1746 { |
|
1747 if(anArg == 1) |
|
1748 { |
|
1749 iFdAttrib |= SET_CLOSE_ON_EXEC_FLG; // (1 << 2) ; |
|
1750 retVal = anArg; |
|
1751 } |
|
1752 else if(anArg == 0 ) |
|
1753 { |
|
1754 iFdAttrib &= ~SET_CLOSE_ON_EXEC_FLG; //(1 << 2) ; |
|
1755 retVal = anArg; |
|
1756 } |
|
1757 else |
|
1758 { |
|
1759 retVal = KErrArgument; //return invalid argument |
|
1760 } |
|
1761 break; |
|
1762 } |
|
1763 } |
|
1764 return retVal; |
|
1765 } |
|
1766 |
|
1767 // ----------------------------------------------------------------------------- |
|
1768 // CSharedMemDesc::FStat |
|
1769 // stat Implementation for sharedmemory |
|
1770 // ----------------------------------------------------------------------------- |
|
1771 // |
|
1772 TInt CSharedMemDesc::FStat(struct stat *st) |
|
1773 { |
|
1774 // set mode as directory |
|
1775 TInt err = 0, aerr =0; |
|
1776 TInt shmid = 0; |
|
1777 struct shmid_ds shmDesc; |
|
1778 st->st_mode = S_IFREG|iPerms; |
|
1779 shmid = Backend()->iIpcS.shmget(iKey, SHM_CHUNKSIZE, iPerms, aerr); |
|
1780 Backend()->iIpcS.shmctl(shmid, IPC_STAT, &shmDesc, aerr); |
|
1781 st->st_uid = shmDesc.shm_perm.cuid; |
|
1782 st->st_gid = 0; |
|
1783 st->st_size = Backend()->iIpcS.GetShmSize(iKey, err); |
|
1784 return KErrNone; |
|
1785 } |
|
1786 |
|
1787 // ----------------------------------------------------------------------------- |
|
1788 // CSharedMemDesc::FinalClose |
|
1789 // stat Implementation for sharedmemory |
|
1790 // ----------------------------------------------------------------------------- |
|
1791 // |
|
1792 TInt CSharedMemDesc::FinalClose() |
|
1793 { |
|
1794 TInt err = 0; |
|
1795 TInt ret = KErrNone; |
|
1796 ret = Backend()->iIpcS.shmdt(iPtr, err); |
|
1797 iLock.Close(); |
|
1798 return ret; |
|
1799 } |
|
1800 |
|
1801 TInt CSharedMemDesc::Pos() |
|
1802 { |
|
1803 return iPos; |
|
1804 } |
|
1805 |
|
1806 TInt CSharedMemDesc::Ext() |
|
1807 { |
|
1808 return iExt; |
|
1809 } |
|
1810 |
|
1811 //------------------------------------------------------------------------------- |
|
1812 // Function Name : CSharedMemDesc::LSeek() |
|
1813 // Description : Shall seek to specific position of shared memory |
|
1814 //------------------------------------------------------------------------------- |
|
1815 TInt CSharedMemDesc::LSeek (off_t& offset, int whence) |
|
1816 { |
|
1817 //return if its an invalid fd |
|
1818 //This scenario comes when the file is closed, but mmap still exists |
|
1819 if(iFdAttrib & KInvalidFd) |
|
1820 { |
|
1821 return KErrBadHandle; |
|
1822 } |
|
1823 TInt pos=offset; |
|
1824 TInt ext=Ext(); |
|
1825 switch (whence) |
|
1826 { |
|
1827 case SEEK_SET: |
|
1828 break; |
|
1829 case SEEK_CUR: |
|
1830 pos += Pos(); |
|
1831 break; |
|
1832 case SEEK_END: |
|
1833 pos += ext; |
|
1834 break; |
|
1835 default: |
|
1836 return KErrArgument; |
|
1837 } |
|
1838 if (pos < 0) |
|
1839 { |
|
1840 return KErrArgument; |
|
1841 } |
|
1842 iPos = pos; |
|
1843 offset = pos; |
|
1844 return KErrNone; |
|
1845 } |
|
1846 |
|
1847 #endif //SYMBIAN_OE_LIBRT |
|
1848 |