core/builtins/gobble.cpp
changeset 68 377ac716dabb
parent 0 7f656887cf89
equal deleted inserted replaced
67:683f4b1f08ce 68:377ac716dabb
     9 // Initial Contributors:
     9 // Initial Contributors:
    10 // Accenture - Initial contribution
    10 // Accenture - Initial contribution
    11 //
    11 //
    12 
    12 
    13 #include "gobble.h"
    13 #include "gobble.h"
       
    14 #include <fshell/ltkutils.h>
    14 
    15 
    15 
    16 
    16 CCommandBase* CCmdGobble::NewLC()
    17 CCommandBase* CCmdGobble::NewLC()
    17 	{
    18 	{
    18 	CCmdGobble* self = new(ELeave) CCmdGobble();
    19 	CCmdGobble* self = new(ELeave) CCmdGobble();
    36 	return KName;
    37 	return KName;
    37 	}
    38 	}
    38 
    39 
    39 void CCmdGobble::DoRunL()
    40 void CCmdGobble::DoRunL()
    40 	{
    41 	{
    41 	if (iAmount < iBlockSize)
    42 	RFs& fs = FsL();
       
    43 	if (iAmount == 0)
    42 		{
    44 		{
    43 		LeaveIfErr(KErrArgument, _L("The amount to consume must be less than the block size (%d)"), iBlockSize);
    45 		TInt drive = EDriveC;
       
    46 		if (iFileName.Length() == 0) LeaveIfErr(KErrBadName, _L("Bad file name"));
       
    47 		RFs::CharToDrive(iFileName[0], drive);
       
    48 		TVolumeInfo volInfo;
       
    49 		LeaveIfErr(fs.Volume(volInfo, drive), _L("Couldn't get volume information for drive %c"), iFileName[0]);
       
    50 		iAmount = volInfo.iFree;
       
    51 		}
       
    52 
       
    53 	if (!iNoWrite && iAmount < iBlockSize)
       
    54 		{
       
    55 		LeaveIfErr(KErrArgument, _L("The amount to consume must be greater than the block size (%d)"), iBlockSize);
    44 		}
    56 		}
    45 	if (iAmount & 0x80000000)
    57 	if (iAmount & 0x80000000)
    46 		{
    58 		{
    47 		LeaveIfErr(KErrArgument, _L("The amount to consume is too large (maximum is %d)"), KMaxTInt);
    59 		LeaveIfErr(KErrArgument, _L("The amount to consume is too large (maximum is %d)"), KMaxTInt);
    48 		}
    60 		}
    49 	if (iBlockSize & 0x80000000)
    61 	if (iBlockSize & 0x80000000)
    50 		{
    62 		{
    51 		LeaveIfErr(KErrArgument, _L("The block size is too large (maximum is %d)"), KMaxTInt);
    63 		LeaveIfErr(KErrArgument, _L("The block size is too large (maximum is %d)"), KMaxTInt);
    52 		}
    64 		}
    53 	RFs& fs = FsL();
       
    54 	fs.MkDirAll(iFileName);
    65 	fs.MkDirAll(iFileName);
    55 	RFile file;
    66 	RFile file;
    56 	TInt err = file.Open(fs, iFileName, EFileWrite);
    67 	TInt err = file.Open(fs, iFileName, EFileWrite);
    57 	if (err == KErrNotFound)
    68 	if (err == KErrNotFound)
    58 		{
    69 		{
    59 		err = file.Create(fs, iFileName, EFileWrite);
    70 		err = file.Create(fs, iFileName, EFileWrite);
    60 		}
    71 		}
    61 	User::LeaveIfError(err);
    72 	LeaveIfErr(err, _L("Couldn't create file %S"), &iFileName);
    62 	CleanupClosePushL(file);
    73 	CleanupClosePushL(file);
    63 	TInt pos = 0;
       
    64 	User::LeaveIfError(file.Seek(ESeekEnd, pos));
       
    65 
    74 
    66 	HBufC8* buf = HBufC8::NewLC(iBlockSize);
    75 	if (iNoWrite)
    67 	TPtr8 ptr(buf->Des());
       
    68 	ptr.Fill(TChar('x'), iBlockSize);
       
    69 	
       
    70 	TInt toWrite = static_cast<TInt>(iAmount);
       
    71 	do
       
    72 		{
    76 		{
    73 		TInt writeSize;
    77 		TInt size = 0;
    74 		if (toWrite > static_cast<TInt>(iBlockSize))
    78 		LeaveIfErr(file.Size(size), _L("Couldn't get file size"));
       
    79 		size += iAmount;
       
    80 		LeaveIfErr(file.SetSize(size), _L("Couldn't set filesize to %d"), size);
       
    81 		}
       
    82 	else
       
    83 		{
       
    84 		TInt pos = 0;
       
    85 		User::LeaveIfError(file.Seek(ESeekEnd, pos));
       
    86 		HBufC8* buf = HBufC8::NewLC(iBlockSize);
       
    87 		TPtr8 ptr(buf->Des());
       
    88 		ptr.Fill(TChar('x'), iBlockSize);
       
    89 		
       
    90 		TInt toWrite = static_cast<TInt>(iAmount);
       
    91 		do
    75 			{
    92 			{
    76 			writeSize = static_cast<TInt>(iBlockSize);
    93 			TInt writeSize;
       
    94 			if (toWrite > static_cast<TInt>(iBlockSize))
       
    95 				{
       
    96 				writeSize = static_cast<TInt>(iBlockSize);
       
    97 				}
       
    98 			else
       
    99 				{
       
   100 				writeSize = toWrite;
       
   101 				}
       
   102 			ptr.SetLength(writeSize);
       
   103 			err = file.Write(ptr);
       
   104 			if (err == KErrNone)
       
   105 				{
       
   106 				if (iVerbose)
       
   107 					{
       
   108 					Printf(_L("\rWrote %d"), iAmount - toWrite);
       
   109 					}
       
   110 				toWrite -= writeSize;
       
   111 				}
    77 			}
   112 			}
    78 		else
       
    79 			{
       
    80 			writeSize = toWrite;
       
    81 			}
       
    82 		ptr.SetLength(writeSize);
       
    83 		err = file.Write(ptr);
       
    84 		if (err == KErrNone)
       
    85 			{
       
    86 			if (iVerbose)
       
    87 				{
       
    88 				Printf(_L("\rWrote %d"), iAmount - toWrite);
       
    89 				}
       
    90 			toWrite -= writeSize;
       
    91 			}
       
    92 		}
       
    93 		while ((err == KErrNone) && (toWrite > 0));
   113 		while ((err == KErrNone) && (toWrite > 0));
    94 		if (iVerbose)
   114 		if (iVerbose)
    95 			{
   115 			{
    96 			Printf(_L("\rWrote %d"), iAmount - toWrite);
   116 			Printf(_L("\rWrote %d"), iAmount - toWrite);
    97 			}
   117 			}
    98 		
   118 		CleanupStack::PopAndDestroy(buf);
    99 	CleanupStack::PopAndDestroy(2, &file);
   119 		}
       
   120 	CleanupStack::PopAndDestroy(&file);
   100 	}
   121 	}
   101 
   122 
   102 void CCmdGobble::OptionsL(RCommandOptionList& aOptions)
   123 void CCmdGobble::OptionsL(RCommandOptionList& aOptions)
   103 	{
   124 	{
   104 	_LIT(KCmdOptVerbose, "verbose");
   125 	_LIT(KCmdOptVerbose, "verbose");
   105 	aOptions.AppendBoolL(iVerbose, KCmdOptVerbose);
   126 	aOptions.AppendBoolL(iVerbose, KCmdOptVerbose);
       
   127 
       
   128 	_LIT(KCmdOptNoWrite, "no-write");
       
   129 	aOptions.AppendBoolL(iNoWrite, KCmdOptNoWrite);
   106 	}
   130 	}
   107 
   131 
   108 void CCmdGobble::ArgumentsL(RCommandArgumentList& aArguments)
   132 void CCmdGobble::ArgumentsL(RCommandArgumentList& aArguments)
   109 	{
   133 	{
   110 	_LIT(KArgFileName, "file_name");
   134 	_LIT(KArgFileName, "file_name");