os/ossrv/genericopenlibs/cstdlib/USTLIB/FTABLE.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Handle a table of CFileDescBase pointers
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "SYSIF.H"
sl@0
    19
#include "FDESC.H"
sl@0
    20
#include "LTIME.H"
sl@0
    21
#include "LPOSIX.H"
sl@0
    22
#include <fcntl.h>
sl@0
    23
#include <sys/errno.h>
sl@0
    24
sl@0
    25
#define RESERVED(i)	((CFileDescBase*)((i)+1))
sl@0
    26
sl@0
    27
const TInt MINFILES = 8;
sl@0
    28
sl@0
    29
CFileTable::CFileTable() : iFids(MINFILES)
sl@0
    30
	{
sl@0
    31
	}
sl@0
    32
sl@0
    33
void CFileTable::InitL()
sl@0
    34
	{
sl@0
    35
	CFileDescBase* ptr=0;
sl@0
    36
	iFids.InsertL(0, ptr, 8);	// set up the first 8 entries with zero
sl@0
    37
	}
sl@0
    38
sl@0
    39
CFileTable::~CFileTable()
sl@0
    40
	{
sl@0
    41
	Close();
sl@0
    42
	}
sl@0
    43
sl@0
    44
void CFileTable::Close()
sl@0
    45
	{
sl@0
    46
	for (TInt aFid=0; aFid<iFids.Count(); aFid++)
sl@0
    47
		{
sl@0
    48
		if (iFids[aFid]!=0 && iFids[aFid]!=RESERVED(aFid))
sl@0
    49
			{
sl@0
    50
			iFids[aFid]->RecvFromCancel();
sl@0
    51
			iFids[aFid]->Close();
sl@0
    52
			}
sl@0
    53
		Release(aFid);
sl@0
    54
		}
sl@0
    55
	}
sl@0
    56
sl@0
    57
void CFileTable::Release(TInt aFid)
sl@0
    58
	{
sl@0
    59
	iFids[aFid]=0;
sl@0
    60
	if (aFid<MINFILES || aFid<iFids.Count()-1)
sl@0
    61
		return;
sl@0
    62
	// opportunity to shrink the array
sl@0
    63
	while (aFid>=MINFILES && iFids[aFid]==0)
sl@0
    64
		{
sl@0
    65
		iFids.Delete(aFid);
sl@0
    66
		aFid--;
sl@0
    67
		}
sl@0
    68
	iFids.Compress();
sl@0
    69
	}
sl@0
    70
sl@0
    71
void CFileTable::Default(CFileDescBase* aConsole)
sl@0
    72
	{
sl@0
    73
	if (iFids[0]==0)
sl@0
    74
		iFids[0]=aConsole->Dup();
sl@0
    75
	if (iFids[1]==0)
sl@0
    76
		iFids[1]=aConsole->Dup();
sl@0
    77
	if (iFids[2]==0)
sl@0
    78
		iFids[2]=aConsole->Dup();
sl@0
    79
	}
sl@0
    80
sl@0
    81
TInt CFileTable::Attach(TInt aFid, CFileDescBase* aFile)
sl@0
    82
	{
sl@0
    83
	if (aFid<0 || aFid>=iFids.Count() || (iFids[aFid] != 0 && iFids[aFid]!=RESERVED(aFid)))
sl@0
    84
		return KErrArgument;
sl@0
    85
	iFids[aFid]=aFile;
sl@0
    86
	return KErrNone;
sl@0
    87
	}
sl@0
    88
sl@0
    89
TInt CFileTable::Reserve()
sl@0
    90
	{
sl@0
    91
	TInt aFid=0;
sl@0
    92
	TInt err=KErrNone;
sl@0
    93
	for (aFid=0; aFid<iFids.Count(); aFid++)
sl@0
    94
		if (iFids[aFid]==0)
sl@0
    95
			goto success;
sl@0
    96
	TRAP(err,iFids.ExtendL());
sl@0
    97
	if (err!=KErrNone)
sl@0
    98
		return err;
sl@0
    99
	aFid=iFids.Count()-1;
sl@0
   100
sl@0
   101
success:
sl@0
   102
	iFids[aFid]=RESERVED(aFid);
sl@0
   103
	return aFid;
sl@0
   104
	}
sl@0
   105
sl@0
   106
