Skip to content

Commit

Permalink
Merge pull request #10 from staticfloat/sf/do_block
Browse files Browse the repository at this point in the history
Add do-block form of `readmeta()`
  • Loading branch information
staticfloat authored Jun 1, 2018
2 parents c69f493 + d3e622f commit 2b58bd3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
27 changes: 19 additions & 8 deletions src/Abstract/ObjectHandle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,31 @@ function readmeta(io::IO)
Object file is not any of $(join(ObjTypes, ", "))!
To force one object file format use readmeta(io, T).
""")
error(replace(msg, "\n" => " "))
throw(MagicMismatch(replace(msg, "\n" => " ")))
end

"""
readmeta(path::AbstractStriong)
Read an Object File out from a file `path`, guessing at the type of object
within the stream by calling `readmeta(io, T)` for each `T` within `ObjTypes`,
and returning the first that does not throw a `MagicMismatch`.
"""
function readmeta(file::AbstractString)
warn("`readmeta(file::AbstractString)` is deprecated, use the do-block variant instead.")
return readmeta(open(file, "r"))
end

"""
readmeta(f::Function, file::AbstractString)
Do-block variant of `readmeta()`. Use via something like:
readmeta("libfoo.so") do f
...
end
"""
function readmeta(f::Function, file::AbstractString)
io = open(file, "r")
try
return f(readmeta(io))
finally
close(io)
end
end


## IOStream-like operations
Expand Down
5 changes: 5 additions & 0 deletions src/MachO/MachOHandle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ function readmeta(io::IO,::Type{MachOHandle})
header_type = macho_header_type(magic)
endianness = macho_endianness(magic)

# If it's fat, just throw MagicMismatch
if header_type <: MachOFatHeader
throw(MagicMismatch("FAT header"))
end

# Unpack the header
header = unpack(io, header_type, endianness)
return MachOHandle(io, start, header, path(io))
Expand Down
10 changes: 8 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ using Compat, Compat.Test

function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
# Actually read it in
oh_exe = readmeta(fooifier_path)
oh_lib = readmeta(libfoo_path)
oh_exe = readmeta(open(fooifier_path, "r"))
oh_lib = readmeta(open(libfoo_path, "r"))

# Tease out some information from the containing folder name
dir_path = basename(dirname(libfoo_path))
Expand Down Expand Up @@ -153,6 +153,12 @@ test_libfoo_and_fooifier("./linux64/fooifier", "./linux64/libfoo.so")
# Run MachO tests
test_libfoo_and_fooifier("./mac64/fooifier", "./mac64/libfoo.dylib")

# Ensure that fat Mach-O files don't look like anything to us
@testset "macfat" begin
@test_throws ObjectFile.MagicMismatch readmeta(open("./macfat/fooifier","r"))
@test_throws ObjectFile.MagicMismatch readmeta(open("./macfat/libfoo.dylib","r"))
end

# Run COFF tests
test_libfoo_and_fooifier("./win32/fooifier.exe", "./win32/libfoo.dll")
test_libfoo_and_fooifier("./win64/fooifier.exe", "./win64/libfoo.dll")

0 comments on commit 2b58bd3

Please sign in to comment.