Working on how to actually write the archive.

Made SuperblockFlags public so you can set options when writing
This commit is contained in:
Caleb Gardner
2021-01-16 15:42:55 -06:00
parent 4187598783
commit 69f56d6951
5 changed files with 116 additions and 40 deletions
+15
View File
@@ -0,0 +1,15 @@
package squashfs
import (
"io"
)
type bufferedWriter struct {
w io.Writer
buffer []bufferedBytes
}
type bufferedBytes struct {
data []byte
offset int
}
+3 -3
View File
@@ -12,7 +12,7 @@ import (
) )
const ( const (
magic = 0x73717368 magic uint32 = 0x73717368
) )
var ( var (
@@ -34,7 +34,7 @@ type Reader struct {
fragOffsets []uint64 fragOffsets []uint64
idTable []uint32 idTable []uint32
super superblock super superblock
flags superblockFlags flags SuperblockFlags
} }
//NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt //NewSquashfsReader returns a new squashfs.Reader from an io.ReaderAt
@@ -53,7 +53,7 @@ func NewSquashfsReader(r io.ReaderAt) (*Reader, error) {
// return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt") // return nil, errors.New("BlockSize and BlockLog doesn't match. The archive is probably corrupt")
// } // }
rdr.flags = rdr.super.GetFlags() rdr.flags = rdr.super.GetFlags()
if rdr.flags.CompressorOptions { if rdr.flags.compressorOptions {
switch rdr.super.CompressionType { switch rdr.super.CompressionType {
case GzipCompression: case GzipCompression:
var gzip *compression.Gzip var gzip *compression.Gzip
+69 -17
View File
@@ -33,28 +33,38 @@ type superblock struct {
ExportTableStart uint64 ExportTableStart uint64
} }
//SuperblockFlags is the parsed version of Superblock.Flags //SuperblockFlags is the series of flags describing how a squashfs archive is packed.
type superblockFlags struct { type SuperblockFlags struct {
UncompressedInodes bool //If true, inodes are stored uncompressed
UncompressedData bool UncompressedInodes bool
Check bool //If true, data is stored uncompressed
UncompressedData bool
check bool
//If true, fragments are stored uncompressed
UncompressedFragments bool UncompressedFragments bool
NoFragments bool //If true, ALL data is stored in sequential data blocks instead of utilizing fragments
AlwaysFragments bool NoFragments bool
Duplicates bool //If true, the last block of data will always be stored as a fragment if it's less then the block size.
Exportable bool AlwaysFragments bool
UncompressedXattr bool //If true, duplicate files are only stored once.
NoXattr bool Duplicates bool
CompressorOptions bool //If true, the export table is populated
UncompressedIDs bool Exportable bool
//If true, the xattr table is uncompressed
UncompressedXattr bool
//If true, the xattr table is not populated
NoXattr bool
compressorOptions bool
//If true, the UID/GID table is stored uncompressed
UncompressedIDs bool
} }
//GetFlags returns a SuperblockFlags for a given superblock. //GetFlags returns a SuperblockFlags for a given superblock.
func (s *superblock) GetFlags() superblockFlags { func (s *superblock) GetFlags() SuperblockFlags {
return superblockFlags{ return SuperblockFlags{
UncompressedInodes: s.Flags&0x1 == 0x1, UncompressedInodes: s.Flags&0x1 == 0x1,
UncompressedData: s.Flags&0x2 == 0x2, UncompressedData: s.Flags&0x2 == 0x2,
Check: s.Flags&0x4 == 0x4, check: s.Flags&0x4 == 0x4,
UncompressedFragments: s.Flags&0x8 == 0x8, UncompressedFragments: s.Flags&0x8 == 0x8,
NoFragments: s.Flags&0x10 == 0x10, NoFragments: s.Flags&0x10 == 0x10,
AlwaysFragments: s.Flags&0x20 == 0x20, AlwaysFragments: s.Flags&0x20 == 0x20,
@@ -62,7 +72,49 @@ func (s *superblock) GetFlags() superblockFlags {
Exportable: s.Flags&0x80 == 0x80, Exportable: s.Flags&0x80 == 0x80,
UncompressedXattr: s.Flags&0x100 == 0x100, UncompressedXattr: s.Flags&0x100 == 0x100,
NoXattr: s.Flags&0x200 == 0x200, NoXattr: s.Flags&0x200 == 0x200,
CompressorOptions: s.Flags&0x400 == 0x400, compressorOptions: s.Flags&0x400 == 0x400,
UncompressedIDs: s.Flags&0x800 == 0x800, UncompressedIDs: s.Flags&0x800 == 0x800,
} }
} }
//ToUint returns the uint16 representation of the given SuperblockFlags
func (s *SuperblockFlags) ToUint() uint16 {
var out uint16
if s.UncompressedInodes {
out = out | 0x1
}
if s.UncompressedData {
out = out | 0x2
}
if s.check {
out = out | 0x4
}
if s.UncompressedFragments {
out = out | 0x8
}
if s.NoFragments {
out = out | 0x10
}
if s.AlwaysFragments {
out = out | 0x20
}
if s.Duplicates {
out = out | 0x40
}
if s.Exportable {
out = out | 0x80
}
if s.UncompressedXattr {
out = out | 0x100
}
if s.NoXattr {
out = out | 0x200
}
if s.compressorOptions {
out = out | 0x400
}
if s.UncompressedIDs {
out = out | 0x800
}
return out
}
+17 -20
View File
@@ -13,18 +13,6 @@ import (
"github.com/CalebQ42/squashfs/internal/compression" "github.com/CalebQ42/squashfs/internal/compression"
) )
type fileHolder struct {
reader io.Reader
path string
name string
symLocation string
UID int
GUID int
perm int
folder bool
symlink bool
}
//Writer is used to creaste squashfs archives. Currently unusable //Writer is used to creaste squashfs archives. Currently unusable
//TODO: Make usable //TODO: Make usable
type Writer struct { type Writer struct {
@@ -33,8 +21,10 @@ type Writer struct {
symlinkTable map[string]string //[oldpath]newpath symlinkTable map[string]string //[oldpath]newpath
uidGUIDTable []int uidGUIDTable []int
compressionType int compressionType int
Flags superblockFlags //Flags are the SuperblockFlags used when writing the archive.
allowErrors bool //Currently Duplicates, Exportable, UncompressedXattr, NoXattr values are ignored
Flags SuperblockFlags
allowErrors bool
} }
//NewWriter creates a new with the default options (Gzip compression and allow errors) //NewWriter creates a new with the default options (Gzip compression and allow errors)
@@ -62,6 +52,19 @@ func NewWriterWithOptions(compressionType int, allowErrors bool) (*Writer, error
}, nil }, nil
} }
//fileHolder holds the necessary information about a given file inside of a squashfs
type fileHolder struct {
reader io.Reader
path string
name string
symLocation string
UID int
GUID int
perm int
folder bool
symlink bool
}
//AddFile attempts to add an os.File to the archive at it's root. //AddFile attempts to add an os.File to the archive at it's root.
func (w *Writer) AddFile(file *os.File) error { func (w *Writer) AddFile(file *os.File) error {
return w.AddFileToFolder("/", file) return w.AddFileToFolder("/", file)
@@ -318,9 +321,3 @@ func (w *Writer) WriteToFilename(filepath string) error {
_, err = w.WriteTo(newFil) _, err = w.WriteTo(newFil)
return err return err
} }
//WriteTo attempts to write the archive to the given io.Writer.
func (w *Writer) WriteTo(write io.Writer) (int64, error) {
//TODO
return 0, errors.New("I SAID DON'T")
}
+12
View File
@@ -0,0 +1,12 @@
package squashfs
import (
"errors"
"io"
)
//WriteTo attempts to write the archive to the given io.Writer.
func (w *Writer) WriteTo(write io.Writer) (int64, error) {
//TODO
return 0, errors.New("I SAID DON'T")
}