releasing/cbrtools/perl/RemoteSite/FTP.pm
author jjkang
Wed, 30 Jun 2010 11:35:58 +0800
changeset 607 378360dbbdba
parent 602 3145852acc89
permissions -rw-r--r--
Merge missed changeset 11 (raptor v2.14 and helium 10.0)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
# Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
# RemoteSite::FTP.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
package RemoteSite::FTP;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
use Net::FTP;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
use File::Basename;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
use IO::File;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
use RemoteSite;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
use vars qw(@ISA);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
@ISA=("RemoteSite");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
# Constants
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
use constant DEFAULTRECONNECTS => 5;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
use constant DEFAULTTIMEOUT => 30;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
use constant BLOCKSIZE => 32768;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
# Initialization
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
sub Initialize {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
  my %args = @_;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
  $self->{username} = $args{username};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
  $self->{password} = $args{password};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
  $self->{passiveMode} = $args{passive_mode};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
  $self->{resumeMode} = $args{resume_mode};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
  $self->{timeout} = $args{timeout};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
  $self->{reconnects} = $args{reconnects};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
  #call base class initialization
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
  $self->SUPER::Initialize(@_);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
  #if username or password not defined ask for them interactively
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
  unless ($self->Username()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
    $self->HandleError("No remote host defined.") unless $self->Host();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
    print 'FTP username: ';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
    my $userName = <STDIN>;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
    if ($userName) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
      chomp ($userName);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
      $self->Username($userName);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
  unless ($self->Password()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
    print 'FTP password: ';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
    $self->Password(Utils::QueryPassword());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
  #set timeout to default value if not set or not a positive integer
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
  unless (defined $self->{timeout} and $self->{timeout} =~ /^\d+$/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
    $self->{timeout} = DEFAULTTIMEOUT;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
  #set reconnects to default value if not set or not a positive integer
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
  unless (defined $self->{reconnects} and $self->{reconnects} =~ /^\d+$/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
    $self->{reconnects} = DEFAULTRECONNECTS;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
  #connect to FTP site, login and set to binary mode
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
  $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
# Public getters/setters
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
sub Username {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
  if (defined $_[0]) {$self->{username} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
  return $self->{username};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
sub Password {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
  if (defined $_[0]) {$self->{password} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
  return $self->{password};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
sub PassiveMode {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
  if (defined $_[0]) {$self->{passiveMode} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
  return $self->{passiveMode};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
sub ResumeMode {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
  if (defined $_[0]) {$self->{resumeMode} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
  return $self->{resumeMode};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
sub Timeout {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
  return $self->{timeout};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
sub Reconnects {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
  return $self->{reconnects};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
# Public (from RemoteSite)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
sub SendFile {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
  my $localFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
  unless (defined $localFile and defined $remoteFile) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
    $self->HandleError("Incorrect args passed to ".ref($self)."::SendFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
  $remoteFile =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
  my $localFileSize = Utils::FileSize($localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
  if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
    print 'Uploading '.basename($localFile).' to FTP site '.$self->Host()." ...\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
  elsif ($localFileSize) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
    print 'Uploading '.basename($localFile).':    ';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
  #check the file to upload exists
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
  unless (-e $localFile) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
    $self->HandleError("Local file $localFile does not exist");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
  #check remote dir exists and create it if it doesn't
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
  my $remoteDir = dirname($remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
  unless ($self->DirExists($remoteDir)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
    $self->MakeDir($remoteDir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
  #if a file with same name as the remote file already exists delete it (even if it has different case)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
  if (my $actualFileName = $self->FileExists($remoteFile)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
    $self->DeleteFile($actualFileName);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
  #create a temporary file name in the remote directory for uploading to
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
  my $tmpFile = $self->CreateTemporaryFile($remoteDir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
  #send the file
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
  if ($self->ResumeMode()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
    $self->SendFileWithResume($localFile, $tmpFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
    if ($self->{verbose} and $localFileSize) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
      print "Upload progress: ";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
    $self->DisplayProgress($localFileSize);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
    $self->SendFileWithoutResume($localFile, $tmpFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   178
  #rename the temporary file to the final remote file name
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   179
  $self->MoveFile($tmpFile, $remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   180
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   181
  if ($self->{verbose} > 1) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   182
    print "Upload successful. Stored as $remoteFile on FTP site.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   183
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   184
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   185
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   186
sub GetFile {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   187
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   188
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   189
  my $localFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   190
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   191
  unless (defined $localFile and defined $remoteFile) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   192
    $self->HandleError("Incorrect args passed to ".ref($self)."::GetFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   193
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   194
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   195
  $remoteFile =~ s{\\}{\/}g;     #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   196
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   197
  if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   198
    print "Downloading ".$remoteFile." from FTP site ".$self->Host()." ...\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   199
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   200
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   201
    print "Downloading ".basename($remoteFile).":    ";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   202
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   203
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   204
  #check that the file to download exists
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   205
  my $actualFileName;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   206
  unless ($actualFileName = $self->FileExists($remoteFile)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   207
    $self->HandleError("Remote file $remoteFile does not exist");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   208
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   209
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   210
  $remoteFile = $actualFileName;  #handles case sensitivity correctly
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   211
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   212
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   213
  #check local dir exists and create it if it doesn't
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   214
  my $localDir = dirname($localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   215
  unless (-e $localDir) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   216
    Utils::MakeDir($localDir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   217
    if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   218
      print "Created directory $localDir on local drive\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   219
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   220
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   221
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   222
  my $remoteFileSize = $self->FileSize($remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   223
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   224
  if ($self->{verbose} and $remoteFileSize) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   225
    print "Download progress: ";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   226
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   227
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   228
  #get the file
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   229
  if ($self->ResumeMode()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   230
    $self->DisplayProgress($remoteFileSize);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   231
    $self->GetFileWithResume($remoteFile, $localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   232
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   233
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   234
    $self->DisplayProgress($remoteFileSize);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   235
    $self->GetFileWithoutResume($remoteFile, $localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   236
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   237
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   238
  if ($self->{verbose} > 1) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   239
    print "Download successful. Stored as $localFile on local site.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   240
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   241
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   242
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   243
sub FileExists {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   244
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   245
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   246
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   247
  unless (defined $remoteFile) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   248
    return 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   249
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   250
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   251
  #use Carp qw/cluck/;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   252
  #cluck "Called FileExists";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   253
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   254
  # List the directory the file is in, and see if the file name is in it.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   255
  $remoteFile =~ s{\/}{\\}g;     #convert forward slashes to back slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   256
  (my $path, my $baseName, my $ext) = Utils::SplitFileName($remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   257
  my $fileName = $baseName . $ext;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   258
  $path =~ s/\\$//;       #remove trailing slash
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   259
  $path =~ s/\\/\//g;     #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   260
  my $ls = $self->DirList($path);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   261
  print "Checking for existence of remote file \"$remoteFile\" by looking for \"$fileName\" in \"$path\".\n" if ($self->{verbose} && $ls);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   262
  return 0 unless $ls; # definitely doesn't exist if nothing in the directory
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   263
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   264
  my @present = grep /(\/|\\|^\s*)\Q$fileName\E\s*$/i, @$ls;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   265
  if (@present) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   266
    print "Have found file: YES\n" if ($self->{verbose});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   267
    $present[0] = $path."/".$present[0] if ( $present[0] !~ /\// );
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   268
    return $present[0];
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   269
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   270
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   271
    print "Have found file: NO\n" if ($self->{verbose});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   272
    return 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   273
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   274
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   275
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   276
sub DirList {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   277
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   278
  my $remoteDir = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   279
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   280
  print "Listing FTP directory $remoteDir\n" if ($self->{verbose});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   281
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   282
  my $dirlist_retries = 3;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   283
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   284
  $remoteDir =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   285
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   286
  my $retry;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   287
  for ($retry = 0; $retry < $dirlist_retries; $retry++) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   288
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   289
    unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   290
      $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   291
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   292
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   293
    # The Net::FTP module that we're using here has two options for listing the contents
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   294
    # of a directory. They are the 'ls' and 'dir' calls.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   295
    # The 'ls' call is great, and just returns a list of the items. But, irritatingly, it
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   296
    # misses out directories: the returned list just contains names of *files*.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   297
    # dir is better, in some ways, as it lists directories too, but its output format
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   298
    # varies from one FTP site to the next. So we have to stick with ls.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   299
    print "About to call dir(\"$remoteDir\")\n" if ($self->{verbose});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   300
    my $ls = $self->{ftp}->ls($remoteDir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   301
    my $resp = $self->{ftp}->message;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   302
    print "FTP response to list command was \"$resp\"\n" if ($self->{verbose});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   303
    if (ref $ls) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   304
      print "FTP dir returned \"$ls\" which is a ".(ref $ls)." containing ".(scalar @$ls)." items\n" if ($self->{verbose});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   305
      $ls = undef if ($resp eq ""); # if we didn't get "Opening BINARY mode connection..." or something similar, then we've
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   306
        # come across the problem where Net::FTP says Net::FTP: Unexpected EOF on command channel at d:/reltools/2.6x/personal/bin/Net
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   307
        # /FTP/dataconn.pm line 73. Unfortunately, it doesn't die, and it returns an empty array, so the only way to find out this has
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   308
        # happened is to check message.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   309
      $ls = undef if ($resp =~ m/^connection closed/i);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   310
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   311
    # $ls might now be undef
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   312
    if (ref($ls)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   313
      return $ls;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   314
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   315
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   316
      if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   317
        return undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   318
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   319
      else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   320
        print "Warning: Listing of \"$remoteDir\" failed due to an FTP site problem: " . $self->{ftp}->message . ". ";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   321
        if ($self->PassiveMode()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   322
          print "PASV mode FTP is currently enabled. This can cause connectivity issues under certain circumstances. ",
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   323
            "To disable, remove the pasv_transfer_mode directive from your reltools.ini file.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   324
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   325
        else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   326
          print "PASV mode FTP is currently disabled. Enabling it can prevent connectivity issues under certain circumstances. ",
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   327
            "To enable, add the pasv_transfer_mode directive to your reltools.ini file.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   328
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   329
        # Fall through to next loop iteration
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   330
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   331
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   332
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   333
  die "Error: have tried to list \"$remoteDir\" $retry times with no success - giving up\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   334
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   335
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   336
sub MakeDir {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   337
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   338
  my $remoteDir = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   339
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   340
  $remoteDir =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   341
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   342
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   343
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   344
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   345
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   346
  if ($self->{ftp}->mkdir($remoteDir, 1)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   347
    if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   348
      print "Created directory $remoteDir on FTP site\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   349
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   350
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   351
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   352
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   353
      $self->HandleError("Cannot make directory $remoteDir on FTP site");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   354
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   355
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   356
      $self->MakeDir($remoteDir);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   357
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   358
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   359
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   360
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   361
sub FileSize {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   362
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   363
  my $file = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   364
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   365
  $file =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   366
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   367
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   368
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   369
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   370
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   371
  my $size;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   372
  if (defined($size = $self->{ftp}->size($file))) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   373
    return $size;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   374
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   375
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   376
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   377
      return 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   378
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   379
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   380
      $self->FileSize($file);  #try to get the size again after reconnecting
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   381
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   382
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   383
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   384
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   385
sub DeleteFile {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   386
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   387
  my $file = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   388
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   389
  $file =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   390
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   391
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   392
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   393
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   394
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   395
  if ($self->{ftp}->delete($file)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   396
    return;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   397
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   398
  elsif ($self->{ftp}->rmdir($file)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   399
    return;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   400
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   401
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   402
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   403
      $self->HandleError("Cannot delete $file on FTP site");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   404
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   405
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   406
      $self->DeleteFile($file);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   407
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   408
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   409
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   410
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   411
sub MoveFile {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   412
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   413
  my $oldFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   414
  my $newFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   415
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   416
  $oldFile =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   417
  $newFile =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   418
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   419
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   420
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   421
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   422
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   423
  if ($self->{ftp}->rename($oldFile, $newFile)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   424
    return;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   425
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   426
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   427
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   428
      $self->HandleError("Cannot move $oldFile to $newFile on FTP site");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   429
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   430
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   431
      $self->MoveFile($oldFile, $newFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   432
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   433
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   434
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   435
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   436
sub FileModifiedTime {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   437
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   438
  my $file = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   439
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   440
  $file =~ s{\\}{\/}g;   #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   441
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   442
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   443
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   444
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   445
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   446
  my $modifiedTime;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   447
  if (defined($modifiedTime = $self->{ftp}->mdtm($file))) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   448
    return $modifiedTime;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   449
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   450
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   451
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   452
      print "Warning: failed to find modified time for file \"$file\"\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   453
      return undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   454
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   455
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   456
      $self->FileModifiedTime($file);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   457
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   458
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   459
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   460
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   461
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   462
# Private
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   463
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   464
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   465
sub Connect {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   466
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   467
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   468
  unless ($self->Host()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   469
    $self->HandleError("Cannot connect FTP host name not defined");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   470
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   471
  my $debug = (($self->{verbose} && $self->{verbose} > 1) ? 1 : 0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   472
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   473
  #Attempt to connect (or reconnect if connection fails)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   474
  for (1..$self->Reconnects()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   475
    $self->{ftp} = undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   476
    if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   477
      print "Connecting to FTP site ".$self->Host()."...\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   478
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   479
    $self->{ftp} = Net::FTP->new($self->Host(),
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   480
				 Passive => $self->PassiveMode(),
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   481
				 Debug => $debug,
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   482
				 Timeout => $self->Timeout());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   483
    if (defined $self->{ftp}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   484
      #login to FTP site
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   485
      $self->{ftp}->login($self->Username(), $self->Password())
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   486
	or $self->HandleError("FTP login failed");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   487
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   488
      #change transfer mode to binary
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   489
      $self->{ftp}->binary()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   490
	or $self->HandleError("Failed to set FTP server to binary transfer mode");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   491
      return;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   492
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   493
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   494
  $self->HandleError("Cannot connect to FTP site ".$self->Host());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   495
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   496
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   497
sub Connected {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   498
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   499
  return (defined $self->{ftp} and defined $self->{ftp}->pwd);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   500
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   501
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   502
sub SendFileWithResume {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   503
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   504
  my $localFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   505
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   506
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   507
  #open the local file for reading
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   508
  $self->{localfh} = IO::File->new("< $localFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   509
  binmode($self->{localfh});
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   510
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   511
  my $localFileSize = Utils::FileSize($localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   512
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   513
  my $buffer;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   514
  my $bytesSent;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   515
  my $totalBytesSent = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   516
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   517
 RESUME:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   518
  #Open the temporary file on the FTP site for writing/appending
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   519
  $self->{dataconn} = $self->OpenRemoteFileForAppending($remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   520
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   521
  if ($self->{verbose} and $localFileSize) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   522
    print "Upload progress:    ";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   523
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   524
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   525
  #upload temporary file in blocks
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   526
  while ($self->{localfh}->read($buffer, BLOCKSIZE)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   527
    eval {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   528
      $bytesSent = $self->{dataconn}->write($buffer, length($buffer));
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   529
    };
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   530
    unless ($bytesSent) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   531
      if (my $ftpResponse = $self->{ftp}->getline()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   532
        $self->{ftp}->ungetline($ftpResponse);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   533
        next if ($ftpResponse !~ m/^(3|4|5)/);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   534
        chomp $ftpResponse;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   535
        print "\nError: The FTP server returned \'$ftpResponse\'\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   536
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   537
      
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   538
      if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   539
	$self->HandleError("Cannot append to remote file $remoteFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   540
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   541
      else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   542
	#connection dropped. Reconnect and resume upload
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   543
	if ($self->{verbose}) {print "\n"}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   544
	$self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   545
	$totalBytesSent = $self->FileSize($remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   546
	seek($self->{localfh}, $totalBytesSent, 0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   547
	goto RESUME;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   548
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   549
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   550
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   551
      $totalBytesSent += $bytesSent;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   552
      $self->UpdateProgress($totalBytesSent, $localFileSize);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   553
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   554
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   555
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   556
  #close the remote and local files now the transfer has finished
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   557
  $self->CloseAllOpenFiles();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   558
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   559
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   560
sub SendFileWithoutResume {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   561
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   562
  my $localFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   563
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   564
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   565
  my $putSuccess;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   566
  eval {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   567
    $putSuccess = $self->{ftp}->put($localFile, $remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   568
  };
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   569
  unless ($putSuccess) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   570
    $self->HandleError("Problem occurred during FTP upload of $localFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   571
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   572
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   573
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   574
sub GetFileWithResume {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   575
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   576
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   577
  my $localFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   578
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   579
  my $totalBytesReceived = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   580
  my $getSuccess;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   581
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   582
 RESUME:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   583
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   584
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   585
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   586
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   587
  eval {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   588
    $getSuccess = $self->{ftp}->get($remoteFile, $localFile, $totalBytesReceived);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   589
  };
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   590
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   591
  unless ($getSuccess or !$@) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   592
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   593
      $self->HandleError("Problem occurred during FTP download of $remoteFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   594
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   595
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   596
      $totalBytesReceived = Utils::FileSize($localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   597
      goto RESUME;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   598
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   599
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   600
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   601
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   602
sub GetFileWithoutResume {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   603
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   604
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   605
  my $localFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   606
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   607
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   608
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   609
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   610
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   611
  my $getSuccess;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   612
  eval {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   613
    $getSuccess = $self->{ftp}->get($remoteFile, $localFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   614
  };
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   615
  unless ($getSuccess) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   616
    $self->HandleError("Problem occurred during FTP download of $remoteFile");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   617
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   618
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   619
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   620
sub DirExists {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   621
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   622
  my $remoteDir = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   623
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   624
  $remoteDir =~ s{\\}{\/}g;     #convert back slashes to forward slashes
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   625
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   626
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   627
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   628
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   629
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   630
  my $pwd = $self->{ftp}->pwd() or $self->HandleError("Problem reading current working directory on FTP site\n");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   631
  my $exists = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   632
  if ($self->{ftp}->cwd($remoteDir)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   633
    $exists = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   634
    $self->{ftp}->cwd($pwd) or $self->HandleError("Problem changing current working directory back to $pwd on FTP site\n");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   635
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   636
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   637
  return $exists;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   638
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   639
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   640
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   641
sub OpenRemoteFileForAppending {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   642
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   643
  my $remoteFile = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   644
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   645
  unless ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   646
    $self->Connect();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   647
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   648
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   649
  my $dataconn;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   650
  if (defined($dataconn = $self->{ftp}->appe($remoteFile))) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   651
    return $dataconn;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   652
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   653
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   654
    if ($self->Connected()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   655
      $self->HandleError("Cannot open $remoteFile for appending on FTP site");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   656
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   657
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   658
      $self->OpenRemoteFileForAppending($remoteFile);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   659
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   660
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   661
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   662
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   663
sub CloseAllOpenFiles {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   664
   my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   665
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   666
  if ($self->{localfh}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   667
    $self->{localfh}->close;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   668
    $self->{localfh} = undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   669
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   670
  if ($self->{dataconn}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   671
    $self->{dataconn}->close();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   672
    $self->{dataconn} = undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   673
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   674
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   675
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   676
sub DisplayProgress {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   677
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   678
  my $total = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   679
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   680
  my $numHashes = 50;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   681
  my $bytesPerHash = int $total / $numHashes;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   682
  if ($total) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   683
    $self->{ftp}->hash(\*STDERR, $bytesPerHash);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   684
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   685
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   686
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   687
sub UpdateProgress {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   688
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   689
  my $current = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   690
  my $total = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   691
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   692
  my $bytesPerPercent = int $total/100;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   693
  if ($current == $total) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   694
    print "\b\b\b100%\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   695
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   696
  elsif ($bytesPerPercent == 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   697
    print "\b\b0%";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   698
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   699
  else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   700
    my $percentComplete = int $current/$bytesPerPercent;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   701
    if ($percentComplete < 10) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   702
      print "\b\b$percentComplete%";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   703
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   704
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   705
      print "\b\b\b$percentComplete%";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   706
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   707
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   708
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   709
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   710
sub HandleError {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   711
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   712
  my $errorString = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   713
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   714
  if (defined $self->{ftp}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   715
    $self->{ftp}->quit();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   716
    $self->{ftp} = undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   717
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   718
  $self->CloseAllOpenFiles();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   719
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   720
  #call the super class error handler
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   721
  $self->SUPER::HandleError($errorString);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   722
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   723
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   724
sub CreateTemporaryFile {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   725
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   726
  my $remoteDir = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   727
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   728
  my $fileNum = 10000;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   729
  my $tmpFile = $remoteDir.'/lpdrt'.$fileNum.'.tmp';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   730
  while ($self->FileExists($tmpFile)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   731
    ++$fileNum;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   732
    $tmpFile = $remoteDir.'/lpdrt'.$fileNum.'.tmp';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   733
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   734
  return $tmpFile;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   735
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   736
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   737
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   738
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   739
# Destructor
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   740
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   741
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   742
sub DESTROY {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   743
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   744
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   745
  $self->CloseAllOpenFiles();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   746
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   747
  if (defined $self->{ftp}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   748
    if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   749
      print "Dropping connection to FTP site ".$self->Host()."\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   750
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   751
    $self->{ftp}->quit();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   752
    $self->{ftp} = undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   753
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   754
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   755
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   756
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   757
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   758
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   759
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   760
RemoteSite::FTP.pm - Access a remote FTP site.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   761
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   762
=head1 SYNOPSIS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   763
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   764
 use RemoteSite::FTP;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   765
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   766
 $ftp = RemoteSite::FTP->New(host => 'ftp.somehost.com',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   767
	         	     username => 'myusername',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   768
			     password => 'mypassword',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   769
			     verbose => 1);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   770
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   771
 if ($ftp->FileExists('/somedir/someremotefile')) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   772
   do something...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   773
 }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   774
 $ftp->SendFile('somelocalfile', 'someremotefile');
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   775
 $ftp->GetFile('someremotefile', 'somelocalfile');
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   776
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   777
=head1 DESCRIPTION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   778
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   779
C<RemoteSite::FTP> 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 an FTP server.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   780
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   781
=head1 INTERFACE
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   782
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   783
=head2 New
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   784
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   785
Passed an argument list in the form of hash key value pairs. The supported arguments are...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   786
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   787
  host             => $host_address_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   788
  username         => $user_name_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   789
  password         => $pass_word_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   790
  passiveMode      => $passive_mode_bool
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   791
  resumeTransfers  => $resume_transfers_bool
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   792
  timeout          => $timeout_integer
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   793
  reconnects       => $reconnects_integer
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   794
  verbose          => $verbosity_integer
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   795
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   796
Returns a reference to a C<RemoteSite::FTP> object
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   797
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   798
=head2 Host
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   799
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   800
Returns the current value of the C<host> attribute which contains the host FTP address. If passed an argument sets the attribute to this new value.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   801
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   802
=head2 Username
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   803
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   804
Returns the current value of the C<username> attribute which stores the user name required to access the FTP site. If passed an argument sets the attribute to this new value.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   805
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   806
=head2 Password
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   807
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   808
Returns the current value of the C<password> attribute which stores the password required to access the FTP site. If passed an argument sets the attribute to this new value.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   809
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   810
=head2 SendFile
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   811
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   812
Passed a local and a remote file name. Uploads the local file to the FTP site. Dies if upload fails
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   813
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   814
=head2 GetFile
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   815
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   816
Passed a remote and local file name. Downloads the remote file from the FTP site and stores it on the local drive. Dies if download fails.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   817
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   818
=head2 FileExists
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   819
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   820
Passed a filename (with full path) on the FTP site. Returns a non zero value if the file exists.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   821
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   822
=head2 DirList
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   823
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   824
Passed a directory name. Returns a list of files contained in the directory or undef if fails to read directory
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   825
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   826
=head2 MakeDir
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   827
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   828
Passed a directory name. Creates the directory on the FTP site
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   829
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   830
=head2 DeleteFile
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   831
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   832
Passed a file name. Deletes the file on the FTP site. Dies if fails
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   833
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   834
=head2 FileSize
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   835
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   836
Passed a file name. Returns the size of the file. Returns 0 if fails.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   837
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   838
=head2 FileModifiedTime
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   839
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   840
Passed a file name. Returns the last modified time stamp of the file. Returns undef if fails
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   841
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   842
=head2 MoveFile
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   843
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   844
Passed two file names. Renames the first file to the second file name. Dies if fails.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   845
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   846
=head1 KNOWN BUGS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   847
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   848
None
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   849
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   850
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   851
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   852
 Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   853
 All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   854
 This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   855
 under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   856
 which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   857
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   858
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   859
 Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   860
 Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   861
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   862
 Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   863
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   864
 Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   865
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   866
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   867
=cut