packed: add ability to load from memory, add MapAndLoad
The ability to load a packfile from memory means that it becomes easy to use the "embed" package to embed a small packfile. The refactor to allow loading from memory also makes it easy to add a MapAndLoad implementation, which means the file only needs to be opened / mmap()ed once.
This commit is contained in:
parent
5412b97411
commit
cd9c8b600e
4 changed files with 154 additions and 33 deletions
39
packed/load_test.go
Normal file
39
packed/load_test.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package packed
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMapAndLoad(t *testing.T) {
|
||||
rawPack, err := os.ReadFile("testdata/helloworld.pack")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rawText, err := os.ReadFile("testdata/helloworld.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mapped, err := MapAndLoad("testdata/helloworld.pack")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer mapped.Close()
|
||||
|
||||
if !bytes.Equal(mapped.Data, rawPack) {
|
||||
t.Error("mapped data does not match raw file")
|
||||
}
|
||||
|
||||
file := mapped.Directory.Files["/helloworld.txt"]
|
||||
if file == nil {
|
||||
t.Fatal("directory does not contain /helloworld.txt")
|
||||
}
|
||||
|
||||
start := file.Uncompressed.Offset
|
||||
end := start + file.Uncompressed.Length
|
||||
if !bytes.Equal(rawText, mapped.Data[start:end]) {
|
||||
t.Error("uncompressed file does not match testdata/helloworld.txt")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue