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