Bugs: Browse | Submit New | Admin

[#11524] net/ftp: failed download creates empty file

Date:
2007-06-12 10:05
Priority:
3
Submitted By:
Ronald Fischer (rovf)
Assigned To:
Akinori MUSHA (knu)
Category:
Network / Comm / Protocols
State:
Open
Platform:
 
Summary:
net/ftp: failed download creates empty file

Detailed description
I'm downloading a file via FTP like this:

require 'net/ftp'
ftp=Net::FTP.new(hostname,userid,password)
begin
  ftp.getbinaryfile('fluffi')
rescue Net::FTPPermError
  puts "download #{filename} failed (#{$!.to_s.chomp})"
end

This works basically fine, with one minor glitch: If the file ('fluffi')
does not exist (i.e. the exception is thrown - "download fluffi failed (550 Can't
open fluffi: No such file or directory)"), there is still a file 'fluffi'
created with length 0.

Is this a bug or intended behaviour? 

Add A Comment: Notepad

Please login


Followup

Message
Date: 2007-06-20 18:22
Sender: Daniel Berger

Yup, that's a bug. This patch fixes it:

--- ftp.orig	Wed Jun 20 12:07:24 2007
+++ ftp.rb	Wed Jun 20 12:18:31 2007
@@ -488,22 +488,27 @@
     # chunks.
     #
     def getbinaryfile(remotefile, localfile =
File.basename(remotefile),
-		      blocksize = DEFAULT_BLOCKSIZE, &block) # :yield:
data
+      blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
       if @resume
-	rest_offset = File.size?(localfile)
-	f = open(localfile, "a")
+         rest_offset = File.size?(localfile)
+         f = open(localfile, "a")
       else
-	rest_offset = nil
-	f = open(localfile, "w")
+         rest_offset = nil
+         f = open(localfile, "w")
       end
+
       begin
-	f.binmode
-	retrbinary("RETR " + remotefile, blocksize, rest_offset)
do |data|
-	  f.write(data)
-	  yield(data) if block
-	end
+         f.binmode
+         retrbinary("RETR " + remotefile, blocksize,
rest_offset) do |data|
+            f.write(data)
+            yield(data) if block
+         end
+      rescue FTPError
+         f.close
+         File.delete(localfile) if File.exists?(localfile)
+         raise
       ensure
-	f.close
+         f.close unless f.closed?
       end
     end
     
@@ -515,12 +520,16 @@
     def gettextfile(remotefile, localfile
= File.basename(remotefile), &block) # :yield: line
       f = open(localfile, "w")
       begin
-	retrlines("RETR " + remotefile) do |line|
-	  f.puts(line)
-	  yield(line) if block
-	end
+         retrlines("RETR " + remotefile) do |line|
+            f.puts(line)
+            yield(line) if block
+         end
+      rescue FTPError
+         f.close
+         File.delete(localfile) if File.exists?(localfile)
+         raise
       ensure
-	f.close
+         f.close unless f.closed?
       end
     end

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

No Changes Have Been Made to This Item