From dd08d3516d6a69d68adf59edf1c2a6920f9a7d1c Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Wed, 20 Jan 2021 14:10:14 -0600 Subject: [PATCH] Reader will now check BlockLog --- file.go | 8 +------- reader.go | 10 ++++++---- reader_test.go | 11 +++-------- writer.go | 7 ++++--- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/file.go b/file.go index c52d9fa..6b391d2 100644 --- a/file.go +++ b/file.go @@ -105,6 +105,7 @@ func (f *File) Sys() interface{} { // } //Close does nothing. It's simply here to satisfy fs.File +//TODO: add actual implementation // func (f *File) Close() error { // return nil // } @@ -293,13 +294,6 @@ func (f *File) Mode() os.FileMode { return mode } -//TODO: Implement with 1.16 - -//Type returns the type bits from fs.FileMode -// func (f *File) Type() fs.FileInfo { -// return Mod() ^&fs.ModePerm -// } - //ExtractTo extracts the file to the given path. This is the same as ExtractWithOptions(path, false, false, os.ModePerm, false). //Will NOT try to keep symlinks valid, folders extracted will have the permissions set by the squashfs, but the folder to make path will have full permissions (777). // diff --git a/reader.go b/reader.go index 120516b..81d41c9 100644 --- a/reader.go +++ b/reader.go @@ -26,6 +26,8 @@ var ( ErrOptions = errors.New("Possibly incompatible compressor options") ) +//TODO: implement fs.FS, possibly more FS types for compatibility. Most of this work will mostly be handed off to root anyway so this shouldn't be too difficult. + //Reader processes and reads a squashfs archive. type Reader struct { r io.ReaderAt @@ -39,7 +41,6 @@ type Reader struct { //NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt func NewSquashfsReader(r io.ReaderAt) (*Reader, error) { - hasUnsupportedOptions := false var rdr Reader rdr.r = r err := binary.Read(io.NewSectionReader(rdr.r, 0, int64(binary.Size(rdr.super))), binary.LittleEndian, &rdr.super) @@ -49,9 +50,10 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) { if rdr.super.Magic != magic { return nil, errNoMagic } - // if rdr.super.BlockLog == uint16(math.Log2(float64(rdr.super.BlockSize))) { - // return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt") - // } + if rdr.super.BlockLog != uint16(math.Log2(float64(rdr.super.BlockSize))) { + return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt") + } + hasUnsupportedOptions := false rdr.flags = rdr.super.GetFlags() if rdr.flags.compressorOptions { switch rdr.super.CompressionType { diff --git a/reader_test.go b/reader_test.go index 30aacdb..8480e0a 100644 --- a/reader_test.go +++ b/reader_test.go @@ -3,6 +3,7 @@ package squashfs import ( "fmt" "io" + "math" "net/http" "os" "os/exec" @@ -69,19 +70,13 @@ func TestAppImage(t *testing.T) { } defer aiFil.Close() stat, _ := aiFil.Stat() - os.RemoveAll(wd + "/testing/firefox") ai := goappimage.NewAppImage(wd + "/testing/" + appImageName) rdr, err := NewSquashfsReader(io.NewSectionReader(aiFil, ai.Offset, stat.Size()-ai.Offset)) if err != nil { t.Fatal(err) } - desktop := rdr.GetFileAtPath("*.desktop") - if desktop == nil { - t.Fatal("Problema!") - } - os.Remove(wd + "/testing/cura.desktop") - deskFil, _ := os.Create(wd + "/testing/cura.desktop") - io.Copy(deskFil, desktop.Sys().(io.Reader)) + fmt.Println(rdr.super.BlockLog, strconv.FormatInt(int64(rdr.super.BlockSize), 2)) + fmt.Println(math.Log2(float64(rdr.super.BlockSize))) t.Fatal("No problemo!") } diff --git a/writer.go b/writer.go index ff00b70..6521210 100644 --- a/writer.go +++ b/writer.go @@ -13,7 +13,7 @@ import ( "github.com/CalebQ42/squashfs/internal/compression" ) -//Writer is used to creaste squashfs archives. Currently unusable +//Writer is used to creaste squashfs archives. Currently unusable. //TODO: Make usable type Writer struct { compressor compression.Compressor @@ -21,8 +21,9 @@ type Writer struct { symlinkTable map[string]string //[oldpath]newpath uidGUIDTable []int compressionType int - //BlockSize is how large the data blocks are. Can be between 4096 (4KB) and 1048576 (1 MB). Default is 1048576. - //If BlockSize is not inside that range, it will be set to within the range before writing + //BlockSize is how large the data blocks are. Can be between 4096 (4KB) and 1048576 (1 MB). + //If BlockSize is not inside that range, it will be set to within the range before writing. + //Default is 1048576. BlockSize uint32 //Flags are the SuperblockFlags used when writing the archive. //Currently Duplicates, Exportable, UncompressedXattr, NoXattr values are ignored