os/mm/imagingandcamerafws/cameraunittest/src/copyfile/copyfile.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 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
//
sl@0
    15
sl@0
    16
#include <e32base.h>
sl@0
    17
#include <e32std.h>
sl@0
    18
#include <f32file.h>
sl@0
    19
sl@0
    20
_LIT(KSeparator, "|"); // Invalid filepath char used to separate filenames
sl@0
    21
sl@0
    22
sl@0
    23
// Copy the files specified.
sl@0
    24
void CopyFileL(const TDesC& anOld, const TDesC& aNew)
sl@0
    25
	{	
sl@0
    26
	RFs fs;
sl@0
    27
	fs.Connect();	
sl@0
    28
	CleanupClosePushL(fs);
sl@0
    29
sl@0
    30
	CFileMan* fileMan = CFileMan::NewL(fs);
sl@0
    31
	CleanupStack::PushL(fileMan); 
sl@0
    32
sl@0
    33
	// Check if the destination directory exists, if so then delete files if they are present
sl@0
    34
	TInt err = fs.MkDirAll(aNew);	
sl@0
    35
	if (err == KErrAlreadyExists)
sl@0
    36
		{
sl@0
    37
		err = fileMan->Attribs(aNew, 0, KEntryAttReadOnly, TTime(0), 0);
sl@0
    38
		if ((err != KErrNone) && (err != KErrNotFound))
sl@0
    39
			{
sl@0
    40
			User::Leave(err);
sl@0
    41
			}
sl@0
    42
		else if(err == KErrNone)
sl@0
    43
			{
sl@0
    44
			User::LeaveIfError(fs.Delete(aNew));
sl@0
    45
			}
sl@0
    46
		}
sl@0
    47
	else if (err != KErrNone)
sl@0
    48
		{
sl@0
    49
		User::Leave(err);
sl@0
    50
		}
sl@0
    51
sl@0
    52
	// Make the destination file writeable 
sl@0
    53
    err = fileMan->Attribs(aNew, 0, KEntryAttReadOnly, TTime(0), 0);
sl@0
    54
	if ((err != KErrNone) && (err != KErrNotFound))
sl@0
    55
		{
sl@0
    56
		User::Leave(err);
sl@0
    57
		}
sl@0
    58
sl@0
    59
	// Do the file copy	
sl@0
    60
    User::LeaveIfError(fileMan->Copy(anOld, aNew));
sl@0
    61
sl@0
    62
	CleanupStack::PopAndDestroy(2); 
sl@0
    63
	}
sl@0
    64
sl@0
    65
sl@0
    66
// Format of aFileNames is [srcFile]|[dstFile]
sl@0
    67
GLDEF_C TInt E32Main()
sl@0
    68
	{
sl@0
    69
	CTrapCleanup* cleanup =	CTrapCleanup::New(); 
sl@0
    70
sl@0
    71
	TBuf<KMaxFileName*2> names;
sl@0
    72
	User::CommandLine(names);
sl@0
    73
	TInt pos = names.Find(KSeparator);
sl@0
    74
	TFileName srcFile(names.Mid(0, pos));
sl@0
    75
	TFileName dstFile(names.Mid(pos + 1, names.Length() - (pos + 1)));
sl@0
    76
sl@0
    77
	TInt err = KErrNone;
sl@0
    78
	TRAP(err, CopyFileL(srcFile, dstFile));
sl@0
    79
	if (err == KErrNone)
sl@0
    80
		{
sl@0
    81
   		RDebug::Print(_L("CFileMan Copy file '%S' to '%S' succeeded.\n"), &srcFile, &dstFile);
sl@0
    82
		}
sl@0
    83
	else
sl@0
    84
    	{
sl@0
    85
    	RDebug::Print(_L("CFileMan Copy file '%S' to '%S' failed with error = %d.\n"), &srcFile, &dstFile, err);
sl@0
    86
    	}
sl@0
    87
sl@0
    88
	delete cleanup;
sl@0
    89
	return err;
sl@0
    90
	}