Skip to content

Commit

Permalink
feat: add OpenReader API
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Oct 16, 2024
1 parent ba08dad commit 5e712d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 8 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ func Open(filePath string) (*GoFile, error) {
if err != nil {
return nil, err
}

_, err = f.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}

return OpenReader(f)
}

// OpenReader opens a reader and returns a handler to the file.
func OpenReader(f io.ReaderAt) (*GoFile, error) {
buf := make([]byte, maxMagicBufLen)
n, err := f.Read(buf)
_ = f.Close()
n, err := f.ReadAt(buf, 0)
if err != nil {
return nil, err
}
Expand All @@ -79,11 +82,11 @@ func Open(filePath string) (*GoFile, error) {
}
gofile.fh = pe
} else if fileMagicMatch(buf, machoMagic1) || fileMagicMatch(buf, machoMagic2) || fileMagicMatch(buf, machoMagic3) || fileMagicMatch(buf, machoMagic4) {
macho, err := openMachO(f)
machO, err := openMachO(f)
if err != nil {
return nil, err
}
gofile.fh = macho
gofile.fh = machO
} else {
return nil, ErrUnsupportedFile
}
Expand Down
5 changes: 2 additions & 3 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ func TestGoldFiles(t *testing.T) {
require.NotNil(version, "Version should not be nil")
assert.Equal("go"+actualVersion, version.Name, "Incorrect version for "+file)

osFile := f.GetFile()
require.NotNil(osFile, "File should not be nil")
assert.IsType(&os.File{}, osFile, "File should be of type *os.File")
reader := f.GetReader()
require.NotNil(reader, "File should not be nil")

switch f.GetParsedFile().(type) {
case *elf.File:
Expand Down

0 comments on commit 5e712d2

Please sign in to comment.