From: Matthias Lederhofer Date: 2006-12-12T03:45:34+09:00 Subject: [PATCH] File.syscopy: close from if open on to fails File.syscopy did not close from if open on to failed. The garbage collector seems to take care of this after a while. This patch should not change the semantics of the function. I have two additional questions about this method: 1. Why does the function return false if syswrite/sysread fail but throws an exception on any other error? 2. Shouldn't open(to) already get the permissions from the old file if the file does not exist? Copying a non-world-readable file with this method may expose it during copying even though it gets stricter permissions after copying. PS: Can I post something like this without subscribing to the mailing list? --- lib/ftools.rb | 29 +++++++++++++++-------------- 1 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/ftools.rb b/lib/ftools.rb index 5f08233..fd9dbcf 100644 --- a/lib/ftools.rb +++ b/lib/ftools.rb @@ -57,25 +57,26 @@ class << File to = catname(from, to) fmode = stat(from).mode - tpath = to - not_exist = !exist?(tpath) - - from = open(from, "rb") - to = open(to, "wb") + not_exist = !exist?(to) begin - while true - to.syswrite from.sysread(BUFSIZE) + ffile = open(from, "rb") + tfile = open(to, "wb") + + begin + while true + tfile.syswrite ffile.sysread(BUFSIZE) + end + rescue EOFError + ret = true + rescue + ret = false end - rescue EOFError - ret = true - rescue - ret = false ensure - to.close - from.close + tfile.close if tfile + ffile.close if ffile end - chmod(fmode, tpath) if not_exist + chmod(fmode, to) if not_exist ret end -- 1.4.4.1