From 5647659f27f8aa18bc10ca4742f8856162325d5c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 6 Jul 2022 01:48:48 -0600 Subject: file reading! --- pkg/btrfs/btrfsitem/item_fileextent.go | 52 ++++++++++++++-------------------- pkg/btrfsmisc/print_tree.go | 16 +++++------ 2 files changed, 29 insertions(+), 39 deletions(-) (limited to 'pkg') diff --git a/pkg/btrfs/btrfsitem/item_fileextent.go b/pkg/btrfs/btrfsitem/item_fileextent.go index f3cbffe..f480207 100644 --- a/pkg/btrfs/btrfsitem/item_fileextent.go +++ b/pkg/btrfs/btrfsitem/item_fileextent.go @@ -24,26 +24,17 @@ type FileExtent struct { // EXTENT_DATA=108 binstruct.End `bin:"off=0x15"` // only one of these, depending on .Type - BodyInline []byte `bin:"-"` - BodyReg struct { - // Position of extent within the device + BodyInline []byte `bin:"-"` // .Type == FILE_EXTENT_INLINE + BodyExtent struct { // .Type == FILE_EXTENT_REG or FILE_EXTENT_PREALLOC + // Position and size of extent within the device DiskByteNr btrfsvol.LogicalAddr `bin:"off=0x0, siz=0x8"` DiskNumBytes btrfsvol.AddrDelta `bin:"off=0x8, siz=0x8"` // Position of data within the extent - Offset btrfsvol.AddrDelta `bin:"off=0x10, siz=0x8"` - NumBytes btrfsvol.AddrDelta `bin:"off=0x18, siz=0x8"` + Offset btrfsvol.AddrDelta `bin:"off=0x10, siz=0x8"` - binstruct.End `bin:"off=0x20"` - } `bin:"-"` - BodyPrealloc struct { - // Position of extent within the device - DiskByteNr btrfsvol.LogicalAddr `bin:"off=0x0, siz=0x8"` - DiskNumBytes btrfsvol.AddrDelta `bin:"off=0x8, siz=0x8"` - - // Position of data within the extent - Offset btrfsvol.AddrDelta `bin:"off=0x10, siz=0x8"` - NumBytes btrfsvol.AddrDelta `bin:"off=0x18, siz=0x8"` + // Decompressed/unencrypted size + NumBytes int64 `bin:"off=0x18, siz=0x8"` binstruct.End `bin:"off=0x20"` } `bin:"-"` @@ -58,14 +49,8 @@ func (o *FileExtent) UnmarshalBinary(dat []byte) (int, error) { case FILE_EXTENT_INLINE: o.BodyInline = dat[n:] n += len(o.BodyInline) - case FILE_EXTENT_REG: - _n, err := binstruct.Unmarshal(dat[n:], &o.BodyReg) - n += _n - if err != nil { - return n, err - } - case FILE_EXTENT_PREALLOC: - _n, err := binstruct.Unmarshal(dat[n:], &o.BodyPrealloc) + case FILE_EXTENT_REG, FILE_EXTENT_PREALLOC: + _n, err := binstruct.Unmarshal(dat[n:], &o.BodyExtent) n += _n if err != nil { return n, err @@ -84,14 +69,8 @@ func (o FileExtent) MarshalBinary() ([]byte, error) { switch o.Type { case FILE_EXTENT_INLINE: dat = append(dat, o.BodyInline...) - case FILE_EXTENT_REG: - bs, err := binstruct.Marshal(o.BodyReg) - dat = append(dat, bs...) - if err != nil { - return dat, err - } - case FILE_EXTENT_PREALLOC: - bs, err := binstruct.Marshal(o.BodyPrealloc) + case FILE_EXTENT_REG, FILE_EXTENT_PREALLOC: + bs, err := binstruct.Marshal(o.BodyExtent) dat = append(dat, bs...) if err != nil { return dat, err @@ -110,6 +89,17 @@ const ( FILE_EXTENT_PREALLOC ) +func (o FileExtent) Size() (int64, error) { + switch o.Type { + case FILE_EXTENT_INLINE: + return int64(len(o.BodyInline)), nil + case FILE_EXTENT_REG, FILE_EXTENT_PREALLOC: + return o.BodyExtent.NumBytes, nil + default: + return 0, fmt.Errorf("unknown file extent type %v", o.Type) + } +} + func (fet FileExtentType) String() string { names := map[FileExtentType]string{ FILE_EXTENT_INLINE: "inline", diff --git a/pkg/btrfsmisc/print_tree.go b/pkg/btrfsmisc/print_tree.go index 6b4ff96..61c5423 100644 --- a/pkg/btrfsmisc/print_tree.go +++ b/pkg/btrfsmisc/print_tree.go @@ -156,18 +156,18 @@ func PrintTree(fs *btrfs.FS, root btrfsvol.LogicalAddr) error { len(body.BodyInline), body.RAMBytes, body.Compression) case btrfsitem.FILE_EXTENT_PREALLOC: fmt.Printf("\t\tprealloc data disk byte %v nr %v\n", - body.BodyPrealloc.DiskByteNr, - body.BodyPrealloc.DiskNumBytes) + body.BodyExtent.DiskByteNr, + body.BodyExtent.DiskNumBytes) fmt.Printf("\t\tprealloc data offset %v nr %v\n", - body.BodyPrealloc.Offset, - body.BodyPrealloc.NumBytes) + body.BodyExtent.Offset, + body.BodyExtent.NumBytes) case btrfsitem.FILE_EXTENT_REG: fmt.Printf("\t\textent data disk byte %d nr %d\n", - body.BodyReg.DiskByteNr, - body.BodyReg.DiskNumBytes) + body.BodyExtent.DiskByteNr, + body.BodyExtent.DiskNumBytes) fmt.Printf("\t\textent data offset %d nr %d ram %v\n", - body.BodyReg.Offset, - body.BodyReg.NumBytes, + body.BodyExtent.Offset, + body.BodyExtent.NumBytes, body.RAMBytes) fmt.Printf("\t\textent compression %v\n", body.Compression) -- cgit v1.2.3-2-g168b