os/kernelhwsrv/userlibandfileserver/fileserver/swins/elocal.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) 1995-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 the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "elocal.h"
sl@0
    17
#include <emulator.h>
sl@0
    18
#include <TCHAR.h>
sl@0
    19
sl@0
    20
const TInt KMajorVersionNumber=1;
sl@0
    21
const TInt KMinorVersionNumber=0;
sl@0
    22
sl@0
    23
const TUint KInvalidSetFilePointer = 0xffffffff;	// INVALID_SET_FILE_POINTER
sl@0
    24
sl@0
    25
#pragma data_seg(".data2")
sl@0
    26
#ifdef __VC32__
sl@0
    27
#pragma bss_seg(".data2")
sl@0
    28
#endif
sl@0
    29
static TInt ReadSpeed;
sl@0
    30
static TInt WriteSpeed;
sl@0
    31
#pragma data_seg()
sl@0
    32
#ifdef __VC32__
sl@0
    33
#pragma bss_seg()
sl@0
    34
#endif
sl@0
    35
sl@0
    36
static void Panic(TPanic aPanic)
sl@0
    37
	{
sl@0
    38
	User::Panic(_L("LocalFSys"),aPanic);
sl@0
    39
	}
sl@0
    40
sl@0
    41
sl@0
    42
//
sl@0
    43
// Map aDrive to a path given by environment variables
sl@0
    44
//
sl@0
    45
TBool MapDrive(TDes& aFileName, TInt aDrive)
sl@0
    46
	{
sl@0
    47
sl@0
    48
	TDriveName root(TDriveUnit(aDrive).Name());
sl@0
    49
	TFileName path;
sl@0
    50
	TBuf<3> rootWithSlash(root);
sl@0
    51
	rootWithSlash.Append('\\');
sl@0
    52
sl@0
    53
	if (MapEmulatedFileName(path, rootWithSlash) == KErrNone)
sl@0
    54
		{
sl@0
    55
		aFileName=path.Left(3);	// drive letter, colon and backslash
sl@0
    56
		return(ETrue);
sl@0
    57
		}
sl@0
    58
	aFileName=root;  // no trailing backslash
sl@0
    59
	return(EFalse);
sl@0
    60
	}
sl@0
    61
	
sl@0
    62
TInt MapFileName(TDes& aFileName, TInt aDrive, const TDesC& aName)
sl@0
    63
	{
sl@0
    64
	TFileName n(TDriveUnit(aDrive).Name());
sl@0
    65
	n.Append(aName);
sl@0
    66
	return MapEmulatedFileName(aFileName,n);
sl@0
    67
	}
sl@0
    68
sl@0
    69
void MapFileNameL(TDes& aFileName, TInt aDrive, const TDesC& aName)
sl@0
    70
	{
sl@0
    71
	User::LeaveIfError(MapFileName(aFileName,aDrive,aName));
sl@0
    72
	}
sl@0
    73
sl@0
    74
sl@0
    75
/**
sl@0
    76
Check whether a descriptor has enough space for null-terminating and append a zero terminator if it does.
sl@0
    77
Supposed to be used for Win32 file operations, taking C-like strings as parameters.
sl@0
    78
The main purpose is to avoid panics caused by descriptors overflow.
sl@0
    79
sl@0
    80
@param  aDes descriptor to be null-terminated; a file(directory) name presumably.
sl@0
    81
@return cast to LPCTSTR value of the descriptor, supposed to be the unicode string
sl@0
    82
@leave  KErrBadName if there is no room for trailing zero 
sl@0
    83
*/
sl@0
    84
LPCTSTR StrPtrZL(TDes16& aDes)
sl@0
    85
    {
sl@0
    86
    if(aDes.MaxLength() - aDes.Length() < 1)
sl@0
    87
        User::Leave(KErrBadName);  //-- no room for terminating zero
sl@0
    88
    
sl@0
    89
    return (LPCTSTR)aDes.PtrZ();
sl@0
    90
    }
sl@0
    91
sl@0
    92
sl@0
    93
sl@0
    94
//
sl@0
    95
// Converts a TTime structure to a Windows/NT filetime.  Assumes that aTime is a
sl@0
    96
// UTC (system) time
sl@0
    97
//
sl@0
    98
static void timeToFileTimeL(const TTime& aTime,FILETIME* f)
sl@0
    99
	{
sl@0
   100
sl@0
   101
	TDateTime dateTime=aTime.DateTime();
sl@0
   102
	SYSTEMTIME t;
sl@0
   103
	#pragma warning( disable : 4244 ) // conversion from 'const int' to 'unsigned short', possible loss of data
sl@0
   104
	t.wYear=dateTime.Year();
sl@0
   105
	t.wMonth=dateTime.Month()+1;
sl@0
   106
	t.wDay=dateTime.Day()+1;
sl@0
   107
	t.wDayOfWeek=(aTime.DayNoInWeek()+1)%7;
sl@0
   108
	t.wHour=dateTime.Hour();
sl@0
   109
	t.wMinute=dateTime.Minute();
sl@0
   110
	t.wSecond=dateTime.Second();
sl@0
   111
	t.wMilliseconds=dateTime.MicroSecond()/1000;
sl@0
   112
	#pragma warning( default : 4244 ) // conversion from 'const int' to 'unsigned short', possible loss of data
sl@0
   113
	__ASSERT_ALWAYS(SystemTimeToFileTime(&t,f)==TRUE,User::Leave(KErrArgument));
sl@0
   114
	}
sl@0
   115
sl@0
   116
//
sl@0
   117
//	Convert Windows/NT file time to TTime
sl@0
   118
//	Assumes the NT file time is UTC
sl@0
   119
//
sl@0
   120
static void fileTimeToTime(FILETIME* f,TTime& aTime)
sl@0
   121
	{
sl@0
   122
	SYSTEMTIME t;
sl@0
   123
	__ASSERT_ALWAYS(FileTimeToSystemTime(f,&t)==TRUE,Panic(EFileTimeToSystemTime));
sl@0
   124
	aTime=TDateTime(t.wYear,TMonth(t.wMonth-1),t.wDay-1,t.wHour,t.wMinute,t.wSecond,t.wMilliseconds*1000);
sl@0
   125
	}
sl@0
   126
sl@0
   127
//
sl@0
   128
// Return the size and free space on a drive.
sl@0
   129
//
sl@0
   130
static TInt GetMediaSize(TInt aDriveNumber,TInt64& aSize,TInt64& aFree)
sl@0
   131
	{
sl@0
   132
sl@0
   133
	TBuf<4> driveName;
sl@0
   134
	MapDrive(driveName,aDriveNumber);
sl@0
   135
	DWORD sectorsPerCluster;
sl@0
   136
	DWORD bytesPerSector;
sl@0
   137
	DWORD freeClusters;
sl@0
   138
	DWORD sizeClusters;
sl@0
   139
	// this function should be upgraded to GetDiskFreeSpaceEx
sl@0
   140
	BOOL b=Emulator::GetDiskFreeSpace((LPCTSTR)driveName.PtrZ(),&sectorsPerCluster,&bytesPerSector,&freeClusters,&sizeClusters);
sl@0
   141
	if (!b)
sl@0
   142
		return Emulator::LastError();
sl@0
   143
sl@0
   144
	TInt64 bytesPerCluster=(TInt)(sectorsPerCluster*bytesPerSector);
sl@0
   145
	aSize=TInt64((TInt)sizeClusters)*bytesPerCluster;
sl@0
   146
	aFree=TInt64((TInt)freeClusters)*bytesPerCluster;
sl@0
   147
	return(KErrNone);
sl@0
   148
	}
sl@0
   149
sl@0
   150
//
sl@0
   151
// Return the volume name and uniqueID.
sl@0
   152
//
sl@0
   153
static TInt GetVolumeId(TInt aDriveNumber,TUint& aUniqueID)
sl@0
   154
	{
sl@0
   155
sl@0
   156
	TBuf<4> driveName;
sl@0
   157
	MapDrive(driveName,aDriveNumber);
sl@0
   158
	DWORD uniqueID,componentLength,flags;
sl@0
   159
	BOOL b=Emulator::GetVolumeInformation((LPCTSTR)driveName.PtrZ(),NULL,0,&uniqueID,&componentLength,&flags,NULL,0);
sl@0
   160
	if (!b)
sl@0
   161
		return Emulator::LastError();
sl@0
   162
sl@0
   163
	aUniqueID=uniqueID;
sl@0
   164
	return(KErrNone);
sl@0
   165
	}
sl@0
   166
sl@0
   167
//#########################################################################################################################
sl@0
   168
//##        CLocalMountCB class implementation
sl@0
   169
//#########################################################################################################################
sl@0
   170
sl@0
   171
sl@0
   172
CLocalMountCB::CLocalMountCB()
sl@0
   173
	{
sl@0
   174
	}
sl@0
   175
sl@0
   176
CLocalMountCB::~CLocalMountCB()
sl@0
   177
	{
sl@0
   178
	}
sl@0
   179
sl@0
   180
sl@0
   181
//
sl@0
   182
// Returns ETrue if the drive == EDriveZ
sl@0
   183
//
sl@0
   184
TBool CLocalMountCB::IsRomDrive() const
sl@0
   185
	{
sl@0
   186
	// WINS emulated rom drive is Z:
sl@0
   187
	return(Drive().DriveNumber()==EDriveZ);
sl@0
   188
	}
sl@0
   189
sl@0
   190
//-------------------------------------------------------------------------------------------------------------------
sl@0
   191
sl@0
   192
/**
sl@0
   193
Mount a volume. 
sl@0
   194
sl@0
   195
@param aForceMount Flag to indicate whether mount should be forced to succeed if an error occurs
sl@0
   196
@leave KErrNoMemory,KErrNotReady,KErrCorrupt,KErrUnknown.
sl@0
   197
*/
sl@0
   198
void CLocalMountCB::MountL(TBool /*aForceMount*/)
sl@0
   199
	{
sl@0
   200
	TInt64 s,f;
sl@0
   201
	const TInt driveNum=Drive().DriveNumber();
sl@0
   202
	User::LeaveIfError(GetMediaSize(driveNum,s,f));
sl@0
   203
	
sl@0
   204
    iSize=s;
sl@0
   205
    if (driveNum==EDriveZ)
sl@0
   206
		iSize=4*1048576;
sl@0
   207
	
sl@0
   208
    User::LeaveIfError(GetVolumeId(driveNum,iUniqueID));
sl@0
   209
	SetVolumeName(HBufC::NewL(0));	// all Win32 volumes are unnamed
sl@0
   210
	
sl@0
   211
    //-- assign default value, 4G-1
sl@0
   212
    iMaxFileSizeSupported = ((TUint64)4 << 30)-1;
sl@0
   213
sl@0
   214
        {
sl@0
   215
        //-- find out the maximal supported file size. For this we need to query the name of the windows filesystem that is
sl@0
   216
        //-- used for the emulated drive
sl@0
   217
        TBuf<20> bufWinDrive;
sl@0
   218
        MapDrive(bufWinDrive, Drive().DriveNumber());
sl@0
   219
        ASSERT(bufWinDrive.Length() >= 3);
sl@0
   220
sl@0
   221
        TCHAR rootName[10];
sl@0
   222
        TCHAR volName[30];
sl@0
   223
        TCHAR volFSFileName[30];
sl@0
   224
        DWORD volSerNum;
sl@0
   225
        DWORD volMaxCompLen;
sl@0
   226
        DWORD volFsFlags;
sl@0
   227
        
sl@0
   228
        memset(rootName, 0, sizeof(rootName));
sl@0
   229
		//SL: added the cast
sl@0
   230
        wcsncpy(rootName, (const wchar_t*)bufWinDrive.Ptr(), 3); //- something like "k:\\"
sl@0
   231
sl@0
   232
        BOOL b = GetVolumeInformation(rootName, volName, sizeof(volName)/sizeof(TCHAR), &volSerNum, &volMaxCompLen, &volFsFlags, volFSFileName, sizeof(volFSFileName)/sizeof(TCHAR));
sl@0
   233
        if(b)
sl@0
   234
            {
sl@0
   235
            if(_wcsicmp(volFSFileName, _TEXT("NTFS")) == 0)
sl@0
   236
                {//-- this is NTFS
sl@0
   237
                iMaxFileSizeSupported = 0xFFFFFFF0000; //-- max. file size for NTFS
sl@0
   238
                }
sl@0
   239
             else
sl@0
   240
                {//-- theoretically other than FAT & NTFS filesystem are possible.. Figure yourself.
sl@0
   241
            }   }
sl@0
   242
        }
sl@0
   243
sl@0
   244
sl@0
   245
    }
