|
1 # Copyright (c) 2000-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 the License "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 # Description: |
|
17 # RemoteSite::NetDrive.pm |
|
18 # |
|
19 |
|
20 package RemoteSite::NetDrive; |
|
21 |
|
22 use strict; |
|
23 use File::Copy; |
|
24 use File::Basename; |
|
25 |
|
26 use RemoteSite; |
|
27 use vars qw(@ISA); |
|
28 @ISA=("RemoteSite"); |
|
29 |
|
30 # |
|
31 # Initialization |
|
32 # |
|
33 |
|
34 sub Initialize { |
|
35 my $self = shift; |
|
36 $self->SUPER::Initialize(@_); |
|
37 |
|
38 #connect to network drive |
|
39 $self->Connect(); |
|
40 } |
|
41 |
|
42 # |
|
43 # Public (from RemoteSite) |
|
44 # |
|
45 |
|
46 sub SendFile { |
|
47 my $self = shift; |
|
48 my $localFile = shift; |
|
49 my $remoteFile = shift; |
|
50 |
|
51 unless (defined $localFile and defined $remoteFile) { |
|
52 $self->HandleError("Incorrect args passed to ".ref($self)."::SendFile"); |
|
53 } |
|
54 |
|
55 $remoteFile = Utils::ConcatenateDirNames($self->Host(), $remoteFile); |
|
56 $remoteFile =~ s{\\}{\/}g; |
|
57 |
|
58 if ($self->{verbose}) { |
|
59 print "Copying ".basename($localFile)." to network drive ".$self->Host()."...\n"; |
|
60 } |
|
61 elsif (Utils::FileSize($localFile)) { |
|
62 print "Copying ".basename($localFile)."...\n"; |
|
63 } |
|
64 |
|
65 unless (-e $localFile) { |
|
66 $self->HandleError("Local file $localFile does not exist"); |
|
67 } |
|
68 |
|
69 $self->Connect(); |
|
70 |
|
71 my $remoteDir = dirname($remoteFile); |
|
72 unless (-e $remoteDir) { |
|
73 eval { |
|
74 Utils::MakeDir($remoteDir); |
|
75 }; |
|
76 if ($@) { |
|
77 $self->HandleError("Cannot make directory $remoteDir on network drive ".$self->Host()); |
|
78 } |
|
79 if ($self->{verbose}) { |
|
80 print "Created directory $remoteDir on network drive\n"; |
|
81 } |
|
82 } |
|
83 |
|
84 #use a temporary file during uploads |
|
85 my $tmpFile = $remoteDir.'/TMP_'.basename($remoteFile); |
|
86 |
|
87 unless (copy($localFile, $tmpFile)){ |
|
88 my $flag = 0; |
|
89 my $errormessage = $!; |
|
90 |
|
91 if(-e $tmpFile) { |
|
92 unlink $tmpFile or $flag=1; |
|
93 } |
|
94 |
|
95 if($errormessage =~ /No such file or directory/i) { |
|
96 $errormessage = "Unknown Error - Check disk space or missing file/directory"; |
|
97 } |
|
98 |
|
99 if($flag) { |
|
100 $self->HandleError("Unable to cleanup $tmpFile, after the copy of $localFile failed : $errormessage"); |
|
101 } |
|
102 $self->HandleError("Unable to copy $localFile to $tmpFile : $errormessage"); |
|
103 } |
|
104 |
|
105 unless (move($tmpFile, $remoteFile)){ |
|
106 unlink $tmpFile; |
|
107 $self->HandleError("Unable to move $tmpFile to $remoteFile : $!"); |
|
108 } |
|
109 |
|
110 if ($self->{verbose} > 1) { |
|
111 print "Copy successful. Stored as $remoteFile on network drive.\n"; |
|
112 } |
|
113 } |
|
114 |
|
115 sub GetFile { |
|
116 my $self = shift; |
|
117 my $remoteFile = shift; |
|
118 my $localFile = shift; |
|
119 |
|
120 unless (defined $localFile and defined $remoteFile) { |
|
121 $self->HandleError("Incorrect args passed to ".ref($self)."::GetFile"); |
|
122 } |
|
123 |
|
124 my $host = $self->Host(); |
|
125 $host =~ s{\\}{\/}g; |
|
126 $remoteFile =~ s{\\}{\/}g; |
|
127 |
|
128 if ($self->{verbose}) { |
|
129 print "Copying ".basename($remoteFile)." from network drive $host...\n"; |
|
130 } |
|
131 else { |
|
132 print "Copying ".basename($remoteFile)."...\n"; |
|
133 } |
|
134 |
|
135 $self->Connect(); |
|
136 |
|
137 if ($self->{verbose}) { |
|
138 print "Checking whether \"$remoteFile\" exists...\n"; |
|
139 } |
|
140 unless ($self->FileExists($remoteFile)) { |
|
141 $self->HandleError("Remote file $remoteFile does not exist on $host"); |
|
142 } |
|
143 |
|
144 #check local dir exists and create it if it doesn't |
|
145 my $localDir = dirname($localFile); |
|
146 unless (-e $localDir) { |
|
147 Utils::MakeDir($localDir); |
|
148 if ($self->{verbose}) { |
|
149 print "Created directory $localDir on local drive\n"; |
|
150 } |
|
151 } |
|
152 |
|
153 unless (copy($host.$remoteFile, $localFile)) { |
|
154 unlink $localFile; |
|
155 $self->HandleError("Transfer of $remoteFile from $host to local drive failed"); |
|
156 } |
|
157 if ($self->{verbose} > 1) { |
|
158 print "Copy successful. Stored as $localFile on local drive.\n"; |
|
159 } |
|
160 } |
|
161 |
|
162 sub FileExists { |
|
163 my $self = shift; |
|
164 my $remoteFile = shift; |
|
165 |
|
166 unless (defined $remoteFile) { |
|
167 return 0; |
|
168 } |
|
169 |
|
170 $self->Connect(); |
|
171 |
|
172 $remoteFile = Utils::ConcatenateDirNames($self->Host(), $remoteFile); |
|
173 $remoteFile =~ s{\\}{\/}g; |
|
174 return (-e $remoteFile); |
|
175 } |
|
176 |
|
177 sub DirExists { |
|
178 my $self = shift; |
|
179 my $remoteDir = shift; |
|
180 return $self->FileExists($remoteDir); |
|
181 } |
|
182 |
|
183 sub DirList { |
|
184 my $self = shift; |
|
185 my $remoteDir = shift; |
|
186 |
|
187 my $host = $self->Host(); |
|
188 $host =~ s{\\}{\/}g; |
|
189 $remoteDir =~ s{\\}{\/}g; |
|
190 |
|
191 opendir(DIR, Utils::ConcatenateDirNames($host, $remoteDir)) or $self->HandleError("Cannot open $remoteDir on network drive ".$self->Host()); |
|
192 my @dir = map {"$remoteDir/$_"} grep {$_ ne '.' and $_ ne '..'} readdir DIR; |
|
193 closedir(DIR); |
|
194 return \@dir; |
|
195 } |
|
196 |
|
197 sub MakeDir { |
|
198 my $self = shift; |
|
199 my $remoteDir = shift; |
|
200 |
|
201 $remoteDir = $self->Host().$remoteDir; |
|
202 $remoteDir =~ s{\\}{\/}g; |
|
203 |
|
204 eval { |
|
205 Utils::MakeDir($remoteDir); |
|
206 }; |
|
207 if ($@) { |
|
208 $self->HandleError("Cannot make directory $remoteDir on network drive ".$self->Host()); |
|
209 } |
|
210 } |
|
211 |
|
212 sub FileSize { |
|
213 my $self = shift; |
|
214 my $remoteFile = shift; |
|
215 |
|
216 $remoteFile = Utils::ConcatenateDirNames($self->Host(), $remoteFile); |
|
217 $remoteFile =~ s{\\}{\/}g; |
|
218 |
|
219 return Utils::FileSize($remoteFile); |
|
220 } |
|
221 |
|
222 sub DeleteFile { |
|
223 my $self = shift; |
|
224 my $remoteFile = shift; |
|
225 |
|
226 $remoteFile = Utils::ConcatenateDirNames($self->Host(), $remoteFile); |
|
227 $remoteFile =~ s{\\}{\/}g; |
|
228 |
|
229 rmdir $remoteFile or unlink $remoteFile or $self->HandleError("Cannot delete $remoteFile on network dirve ($!)"); |
|
230 } |
|
231 |
|
232 sub MoveFile { |
|
233 my $self = shift; |
|
234 my $oldFile = shift; |
|
235 my $newFile = shift; |
|
236 |
|
237 $oldFile = Utils::ConcatenateDirNames($self->Host(), $oldFile); |
|
238 $oldFile =~ s{\\}{\/}g; |
|
239 $newFile = Utils::ConcatenateDirNames($self->Host(), $newFile); |
|
240 $newFile =~ s{\\}{\/}g; |
|
241 |
|
242 move($oldFile, $newFile) or $self->HandleError("Cannot move $oldFile to $newFile on network drive"); |
|
243 } |
|
244 |
|
245 sub FileModifiedTime { |
|
246 my $self = shift; |
|
247 my $remoteFile = shift; |
|
248 |
|
249 $remoteFile = Utils::ConcatenateDirNames($self->Host(), $remoteFile); |
|
250 $remoteFile =~ s{\\}{\/}g; |
|
251 |
|
252 return Utils::FileModifiedTime($remoteFile); |
|
253 } |
|
254 |
|
255 |
|
256 # |
|
257 # Private |
|
258 # |
|
259 |
|
260 sub Connect { |
|
261 my $self = shift; |
|
262 |
|
263 unless ($self->Host()) { |
|
264 $self->HandleError("Network drive host name not defined"); |
|
265 } |
|
266 my $hostName = $self->Host(); |
|
267 unless (-e $hostName) { |
|
268 $self->HandleError("Cannot connect to network drive $hostName"); |
|
269 } |
|
270 } |
|
271 |
|
272 1; |
|
273 |
|
274 =head1 NAME |
|
275 |
|
276 RemoteSite::NetDrive.pm - Access a remote network drive |
|
277 |
|
278 =head1 SYNOPSIS |
|
279 |
|
280 use RemoteSite::NetDrive; |
|
281 |
|
282 $drive = RemoteSite::NetDrive->New(host => '\\server\share', |
|
283 verbose => 1); |
|
284 |
|
285 if ($drive->FileExists('/somedir/someremotefile')) { |
|
286 do something... |
|
287 } |
|
288 $drive->SendFile('somelocalfile', 'someremotefile'); |
|
289 $drive->GetFile('someremotefile', 'somelocalfile'); |
|
290 |
|
291 =head1 DESCRIPTION |
|
292 |
|
293 C<RemoteSite::NetDrive> is inherited from the abstract base class C<RemoteSite>, implementing the abstract methods required for transfer of files to and from a remote site when the remote site is a network drive. |
|
294 |
|
295 =head1 INTERFACE |
|
296 |
|
297 =head2 New |
|
298 |
|
299 Passed an argument list in the form of hash key value pairs. The supported arguments are... |
|
300 |
|
301 host => $host_address_string |
|
302 verbose => $verbosity_integer |
|
303 |
|
304 Returns a reference to a C<RemoteSite::NetDrive> object |
|
305 |
|
306 =head2 Host |
|
307 |
|
308 Returns the current value of the C<host> attribute which contains the UNC path of the network drive. If passed an argument sets the attribute to this new value. |
|
309 |
|
310 =head2 SendFile |
|
311 |
|
312 Passed a local and a remote file name. Uploads the local file to the network drive. |
|
313 |
|
314 =head2 GetFile |
|
315 |
|
316 Passed a remote and local file name. Downloads the remote file from the network drive and stores it on the local drive. |
|
317 |
|
318 =head2 FileExists |
|
319 |
|
320 Passed a filename (with full path) on the network drive. Returns a non zero value if the file exists. |
|
321 |
|
322 =head1 KNOWN BUGS |
|
323 |
|
324 None |
|
325 |
|
326 =head1 COPYRIGHT |
|
327 |
|
328 Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
329 All rights reserved. |
|
330 This component and the accompanying materials are made available |
|
331 under the terms of the License "Eclipse Public License v1.0" |
|
332 which accompanies this distribution, and is available |
|
333 at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
334 |
|
335 Initial Contributors: |
|
336 Nokia Corporation - initial contribution. |
|
337 |
|
338 Contributors: |
|
339 |
|
340 Description: |
|
341 |
|
342 |
|
343 =cut |