lib/syncftp.rb in syncftp-0.0.2 vs lib/syncftp.rb in syncftp-0.0.3

- old
+ new

@@ -50,11 +50,15 @@ altdir = dir altdir = dir[2..-1] if dir[0,2] == "./" return true if dir == "." - nlst( path ).include?( find ) or nlst( path ).include?( dir ) or nlst( path ).include?( altdir ) + begin + nlst( path ).include?( find ) or nlst( path ).include?( dir ) or nlst( path ).include?( altdir ) + rescue Net::FTPTempError + return false + end end # # Net::FTP extension # @@ -132,15 +136,16 @@ # # * +:local+ : the local directory (default = ".") # * +:remote+ : the remote directory (default = ".") # def sync( options = {} ) - options = { :local => ".", :remote => "." }.merge( options ) - local, remote = options[:local], options[:remote] + options = { :local => ".", :remote => "." , :passive => false}.merge( options ) + local, remote , passive = options[:local], options[:remote], options[:passive] tmpname = tmpfilename connect do |ftp| + ftp.passive = passive # Read remote .syncftp begin ftp.gettextfile( remote+"/"+".syncftp", tmpname ) @remote_md5s = YAML.load( File.open( tmpname ).read ) rescue Net::FTPPermError => e @@ -152,10 +157,30 @@ # Write new .syncftp File.open( tmpname, 'w' ) do |out| YAML.dump( @local_md5s, out ) end + + # Delete files + @delete_dirs = [] + @delete_files = [] + @remote_md5s.keys.clone.delete_if{ |f| @local_md5s.keys.include?(f) }.each do |f| + if @remote_md5s[f] == "*" + @delete_dirs << f + else + @delete_files << f + end + end + @delete_files.each do |f| + @log.info "Delete ftp://#{@host}:#{@port}/#{f}" + ftp.delete( f ) + end + @delete_dirs.each do |f| + @log.info "Delete ftp://#{@host}:#{@port}/#{f}" + ftp.delete( f ) + end + ftp.puttextfile( tmpname, remote+"/"+".syncftp" ) end File.delete( tmpname ) end @@ -196,32 +221,37 @@ yield ftp ftp.close end def send_dir(ftp, local, remote) #:nodoc: - ftp.mkdir_p(remote) unless ftp.remote_dir_exist?(remote) + unless ftp.remote_dir_exist?(remote) + @log.info "Create directory ftp://#{@host}:#{@port}/#{remote}" + ftp.mkdir_p(remote) + end Dir.foreach(local) do |file| next if file == "." or file == ".." local_file = File.join( local, file ) remote_file = remote + "/" + file - + if File.stat(local_file).directory? # It is a directory, we recursively send it + @local_md5s[remote_file] = "*" send_dir(ftp, local_file, remote_file) else @local_md5s[remote_file] = Digest::MD5.hexdigest( File.open(local_file).read ) - + + # Local file still exist... Copy... if( @local_md5s[remote_file] != @remote_md5s[remote_file] ) # It's a file, we just send it if File.binary?(local_file) @log.info "Copy [Binary] #{local_file} to ftp://#{@host}:#{@port}/#{remote_file}" - + ftp.putbinaryfile(local_file, remote_file) else @log.info "Copy [Text] #{local_file} to ftp://#{@host}:#{@port}/#{remote_file}" - + ftp.puttextfile(local_file, remote_file) end else @log.info "#{local_file} don't need to be overwritten !" end