sl@0
   246
sl@0
   247
//-------------------------------------------------------------------------------------------------------------------
sl@0
   248
/**
sl@0
   249
    Try remount this volume. Checks if the volume parameters remained the same as on original MountL() call, and
sl@0
   250
    if they are, re-initialises the mount. 
sl@0
   251
    @return KErrNone if the remount was OK
sl@0
   252
            system-wide error code otherwise
sl@0
   253
*/
sl@0
   254
TInt CLocalMountCB::ReMount()
sl@0
   255
	{
sl@0
   256
sl@0
   257
	TInt d=Drive().DriveNumber();
sl@0
   258
	TUint uniqueID;
sl@0
   259
	TInt r=GetVolumeId(d,uniqueID);
sl@0
   260
	if (r==KErrNone && uniqueID!=iUniqueID)
sl@0
   261
		r=KErrGeneral;
sl@0
   262
	return(r);
sl@0
   263
	}
sl@0
   264
sl@0
   265
//-------------------------------------------------------------------------------------------------------------------
sl@0
   266
void CLocalMountCB::Dismounted()
sl@0
   267
	{
sl@0
   268
    }
sl@0
   269
sl@0
   270
//-------------------------------------------------------------------------------------------------------------------
sl@0
   271
//
sl@0
   272
// Return the volume info.
sl@0
   273
//
sl@0
   274
void CLocalMountCB::VolumeL(TVolumeInfo& aVolume) const
sl@0
   275
	{
sl@0
   276
sl@0
   277
	TInt64 s,f;
sl@0
   278
	TInt driveNum=Drive().DriveNumber();
sl@0
   279
	User::LeaveIfError(GetMediaSize(driveNum,s,f));
sl@0
   280
	if (driveNum==EDriveZ)
sl@0
   281
		aVolume.iFree=0;
sl@0
   282
	else
sl@0
   283
		aVolume.iFree=f;
sl@0
   284
	}
sl@0
   285
sl@0
   286
//-------------------------------------------------------------------------------------------------------------------
sl@0
   287
//
sl@0
   288
// Set the volume label. Not supported on Win32 volumes
sl@0
   289
//
sl@0
   290
void CLocalMountCB::SetVolumeL(TDes&)
sl@0
   291
	{
sl@0
   292
sl@0
   293
	User::Leave(IsRomDrive() ? KErrAccessDenied : KErrNotSupported);
sl@0
   294
	}
sl@0
   295
sl@0
   296
//-------------------------------------------------------------------------------------------------------------------
sl@0
   297
//
sl@0
   298
// Return the address of the file if it is in rom
sl@0
   299
//
sl@0
   300
void CLocalMountCB::IsFileInRom(const TDesC& aName,TUint8*& aFileStart)
sl@0
   301
	{
sl@0
   302
sl@0
   303
	aFileStart=NULL;
sl@0
   304
	if (!IsRomDrive())
sl@0
   305
		return;
sl@0
   306
sl@0
   307
	TFileName n;
sl@0
   308
	if (MapFileName(n,Drive().DriveNumber(),aName)!=KErrNone)
sl@0
   309
		return;
sl@0
   310
	
sl@0
   311
	DWORD access=GENERIC_READ;
sl@0
   312
	DWORD share=FILE_SHARE_WRITE|FILE_SHARE_READ;
sl@0
   313
	DWORD create=OPEN_EXISTING;
sl@0
   314
	HANDLE h=Emulator::CreateFile((LPCTSTR)n.PtrZ(),access,share,NULL,create,FILE_FLAG_RANDOM_ACCESS,NULL);
sl@0
   315
	if (h==INVALID_HANDLE_VALUE)
sl@0
   316
		return;
sl@0
   317
sl@0
   318
	CLocalFileCB::RomAddress(aName, h, aFileStart);
sl@0
   319
	CloseHandle(h);
sl@0
   320
	}
sl@0
   321
sl@0
   322
//-------------------------------------------------------------------------------------------------------------------
sl@0
   323
/**
sl@0
   324
    Make a directory.
sl@0
   325
    @param aName full path to the directory to create. Name validity is checked by file server.
sl@0
   326
*/
sl@0
   327
void CLocalMountCB::MkDirL(const TDesC& aName)
sl@0
   328
	{
sl@0
   329
sl@0
   330
	if (IsRomDrive())
sl@0
   331
		User::Leave(KErrAccessDenied);
sl@0
   332
	TFileName n;
sl@0
   333
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   334
	BOOL b=Emulator::CreateDirectory(StrPtrZL(n),NULL);
sl@0
   335
	
sl@0
   336
	if (b)
sl@0
   337
		return;
sl@0
   338
	TInt r=Emulator::LastError();
sl@0
   339
	if (r!=KErrAlreadyExists)
sl@0
   340
		User::Leave(r);
sl@0
   341
	TEntry e;
sl@0
   342
	EntryL(aName,e);
sl@0
   343
sl@0
   344
	if (e.IsDir())
sl@0
   345
		User::Leave(KErrAlreadyExists);
sl@0
   346
	else
sl@0
   347
		User::Leave(KErrAccessDenied);
sl@0
   348
	}
sl@0
   349
sl@0
   350
//-------------------------------------------------------------------------------------------------------------------
sl@0
   351
/**
sl@0
   352
    Remove a directory.
sl@0
   353
    @param aName directory name
sl@0
   354
*/
sl@0
   355
void CLocalMountCB::RmDirL(const TDesC& aName)
sl@0
   356
	{
sl@0
   357
sl@0
   358
	if (IsRomDrive())
sl@0
   359
		User::Leave(KErrAccessDenied);
sl@0
   360
	
sl@0
   361
    TFileName n;
sl@0
   362
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   363
	BOOL b=Emulator::RemoveDirectory(StrPtrZL(n));
sl@0
   364
	
sl@0
   365
	if (!b)
sl@0
   366
		User::Leave(Emulator::LastError());
sl@0
   367
	}
sl@0
   368
sl@0
   369
//-------------------------------------------------------------------------------------------------------------------
sl@0
   370
//
sl@0
   371
// Delete a file.
sl@0
   372
//
sl@0
   373
void CLocalMountCB::DeleteL(const TDesC& aName)
sl@0
   374
	{
sl@0
   375
sl@0
   376
	if (IsRomDrive())
sl@0
   377
		User::Leave(KErrAccessDenied);
sl@0
   378
	
sl@0
   379
    //-- check entry attributes
sl@0
   380
    TEntry entry;
sl@0
   381
    EntryL(aName, entry);
sl@0
   382
	if (entry.IsDir() ||  entry.IsReadOnly())
sl@0
   383
	    User::Leave(KErrAccessDenied);
sl@0
   384
sl@0
   385
    TFileName n;
sl@0
   386
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   387
	BOOL b=Emulator::DeleteFile(StrPtrZL(n));
sl@0
   388
	
sl@0
   389
	if (!b)
sl@0
   390
		User::Leave(Emulator::LastError());
sl@0
   391
	}
sl@0
   392
sl@0
   393
//-------------------------------------------------------------------------------------------------------------------
sl@0
   394
//
sl@0
   395
// Rename a file or directory.
sl@0
   396
//
sl@0
   397
void CLocalMountCB::RenameL(const TDesC& aOldName,const TDesC& aNewName)
sl@0
   398
	{
sl@0
   399
sl@0
   400
	if (IsRomDrive())
sl@0
   401
		User::Leave(KErrAccessDenied);
sl@0
   402
	TEntry entry;
sl@0
   403
	TRAPD(r,EntryL(aNewName,entry));
sl@0
   404
	if (r!=KErrNone && r!=KErrNotFound)
sl@0
   405
		User::Leave(r);
sl@0
   406
	TFileName old;
sl@0
   407
	MapFileNameL(old,Drive().DriveNumber(),aOldName);
sl@0
   408
	TFileName n;
sl@0
   409
	MapFileNameL(n,Drive().DriveNumber(),aNewName);
sl@0
   410
	BOOL b=Emulator::MoveFile(StrPtrZL(old),StrPtrZL(n));
sl@0
   411
	
sl@0
   412
	if (!b)
sl@0
   413
		User::Leave(Emulator::LastError());
sl@0
   414
	}
sl@0
   415
sl@0
   416
//-------------------------------------------------------------------------------------------------------------------
sl@0
   417
void CLocalMountCB::ReplaceL(const TDesC& aOldName,const TDesC& aNewName)
sl@0
   418
//
sl@0
   419
// Delete aNewName if it exists and rename anOldName.
sl@0
   420
//
sl@0
   421
	{
sl@0
   422
sl@0
   423
	if (IsRomDrive())
sl@0
   424
		User::Leave(KErrAccessDenied);
sl@0
   425
	TEntry entry;
sl@0
   426
	if(FileNamesIdentical(aOldName,aNewName))
sl@0
   427
		{
sl@0
   428
		return;
sl@0
   429
		}
sl@0
   430
	TRAPD(r,DeleteL(aNewName));
sl@0
   431
	if (r!=KErrNotFound && r!=KErrNone)
sl@0
   432
		User::Leave(r);
sl@0
   433
	TFileName old;
sl@0
   434
	MapFileNameL(old,Drive().DriveNumber(),aOldName);
sl@0
   435
	TFileName n;
sl@0
   436
	MapFileNameL(n,Drive().DriveNumber(),aNewName);
sl@0
   437
	BOOL b=Emulator::MoveFile(StrPtrZL(old),StrPtrZL(n));
sl@0
   438
	if (!b)
sl@0
   439
		User::Leave(Emulator::LastError());
sl@0
   440
	}
sl@0
   441
	
sl@0
   442
//-------------------------------------------------------------------------------------------------------------------
sl@0
   443
//
sl@0
   444
//	Set and get file pointer for windows files
sl@0
   445
//	
sl@0
   446
static DWORD SetFilePointerL(HANDLE hFile,LONG lDistanceToMove,DWORD dwMoveMethod)
sl@0
   447
	
sl@0
   448
	{
sl@0
   449
	DWORD dwRet;
sl@0
   450
	
sl@0
   451
	dwRet=SetFilePointer(hFile,lDistanceToMove,0,dwMoveMethod);
sl@0
   452
	if (dwRet==KInvalidSetFilePointer)	//	INVALID_HANDLE_VALUE
sl@0
   453
		User::Leave(Emulator::LastError());
sl@0
   454
sl@0
   455
	return (dwRet);	
sl@0
   456
	}
sl@0
   457
sl@0
   458
//-------------------------------------------------------------------------------------------------------------------
sl@0
   459
//
sl@0
   460
//	Set and get file pointer for windows files
sl@0
   461
//	
sl@0
   462
