Skip to content

Commit

Permalink
bugfix: sendfile did not close files on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Apr 28, 2015
1 parent 74c2f72 commit 4ff969f
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions base/fs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ uvtype(::File) = Base.UV_RAW_FD

_uv_fs_result(req) = ccall(:jl_uv_fs_result,Int32,(Ptr{Void},),req)

function open(f::File,flags::Integer,mode::Integer)
function open(f::File,flags::Integer,mode::Integer=0)
req = Libc.malloc(_sizeof_uv_fs)
ret = ccall(:uv_fs_open,Int32,(Ptr{Void},Ptr{Void},Ptr{UInt8},Int32,Int32,Ptr{Void}),
eventloop(), req, f.path, flags,mode, C_NULL)
Expand Down Expand Up @@ -127,27 +127,31 @@ end

# For copy command
function sendfile(src::AbstractString, dst::AbstractString)
src_file = open(src, JL_O_RDONLY)
if !src_file.open
throw(ArgumentError("source file \"$(src.path)\" is not open"))
end

dst_file = open(dst, JL_O_CREAT | JL_O_TRUNC | JL_O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP| S_IROTH | S_IWOTH)
if !dst_file.open
throw(ArgumentError("destination file \"$(dst.path)\" is not open"))
end

src_stat = stat(src_file)
err = ccall(:jl_fs_sendfile, Int32, (Int32, Int32, Int64, Csize_t),
fd(src_file), fd(dst_file), 0, src_stat.size)
uv_error("sendfile", err)

if src_file.open
close(src_file)
end
if dst_file.open
close(dst_file)
src_file = File(src)
dst_file = File(dst)
try
open(src_file, JL_O_RDONLY)
if !src_file.open
throw(ArgumentError("source file \"$(src.path)\" is not open"))
end

open(dst_file, JL_O_CREAT | JL_O_TRUNC | JL_O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP| S_IROTH | S_IWOTH)
if !dst_file.open
throw(ArgumentError("destination file \"$(dst.path)\" is not open"))
end

src_stat = stat(src_file)
err = ccall(:jl_fs_sendfile, Int32, (Int32, Int32, Int64, Csize_t),
fd(src_file), fd(dst_file), 0, src_stat.size)
uv_error("sendfile", err)
finally
if src_file.open
close(src_file)
end
if dst_file.open
close(dst_file)
end
end
end

Expand Down

0 comments on commit 4ff969f

Please sign in to comment.