Skip to content

Commit 2b58bd3

Browse files
authored
Merge pull request #10 from staticfloat/sf/do_block
Add do-block form of `readmeta()`
2 parents c69f493 + d3e622f commit 2b58bd3

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/Abstract/ObjectHandle.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,31 @@ function readmeta(io::IO)
135135
Object file is not any of $(join(ObjTypes, ", "))!
136136
To force one object file format use readmeta(io, T).
137137
""")
138-
error(replace(msg, "\n" => " "))
138+
throw(MagicMismatch(replace(msg, "\n" => " ")))
139139
end
140140

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

146+
"""
147+
readmeta(f::Function, file::AbstractString)
148+
149+
Do-block variant of `readmeta()`. Use via something like:
150+
151+
readmeta("libfoo.so") do f
152+
...
153+
end
154+
"""
155+
function readmeta(f::Function, file::AbstractString)
156+
io = open(file, "r")
157+
try
158+
return f(readmeta(io))
159+
finally
160+
close(io)
161+
end
162+
end
152163

153164

154165
## IOStream-like operations

src/MachO/MachOHandle.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function readmeta(io::IO,::Type{MachOHandle})
2323
header_type = macho_header_type(magic)
2424
endianness = macho_endianness(magic)
2525

26+
# If it's fat, just throw MagicMismatch
27+
if header_type <: MachOFatHeader
28+
throw(MagicMismatch("FAT header"))
29+
end
30+
2631
# Unpack the header
2732
header = unpack(io, header_type, endianness)
2833
return MachOHandle(io, start, header, path(io))

test/runtests.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ using Compat, Compat.Test
33

44
function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
55
# Actually read it in
6-
oh_exe = readmeta(fooifier_path)
7-
oh_lib = readmeta(libfoo_path)
6+
oh_exe = readmeta(open(fooifier_path, "r"))
7+
oh_lib = readmeta(open(libfoo_path, "r"))
88

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

156+
# Ensure that fat Mach-O files don't look like anything to us
157+
@testset "macfat" begin
158+
@test_throws ObjectFile.MagicMismatch readmeta(open("./macfat/fooifier","r"))
159+
@test_throws ObjectFile.MagicMismatch readmeta(open("./macfat/libfoo.dylib","r"))
160+
end
161+
156162
# Run COFF tests
157163
test_libfoo_and_fooifier("./win32/fooifier.exe", "./win32/libfoo.dll")
158164
test_libfoo_and_fooifier("./win64/fooifier.exe", "./win64/libfoo.dll")

0 commit comments

Comments
 (0)