static DWORD SetFilePointer64L(HANDLE hFile, LARGE_INTEGER * lpDistanceToMove, DWORD dwMoveMethod)
sl@0
   463
	{
sl@0
   464
sl@0
   465
	DWORD dwRet;
sl@0
   466
	
sl@0
   467
	dwRet=SetFilePointer(hFile, lpDistanceToMove->LowPart, &(lpDistanceToMove->HighPart), dwMoveMethod);
sl@0
   468
	
sl@0
   469
    TInt r = Emulator::LastError();
sl@0
   470
	if ((KInvalidSetFilePointer==dwRet) && (r != NO_ERROR))	
sl@0
   471
		User::Leave(r);
sl@0
   472
sl@0
   473
	return (dwRet);	
sl@0
   474
	}
sl@0
   475
sl@0
   476
//-------------------------------------------------------------------------------------------------------------------
sl@0
   477
/**
sl@0
   478
    Read file section without opening this file on a file server side.
sl@0
   479
    
sl@0
   480
    @param  aName       file name; all trailing dots from the name will be removed
sl@0
   481
    @param  aFilePos    start read position within a file
sl@0
   482
    @param  aLength     how many bytes to read; on return will be how many bytes actually read
sl@0
   483
    @param  aDes        local buffer desctriptor
sl@0
   484
    @param  aMessage    from file server, used to write data to the buffer in different address space.
sl@0
   485
sl@0
   486
    @leave on media read error
sl@0
   487
*/
sl@0
   488
void CLocalMountCB::ReadSectionL(const TDesC& aName,TInt aPos,TAny* aTrg,TInt aLength,const RMessagePtr2& aMessage)
sl@0
   489
	{
sl@0
   490
	
sl@0
   491
	TFileName n;
sl@0
   492
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   493
	
sl@0
   494
	WIN32_FIND_DATA d;
sl@0
   495
	HANDLE hFile=Emulator::FindFirstFile(StrPtrZL(n),&d);
sl@0
   496
	if (hFile==INVALID_HANDLE_VALUE)
sl@0
   497
		User::Leave(Emulator::LastError());
sl@0
   498
	FOREVER
sl@0
   499
		{
sl@0
   500
		TPtrC fileName((TText*)(&d.cFileName[0]));
sl@0
   501
		if (fileName!=_L(".") && fileName!=_L(".."))
sl@0
   502
			break;
sl@0
   503
		if (!Emulator::FindNextFile(hFile,&d))
sl@0
   504
			{
sl@0
   505
			TInt r = Emulator::LastError();
sl@0
   506
			User::Leave(r == KErrEof ? KErrNotFound : r);
sl@0
   507
			}
sl@0
   508
		}
sl@0
   509
	
sl@0
   510
	FindClose(hFile);
sl@0
   511
		
sl@0
   512
	hFile=Emulator::CreateFile(StrPtrZL(n),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
sl@0
   513
	if (hFile==INVALID_HANDLE_VALUE)
sl@0
   514
		return;
sl@0
   515
sl@0
   516
	DWORD dwSizeLow, dwSizeHigh;
sl@0
   517
	dwSizeLow=GetFileSize(hFile,&dwSizeHigh);
sl@0
   518
	TInt r = Emulator::LastError();
sl@0
   519
	if((NO_ERROR != r) && (INVALID_FILE_SIZE == dwSizeLow))
sl@0
   520
		User::Leave(r);
sl@0
   521
	
sl@0
   522
	// ReadSectionL can support only upto 2G as aPos is TInt!
sl@0
   523
	const TInt64 fileSize = MAKE_TINT64(dwSizeHigh, dwSizeHigh);
sl@0
   524
	if(fileSize > KMaxTInt)
sl@0
   525
		{
sl@0
   526
		if (!CloseHandle(hFile))
sl@0
   527
			User::Leave(Emulator::LastError());
sl@0
   528
sl@0
   529
		User::Leave(KErrTooBig);
sl@0
   530
		}
sl@0
   531
	
sl@0
   532
//	Check that reading from aPos for aLength lies within the file
sl@0
   533
//	if aPos is within the file, and aLength is too long, read up to EOF
sl@0
   534
//	If aPos is beyond the file, return a zero length descriptor
sl@0
   535
sl@0
   536
	if ((TInt)dwSizeLow>=(aPos+aLength))	//	Can read entire length requested from aPos	
sl@0
   537
		SetFilePointerL(hFile,aPos,FILE_BEGIN);			
sl@0
   538
	
sl@0
   539
	else if ((TInt)dwSizeLow>aPos)		//	Can read from aPos but not entire length requested
sl@0
   540
		{
sl@0
   541
		SetFilePointerL(hFile,aPos,FILE_BEGIN);
sl@0
   542
		aLength=dwSizeLow-aPos;
sl@0
   543
		}	
sl@0
   544
	else						//	Cannot read from aPos because it lies outside file
sl@0
   545
		{						//	Close file and leave with KErrEof
sl@0
   546
		if (!CloseHandle(hFile))
sl@0
   547
			User::Leave(Emulator::LastError());
sl@0
   548
sl@0
   549
		User::Leave(KErrEof);
sl@0
   550
		}
sl@0
   551
sl@0
   552
	TBuf8<0x1000> buf;
sl@0
   553
	TInt pos=0;
sl@0
   554
sl@0
   555
	if (aMessage.Handle() == KLocalMessageHandle)
sl@0
   556
		((TPtr8* )aTrg)->SetLength(0);
sl@0
   557
	
sl@0
   558
	while (aLength)
sl@0
   559
		{
sl@0
   560
		TInt readTotal=Min(aLength,buf.MaxLength());
sl@0
   561
		DWORD ret;
sl@0
   562
		BOOL b=ReadFile(hFile,(TAny*)buf.Ptr(),readTotal,&ret,NULL);
sl@0
   563
		if (!b || ((TInt)ret!=readTotal))	
sl@0
   564
			User::Leave(Emulator::LastError());
sl@0
   565
		buf.SetLength(ret);
sl@0
   566
		
sl@0
   567
		if(aMessage.Handle() == KLocalMessageHandle)
sl@0
   568
			((TPtr8* )aTrg)->Append(buf);
sl@0
   569
		else
sl@0
   570
			aMessage.WriteL(0,buf,pos);
sl@0
   571
	
sl@0
   572
		pos+=ret;
sl@0
   573
		if (((TInt)ret)<readTotal)
sl@0
   574
			break;
sl@0
   575
		aLength-=readTotal;
sl@0
   576
		}
sl@0
   577
			
sl@0
   578
	if (!CloseHandle(hFile))
sl@0
   579
		User::Leave(Emulator::LastError());
sl@0
   580
	}
sl@0
   581
sl@0
   582
sl@0
   583
//-------------------------------------------------------------------------------------------------------------------
sl@0
   584
//
sl@0
   585
// Read the entry uid if present
sl@0
   586
//
sl@0
   587
void CLocalMountCB::ReadUidL(const TDesC& aName,TEntry& anEntry) const
sl@0
   588
	{
sl@0
   589
sl@0
   590
//  First check to see if the first sixteen bytes form a valid UID
sl@0
   591
	TBuf<KMaxFileName + 1> fileName=aName;
sl@0
   592
	HANDLE hFile=Emulator::CreateFile(StrPtrZL(fileName),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
sl@0
   593
	if (hFile==INVALID_HANDLE_VALUE)
sl@0
   594
		return;
sl@0
   595
	DWORD ret;
sl@0
   596
	TBuf8<sizeof(TCheckedUid)> checkedUidBuf;
sl@0
   597
	checkedUidBuf.SetLength(sizeof(TCheckedUid));
sl@0
   598
	ReadFile(hFile,&checkedUidBuf[0],sizeof(TCheckedUid),&ret,NULL);
sl@0
   599
	if (ret!=sizeof(TCheckedUid))
sl@0
   600
		goto close;
sl@0
   601
	{
sl@0
   602
	TCheckedUid checkedUid(checkedUidBuf);
sl@0
   603
	if(checkedUid.UidType()!=TUidType(TUid::Null(),TUid::Null(),TUid::Null()))
sl@0
   604
		{
sl@0
   605
		anEntry.iType=checkedUid.UidType();
sl@0
   606
		goto close;
sl@0
   607
		}
sl@0
   608
	}
sl@0
   609
sl@0
   610
//Look at PE file for UID section
sl@0
   611
		{
sl@0
   612
		const TInt KPeHeaderAddrAddr=0x3c;
sl@0
   613
		const TInt KPeHeaderAddrSize=0x01;
sl@0
   614
		const TInt KNumberOfSectionsOffset=0x06;
sl@0
   615
		const TInt KNumberOfSectionsSize=0x02;
sl@0
   616
		const TInt KSectionTableOffset=0xf8;
sl@0
   617
		const TInt KSectionHeaderSize=0x28;
sl@0
   618
		const TInt KSectionNameLength=0x08;
sl@0
   619
		const TInt KPtrToRawDataOffset=0x14;
sl@0
   620
		const TInt KPtrToRawDataSize=0x04;
sl@0
   621
		const TText8 peText[4]={'P','E',0,0};
sl@0
   622
		const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'};
sl@0
   623
		
sl@0
   624
	//Read address of start of PE header
sl@0
   625
		if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==KInvalidSetFilePointer)
sl@0
   626
			goto close;
sl@0
   627
		TInt peAddr=0;
sl@0
   628
		ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL);
sl@0
   629
		if (ret!=KPeHeaderAddrSize)
sl@0
   630
			goto close;
sl@0
   631
		
sl@0
   632
	//Check it really is the start of PE header
sl@0
   633
		if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==KInvalidSetFilePointer)
sl@0
   634
			goto close;
sl@0
   635
		TText8 text[4];
sl@0
   636
		ReadFile(hFile,text,4,&ret,NULL);
sl@0
   637
		if (*(TInt32*)text!=*(TInt32*)peText)
sl@0
   638
			goto close;
sl@0
   639
		
sl@0
   640
	//Read number of sections
sl@0
   641
		if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==KInvalidSetFilePointer)
sl@0
   642
			goto close;
sl@0
   643
		TInt sections=0;
sl@0
   644
		ReadFile(hFile,&sections,KNumberOfSectionsSize,&ret,NULL);
sl@0
   645
		if (ret!=KNumberOfSectionsSize)
sl@0
   646
			goto close;
sl@0
   647
sl@0
   648
	//Go through section headers looking for UID section
sl@0
   649
		if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==KInvalidSetFilePointer)
sl@0
   650
			goto close;
sl@0
   651
		TInt i=0;
sl@0
   652
		for(;i<sections;i++)
sl@0
   653
			{
sl@0
   654
			TText8 name[KSectionNameLength];
sl@0
   655
			ReadFile(hFile,name,KSectionNameLength,&ret,NULL);
sl@0
   656
			if (ret!=KSectionNameLength)
sl@0
   657
				goto close;
sl@0
   658
			if (*(TInt64*)name==*(TInt64*)uidText)
sl@0
   659
				break;
sl@0
   660
			if (SetFilePointer(hFile,KSectionHeaderSize-KSectionNameLength,0,FILE_CURRENT)==KInvalidSetFilePointer)
sl@0
   661
				goto close;
sl@0
   662
			}
sl@0
   663
		if (i==sections)
sl@0
   664
			goto close;
sl@0
   665
sl@0
   666
	//Read RVA/Offset
sl@0
   667
		if (SetFilePointer(hFile,KPtrToRawDataOffset-KSectionNameLength,0,FILE_CURRENT)==KInvalidSetFilePointer)
sl@0
   668
			goto close;
sl@0
   669
		TInt uidOffset;
sl@0
   670
		ReadFile(hFile,&uidOffset,KPtrToRawDataSize,&ret,NULL);
sl@0
   671
		if (ret!=KPtrToRawDataSize)
sl@0
   672
			goto close;
sl@0
   673
sl@0
   674
	//Read UIDs!
sl@0
   675
		if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==KInvalidSetFilePointer)
