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