core/builtins/ymodem.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // ymodem.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 #include "ymodem.h"
       
    15 
       
    16 using namespace IoUtils;
       
    17 
       
    18 CCommandBase* CCmdYmodem::NewLC()
       
    19 	{
       
    20 	CCmdYmodem* self = new(ELeave) CCmdYmodem();
       
    21 	CleanupStack::PushL(self);
       
    22 	self->BaseConstructL();
       
    23 	self->ConstructL();
       
    24 	return self;
       
    25 	}
       
    26 
       
    27 CCmdYmodem::~CCmdYmodem()
       
    28 	{
       
    29 	iFileNames.Close();
       
    30 	}
       
    31 
       
    32 CCmdYmodem::CCmdYmodem()
       
    33 	{
       
    34 	}
       
    35 
       
    36 const TDesC& CCmdYmodem::Name() const
       
    37 	{
       
    38 	_LIT(KName, "ymodem");	
       
    39 	return KName;
       
    40 	}
       
    41 
       
    42 class TFailedFile
       
    43 	{
       
    44 public:
       
    45 	TFailedFile();
       
    46 public:
       
    47 	TInt iError;
       
    48 	TFileName2 iFileName;
       
    49 	};
       
    50 
       
    51 TFailedFile::TFailedFile()
       
    52 	: iError(KErrNone)
       
    53 	{
       
    54 	}
       
    55 
       
    56 void CCmdYmodem::DoRunL()
       
    57 	{
       
    58 	PrepareConsoleToTransferL();
       
    59 
       
    60 	if (iMode == EReceive)
       
    61 		{
       
    62 		TPtrC receiveDir;
       
    63 		if (iFileNames.Count() > 1)
       
    64 			{
       
    65 			PrintError(KErrArgument, _L("Too many arguments - only the receive directory may be specified in receive mode."));
       
    66 			User::Leave(KErrArgument);
       
    67 			}
       
    68 		else if (iFileNames.Count() == 1)
       
    69 			{
       
    70 			TFileName2 fileName(iFileNames[0]);
       
    71 			receiveDir.Set(iFileNames[0]);
       
    72 
       
    73 			if ((fileName.Exists(FsL()) && !fileName.IsDirL(FsL())) || (!fileName.Exists(FsL()) && !iOverwrite))
       
    74 				{
       
    75 				LeaveIfErr(KErrArgument, _L("\"%S\" is not a directory"), &fileName);
       
    76 				}
       
    77 			}
       
    78 		else
       
    79 			{
       
    80 			receiveDir.Set(Env().Pwd());
       
    81 			}
       
    82 
       
    83 		Progress(_L("YMODEM receive to \"%S\"\r\n"), &receiveDir);
       
    84 		RArray<TFileName2> receivedFiles;
       
    85 		CleanupClosePushL(receivedFiles);
       
    86 		TFailedFile failedFile;
       
    87 		TBool finished(EFalse);
       
    88 		while (!finished)
       
    89 			{
       
    90 			TSyncResult syncResult = SendSyncL();
       
    91 			if (syncResult == EEot)
       
    92 				{
       
    93 				finished = ETrue;
       
    94 				}
       
    95 			else
       
    96 				{
       
    97 				iPacketNumber = 0;
       
    98 				TBool isFinalBlock(EFalse);
       
    99 				TPtrC block(ReceiveBlockL(ETrue, isFinalBlock));
       
   100 				TPtrC fileName(block.Ptr()); // filename is the first null terminated string in the block.
       
   101 				if (fileName.Length() > 0)
       
   102 					{
       
   103 					TFileName2 fileName2(fileName);
       
   104 					fileName2.MakeAbsoluteL(receiveDir);
       
   105 					TLex lex(block.Mid(fileName.Length() + 1));
       
   106 					TInt size;
       
   107 					User::LeaveIfError(lex.Val(size));
       
   108 					TInt err = KErrNone;
       
   109 					if (!iOverwrite && fileName2.Exists(FsL()))
       
   110 						{
       
   111 						err = KErrAlreadyExists;
       
   112 						}
       
   113 					RFile file;
       
   114 					if (err == KErrNone)
       
   115 						{
       
   116 						if (size == 0)
       
   117 							{
       
   118 							if (iOverwrite)
       
   119 								{
       
   120 								err = file.Replace(FsL(), fileName2, EFileWrite);
       
   121 								}
       
   122 							else
       
   123 								{
       
   124 								err = file.Create(FsL(), fileName2, EFileWrite);
       
   125 								}
       
   126 							}
       
   127 						else
       
   128 							{
       
   129 							Progress(_L("Starting receive of \"%S\" (%d bytes)\r\n"), &fileName, size);
       
   130 							SendSyncL();
       
   131 							TRAP(err, ReceiveToFileL(fileName2));
       
   132 							if (err == KErrNone)
       
   133 								{
       
   134 								err = file.Open(FsL(), fileName2, EFileWrite);
       
   135 								if (err == KErrNone)
       
   136 									{
       
   137 									err = file.SetSize(size);
       
   138 									}
       
   139 								}
       
   140 							else
       
   141 								{
       
   142 								Fs().Delete(fileName2);
       
   143 								}
       
   144 							}
       
   145 						}
       
   146 					else
       
   147 						{
       
   148 						TRAP_IGNORE(ReceiveToNullL());
       
   149 						}
       
   150 					file.Close();
       
   151 					if (err)
       
   152 						{
       
   153 						failedFile.iError = err;
       
   154 						failedFile.iFileName = fileName2;
       
   155 						finished = ETrue;
       
   156 						}
       
   157 					else
       
   158 						{
       
   159 						receivedFiles.Append(fileName2); // Ignore error.
       
   160 						}
       
   161 					}
       
   162 				else
       
   163 					{
       
   164 					finished = ETrue;
       
   165 					}
       
   166 				}
       
   167 			}
       
   168 
       
   169 		CleanupClonsoleAfterTransferL();
       
   170 
       
   171 		const TInt numFilesReceived = receivedFiles.Count();
       
   172 		if (numFilesReceived == 0)
       
   173 			{
       
   174 			if (failedFile.iError == KErrNone)
       
   175 				{
       
   176 				Printf(_L("No files to receive.\r\n"));
       
   177 				}
       
   178 			}
       
   179 		else if (numFilesReceived == 1)
       
   180 			{
       
   181 			const TFileName2& fileName = receivedFiles[0];
       
   182 			Printf(_L("Successfully received \"%S\".\r\n"), &fileName);
       
   183 			}
       
   184 		else
       
   185 			{
       
   186 			Printf(_L("Successfully received:\r\n"));
       
   187 			for (TInt i = 0; i < numFilesReceived; ++i)
       
   188 				{
       
   189 				const TFileName2& fileName = receivedFiles[i];
       
   190 				Printf(_L("\t\"%S\"\r\n"), &fileName);
       
   191 				}
       
   192 			}
       
   193 
       
   194 		if (failedFile.iError)
       
   195 			{
       
   196 			PrintError(failedFile.iError, _L("Failed to receive \"%S\""), &failedFile.iFileName);
       
   197 			}
       
   198 		CleanupStack::PopAndDestroy(&receivedFiles);
       
   199 		}
       
   200 	else if (iMode == ESend)
       
   201 		{
       
   202 		const TInt numFiles = iFileNames.Count();
       
   203 		if (numFiles == 0)
       
   204 			{
       
   205 			PrintError(KErrArgument, _L("No files to send specified"));
       
   206 			User::Leave(KErrArgument);
       
   207 			}
       
   208 		for (TInt i = 0; i < numFiles; ++i)
       
   209 			{
       
   210 			const TFileName2& fileName = iFileNames[i];
       
   211 			if (fileName.Exists(FsL()))
       
   212 				{
       
   213 				if (fileName.IsDirL(FsL()))
       
   214 					{
       
   215 					LeaveIfErr(KErrArgument, _L("\"%S\" is not a file"), &fileName);
       
   216 					}
       
   217 				}
       
   218 			else
       
   219 				{
       
   220 				LeaveIfErr(KErrArgument, _L("\"%S\" does not exist"), &fileName);
       
   221 				}
       
   222 			}
       
   223 		WaitForSyncL();
       
   224 		HBufC* buf = HBufC::NewLC(iBlockSize);
       
   225 		TPtr bufPtr(buf->Des());
       
   226 		for (TInt i = 0; i < numFiles; ++i)
       
   227 			{
       
   228 			const TFileName2& fileName = iFileNames[i];
       
   229 			LeaveIfFileNotFound(fileName);
       
   230 			TEntry entry;
       
   231 			User::LeaveIfError(FsL().Entry(fileName, entry));
       
   232 			bufPtr.SetLength(bufPtr.MaxLength());
       
   233 			bufPtr.FillZ();
       
   234 			bufPtr.Copy(fileName.NameAndExt());
       
   235 			bufPtr.SetLength(buf->Length() + 1); // To leave a terminating null after the file name.
       
   236 			bufPtr.AppendNum(entry.iSize);
       
   237 			if (bufPtr.Length() < iBlockSize)
       
   238 				{
       
   239 				// There's not enough data to fill this block, so pad with NULL.
       
   240 				bufPtr.AppendFill(0, iBlockSize - bufPtr.Length());
       
   241 				}
       
   242 			iPacketNumber = 0;
       
   243 			SendBlockL(*buf);
       
   244 			WaitForSyncL();
       
   245 			SendFileL(fileName);
       
   246 			}
       
   247 		iPacketNumber = 0;
       
   248 		bufPtr.SetLength(bufPtr.MaxLength());
       
   249 		bufPtr.FillZ();
       
   250 		SendBlockL(*buf);
       
   251 		CleanupStack::PopAndDestroy(buf);
       
   252 		CleanupClonsoleAfterTransferL();
       
   253 
       
   254 		if (numFiles == 1)
       
   255 			{
       
   256 			Printf(_L("Successfully sent \"%S\".\r\n"), &iFileNames[0]);
       
   257 			}
       
   258 		else
       
   259 			{
       
   260 			Printf(_L("Successfully sent:\r\n"));
       
   261 			for (TInt i = 0; i < numFiles; ++i)
       
   262 				{
       
   263 				const TFileName2& fileName = iFileNames[i];
       
   264 				Printf(_L("\t\"%S\"\r\n"), &fileName);
       
   265 				}
       
   266 			}
       
   267 		}
       
   268 	User::LeaveIfError(Stdin().CancelCaptureAllKeys());
       
   269 	}
       
   270 
       
   271 void CCmdYmodem::ArgumentsL(RCommandArgumentList& aArguments)
       
   272 	{
       
   273 	_LIT(KArg1, "mode");
       
   274 	aArguments.AppendEnumL((TInt&)iMode, KArg1);
       
   275 	_LIT(KArg2, "file_name");
       
   276 	aArguments.AppendFileNameL(iFileNames, KArg2);
       
   277 	}
       
   278 
       
   279 void CCmdYmodem::OptionsL(RCommandOptionList& aOptions)
       
   280 	{
       
   281 	CCmdXmodem::OptionsL(aOptions);
       
   282 	}
       
   283