sl@0
   676
			User::Leave(KErrGeneral);
sl@0
   677
sl@0
   678
		TEmulatorImageHeader header;
sl@0
   679
		ReadFile(hFile,&header,sizeof(header),&ret,NULL);
sl@0
   680
		if (ret==sizeof(header))
sl@0
   681
			anEntry.iType=*(TUidType*)&header;
sl@0
   682
		}
sl@0
   683
//Close file
sl@0
   684
close:
sl@0
   685
	if (!CloseHandle(hFile))
sl@0
   686
		User::Leave(Emulator::LastError());
sl@0
   687
	}
sl@0
   688
sl@0
   689
//-------------------------------------------------------------------------------------------------------------------
sl@0
   690
/**
sl@0
   691
    Try to find a directory entry by the given name and path. 
sl@0
   692
    This method _must_ leave if the entry is not found. See the caller.
sl@0
   693
sl@0
   694
    @param  aName   path to the directory object. all trailing dots from the name will be removed.
sl@0
   695
    @param  anEntry on return will contain the entry data
sl@0
   696
    
sl@0
   697
    @leave  KErrPathNotFound if there is no path to the aName
sl@0
   698
            KErrNotFound     if the entry corresponding to the aName is not found
sl@0
   699
            system-wide erorr code of media read failure.
sl@0
   700
*/
sl@0
   701
void CLocalMountCB::EntryL(const TDesC& aName,TEntry& anEntry) const
sl@0
   702
	{
sl@0
   703
sl@0
   704
	TFileName n;
sl@0
   705
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   706
	WIN32_FIND_DATA d;
sl@0
   707
	HANDLE h=Emulator::FindFirstFile(StrPtrZL(n),&d);
sl@0
   708
	if (h==INVALID_HANDLE_VALUE)
sl@0
   709
		User::Leave(Emulator::LastError());
sl@0
   710
	FOREVER
sl@0
   711
		{
sl@0
   712
		TPtrC fileName((TText*)(&d.cFileName[0]));
sl@0
   713
		if (fileName!=_L(".") && fileName!=_L(".."))
sl@0
   714
			break;
sl@0
   715
		if (!Emulator::FindNextFile(h,&d))
sl@0
   716
			{
sl@0
   717
			TInt r = Emulator::LastError();
sl@0
   718
			User::Leave(r == KErrEof ? KErrNotFound : r);
sl@0
   719
			}
sl@0
   720
		}
sl@0
   721
	FindClose(h);
sl@0
   722
	anEntry.iName.Des()=(TText*)(&d.cFileName[0]);
sl@0
   723
	anEntry.iAtt=d.dwFileAttributes&KEntryAttMaskSupported;
sl@0
   724
	if (IsRomDrive())
sl@0
   725
		anEntry.iAtt|=KEntryAttReadOnly;
sl@0
   726
sl@0
   727
	anEntry.SetFileSize(MAKE_TINT64(d.nFileSizeHigh,d.nFileSizeLow));
sl@0
   728
sl@0
   729
	fileTimeToTime(&d.ftLastWriteTime,anEntry.iModified);
sl@0
   730
	ReadUidL(n,anEntry);
sl@0
   731
	}
sl@0
   732
sl@0
   733
//-------------------------------------------------------------------------------------------------------------------
sl@0
   734
/**
sl@0
   735
    Set directory entry details.
sl@0
   736
    @param  aName           entry name; all trailing dots from the name will be removed
sl@0
   737
    @param  aTime           entry modification time (and last access as well)
sl@0
   738
    @param  aSetAttMask     entry attributes OR mask
sl@0
   739
    @param  aClearAttMask   entry attributes AND mask
sl@0
   740
sl@0
   741
*/
sl@0
   742