TInt CFileTable::Reserve(int aFids[3])
sl@0
   107
	{
sl@0
   108
	TInt i=0;
sl@0
   109
	for (i=0; i<3; i++)
sl@0
   110
		{
sl@0
   111
		if (aFids[i]==-1)
sl@0
   112
			{
sl@0
   113
			TInt fd=Reserve();
sl@0
   114
			if (fd<0)
sl@0
   115
				{
sl@0
   116
				Detach(aFids);	// release the ones we did get
sl@0
   117
				return KErrInUse;
sl@0
   118
				}
sl@0
   119
			aFids[i]=fd;
sl@0
   120
			}
sl@0
   121
		}
sl@0
   122
	return KErrNone;
sl@0
   123
	}
sl@0
   124
sl@0
   125
TInt CFileTable::Detach(int aFids[3])
sl@0
   126
	{
sl@0
   127
	TInt i=0;
sl@0
   128
	for (i=0; i<3; i++)
sl@0
   129
		{
sl@0
   130
		if (aFids[i]>=0)
sl@0
   131
			Attach(aFids[i],0);
sl@0
   132
		}
sl@0
   133
	return KErrInUse;
sl@0
   134
	}
sl@0
   135
sl@0
   136
TInt CFileTable::Detach(TInt aFid, CFileDescBase*& aDetached)
sl@0
   137
	{
sl@0
   138
	if (aFid<0 || aFid>=iFids.Count())
sl@0
   139
		return KErrArgument;
sl@0
   140
	aDetached=iFids[aFid];
sl@0
   141
	Release(aFid);
sl@0
   142
	return KErrNone;
sl@0
   143
	}
sl@0
   144
sl@0
   145
TInt CFileTable::At(TInt aFid, CFileDescBase*& aFound) const
sl@0
   146
	{
sl@0
   147
	if (aFid<0 || aFid>=iFids.Count())
sl@0
   148
		return KErrArgument;
sl@0
   149
	aFound=iFids[aFid];
sl@0
   150
	if (aFound==0)
sl@0
   151
		return KErrNotFound;
sl@0
   152
	return KErrNone;
sl@0
   153
	}
sl@0
   154
sl@0
   155
TInt CFileTable::Dup(TInt& aFid)
sl@0
   156
	{
sl@0
   157
	if (aFid<0 || aFid>=iFids.Count())
sl@0
   158
		return KErrArgument;
sl@0
   159
	CFileDescBase* fdesc=iFids[aFid];
sl@0
   160
	if (fdesc==0)
sl@0
   161
		return KErrNotFound;
sl@0
   162
	TInt aFid2=0;
sl@0
   163
	for (aFid2=0; aFid2<iFids.Count(); aFid2++)
sl@0
   164
		if (iFids[aFid2]==0)
sl@0
   165
			{
sl@0
   166
			iFids[aFid2]=fdesc->Dup();
sl@0
   167
			aFid=aFid2;
sl@0
   168
			return KErrNone;
sl@0
   169
			}
sl@0
   170
	return KErrInUse;
sl@0
   171
	}
sl@0
   172
sl@0
   173
TInt CFileTable::Dup2(TInt aFid, TInt aFid2)
sl@0
   174
	{
sl@0
   175
	if (aFid<0 || aFid>=iFids.Count() || aFid2<0 || aFid2>=iFids.Count() || aFid==aFid2)
sl@0
   176
		return KErrArgument;
sl@0
   177
	CFileDescBase* fdesc=iFids[aFid];
sl@0
   178
	if (fdesc==0)
sl@0
   179
		return KErrNotFound;
sl@0
   180
	CFileDescBase* old=iFids[aFid2];
sl@0
   181
	iFids[aFid2]=fdesc->Dup();
sl@0
   182
	if (old)
sl@0
   183
		old->Close();
sl@0
   184
	return KErrNone;
sl@0
   185
	}
sl@0
   186
sl@0
   187
// Simple synchronous services
sl@0
   188
sl@0
   189
int CFileTable::dup (int fid, int& anErrno)
sl@0
   190
	{
sl@0
   191
	TInt ret=Dup(fid);
sl@0
   192
	if (ret==KErrNone)
sl@0
   193
		return fid;
sl@0
   194
	return MapError(ret,anErrno);
sl@0
   195
	}
sl@0
   196
sl@0
   197
int CFileTable::dup2 (int fid, int fid2, int& anErrno)
sl@0
   198
	{
sl@0
   199
	TInt err=Dup2(fid,fid2);
sl@0
   200
	return MapError(err,anErrno);
sl@0
   201
	}
sl@0
   202
	
sl@0
   203
//int CFileTable::open (const char* name, int mode, int perms, int& anErrno, RFs& aFs)
sl@0
   204
//int CFileTable::open (const wchar_t* name, int mode, int perms, int& anErrno, RFs& aFs)
sl@0
   205
