Skip to content

Commit

Permalink
Fix frag, id, inode table values on block boundries
Browse files Browse the repository at this point in the history
Fixes bug mention in #30
  • Loading branch information
CalebQ42 committed Nov 26, 2024
1 parent 0f8a4e0 commit 03266d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
8 changes: 5 additions & 3 deletions low/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package squashfslow
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
29 changes: 21 additions & 8 deletions low/reader_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package squashfslow_test
package squashfslow

import (
"fmt"
Expand All @@ -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) {
Expand Down Expand Up @@ -50,14 +48,29 @@ 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)
if err != nil {
t.Fatal(err)
}
defer fil.Close()
rdr, err := squashfslow.NewReader(fil)
rdr, err := NewReader(fil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 03266d0

Please sign in to comment.