|
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 strict; |
|
17 use File::Copy; |
|
18 use File::Basename; |
|
19 use File::Path; |
|
20 |
|
21 |
|
22 if (@ARGV!=2) |
|
23 { |
|
24 #........1.........2.........3.........4.........5.........6.........7..... |
|
25 print <<USAGE_EOF; |
|
26 Usage : perl ecopyfile.pl srcfile dstfile |
|
27 |
|
28 If dstfile exists and is identical to srcfile then do nothing, |
|
29 otherwise copy srcfile to dstfile. |
|
30 |
|
31 USAGE_EOF |
|
32 exit 1; |
|
33 } |
|
34 |
|
35 my $srcfile = shift; |
|
36 my $dstfile = shift; |
|
37 |
|
38 # Sanity checking |
|
39 # |
|
40 |
|
41 if (!-e $srcfile) |
|
42 { |
|
43 print STDERR "$srcfile does not exist\n"; |
|
44 exit 1; |
|
45 } |
|
46 |
|
47 if (!-f $srcfile) |
|
48 { |
|
49 print STDERR "$srcfile is not a plain file\n"; |
|
50 exit 1; |
|
51 } |
|
52 |
|
53 my $updating = -e $dstfile; |
|
54 |
|
55 if ($updating && !-f $dstfile) |
|
56 { |
|
57 print STDERR "$dstfile exists, but is not a plain file\n"; |
|
58 exit 1; |
|
59 } |
|
60 |
|
61 # Can we avoid doing the copy? |
|
62 # |
|
63 |
|
64 if ($updating && !defined($ENV{ECOPYFILE_ALWAYS_COPY}) && (-s $srcfile == -s $dstfile)) |
|
65 { |
|
66 # file exists and is the same size - check contents |
|
67 |
|
68 open SRC, $srcfile or print STDERR "Cannot open $srcfile\n" and exit 1; |
|
69 open DST, $dstfile or print STDERR "Cannot open $dstfile\n" and exit 1; |
|
70 |
|
71 binmode SRC; |
|
72 binmode DST; |
|
73 |
|
74 my $srcbuf; |
|
75 my $dstbuf; |
|
76 my $srclen; |
|
77 my $dstlen; |
|
78 my $same = 1; |
|
79 |
|
80 while ($same) |
|
81 { |
|
82 $srclen = read SRC, $srcbuf, 4096; |
|
83 $dstlen = read DST, $dstbuf, 4096; |
|
84 if ($srclen == 0 && $dstlen == 0) |
|
85 { |
|
86 last; |
|
87 } |
|
88 $same= $srcbuf eq $dstbuf; |
|
89 } |
|
90 |
|
91 close SRC; |
|
92 close DST; |
|
93 |
|
94 if ($same) |
|
95 { |
|
96 # Files match - no need to copy |
|
97 exit 0; |
|
98 } |
|
99 |
|
100 # files are different |
|
101 } |
|
102 |
|
103 # Copy srcfile to dstfile |
|
104 # |
|
105 |
|
106 my $action = "create"; |
|
107 |
|
108 if ($updating) { |
|
109 $action = "update"; |
|
110 } |
|
111 else { |
|
112 # see if the destination directory exists to create the file into... |
|
113 my $directory = dirname($dstfile); |
|
114 if (!-e $directory) { |
|
115 # ...and attempt to create it if not. |
|
116 if (!mkpath ($directory)) { |
|
117 print STDERR "Failed to create directory $directory\n"; |
|
118 exit 1; |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 |
|
124 if (!copy ($srcfile, $dstfile)) |
|
125 { |
|
126 print STDERR "Failed to $action file $dstfile\n"; |
|
127 unlink $dstfile; |
|
128 exit 1; |
|
129 } |
|
130 |
|
131 |
|
132 printf "%sd %s\n", ucfirst $action, $dstfile; |
|
133 exit 0; |
|
134 |
|
135 |