sbsv1/abld/genutil/ecopyfile.pl
author lorewang
Wed, 17 Nov 2010 11:24:29 +0800
changeset 685 39f7ecf8fbc7
parent 599 fa7a3cc6effd
permissions -rw-r--r--
pull from trunk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
599
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     1
# Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     2
# All rights reserved.
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     3
# This component and the accompanying materials are made available
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     4
# under the terms of "Eclipse Public License v1.0"
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     5
# which accompanies this distribution, and is available
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     7
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     8
# Initial Contributors:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    10
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    11
# Contributors:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    12
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    13
# Description:
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    14
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    15
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    16
use File::Copy;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    17
use File::Basename;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    18
use File::Path;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    19
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    20
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    21
if (@ARGV!=2)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    22
	{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    23
#........1.........2.........3.........4.........5.........6.........7.....
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    24
	print <<USAGE_EOF;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    25
Usage : perl ecopyfile.pl srcfile dstfile
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    26
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    27
If dstfile exists and is identical to srcfile then do nothing,
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    28
otherwise copy srcfile to dstfile.
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    29
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    30
USAGE_EOF
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    31
	exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    32
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    33
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    34
my $srcfile = shift;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    35
my $dstfile = shift;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    36
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    37
# Sanity checking
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    38
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    39
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    40
if (!-e $srcfile)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    41
	{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    42
	print STDERR "$srcfile does not exist\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    43
	exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    44
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    45
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    46
if (!-f $srcfile)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    47
	{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    48
	print STDERR "$srcfile is not a plain file\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    49
	exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    50
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    51
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    52
my $updating = -e $dstfile;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    53
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    54
if ($updating && !-f $dstfile)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    55
	{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    56
	print STDERR "$dstfile exists, but is not a plain file\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    57
	exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    58
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    59
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    60
# Can we avoid doing the copy?
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    61
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    62
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    63
if ($updating && !defined($ENV{ECOPYFILE_ALWAYS_COPY}) && (-s $srcfile == -s $dstfile))
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    64
	{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    65
	# file exists and is the same size - check contents
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    66
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    67
	open SRC, $srcfile or print STDERR "Cannot open $srcfile\n" and exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    68
	open DST, $dstfile or print STDERR "Cannot open $dstfile\n" and exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    69
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    70
	binmode SRC;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    71
	binmode DST;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    72
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    73
	my $srcbuf;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    74
	my $dstbuf;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    75
	my $srclen;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    76
	my $dstlen;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    77
	my $same = 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    78
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    79
	while ($same)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    80
		{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    81
		$srclen = read SRC, $srcbuf, 4096;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    82
		$dstlen = read DST, $dstbuf, 4096;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    83
		if ($srclen == 0 && $dstlen == 0)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    84
			{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    85
			last;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    86
			}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    87
		$same= $srcbuf eq $dstbuf;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    88
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    89
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    90
	close SRC;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    91
	close DST;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    92
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    93
	if ($same)
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    94
		{
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    95
		# Files match - no need to copy
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    96
		exit 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    97
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    98
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
    99
	# files are different
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   100
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   101
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   102
# Copy srcfile to dstfile
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   103
#
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   104
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   105
my $action = "create";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   106
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   107
if ($updating) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   108
	$action = "update";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   109
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   110
else {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   111
	# see if the destination directory exists to create the file into...
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   112
	my $directory = dirname($dstfile);
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   113
	if (!-e $directory) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   114
		# ...and attempt to create it if not.
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   115
		if (!mkpath ($directory)) {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   116
			print STDERR "Failed to create directory $directory\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   117
			exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   118
		}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   119
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   120
}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   121
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   122
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   123
if (!copy ($srcfile, $dstfile))
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   124
        {
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   125
	print STDERR "Failed to $action file $dstfile\n";
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   126
	unlink $dstfile;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   127
	exit 1;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   128
	}
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   129
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   130
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   131
printf "%sd %s\n", ucfirst $action, $dstfile;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   132
exit 0;
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   133
fa7a3cc6effd Add sbsv1 to new structure
jjkang
parents:
diff changeset
   134