int CFileTable::open (const wchar_t* name, int mode, int perms, int& anErrno, RSessionBase& aFs)
sl@0
   206
	{
sl@0
   207
	int fd=Reserve();
sl@0
   208
	TInt err=fd;
sl@0
   209
	if (fd>=0)
sl@0
   210
		{
sl@0
   211
		//coverity[alloc_fn]
sl@0
   212
		//coverity[var_assign]
sl@0
   213
		CFileDescBase *f=CFileDescBase::Open(aFs,name,mode,perms,err);
sl@0
   214
		if (!err)
sl@0
   215
			{
sl@0
   216
			err=Attach(fd,f);
sl@0
   217
			if (!err)
sl@0
   218
				return fd;
sl@0
   219
			delete f;
sl@0
   220
			}
sl@0
   221
		Attach(fd,0);	// cancel the reservation
sl@0
   222
		//coverity[leaked_storage]
sl@0
   223
		}
sl@0
   224
	return MapError(err,anErrno);
sl@0
   225
	}
sl@0
   226
sl@0
   227
int CFileTable::socket (int family, int style, int protocol, int& anErrno, RSocketServ& aSs)
sl@0
   228
	{
sl@0
   229
	int fd=Reserve();
sl@0
   230
	TInt err=fd;
sl@0
   231
	if (fd>=0)
sl@0
   232
		{
sl@0
   233
		CFileDescBase *f=CFileDescBase::Socket(aSs,family,style,protocol,err);
sl@0
   234
		if (!err)
sl@0
   235
			{
sl@0
   236
			CleanupStack::PushL(f);
sl@0
   237
			err=Attach(fd,f);
sl@0
   238
			CleanupStack::PopAndDestroy(f);
sl@0
   239
			
sl@0
   240
			if (!err)
sl@0
   241
				{
sl@0
   242
				return fd;	
sl@0
   243
				}
sl@0
   244
			
sl@0
   245
			}
sl@0
   246
		
sl@0
   247
		Attach(fd,0);	// cancel the reservation
sl@0
   248
		}
sl@0
   249
sl@0
   250
	return MapError(err,anErrno);
sl@0
   251
	}
sl@0
   252
sl@0
   253
int CFileTable::userclose(int fid, int& anErrno)
sl@0
   254
	{
sl@0
   255
	CFileDescBase *f=0;
sl@0
   256
	TInt err=At(fid,f);
sl@0
   257
	if (!err)
sl@0
   258
		{
sl@0
   259
		f->UserClose();
sl@0
   260
		close(fid, anErrno);
sl@0
   261
		}
sl@0
   262
	else
sl@0
   263
		{
sl@0
   264
		if (KErrNotFound == err)
sl@0
   265
			err = EBADF;
sl@0
   266
		}
sl@0
   267
	return MapError(err,anErrno);
sl@0
   268
	}
sl@0
   269
sl@0
   270
int CFileTable::close (int fid, int& anErrno)
sl@0
   271
	{
sl@0
   272
	CFileDescBase *f=0;
sl@0
   273
	TInt err=Detach(fid,f);
sl@0
   274
	if (!err)
sl@0
   275
	   {
sl@0
   276
		if (f==0)
sl@0
   277
		  {
sl@0
   278
			err=EBADF;
sl@0
   279
		  }
sl@0
   280
		else
sl@0
   281
		  {
sl@0
   282
		    f->RecvFromCancel();
sl@0
   283
			err=f->Close();
sl@0
   284
		  }
sl@0
   285
	   }
sl@0
   286
	return MapError(err,anErrno);
sl@0
   287
	}
sl@0
   288
sl@0
   289
sl@0
   290
sl@0
   291
int CFileTable::lseek (int fid, int offset, int whence, int& anErrno)
sl@0
   292
	{
sl@0
   293
	CFileDescBase *f=0;
sl@0
   294
	TInt err=At(fid,f);
sl@0
   295
	if (!err)
sl@0
   296
		{
sl@0
   297
		err=f->LSeek(offset, whence);
sl@0
   298
		if (err==KErrNone)
sl@0
   299
			return offset;
sl@0
   300
		}
sl@0
   301
	return MapError(err,anErrno);
sl@0
   302
	}
sl@0
   303
sl@0
   304
int CFileTable::fstat (int fid, struct stat *st, int& anErrno)
sl@0
   305
	{
sl@0
   306
	CFileDescBase *f=0;
sl@0
   307
	TInt err=At(fid,f);
sl@0
   308
	if (!err)
sl@0
   309
		err=f->FStat(st);
sl@0
   310
	return MapError(err,anErrno);
sl@0
   311
	}
