Update contrib.
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Handle a table of CFileDescBase pointers
23 #include <sys/errno.h>
25 #define RESERVED(i) ((CFileDescBase*)((i)+1))
27 const TInt MINFILES = 8;
29 CFileTable::CFileTable() : iFids(MINFILES)
33 void CFileTable::InitL()
36 iFids.InsertL(0, ptr, 8); // set up the first 8 entries with zero
39 CFileTable::~CFileTable()
44 void CFileTable::Close()
46 for (TInt aFid=0; aFid<iFids.Count(); aFid++)
48 if (iFids[aFid]!=0 && iFids[aFid]!=RESERVED(aFid))
50 iFids[aFid]->RecvFromCancel();
57 void CFileTable::Release(TInt aFid)
60 if (aFid<MINFILES || aFid<iFids.Count()-1)
62 // opportunity to shrink the array
63 while (aFid>=MINFILES && iFids[aFid]==0)
71 void CFileTable::Default(CFileDescBase* aConsole)
74 iFids[0]=aConsole->Dup();
76 iFids[1]=aConsole->Dup();
78 iFids[2]=aConsole->Dup();
81 TInt CFileTable::Attach(TInt aFid, CFileDescBase* aFile)
83 if (aFid<0 || aFid>=iFids.Count() || (iFids[aFid] != 0 && iFids[aFid]!=RESERVED(aFid)))
89 TInt CFileTable::Reserve()
93 for (aFid=0; aFid<iFids.Count(); aFid++)
96 TRAP(err,iFids.ExtendL());
102 iFids[aFid]=RESERVED(aFid);
106 TInt CFileTable::Reserve(int aFids[3])
116 Detach(aFids); // release the ones we did get
125 TInt CFileTable::Detach(int aFids[3])
136 TInt CFileTable::Detach(TInt aFid, CFileDescBase*& aDetached)
138 if (aFid<0 || aFid>=iFids.Count())
140 aDetached=iFids[aFid];
145 TInt CFileTable::At(TInt aFid, CFileDescBase*& aFound) const
147 if (aFid<0 || aFid>=iFids.Count())
155 TInt CFileTable::Dup(TInt& aFid)
157 if (aFid<0 || aFid>=iFids.Count())
159 CFileDescBase* fdesc=iFids[aFid];
163 for (aFid2=0; aFid2<iFids.Count(); aFid2++)
166 iFids[aFid2]=fdesc->Dup();
173 TInt CFileTable::Dup2(TInt aFid, TInt aFid2)
175 if (aFid<0 || aFid>=iFids.Count() || aFid2<0 || aFid2>=iFids.Count() || aFid==aFid2)
177 CFileDescBase* fdesc=iFids[aFid];
180 CFileDescBase* old=iFids[aFid2];
181 iFids[aFid2]=fdesc->Dup();
187 // Simple synchronous services
189 int CFileTable::dup (int fid, int& anErrno)
194 return MapError(ret,anErrno);
197 int CFileTable::dup2 (int fid, int fid2, int& anErrno)
199 TInt err=Dup2(fid,fid2);
200 return MapError(err,anErrno);
203 //int CFileTable::open (const char* name, int mode, int perms, int& anErrno, RFs& aFs)
204 //int CFileTable::open (const wchar_t* name, int mode, int perms, int& anErrno, RFs& aFs)
205 int CFileTable::open (const wchar_t* name, int mode, int perms, int& anErrno, RSessionBase& aFs)
212 //coverity[var_assign]
213 CFileDescBase *f=CFileDescBase::Open(aFs,name,mode,perms,err);
221 Attach(fd,0); // cancel the reservation
222 //coverity[leaked_storage]
224 return MapError(err,anErrno);
227 int CFileTable::socket (int family, int style, int protocol, int& anErrno, RSocketServ& aSs)
233 CFileDescBase *f=CFileDescBase::Socket(aSs,family,style,protocol,err);
236 CleanupStack::PushL(f);
238 CleanupStack::PopAndDestroy(f);
247 Attach(fd,0); // cancel the reservation
250 return MapError(err,anErrno);
253 int CFileTable::userclose(int fid, int& anErrno)
264 if (KErrNotFound == err)
267 return MapError(err,anErrno);
270 int CFileTable::close (int fid, int& anErrno)
273 TInt err=Detach(fid,f);
286 return MapError(err,anErrno);
291 int CFileTable::lseek (int fid, int offset, int whence, int& anErrno)
297 err=f->LSeek(offset, whence);
301 return MapError(err,anErrno);
304 int CFileTable::fstat (int fid, struct stat *st, int& anErrno)
310 return MapError(err,anErrno);
313 int CFileTable::listen (int fid, int n, int& anErrno)
319 return MapError(err,anErrno);
322 int CFileTable::bind (int fid, TSockAddr& address, int& anErrno)
327 err=f->Bind(address);
328 return MapError(err,anErrno);
331 int CFileTable::sockname (int fid, TSockAddr& address, int anEnd, int& anErrno)
336 err=f->SockName(anEnd, address);
337 return MapError(err,anErrno);
340 int CFileTable::getsockopt (int fid, int level, int opt, void* buf, unsigned long* len, int& anErrno)
346 TPtr8 ptr((TText8 *)buf, *len, *len);
347 err=f->GetSockOpt(opt,level,ptr);
351 return MapError(err,anErrno);
354 int CFileTable::setsockopt (int fid, int level, int opt, void* buf, unsigned long len, int& anErrno)
360 //Negative option is not supported and have not been implemented yet
361 //anErrno is then mapped to ENOSYS(88) (function not implemented yet)
363 return MapError(KErrNotSupported,anErrno);
364 TPtrC8 ptr((TText8 *)buf, len);
365 err=f->SetSockOpt(opt,level,ptr);
367 return MapError(err,anErrno);
370 int CFileTable::ioctlcomplete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno)
375 err=f->IoctlCompletion(cmd,param, aStatus.Int());
376 return MapError(err,anErrno);
379 int CFileTable::ioctlcancel (int fid, int& anErrno)
385 return MapError(err,anErrno);
388 // Preparation for an Asynchronous service
390 // The CFileDescBase* should be Closed after completion: the use of Dup prevents other
391 // clients from invalidating the pointer during an asynchronous operation.
393 TInt CFileTable::Asynch (int fid, CFileDescBase*& aFile)
395 TInt err=At(fid,aFile);