Fix frag, id, inode table values on block boundries

Fixes bug mention in #30
This commit is contained in:
Caleb Gardner
2024-11-26 17:09:39 -06:00
parent 0f8a4e0027
commit 03266d0560
2 changed files with 26 additions and 11 deletions
+5 -3
View File
@@ -3,6 +3,7 @@ package squashfslow
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
@@ -88,7 +89,7 @@ func (r *Reader) Id(i uint16) (uint32, error) {
// Populate the id table as needed
var blockNum uint32
if i != 0 { // If i == 0, we go negatives causing issues with uint32s
blockNum = uint32(math.Ceil(float64(i)/2048)) - 1
blockNum = uint32(math.Ceil(float64(i+1)/2048)) - 1
} else {
blockNum = 0
}
@@ -131,10 +132,11 @@ func (r *Reader) fragEntry(i uint32) (fragEntry, error) {
// Populate the fragment table as needed
var blockNum uint32
if i != 0 { // If i == 0, we go negatives causing issues with uint32s
blockNum = uint32(math.Ceil(float64(i)/512)) - 1
blockNum = uint32(math.Ceil(float64(i+1)/512)) - 1
} else {
blockNum = 0
}
fmt.Println(blockNum)
blocksRead := len(r.fragTable) / 512
blocksToRead := int(blockNum) - blocksRead + 1
@@ -177,7 +179,7 @@ func (r *Reader) inodeRef(i uint32) (uint64, error) {
// Populate the export table as needed
var blockNum uint32
if i != 0 { // If i == 0, we go negatives causing issues with uint32s
blockNum = uint32(math.Ceil(float64(i)/1024)) - 1
blockNum = uint32(math.Ceil(float64(i+1)/1024)) - 1
} else {
blockNum = 0
}
+21 -8
View File
@@ -1,4 +1,4 @@
package squashfslow_test
package squashfslow
import (
"fmt"
@@ -8,13 +8,11 @@ import (
"os/exec"
"path/filepath"
"testing"
squashfslow "github.com/CalebQ42/squashfs/low"
)
const (
squashfsURL = "https://darkstorm.tech/files/LinuxPATest.sfs"
squashfsName = "LinuxPATest.sfs"
squashfsName = "airootfs.sfs"
)
func preTest(dir string) (fil *os.File, err error) {
@@ -50,6 +48,21 @@ func preTest(dir string) (fil *os.File, err error) {
return
}
func TestMisc(t *testing.T) {
tmpDir := "../testing"
fil, err := preTest(tmpDir)
if err != nil {
t.Fatal(err)
}
defer fil.Close()
rdr, err := NewReader(fil)
if err != nil {
t.Fatal(err)
}
t.Log(rdr.Superblock.FragCount)
t.Fatal(rdr.fragEntry(1233))
}
func TestReader(t *testing.T) {
tmpDir := "../testing"
fil, err := preTest(tmpDir)
@@ -57,7 +70,7 @@ func TestReader(t *testing.T) {
t.Fatal(err)
}
defer fil.Close()
rdr, err := squashfslow.NewReader(fil)
rdr, err := NewReader(fil)
if err != nil {
t.Fatal(err)
}
@@ -77,7 +90,7 @@ func TestSingleFile(t *testing.T) {
t.Fatal(err)
}
defer fil.Close()
rdr, err := squashfslow.NewReader(fil)
rdr, err := NewReader(fil)
if err != nil {
t.Fatal(err)
}
@@ -92,7 +105,7 @@ func TestSingleFile(t *testing.T) {
t.Fatal(err)
}
func extractToDir(rdr *squashfslow.Reader, b *squashfslow.FileBase, folder string) error {
func extractToDir(rdr *Reader, b *FileBase, folder string) error {
path := filepath.Join(folder, b.Name)
if b.IsDir() {
d, err := b.ToDir(rdr)
@@ -103,7 +116,7 @@ func extractToDir(rdr *squashfslow.Reader, b *squashfslow.FileBase, folder strin
if err != nil {
return err
}
var nestBast squashfslow.FileBase
var nestBast FileBase
for _, e := range d.Entries {
nestBast, err = rdr.BaseFromEntry(e)
if err != nil {