sl@0
   312
sl@0
   313
int CFileTable::listen (int fid, int n, int& anErrno)
sl@0
   314
	{
sl@0
   315
	CFileDescBase *f=0;
sl@0
   316
	TInt err=At(fid,f);
sl@0
   317
	if (!err)
sl@0
   318
		err=f->Listen(n);
sl@0
   319
	return MapError(err,anErrno);
sl@0
   320
	}
sl@0
   321
sl@0
   322
int CFileTable::bind (int fid, TSockAddr& address, int& anErrno)
sl@0
   323
	{
sl@0
   324
	CFileDescBase *f=0;
sl@0
   325
	TInt err=At(fid,f);
sl@0
   326
	if (!err)
sl@0
   327
		err=f->Bind(address);
sl@0
   328
	return MapError(err,anErrno);
sl@0
   329
	}
sl@0
   330
sl@0
   331
int CFileTable::sockname (int fid, TSockAddr& address, int anEnd, int& anErrno)
sl@0
   332
	{
sl@0
   333
	CFileDescBase *f=0;
sl@0
   334
	TInt err=At(fid,f);
sl@0
   335
	if (!err)
sl@0
   336
		err=f->SockName(anEnd, address);
sl@0
   337
	return MapError(err,anErrno);
sl@0
   338
	}
sl@0
   339
sl@0
   340
int CFileTable::getsockopt (int fid, int level, int opt, void* buf, unsigned long* len, int& anErrno)
sl@0
   341
	{
sl@0
   342
	CFileDescBase *f=0;
sl@0
   343
	TInt err=At(fid,f);
sl@0
   344
	if (!err)
sl@0
   345
		{
sl@0
   346
		TPtr8 ptr((TText8 *)buf, *len, *len);
sl@0
   347
		err=f->GetSockOpt(opt,level,ptr);
sl@0
   348
		if (err==0)
sl@0
   349
			*len=ptr.Length();
sl@0
   350
		}
sl@0
   351
	return MapError(err,anErrno);
sl@0
   352
	}
sl@0
   353
sl@0
   354
int CFileTable::setsockopt (int fid, int level, int opt, void* buf, unsigned long len, int& anErrno)
sl@0
   355
	{
sl@0
   356
	CFileDescBase *f=0;
sl@0
   357
	TInt err=At(fid,f);
sl@0
   358
	if (err==KErrNone)
sl@0
   359
		{
sl@0
   360
		//Negative option is not supported and have not been implemented yet
sl@0
   361
		//anErrno is then mapped to ENOSYS(88) (function not implemented yet)
sl@0
   362
		if (opt<=0)
sl@0
   363
			return MapError(KErrNotSupported,anErrno);
sl@0
   364
		TPtrC8 ptr((TText8 *)buf, len);
sl@0
   365
		err=f->SetSockOpt(opt,level,ptr);
sl@0
   366
		}
sl@0
   367
	return MapError(err,anErrno);
sl@0
   368
	}
sl@0
   369
sl@0
   370
int CFileTable::ioctlcomplete (int fid, int cmd, void* param, TRequestStatus& aStatus, int& anErrno)
sl@0
   371
	{
sl@0
   372
	CFileDescBase *f=0;
sl@0
   373
	TInt err=At(fid,f);
sl@0
   374
	if (!err)
sl@0
   375
		err=f->IoctlCompletion(cmd,param, aStatus.Int());
sl@0
   376
	return MapError(err,anErrno);
sl@0
   377
	}
sl@0
   378
sl@0
   379
int CFileTable::ioctlcancel (int fid, int& anErrno)
sl@0
   380
	{
sl@0
   381
	CFileDescBase *f=0;
sl@0
   382
	TInt err=At(fid,f);
sl@0
   383
	if (!err)
sl@0
   384
		f->IoctlCancel();
sl@0
   385
	return MapError(err,anErrno);
sl@0
   386
	}
sl@0
   387
sl@0
   388
// Preparation for an Asynchronous service
sl@0
   389
//
sl@0
   390
// The CFileDescBase* should be Closed after completion: the use of Dup prevents other
sl@0
   391
// clients from invalidating the pointer during an asynchronous operation.
sl@0
   392
sl@0
   393
TInt CFileTable::Asynch (int fid, CFileDescBase*& aFile)
sl@0
   394
	{
sl@0
   395
	TInt err=At(fid,aFile);
sl@0
   396
	if (!err)
sl@0
   397
		aFile->Dup();
sl@0
   398
	return err;
sl@0
   399
	}
sl@0
   400