void CLocalMountCB::SetEntryL(const TDesC& aName,const TTime& aTime,TUint aSetAttMask,TUint aClearAttMask)
sl@0
   743
	{
sl@0
   744
sl@0
   745
	if (IsRomDrive())
sl@0
   746
		User::Leave(KErrAccessDenied);
sl@0
   747
	TFileName n;
sl@0
   748
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   749
	TUint setAttMask=aSetAttMask&KEntryAttMaskSupported;
sl@0
   750
	DWORD att=Emulator::GetFileAttributes(StrPtrZL(n));
sl@0
   751
	if (att==0xffffffffu)
sl@0
   752
		User::Leave(Emulator::LastError());
sl@0
   753
	
sl@0
   754
    if (setAttMask|aClearAttMask)
sl@0
   755
		{
sl@0
   756
		att|=setAttMask;
sl@0
   757
		att&=(~aClearAttMask);
sl@0
   758
		if (!Emulator::SetFileAttributes((LPCTSTR)n.Ptr(),att))
sl@0
   759
			User::Leave(Emulator::LastError());
sl@0
   760
		}
sl@0
   761
	
sl@0
   762
    if (aSetAttMask&KEntryAttModified)
sl@0
   763
		{
sl@0
   764
		FILETIME f;
sl@0
   765
		timeToFileTimeL(aTime,&f);
sl@0
   766
sl@0
   767
		if (att&KEntryAttReadOnly)
sl@0
   768
			{
sl@0
   769
			DWORD writeableAtt=att&(~KEntryAttReadOnly);
sl@0
   770
			if (!Emulator::SetFileAttributes((LPCTSTR)n.Ptr(),writeableAtt))
sl@0
   771
				User::Leave(Emulator::LastError());
sl@0
   772
			}
sl@0
   773
sl@0
   774
		HANDLE h;
sl@0
   775
		if (att&KEntryAttDir)
sl@0
   776
			{
sl@0
   777
			h=Emulator::CreateFile((LPCTSTR)n.Ptr(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_DIRECTORY|FILE_FLAG_BACKUP_SEMANTICS, NULL);
sl@0
   778
			if (h==INVALID_HANDLE_VALUE)
sl@0
   779
				User::Leave(Emulator::LastError());
sl@0
   780
			}
sl@0
   781
		else
sl@0
   782
			{
sl@0
   783
			h=Emulator::CreateFile((LPCTSTR)n.Ptr(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
sl@0
   784
			if (h==INVALID_HANDLE_VALUE)
sl@0
   785
				User::Leave(Emulator::LastError());
sl@0
   786
			}
sl@0
   787
sl@0
   788
		if (!SetFileTime(h,NULL,&f,&f))
sl@0
   789
			{
sl@0
   790
			TInt error = Emulator::LastError(); 
sl@0
   791
			CloseHandle(h);
sl@0
   792
			User::Leave(error);
sl@0
   793
			}
sl@0
   794
		
sl@0
   795
        if (!CloseHandle(h))
sl@0
   796
			User::Leave(Emulator::LastError());
sl@0
   797
		
sl@0
   798
        if ((att&KEntryAttReadOnly) && !Emulator::SetFileAttributes((LPCTSTR)n.Ptr(),att))
sl@0
   799
			User::Leave(Emulator::LastError());
sl@0
   800
		}
sl@0
   801
	}
sl@0
   802
sl@0
   803
//-------------------------------------------------------------------------------------------------------------------
sl@0
   804
/**
sl@0
   805
    Open/Create/Replace a file on the current mount.
sl@0
   806
    
sl@0
   807
    @param  aName   file name; all trailing dots from the name will be removed
sl@0
   808
    @param  aMode   File open mode, See TFileMode
sl@0
   809
    @param  anOpen  specifies action: open, create or replace the file
sl@0
   810
    @param  aFile   pointer to the CFileCB object to populate
sl@0
   811
sl@0
   812
*/
sl@0
   813
void CLocalMountCB::FileOpenL(const TDesC& aName,TUint aMode,TFileOpen anOpen,CFileCB* aFile)
sl@0
   814
	{
sl@0
   815
sl@0
   816
	if (IsRomDrive() && (anOpen!=EFileOpen || (aMode&EFileWrite)))
sl@0
   817
		User::Leave(KErrAccessDenied);
sl@0
   818
	TFileName n;
sl@0
   819
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   820
	
sl@0
   821
	DWORD access=GENERIC_READ|GENERIC_WRITE;
sl@0
   822
	DWORD share=FILE_SHARE_WRITE|FILE_SHARE_READ;
sl@0
   823
	DWORD create=0;
sl@0
   824
	switch (anOpen)
sl@0
   825
		{
sl@0
   826
	    case EFileOpen: create=OPEN_EXISTING; break;
sl@0
   827
	    case EFileCreate: create=CREATE_NEW; break;
sl@0
   828
	    case EFileReplace: create=CREATE_ALWAYS; break;
sl@0
   829
		}
sl@0
   830
sl@0
   831
	HANDLE h=Emulator::CreateFile(StrPtrZL(n),access,share,NULL,create,FILE_FLAG_RANDOM_ACCESS,NULL);
sl@0
   832
	
sl@0
   833
	if((h==INVALID_HANDLE_VALUE) && !(aMode&EFileWrite))
sl@0
   834
	{
sl@0
   835
	// If windows will not allow write access and it was not requested then open for read only
sl@0
   836
	access=GENERIC_READ;
sl@0
   837
	h=Emulator::CreateFile(StrPtrZL(n),access,share,NULL,create,FILE_FLAG_RANDOM_ACCESS,NULL);
sl@0
   838
	}
sl@0
   839
sl@0
   840
	if (h==INVALID_HANDLE_VALUE)
sl@0
   841
		User::Leave(Emulator::LastError());
sl@0
   842
	CLocalFileCB& file=(*((CLocalFileCB*)aFile));
sl@0
   843
	file.SetHandle(h);
sl@0
   844
	
sl@0
   845
    BY_HANDLE_FILE_INFORMATION info;
sl@0
   846
	if (!GetFileInformationByHandle(h,&info))
sl@0
   847
		User::Leave(Emulator::LastError());
sl@0
   848
	
sl@0
   849
    const TUint64 fileSize = MAKE_TUINT64(info.nFileSizeHigh, info.nFileSizeLow);
sl@0
   850
	
sl@0
   851
    // Check on file size
sl@0
   852
	if(MaxFileSizeSupported() < fileSize)
sl@0
   853
		User::Leave(KErrTooBig);
sl@0
   854
	
sl@0
   855
    file.SetMaxSupportedSize(MaxFileSizeSupported());
sl@0
   856
    file.SetSize64(fileSize, EFalse);
sl@0
   857
	file.SetAtt(info.dwFileAttributes&KEntryAttMaskSupported);
sl@0
   858
sl@0
   859
//	if (IsRomDrive())
sl@0
   860
//		file.iAtt|=KEntryAttReadOnly;
sl@0
   861
	TTime tempTime=file.Modified();
sl@0
   862
	fileTimeToTime(&info.ftLastWriteTime,tempTime);
sl@0
   863
	file.SetModified(tempTime);
sl@0
   864
	}
sl@0
   865
sl@0
   866
void AppendAsteriskL(TDes& aDes)
sl@0
   867
	{
sl@0
   868
	if (aDes.Length()==aDes.MaxLength())
sl@0
   869
		User::Leave(KErrBadName);
sl@0
   870
	aDes.Append('*');
sl@0
   871
	}
sl@0
   872
sl@0
   873
//-------------------------------------------------------------------------------------------------------------------	
sl@0
   874
/**
sl@0
   875
    Open a directory on the current mount.
sl@0
   876
    
sl@0
   877
    @param  aName   path to the object in the directory we want to open; all trailing dots from the name will be removed
sl@0
   878
    @param  aDir    dir. CB to be filled in.
sl@0
   879
    
sl@0
   880
    If there is no such a path, this method must leave with KErrPathNotFound
sl@0
   881
sl@0
   882
    @leave  KErrPathNotFound if thereis no such path
sl@0
   883
    @leave  error code on media read fault
sl@0
   884
*/
sl@0
   885
void CLocalMountCB::DirOpenL(const TDesC& aName,CDirCB* aDir)
sl@0
   886
	{
sl@0
   887
sl@0
   888
	TFileName n;
sl@0
   889
	TParse parse;
sl@0
   890
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
   891
	parse.Set(n,NULL,NULL);
sl@0
   892
	n=parse.DriveAndPath();
sl@0
   893
	AppendAsteriskL(n);
sl@0
   894
	WIN32_FIND_DATA info;
sl@0
   895
	HANDLE h=Emulator::FindFirstFile(StrPtrZL(n),&info);
sl@0
   896
	if (h==INVALID_HANDLE_VALUE)
sl@0
   897
		{
sl@0
   898
		TInt error=Emulator::LastError();
sl@0
   899
		TParse parser;
sl@0
   900
		TInt r=parser.Set(n,NULL,NULL);
sl@0
   901
		if (r!=KErrNone)
sl@0
   902
			User::Leave(r);
sl@0
   903
		if (!parser.IsRoot() || Drive().DriveNumber()!=0 || error!=KErrNotFound)
sl@0
   904
			User::Leave(error);
sl@0
   905
		h=NULL;
sl@0
   906
		}
sl@0
   907
	CLocalDirCB& dir=(*((CLocalDirCB*)aDir));
sl@0
   908
	dir.SetHandle(h);
sl@0
   909
	dir.SetPending(ETrue);
sl@0
   910
	dir.iEntry.iName.Des()=(TText*)(&info.cFileName[0]);
sl@0
   911
	dir.iEntry.iAtt=info.dwFileAttributes&KEntryAttMaskSupported;
sl@0
   912
sl@0
   913
	const TInt64 fileSize = MAKE_TINT64(info.nFileSizeHigh,info.nFileSizeLow);
sl@0
   914
	dir.iEntry.SetFileSize(fileSize);
sl@0
   915
sl@0
   916
	n=parse.FullName();
sl@0
   917
	if (parse.NameAndExt().Length()==0)
sl@0
   918
		AppendAsteriskL(n);
sl@0
   919
	dir.SetFullName(n);
sl@0
   920
	fileTimeToTime(&info.ftLastWriteTime,dir.iEntry.iModified);
sl@0
   921
	}
sl@0
   922
sl@0
   923
//-------------------------------------------------------------------------------------------------------------------	
sl@0
   924
//
sl@0
   925
// Read directly from disk
sl@0
   926
//
sl@0
   927
void CLocalMountCB::RawReadL(TInt64 /*aPos*/,TInt /*aLength*/,const TAny* /*aDes*/,TInt /*anOffset*/,const RMessagePtr2& /*aMessage*/) const
sl@0
   928
	{
sl@0
   929
	User::Leave(KErrNotSupported);
sl@0
   930
	}
sl@0
   931
sl@0
   932
//-------------------------------------------------------------------------------------------------------------------	
sl@0
   933
//
sl@0
   934
// Write directly to disk
sl@0
   935
//
sl@0
   936
void CLocalMountCB::RawWriteL(TInt64 /*aPos*/,TInt /*aLength*/,const TAny* /*aDes*/ ,TInt /*anOffset*/,const RMessagePtr2& /*aMessage*/)
sl@0
   937
	{
sl@0
   938
	User::Leave(KErrNotSupported);
sl@0
   939
	}
sl@0
   940
sl@0
   941
//-------------------------------------------------------------------------------------------------------------------	
sl@0
   942
//
sl@0
   943
// Get the short name associated with aLongName
sl@0
   944
//
sl@0
   945
void CLocalMountCB::GetShortNameL(const TDesC& aLongName,TDes& aShortName)
sl@0
   946
	{
sl@0
   947
sl@0
   948
	if (IsRomDrive())
sl@0
   949
		User::Leave(KErrNotSupported);
sl@0
   950
sl@0
   951
	TFileName n;
sl@0
   952
	MapFileNameL(n,Drive().DriveNumber(),aLongName);
sl@0
   953
	WIN32_FIND_DATA d;
sl@0
   954
	HANDLE h=Emulator::FindFirstFile(StrPtrZL(n),&d);
sl@0
   955
	if (h==INVALID_HANDLE_VALUE)
sl@0
   956
		User::Leave(Emulator::LastError());
sl@0
   957
	FindClose(h);
sl@0
   958
    if (d.cAlternateFileName[0])	// we have a dos name too
sl@0
   959
        aShortName=(TText*)(&d.cAlternateFileName[0]);
sl@0
   960
	else
sl@0
   961
		aShortName=(TText*)(&d.cFileName[0]);
sl@0
   962
	}
sl@0
   963
sl@0
   964
//-------------------------------------------------------------------------------------------------------------------	
sl@0
   965
//
sl@0
   966
// Get the short name associated with aLongName
sl@0
   967
//
sl@0
   968
void CLocalMountCB::GetLongNameL(const TDesC& aShortName,TDes& aLongName)
sl@0
   969
	{
sl@0
   970
sl@0
   971
	if (IsRomDrive())
sl@0
   972
		User::Leave(KErrNotSupported);
sl@0
   973
sl@0
   974
	TFileName n;
sl@0
   975
	MapFileNameL(n,Drive().DriveNumber(),aShortName);
sl@0
   976
	WIN32_FIND_DATA d;
sl@0
   977
	HANDLE h=Emulator::FindFirstFile(StrPtrZL(n),&d);
sl@0
   978
	if (h==INVALID_HANDLE_VALUE)
sl@0
   979
		User::Leave(Emulator::LastError());
sl@0
   980
	FindClose(h);
sl@0
   981
	aLongName=(TText*)(&d.cFileName[0]);
sl@0
   982
	}
sl@0
   983
sl@0
   984
//-------------------------------------------------------------------------------------------------------------------	
sl@0
   985
/**
sl@0
   986
Reports whether the specified interface is supported - if it is,
sl@0
   987
the supplied interface object is modified to it
sl@0
   988
sl@0
   989
@param aInterfaceId     The interface of interest
sl@0
   990
@param aInterface       The interface object
sl@0
   991
@return                 KErrNone if the interface is supported, otherwise KErrNotFound 
sl@0
   992
sl@0
   993
@see CMountCB::GetInterface()
sl@0
   994
*/
sl@0
   995
TInt CLocalMountCB::GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput)
sl@0
   996
    {
sl@0
   997
	switch(aInterfaceId)
sl@0
   998
		{
sl@0
   999
		case EFileExtendedInterface:
sl@0
  1000
			((CMountCB::MFileExtendedInterface*&) aInterface) = this;
sl@0
  1001
			return KErrNone;
sl@0
  1002
sl@0
  1003
    	case ELocalBufferSupport:
sl@0
  1004
    		// CLocalMountCB doesn't ever use any extensions?
sl@0
  1005
	    	// 	- seems to not have any iProxyDrive or LocalDrive() or similar, 
sl@0
  1006
    		// so we'll just return KErrNone here.
sl@0
  1007
   			return KErrNone;
sl@0
  1008
sl@0
  1009
		default:
sl@0
  1010
		    return CMountCB::GetInterface(aInterfaceId,aInterface,aInput);
sl@0
  1011
		}
sl@0
  1012
    }
sl@0
  1013
sl@0
  1014
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1015
TInt CLocalMountCB::LocalBufferSupport()
sl@0
  1016
	{
sl@0
  1017
	TAny* dummyInterface = NULL;
sl@0
  1018
	TAny* dummyInput = NULL;
sl@0
  1019
	return GetInterface(ELocalBufferSupport,dummyInterface,dummyInput);
sl@0
  1020
	}
sl@0
  1021
sl@0
  1022
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1023
/**
sl@0
  1024
    Read file section without opening this file on a file server side.
sl@0
  1025
    
sl@0
  1026
    @param  aName       file name; all trailing dots from the name will be removed
sl@0
  1027
    @param  aFilePos    start read position within a file
sl@0
  1028
    @param  aLength     how many bytes to read; on return will be how many bytes actually read
sl@0
  1029
    @param  aDes        local buffer desctriptor
sl@0
  1030
    @param  aMessage    from file server, used to write data to the buffer in different address space.
sl@0
  1031
sl@0
  1032
    @leave on media read error
sl@0
  1033
*/
sl@0
  1034
void CLocalMountCB::ReadSection64L(const TDesC& aName, TInt64 aPos, TAny* aTrg, TInt aLength, const RMessagePtr2& aMessage)
sl@0
  1035
	{
sl@0
  1036
	TFileName n;
sl@0
  1037
	MapFileNameL(n,Drive().DriveNumber(),aName);
sl@0
  1038
	
sl@0
  1039
	WIN32_FIND_DATA d;
sl@0
  1040
	HANDLE hFile=Emulator::FindFirstFile(StrPtrZL(n),&d);
sl@0
  1041
	if (hFile==INVALID_HANDLE_VALUE)
sl@0
  1042
		User::Leave(Emulator::LastError());
sl@0
  1043
	
sl@0
  1044
	FOREVER
sl@0
  1045
		{
sl@0
  1046
		TPtrC fileName((TText*)(&d.cFileName[0]));
sl@0
  1047
		if (fileName!=_L(".") && fileName!=_L(".."))
sl@0
  1048
			break;
sl@0
  1049
		if (!Emulator::FindNextFile(hFile,&d))
sl@0
  1050
			{
sl@0
  1051
			TInt r = Emulator::LastError();
sl@0
  1052
			User::Leave(r == KErrEof ? KErrNotFound : r);
sl@0
  1053
			}
sl@0
  1054
		}
sl@0
  1055
	
sl@0
  1056
	FindClose(hFile);
sl@0
  1057
	
sl@0
  1058
	hFile=Emulator::CreateFile(StrPtrZL(n),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
sl@0
  1059
	if (hFile==INVALID_HANDLE_VALUE)
sl@0
  1060
		return;
sl@0
  1061
sl@0
  1062
	DWORD dwSizeLow, dwSizeHigh;
sl@0
  1063
	dwSizeLow=GetFileSize(hFile,&dwSizeHigh);
sl@0
  1064
	TInt r = Emulator::LastError();
sl@0
  1065
	if((NO_ERROR != r) && (INVALID_FILE_SIZE == dwSizeLow))
sl@0
  1066
		User::Leave(r);
sl@0
  1067
	
sl@0
  1068
	// Check on file size 
sl@0
  1069
	const TInt64 fileSize = MAKE_TINT64(dwSizeHigh, dwSizeLow);
sl@0
  1070
	if(MaxFileSizeSupported() < (TUint64)fileSize)
sl@0
  1071
		{
sl@0
  1072
		if (!CloseHandle(hFile))
sl@0
  1073
			User::Leave(Emulator::LastError());
sl@0
  1074
		
sl@0
  1075
        User::Leave(KErrTooBig);
sl@0
  1076
		}
sl@0
  1077
	
sl@0
  1078
//	Check that reading from aPos for aLength lies within the file
sl@0
  1079
//	if aPos is within the file, and aLength is too long, read up to EOF
sl@0
  1080
//	If aPos is beyond the file, return a zero length descriptor
sl@0
  1081
sl@0
  1082
	if (fileSize>=aPos+aLength)	//	Can read entire length requested from aPos	
sl@0
  1083
		SetFilePointer64L(hFile,(LARGE_INTEGER *)&aPos,FILE_BEGIN);
sl@0
  1084
	
sl@0
  1085
	else if (fileSize>aPos)		//	Can read from aPos but not entire length requested
sl@0
  1086
		{
sl@0
  1087
		SetFilePointer64L(hFile,(LARGE_INTEGER *)&aPos,FILE_BEGIN);
sl@0
  1088
		aLength=(TInt)(fileSize-aPos);
sl@0
  1089
		}	
sl@0
  1090
	else						//	Cannot read from aPos because it lies outside file
sl@0
  1091
		{						//	Close file and leave with KErrEof
sl@0
  1092
		if (!CloseHandle(hFile))
sl@0
  1093
			User::Leave(Emulator::LastError());
sl@0
  1094
sl@0
  1095
		User::Leave(KErrEof);
sl@0
  1096
		}
sl@0
  1097
sl@0
  1098
	TBuf8<0x1000> buf;
sl@0
  1099
	TInt pos=0;
sl@0
  1100
sl@0
  1101
	if (aMessage.Handle() == KLocalMessageHandle)
sl@0
  1102
		((TPtr8* )aTrg)->SetLength(0);
sl@0
  1103
	
sl@0
  1104
	while (aLength)
sl@0
  1105
		{
sl@0
  1106
		TInt readTotal=Min(aLength,buf.MaxLength());
sl@0
  1107
		DWORD ret;
sl@0
  1108
		BOOL b=ReadFile(hFile,(TAny*)buf.Ptr(),readTotal,&ret,NULL);
sl@0
  1109
		if (!b || ((TInt)ret!=readTotal))	
sl@0
  1110
			User::Leave(Emulator::LastError());
sl@0
  1111
		buf.SetLength(ret);
sl@0
  1112
		
sl@0
  1113
		if(aMessage.Handle() == KLocalMessageHandle)
sl@0
  1114
			((TPtr8* )aTrg)->Append(buf);
sl@0
  1115
		else
sl@0
  1116
			aMessage.WriteL(0,buf,pos);
sl@0
  1117
		
sl@0
  1118
		pos+=ret;
sl@0
  1119
		if (((TInt)ret)<readTotal)
sl@0
  1120
			break;
sl@0
  1121
		aLength-=readTotal;
sl@0
  1122
		}
sl@0
  1123
	
sl@0
  1124
	if (!CloseHandle(hFile))
sl@0
  1125
		User::Leave(Emulator::LastError());
sl@0
  1126
	}
sl@0
  1127
sl@0
  1128
//-------------------------------------------------------------------------------------------------------------------
sl@0
  1129
sl@0
  1130
/**
sl@0
  1131
    CLocalMountCB control method.
sl@0
  1132
    @param  aLevel  specifies the operation to perfrom on the mount
sl@0
  1133
    @param  aOption specific option for the given operation
sl@0
  1134
    @param  aParam  pointer to generic parameter, its meaning depends on aLevel and aOption
sl@0
  1135
sl@0
  1136
    @return standard error code.
sl@0
  1137
*/
sl@0
  1138
sl@0
  1139
TInt CLocalMountCB::MountControl(TInt aLevel, TInt aOption, TAny* aParam)
sl@0
  1140
    {
sl@0
  1141
    //-- File System - specific queries 
sl@0
  1142
    if(aLevel == EMountFsParamQuery && aOption == ESQ_GetMaxSupportedFileSize)
sl@0
  1143
        {//-- this is a query to provide the max. supported file size; aParam is a pointer to TUint64 to return the value
sl@0
  1144
        *(TUint64*)aParam = MaxFileSizeSupported();    
sl@0
  1145
        return KErrNone;
sl@0
  1146
        }
sl@0
  1147
sl@0
  1148
    return KErrNotSupported; 
sl@0
  1149
    }
sl@0
  1150
sl@0
  1151
//#########################################################################################################################
sl@0
  1152
//##        CLocalFileCB class implementation
sl@0
  1153
//#########################################################################################################################
sl@0
  1154
sl@0
  1155
sl@0
  1156
CLocalFileCB::CLocalFileCB()
sl@0
  1157
	{
sl@0
  1158
	}
sl@0
  1159
sl@0
  1160
CLocalFileCB::~CLocalFileCB()
sl@0
  1161
	{
sl@0
  1162
sl@0
  1163
	if (iAtt&KEntryAttModified)
sl@0
  1164
		{
sl@0
  1165
		TRAPD(ret,FlushDataL());
sl@0
  1166
//		if (ret!=KErrNone) // Can fail if floppy disk is removed
sl@0
  1167
//			Panic(EFileClose); // Ignore error
sl@0
  1168
		}
sl@0
  1169
	if (iWinHandle!=NULL && !CloseHandle(iWinHandle))
sl@0
  1170
		Panic(EFileClose);
sl@0
  1171
	}
sl@0
  1172
sl@0
  1173
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1174
//
sl@0
  1175
// Returns ETrue if the drive number == EDriveZ
sl@0
  1176
//
sl@0
  1177
TBool CLocalFileCB::IsRomDrive() const
sl@0
  1178
	{
sl@0
  1179
sl@0
  1180
	// WINS emulated rom drive is Z:
sl@0
  1181
	return(((CLocalFileCB*)this)->Mount().Drive().DriveNumber()==EDriveZ);
sl@0
  1182
	}
sl@0
  1183
sl@0
  1184
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1185
//
sl@0
  1186
//	Check that the file pointer iCurrentPos is positioned correctly
sl@0
  1187
//	in relation to the Win32 file pointer
sl@0
  1188
//
sl@0
  1189
void CLocalFileCB::CheckPosL(TInt64 aPos)
sl@0
  1190
	{
sl@0
  1191
//	Get the current Win32 file pointer position	
sl@0
  1192
	LARGE_INTEGER pos;
sl@0
  1193
	pos.QuadPart = 0;
sl@0
  1194
	DWORD position=SetFilePointer(iWinHandle,pos.LowPart,&pos.HighPart,FILE_CURRENT);
sl@0
  1195
	TInt r = Emulator::LastError();
sl@0
  1196
	if ((KInvalidSetFilePointer == position) && (r != NO_ERROR))
sl@0
  1197
		User::Leave(r);
sl@0
  1198
//	Set iCurrentPos and Win32 file pointers to aPos if they are different to each
sl@0
  1199
//	other or different to aPos
sl@0
  1200
	if ((pos.QuadPart!=iCurrentPos) || (iCurrentPos!=aPos))
sl@0
  1201
		{
sl@0
  1202
		iCurrentPos=(-1);
sl@0
  1203
		pos.QuadPart = aPos;
sl@0
  1204
		position = SetFilePointer(iWinHandle,pos.LowPart,&pos.HighPart,FILE_BEGIN);
sl@0
  1205
		r = Emulator::LastError();
sl@0
  1206
		if ((KInvalidSetFilePointer == position) && (r != NO_ERROR))
sl@0
  1207
			User::Leave(r);
sl@0
  1208
		iCurrentPos=aPos;
sl@0
  1209
		}
sl@0
  1210
	}
sl@0
  1211
sl@0
  1212
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1213
void CLocalFileCB::ReadL(TInt aPos,TInt& aLength,const TAny* aDes,const RMessagePtr2& aMessage)
sl@0
  1214
	{
sl@0
  1215
	ReadL((TInt64)aPos, aLength, (TDes8*)aDes, aMessage, 0);
sl@0
  1216
	}
sl@0
  1217
sl@0
  1218
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1219
void CLocalFileCB::WriteL(TInt aPos,TInt& aLength,const TAny* aDes,const RMessagePtr2& aMessage)
sl@0
  1220
	{
sl@0
  1221
	WriteL((TInt64)aPos, aLength, (TDesC8*)aDes, aMessage, 0);
sl@0
  1222
	}
sl@0
  1223
sl@0
  1224
struct SRomMap
sl@0
  1225
	{
sl@0
  1226
	HBufC* iName;
sl@0
  1227
	TUint8* iAddr;
sl@0
  1228
	};
sl@0
  1229
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1230
sl@0
  1231
TInt CLocalFileCB::RomAddress(const TDesC& aName, HANDLE aFile, TUint8*& aAddr)
sl@0
  1232
	{
sl@0
  1233
	static CArrayFixSeg<SRomMap>* gRomMap = new CArrayFixSeg<SRomMap>(64);
sl@0
  1234
	for (TInt ii=0; ii<gRomMap->Count(); ii++)
sl@0
  1235
		{
sl@0
  1236
		if (*gRomMap->At(ii).iName == aName)
sl@0
  1237
			{
sl@0
  1238
			aAddr = gRomMap->At(ii).iAddr;
sl@0
  1239
			return KErrNone;
sl@0
  1240
			}
sl@0
  1241
		}
sl@0
  1242
sl@0
  1243
	HANDLE fileMapping=CreateFileMappingA(aFile,NULL,PAGE_READONLY,0,0,NULL);
sl@0
  1244
	if (fileMapping==0)
sl@0
  1245
		return Emulator::LastError();
sl@0
  1246
	aAddr=(TUint8*)MapViewOfFile(fileMapping,FILE_MAP_READ,0,0,0);
sl@0
  1247
	SRomMap entry;
sl@0
  1248
	entry.iAddr = aAddr;
sl@0
  1249
	entry.iName = aName.Alloc();
sl@0
  1250
	if (entry.iName)
sl@0
  1251
		{
sl@0
  1252
		TRAPD(ignore, gRomMap->AppendL(entry));
sl@0
  1253
		}
sl@0
  1254
	return KErrNone;
sl@0
  1255
	}
sl@0
  1256
sl@0
  1257
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1258
//
sl@0
  1259
// If ROM file, do a memory map and return the address
sl@0
  1260
//
sl@0
  1261
TInt CLocalFileCB::Address(TInt& aPos) const
sl@0
  1262
	{
sl@0
  1263
sl@0
  1264
	TBool isRomFile=IsRomDrive();
sl@0
  1265
	if (!isRomFile)
sl@0
  1266
		return(KErrNotSupported);
sl@0
  1267
	
sl@0
  1268
	if (aPos>Size64())
sl@0
  1269
		return(KErrEof);
sl@0
  1270
	if (iFilePtr==NULL)
sl@0
  1271
		{
sl@0
  1272
		CLocalFileCB* This=(CLocalFileCB*)this;
sl@0
  1273
		TInt err = RomAddress(*iFileName, iWinHandle, This->iFilePtr);
sl@0
  1274
		if (err)
sl@0
  1275
			return err;
sl@0
  1276
		}
sl@0
  1277
	aPos=(TInt)((TUint8*)iFilePtr+aPos);
sl@0
  1278
	return(KErrNone);
sl@0
  1279
	}
sl@0
  1280
sl@0
  1281
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1282
//
sl@0
  1283
// Set the file size.
sl@0
  1284
//
sl@0
  1285
void CLocalFileCB::SetSizeL(TInt aSize)
sl@0
  1286
	{
sl@0
  1287
	SetSizeL(aSize);
sl@0
  1288
	}
sl@0
  1289
sl@0
  1290
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1291
//
sl@0
  1292
// Set the entry's attributes and modified time.
sl@0
  1293
//
sl@0
  1294
void CLocalFileCB::SetEntryL(const TTime& aTime,TUint aSetAttMask,TUint aClearAttMask)
sl@0
  1295
	{
sl@0
  1296
sl@0
  1297
	if (IsRomDrive())
sl@0
  1298
		User::Leave(KErrAccessDenied);
sl@0
  1299
	TUint setAttMask=aSetAttMask&KEntryAttMaskSupported;
sl@0
  1300
	if (setAttMask|aClearAttMask)
sl@0
  1301
		{
sl@0
  1302
		iAtt|=setAttMask;
sl@0
  1303
		iAtt&=(~aClearAttMask);
sl@0
  1304
		iAtt|=KEntryAttModified;
sl@0
  1305
		}
sl@0
  1306
	if (aSetAttMask&KEntryAttModified)
sl@0
  1307
		iModified=aTime;
sl@0
  1308
	iAtt|=KEntryAttModified;
sl@0
  1309
	}
sl@0
  1310
sl@0
  1311
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1312
//
sl@0
  1313
// Commit any buffered date to the media.
sl@0
  1314
//
sl@0
  1315
void CLocalFileCB::FlushAllL()
sl@0
  1316
	{
sl@0
  1317
	FlushDataL();
sl@0
  1318
	}
sl@0
  1319
sl@0
  1320
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1321
//
sl@0
  1322
// Commit any buffered date to the media.
sl@0
  1323
//
sl@0
  1324
void CLocalFileCB::FlushDataL()
sl@0
  1325
	{
sl@0
  1326
sl@0
  1327
	if (IsRomDrive())
sl@0
  1328
		return;
sl@0
  1329
sl@0
  1330
	TFileName n;
sl@0
  1331
	TInt driveNumber=Mount().Drive().DriveNumber();
sl@0
  1332
	MapFileNameL(n,driveNumber,FileName());
sl@0
  1333
	
sl@0
  1334
    if(!Emulator::SetFileAttributes(StrPtrZL(n),iAtt&KEntryAttMaskSupported))
sl@0
  1335
		User::Leave(Emulator::LastError()); //	Panic(EFileCloseSetAttributes);
sl@0
  1336
	FILETIME f;
sl@0
  1337
	timeToFileTimeL(iModified,&f);
sl@0
  1338
	if (!SetFileTime(iWinHandle,&f,&f,&f))
sl@0
  1339
		User::Leave(Emulator::LastError());
sl@0
  1340
sl@0
  1341
	iAtt&=(~KEntryAttModified);
sl@0
  1342
	}
sl@0
  1343
sl@0
  1344
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1345
//
sl@0
  1346
// Rename the file while open
sl@0
  1347
//
sl@0
  1348
void CLocalFileCB::RenameL(const TDesC& aNewName)
sl@0
  1349
	{
sl@0
  1350
sl@0
  1351
	TInt driveNumber=Mount().Drive().DriveNumber();
sl@0
  1352
sl@0
  1353
	TFileName n1;
sl@0
  1354
	MapFileNameL(n1,driveNumber,FileName());
sl@0
  1355
	TFileName n2;
sl@0
  1356
	MapFileNameL(n2,driveNumber,aNewName);
sl@0
  1357
sl@0
  1358
	CloseHandle(iWinHandle);
sl@0
  1359
	TInt ret=KErrNone;
sl@0
  1360
	if (!Emulator::MoveFile(StrPtrZL(n1),StrPtrZL(n2)))
sl@0
  1361
		{
sl@0
  1362
		ret=Emulator::LastError();
sl@0
  1363
		n2=n1;
sl@0
  1364
		}
sl@0
  1365
	DWORD access=GENERIC_READ|GENERIC_WRITE;
sl@0
  1366
	DWORD share=FILE_SHARE_WRITE|FILE_SHARE_READ;
sl@0
  1367
	DWORD create=OPEN_EXISTING;
sl@0
  1368
	iWinHandle=Emulator::CreateFile(StrPtrZL(n2),access,share,NULL,create,FILE_FLAG_RANDOM_ACCESS,NULL);
sl@0
  1369
	if (iWinHandle==INVALID_HANDLE_VALUE)
sl@0
  1370
		User::Leave(Emulator::LastError());
sl@0
  1371
	
sl@0
  1372
	LARGE_INTEGER pos;
sl@0
  1373
	pos.QuadPart = iCurrentPos;
sl@0
  1374
	DWORD position = SetFilePointer(iWinHandle,pos.LowPart,&pos.HighPart,FILE_BEGIN);
sl@0
  1375
	TInt r = Emulator::LastError();
sl@0
  1376
	if ((KInvalidSetFilePointer == position) && (r != NO_ERROR))
sl@0
  1377
		User::Leave(r);	
sl@0
  1378
	
sl@0
  1379
	User::LeaveIfError(ret);
sl@0
  1380
	AllocBufferL(iFileName,aNewName);
sl@0
  1381
	}
sl@0
  1382
sl@0
  1383
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1384
TInt CLocalFileCB::GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput)
sl@0
  1385
	{
sl@0
  1386
	switch(aInterfaceId)
sl@0
  1387
		{
sl@0
  1388
		case EExtendedFileInterface:
sl@0
  1389
			((CFileCB::MExtendedFileInterface*&) aInterface) = this;
sl@0
  1390
			return KErrNone;
sl@0
  1391
sl@0
  1392
		default:
sl@0
  1393
			return CFileCB::GetInterface(aInterfaceId,aInterface,aInput);
sl@0
  1394
		}
sl@0
  1395
	}
sl@0
  1396
sl@0
  1397
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1398
/**
sl@0
  1399
    Read data from the file.
sl@0
  1400
    
sl@0
  1401
    @param  aFilePos    start read position within a file
sl@0
  1402
    @param  aLength     how many bytes to read; on return will be how many bytes actually read
sl@0
  1403
    @param  aDes        local buffer desctriptor
sl@0
  1404
    @param  aMessage    from file server, used to write data to the buffer in different address space.
sl@0
  1405
    @param  aDesOffset  offset within data descriptor where the data will be copied
sl@0
  1406
sl@0
  1407
    @leave on media read error
sl@0
  1408
sl@0
  1409
*/
sl@0
  1410
void CLocalFileCB::ReadL(TInt64 aPos,TInt& aLength,TDes8* aDes,const RMessagePtr2& aMessage, TInt aOffset)
sl@0
  1411
	{
sl@0
  1412
sl@0
  1413
	const TUint64 KMaxFilePosition = LocalMount().MaxFileSizeSupported()-1;
sl@0
  1414
    
sl@0
  1415
sl@0
  1416
    if(KMaxFilePosition < (TUint64)aPos)
sl@0
  1417
		User::Leave(KErrNotSupported);
sl@0
  1418
	
sl@0
  1419
	CheckPosL(aPos);
sl@0
  1420
	TInt pos=0;
sl@0
  1421
	TInt len=aLength;
sl@0
  1422
	TBuf8<65536> buf;
sl@0
  1423
sl@0
  1424
	if (aMessage.Handle() == KLocalMessageHandle)
sl@0
  1425
		((TPtr8* )aDes)->SetLength(0);
sl@0
  1426
sl@0
  1427
	while (len)
sl@0
  1428
		{
sl@0
  1429
		TInt s=Min(len,buf.MaxLength());
sl@0
  1430
		DWORD res;
sl@0
  1431
		BOOL b=ReadFile(iWinHandle,(TAny*)buf.Ptr(),s,&res,NULL);
sl@0
  1432
		if(!b)
sl@0
  1433
			User::Leave(Emulator::LastError());
sl@0
  1434
sl@0
  1435
		buf.SetLength(res);
sl@0
  1436
sl@0
  1437
	if (aMessage.Handle() == KLocalMessageHandle)
sl@0
  1438
		((TPtr8* )aDes)->Append(buf);
sl@0
  1439
	else
sl@0
  1440
		aMessage.WriteL(0,buf,pos + aOffset);
sl@0
  1441
	
sl@0
  1442
		pos+=res;
sl@0
  1443
		if (((TInt)res)<s)
sl@0
  1444
			break;
sl@0
  1445
		len-=s;
sl@0
  1446
		}
sl@0
  1447
	TInt delay = (ReadSpeed * aLength) >> 10;
sl@0
  1448
	if (delay)
sl@0
  1449
		User::AfterHighRes(delay);
sl@0
  1450
	aLength=pos;
sl@0
  1451
	iCurrentPos=aPos+pos;
sl@0
  1452
	}
sl@0
  1453
sl@0
  1454
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1455
/**
sl@0
  1456
    Write data to the file.
sl@0
  1457
    
sl@0
  1458
    @param  aFilePos    start write position within a file
sl@0
  1459
    @param  aLength     how many bytes to write; on return contain amount of data actually written
sl@0
  1460
    @param  aDes        local buffer desctriptor
sl@0
  1461
    @param  aMessage    from file server, used to write data to the media from different address space.
sl@0
  1462
    @param  aDesOffset  offset within data descriptor 
sl@0
  1463
sl@0
  1464
    @leave on media read error
sl@0
  1465
sl@0
  1466
*/
sl@0
  1467
void CLocalFileCB::WriteL(TInt64 aPos,TInt& aLength,const TDesC8* aDes,const RMessagePtr2& aMessage, TInt aOffset)
sl@0
  1468
	{
sl@0
  1469
	if (IsRomDrive())
sl@0
  1470
		User::Leave(KErrAccessDenied);
sl@0
  1471
	
sl@0
  1472
	
sl@0
  1473
    const TUint64 KMaxFileSize = LocalMount().MaxFileSizeSupported();
sl@0
  1474
    const TUint64 KMaxFilePosition = KMaxFileSize - 1;
sl@0
  1475
sl@0
  1476
	if( KMaxFilePosition < (TUint64)aPos || KMaxFileSize < (TUint64)(aPos + aLength) )
sl@0
  1477
		User::Leave(KErrNotSupported);
sl@0
  1478
	
sl@0
  1479
	CheckPosL(aPos);
sl@0
  1480
	TInt pos=0;
sl@0
  1481
	TInt len=aLength;
sl@0
  1482
	TBuf8<65536> buf;
sl@0
  1483
sl@0
  1484
	while (len)
sl@0
  1485
		{
sl@0
  1486
		TInt s=Min(len,buf.MaxLength());
sl@0
  1487
sl@0
  1488
		if (aMessage.Handle() == KLocalMessageHandle)
sl@0
  1489
			buf.Copy( ((TPtr8* )aDes)->MidTPtr(pos, s) );
sl@0
  1490
		else
sl@0
  1491
			aMessage.ReadL(0,buf,pos + aOffset);
sl@0
  1492
sl@0
  1493
		DWORD res;
sl@0
  1494
		BOOL b=WriteFile(iWinHandle,buf.Ptr(),s,&res,NULL);
sl@0
  1495
		
sl@0
  1496
        if (!b)
sl@0
  1497
			User::Leave(Emulator::LastError());
sl@0
  1498
sl@0
  1499
		if (((TInt)res)<s)
sl@0
  1500
			User::Leave(KErrCorrupt);
sl@0
  1501
		
sl@0
  1502
        len-=s;
sl@0
  1503
		pos+=s;
sl@0
  1504
		}
sl@0
  1505
	TInt delay = (WriteSpeed * aLength) >> 10;
sl@0
  1506
	if (delay)
sl@0
  1507
		User::AfterHighRes(delay);
sl@0
  1508
	aLength=pos;
sl@0
  1509
	iCurrentPos=aPos+pos;
sl@0
  1510
	}
sl@0
  1511
sl@0
  1512
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1513
/**
sl@0
  1514
    Set file size.
sl@0
  1515
    @param aSize new file size.
sl@0
  1516
*/
sl@0
  1517
void CLocalFileCB::SetSizeL(TInt64 aSize)
sl@0
  1518
	{
sl@0
  1519
    const TUint64 KMaxFileSize = LocalMount().MaxFileSizeSupported();
sl@0
  1520
sl@0
  1521
	if(KMaxFileSize < (TUint64)aSize)
sl@0
  1522
		User::Leave(KErrNotSupported);
sl@0
  1523
	
sl@0
  1524
	CheckPosL(aSize);
sl@0
  1525
	if(!SetEndOfFile(iWinHandle))
sl@0
  1526
		{
sl@0
  1527
		iCurrentPos= -1;
sl@0
  1528
		User::Leave(Emulator::LastError());
sl@0
  1529
		}
sl@0
  1530
sl@0
  1531
	SetSize64(aSize, EFalse);
sl@0
  1532
	}
sl@0
  1533
sl@0
  1534
//#########################################################################################################################
sl@0
  1535
//##        CLocalDirCB class implementation
sl@0
  1536
//#########################################################################################################################
sl@0
  1537
sl@0
  1538
CLocalDirCB::CLocalDirCB()
sl@0
  1539
	        :iEntry()
sl@0
  1540
	{
sl@0
  1541
	}
sl@0
  1542
sl@0
  1543
CLocalDirCB::~CLocalDirCB()
sl@0
  1544
	{
sl@0
  1545
sl@0
  1546
	if (iWinHandle!=NULL && !FindClose(iWinHandle))
sl@0
  1547
		Panic(EDirClose);
sl@0
  1548
	}
sl@0
  1549
sl@0
  1550
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1551
TBool CLocalDirCB::MatchUid()
sl@0
  1552
	{
sl@0
  1553
sl@0
  1554
	if (iUidType[0]!=TUid::Null() || iUidType[1]!=TUid::Null() || iUidType[2]!=TUid::Null())
sl@0
  1555
		return(ETrue);
sl@0
  1556
	
sl@0
  1557
    return(EFalse);
sl@0
  1558
	}
sl@0
  1559
sl@0
  1560
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1561
/** @return  ETrue if the aUidTrg matches aUidSuitor */
sl@0
  1562
static TBool CompareUid(const TUidType& aUidTrg, const TUidType& aUidSuitor)
sl@0
  1563
	{
sl@0
  1564
	
sl@0
  1565
	if (aUidTrg[0]!=TUid::Null() && aUidTrg[0]!=aUidSuitor[0])
sl@0
  1566
		return(EFalse);
sl@0
  1567
	if (aUidTrg[1]!=TUid::Null() && aUidTrg[1]!=aUidSuitor[1])
sl@0
  1568
		return(EFalse);
sl@0
  1569
	if (aUidTrg[2]!=TUid::Null() && aUidTrg[2]!=aUidSuitor[2])
sl@0
  1570
		return(EFalse);
sl@0
  1571
	return(ETrue);
sl@0
  1572
	}
sl@0
  1573
sl@0
  1574
//-------------------------------------------------------------------------------------------------------------------	
sl@0
  1575
/**
sl@0
  1576
    Read current entry from the directory and move to the next one.
sl@0
  1577
    This function must leave KErrEof when the end of directory is reached
sl@0
  1578
sl@0
  1579
    @param anEntry extracted directory entry
sl@0
  1580
    @leave KErrEof when there are no more entries in the directory
sl@0
  1581
           system-wide error code on media read fault.
sl@0
  1582
sl@0
  1583
*/
sl@0
  1584
void CLocalDirCB::ReadL(TEntry& anEntry)
sl@0
  1585
	{
sl@0
  1586
sl@0
  1587
	if (iWinHandle==NULL)
sl@0
  1588
		User::Leave(KErrEof);
sl@0
  1589
sl@0
  1590
	FOREVER
sl@0
  1591
		{
sl@0
  1592
		if (!iPending)
sl@0
  1593
			{
sl@0
  1594
			WIN32_FIND_DATA info;
sl@0
  1595
			if (!Emulator::FindNextFile(iWinHandle,&info))
sl@0
  1596
				User::Leave(Emulator::LastError());
sl@0
  1597
sl@0
  1598
			iEntry.iName.Des()=(TText*)(&info.cFileName[0]);
sl@0
  1599
			iEntry.iAtt=info.dwFileAttributes&KEntryAttMaskSupported;
sl@0
  1600
			iEntry.SetFileSize(MAKE_TINT64(info.nFileSizeHigh,info.nFileSizeLow));
sl@0
  1601
			fileTimeToTime(&info.ftLastWriteTime,iEntry.iModified);
sl@0
  1602
			}
sl@0
  1603
		iPending=EFalse;
sl@0
  1604
		anEntry=iEntry;
sl@0
  1605
		if (anEntry.iName==_L(".") || anEntry.iName==_L(".."))
sl@0
  1606
			continue;
sl@0
  1607
		if ((iFullName.NameAndExt()==_L("*.*") || iFullName.NameAndExt()==_L("*") || anEntry.iName.MatchF(iFullName.NameAndExt())!=KErrNotFound) && Mount().MatchEntryAtt(anEntry.iAtt&KEntryAttMaskSupported,iAtt))
sl@0
  1608
			{
sl@0
  1609
			if (MatchUid())
sl@0
  1610
				{
sl@0
  1611
				TParse fileName;
sl@0
  1612
				TBuf<KMaxFileName> driveAndPath=iFullName.DriveAndPath();
sl@0
  1613
				fileName.Set(anEntry.iName,&driveAndPath,NULL);
sl@0
  1614
				(*(CLocalMountCB*)&Mount()).ReadUidL(fileName.FullName(),anEntry);
sl@0
  1615
				if (CompareUid(iUidType,anEntry.iType))
sl@0
  1616
					break;
sl@0
  1617
				}
sl@0
  1618
			else
sl@0
  1619
				break;
sl@0
  1620
			}
sl@0
  1621
		}
sl@0
  1622
	if ((iAtt&KEntryAttAllowUid)==0 || anEntry.iAtt&KEntryAttDir || MatchUid())
sl@0
  1623
		return;
sl@0
  1624
	TParse fileName;
sl@0
  1625
	TBuf<KMaxFileName> driveAndPath=iFullName.DriveAndPath();
sl@0
  1626
	fileName.Set(anEntry.iName,&driveAndPath,NULL);
sl@0
  1627
	(*(CLocalMountCB*)&Mount()).ReadUidL(fileName.FullName(),anEntry);
sl@0
  1628
	}
sl@0
  1629
sl@0
  1630
//#########################################################################################################################
sl@0
  1631
//##        CLocalFormatCB class implementation
sl@0
  1632
//#########################################################################################################################
sl@0
  1633
sl@0
  1634
CLocalFormatCB::CLocalFormatCB()
sl@0
  1635
	{
sl@0
  1636
	}
sl@0
  1637
sl@0
  1638
CLocalFormatCB::~CLocalFormatCB()
sl@0
  1639
	{
sl@0
  1640
	}
sl@0
  1641
sl@0
  1642
void CLocalFormatCB::DoFormatStepL()
sl@0
  1643
	{
sl@0
  1644
	iCurrentStep=0;
sl@0
  1645
	User::Leave(KErrNotSupported);
sl@0
  1646
	}
sl@0
  1647
sl@0
  1648
sl@0
  1649
//#########################################################################################################################
sl@0
  1650
//##        CLocal File System class implementation
sl@0
  1651
//#########################################################################################################################
sl@0
  1652
sl@0
  1653
extern "C" 
sl@0
  1654
{
sl@0
  1655
//
sl@0
  1656
// Create a new file system
sl@0
  1657
//
sl@0
  1658
EXPORT_C CFileSystem* CreateFileSystem()
sl@0
  1659
	{
sl@0
  1660
	return(new CLocal);
sl@0
  1661
	}
sl@0
  1662
}
sl@0
  1663
sl@0
  1664
CLocal::CLocal()
sl@0
  1665
	{
sl@0
  1666
	}
sl@0
  1667
sl@0
  1668
TInt CLocal::Install()
sl@0
  1669
	{
sl@0
  1670
sl@0
  1671
	SetErrorMode(SEM_FAILCRITICALERRORS);
sl@0
  1672
	EmulatorDiskSpeed(ReadSpeed, WriteSpeed);
sl@0
  1673
	iVersion=TVersion(KMajorVersionNumber,KMinorVersionNumber,KF32BuildVersionNumber);
sl@0
  1674
	_LIT(KWin32Name,"Win32");
sl@0
  1675
	return(SetName(&KWin32Name));
sl@0
  1676
	}
sl@0
  1677
sl@0
  1678
CMountCB* CLocal::NewMountL() const
sl@0
  1679
//
sl@0
  1680
// Create a new mount control block.
sl@0
  1681
//
sl@0
  1682
	{
sl@0
  1683
sl@0
  1684
	return(new(ELeave) CLocalMountCB);
sl@0
  1685
	}
sl@0
  1686
sl@0
  1687
CFileCB* CLocal::NewFileL() const
sl@0
  1688
//
sl@0
  1689
// Create a new file.
sl@0
  1690
//
sl@0
  1691
	{
sl@0
  1692
sl@0
  1693
	return(new(ELeave) CLocalFileCB);
sl@0
  1694
	}
sl@0
  1695
sl@0
  1696
CDirCB* CLocal::NewDirL() const
sl@0
  1697
//
sl@0
  1698
// Create a new directory lister.
sl@0
  1699
//
sl@0
  1700
	{
sl@0
  1701
	return(new(ELeave) CLocalDirCB);
sl@0
  1702
	}
sl@0
  1703
sl@0
  1704
CFormatCB* CLocal::NewFormatL() const
sl@0
  1705
//
sl@0
  1706
// Create a new media formatter.
sl@0
  1707
//
sl@0
  1708
	{
sl@0
  1709
	return(new(ELeave) CLocalFormatCB);
sl@0
  1710
	}
sl@0
  1711
sl@0
  1712
TInt CLocal::DefaultPath(TDes& aPath) const
sl@0
  1713
//
sl@0
  1714
// Return the initial default path.
sl@0
  1715
//
sl@0
  1716
	{
sl@0
  1717
	aPath=_L("?:\\");
sl@0
  1718
	aPath[0] = (TUint8) RFs::GetSystemDriveChar();
sl@0
  1719
	return(KErrNone);
sl@0
  1720
	}
sl@0
  1721
sl@0
  1722
void CLocal::DriveInfo(TDriveInfo& anInfo,TInt aDriveNumber) const
sl@0
  1723
//
sl@0
  1724
// Return the drive info.
sl@0
  1725
//
sl@0
  1726
	{
sl@0
  1727
sl@0
  1728
	anInfo.iMediaAtt=0;
sl@0
  1729
sl@0
  1730
// Get Fake drive info.
sl@0
  1731
	if (aDriveNumber==EDriveZ)
sl@0
  1732
		{
sl@0
  1733
		anInfo.iType=EMediaRom;
sl@0
  1734
		anInfo.iMediaAtt=KMediaAttWriteProtected;
sl@0
  1735
		anInfo.iDriveAtt=KDriveAttRom|KDriveAttInternal;
sl@0
  1736
		anInfo.iConnectionBusType=EConnectionBusInternal;
sl@0
  1737
		return;
sl@0
  1738
		}
sl@0
  1739
	if (aDriveNumber==EDriveC)
sl@0
  1740
		{
sl@0
  1741
		anInfo.iType=EMediaHardDisk;
sl@0
  1742
		anInfo.iMediaAtt=KMediaAttVariableSize;
sl@0
  1743
		anInfo.iDriveAtt=KDriveAttLocal|KDriveAttInternal;
sl@0
  1744
		anInfo.iConnectionBusType=EConnectionBusInternal;
sl@0
  1745
		return;
sl@0
  1746
		}
sl@0
  1747
	TFileName envValue;
sl@0
  1748
	if (MapDrive(envValue,aDriveNumber))
sl@0
  1749
		{
sl@0
  1750
		anInfo.iType=EMediaHardDisk;
sl@0
  1751
		anInfo.iDriveAtt=KDriveAttLocal|KDriveAttInternal;
sl@0
  1752
		anInfo.iConnectionBusType=EConnectionBusInternal;
sl@0
  1753
		return;		
sl@0
  1754
		}
sl@0
  1755
	anInfo.iType=EMediaNotPresent;
sl@0
  1756
	anInfo.iDriveAtt=0;
sl@0
  1757
	}
sl@0
  1758
sl@0
  1759
sl@0
  1760
sl@0
  1761
sl@0
  1762
sl@0
  1763
sl@0
  1764
sl@0
  1765
sl@0
  1766
sl@0
  1767
sl@0
  1768
sl@0
  1769
sl@0
  1770
sl@0
  1771
sl@0
  1772