From 18092c63aac7c0b7449fb503acf0caa5da4b01c6 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sat, 9 Jan 2021 09:53:58 -0600 Subject: [PATCH] Some cleanup, no change in functionality --- datareader.go | 14 +-- file.go | 34 +++---- filereader.go | 16 ++-- fragment.go | 22 ++--- internal/directory/directory.go | 14 +-- internal/inode/inodetypes.go | 165 ++++++++++++++++---------------- internal/inode/process.go | 42 ++++---- 7 files changed, 154 insertions(+), 153 deletions(-) diff --git a/datareader.go b/datareader.go index 7475501..5d61fbe 100644 --- a/datareader.go +++ b/datareader.go @@ -63,12 +63,12 @@ func (r *Reader) newDataReaderFromInode(i *inode.Inode) (*dataReader, error) { var rdr dataReader rdr.r = r switch i.Type { - case inode.BasicFileType: - fil := i.Info.(inode.BasicFile) - if fil.Init.BlockStart == 0 { + case inode.FileType: + fil := i.Info.(inode.File) + if fil.BlockStart == 0 { return nil, errInodeOnlyFragment } - rdr.offset = int64(fil.Init.BlockStart) + rdr.offset = int64(fil.BlockStart) for _, sizes := range fil.BlockSizes { rdr.blocks = append(rdr.blocks, newDataBlock(sizes)) } @@ -76,11 +76,11 @@ func (r *Reader) newDataReaderFromInode(i *inode.Inode) (*dataReader, error) { rdr.blocks = rdr.blocks[:len(rdr.blocks)-1] } case inode.ExtFileType: - fil := i.Info.(inode.ExtendedFile) - if fil.Init.BlockStart == 0 { + fil := i.Info.(inode.ExtFile) + if fil.BlockStart == 0 { return nil, errInodeOnlyFragment } - rdr.offset = int64(fil.Init.BlockStart) + rdr.offset = int64(fil.BlockStart) for _, sizes := range fil.BlockSizes { rdr.blocks = append(rdr.blocks, newDataBlock(sizes)) } diff --git a/file.go b/file.go index 56aaf0f..706c0be 100644 --- a/file.go +++ b/file.go @@ -61,10 +61,10 @@ func (f *File) Name() string { //Size is the complete size of the file. Zero if it's not a file. func (f *File) Size() int64 { switch f.filType { - case inode.BasicFileType: - return int64(f.in.Info.(inode.BasicFile).Init.Size) + case inode.FileType: + return int64(f.in.Info.(inode.File).Size) case inode.ExtFileType: - return int64(f.in.Info.(inode.ExtendedFile).Init.Size) + return int64(f.in.Info.(inode.ExtFile).Size) default: return 0 } @@ -204,27 +204,27 @@ func (f *File) GetFileAtPath(dirPath string) *File { //IsDir returns if the file is a directory. func (f *File) IsDir() bool { - return f.filType == inode.BasicDirectoryType || f.filType == inode.ExtDirType + return f.filType == inode.DirType || f.filType == inode.ExtDirType } //IsSymlink returns if the file is a symlink. func (f *File) IsSymlink() bool { - return f.filType == inode.BasicSymlinkType || f.filType == inode.ExtSymlinkType + return f.filType == inode.SymType || f.filType == inode.ExtSymlinkType } //IsFile returns if the file is a file. func (f *File) IsFile() bool { - return f.filType == inode.BasicFileType || f.filType == inode.ExtFileType + return f.filType == inode.FileType || f.filType == inode.ExtFileType } //SymlinkPath returns the path the symlink is pointing to. If the file ISN'T a symlink, will return an empty string. //If a path begins with "/" then the symlink is pointing to an absolute path (starting from root, and not a file inside the archive) func (f *File) SymlinkPath() string { switch f.filType { - case inode.BasicSymlinkType: - return f.in.Info.(inode.BasicSymlink).Path + case inode.SymType: + return f.in.Info.(inode.Sym).Path case inode.ExtSymlinkType: - return f.in.Info.(inode.ExtendedSymlink).Path + return f.in.Info.(inode.Sym).Path default: return "" } @@ -500,14 +500,14 @@ func (r *Reader) readDirFromInode(i *inode.Inode) (*directory.Directory, error) var metaOffset uint16 var size uint32 switch i.Type { - case inode.BasicDirectoryType: - offset = i.Info.(inode.BasicDirectory).DirectoryIndex - metaOffset = i.Info.(inode.BasicDirectory).DirectoryOffset - size = uint32(i.Info.(inode.BasicDirectory).DirectorySize) + case inode.DirType: + offset = i.Info.(inode.Dir).DirectoryIndex + metaOffset = i.Info.(inode.Dir).DirectoryOffset + size = uint32(i.Info.(inode.Dir).DirectorySize) case inode.ExtDirType: - offset = i.Info.(inode.ExtendedDirectory).Init.DirectoryIndex - metaOffset = i.Info.(inode.ExtendedDirectory).Init.DirectoryOffset - size = i.Info.(inode.ExtendedDirectory).Init.DirectorySize + offset = i.Info.(inode.ExtDir).DirectoryIndex + metaOffset = i.Info.(inode.ExtDir).DirectoryOffset + size = i.Info.(inode.ExtDir).DirectorySize default: return nil, errors.New("Not a directory inode") } @@ -532,7 +532,7 @@ func (r *Reader) getInodeFromEntry(en *directory.Entry) (*inode.Inode, error) { if err != nil { return nil, err } - _, err = br.Seek(int64(en.Init.Offset), io.SeekStart) + _, err = br.Seek(int64(en.Offset), io.SeekStart) if err != nil { return nil, err } diff --git a/filereader.go b/filereader.go index 22eb0a2..d9bf52e 100644 --- a/filereader.go +++ b/filereader.go @@ -29,20 +29,20 @@ var ( func (r *Reader) newFileReader(in *inode.Inode) (*fileReader, error) { var rdr fileReader rdr.in = in - if in.Type != inode.BasicFileType && in.Type != inode.ExtFileType { + if in.Type != inode.FileType && in.Type != inode.ExtFileType { return nil, errPathIsNotFile } switch in.Type { - case inode.BasicFileType: - fil := in.Info.(inode.BasicFile) + case inode.FileType: + fil := in.Info.(inode.File) rdr.fragged = fil.Fragmented - rdr.fragOnly = fil.Init.BlockStart == 0 - rdr.FileSize = int(fil.Init.Size) + rdr.fragOnly = fil.BlockStart == 0 + rdr.FileSize = int(fil.Size) case inode.ExtFileType: - fil := in.Info.(inode.ExtendedFile) + fil := in.Info.(inode.ExtFile) rdr.fragged = fil.Fragmented - rdr.fragOnly = fil.Init.BlockStart == 0 - rdr.FileSize = int(fil.Init.Size) + rdr.fragOnly = fil.BlockStart == 0 + rdr.FileSize = int(fil.Size) } var err error if rdr.fragged { diff --git a/fragment.go b/fragment.go index 281eb47..e1aac5d 100644 --- a/fragment.go +++ b/fragment.go @@ -21,30 +21,30 @@ func (r *Reader) getFragmentDataFromInode(in *inode.Inode) ([]byte, error) { var size uint32 var fragIndex uint32 var fragOffset uint32 - if in.Type == inode.BasicFileType { - bf := in.Info.(inode.BasicFile) + if in.Type == inode.FileType { + bf := in.Info.(inode.File) if !bf.Fragmented { return make([]byte, 0), nil } - if bf.Init.BlockStart == 0 { - size = bf.Init.Size + if bf.BlockStart == 0 { + size = bf.Size } else { size = bf.BlockSizes[len(bf.BlockSizes)-1] } - fragIndex = bf.Init.FragmentIndex - fragOffset = bf.Init.FragmentOffset + fragIndex = bf.FragmentIndex + fragOffset = bf.FragmentOffset } else if in.Type == inode.ExtFileType { - bf := in.Info.(inode.ExtendedFile) + bf := in.Info.(inode.ExtFile) if !bf.Fragmented { return make([]byte, 0), nil } - if bf.Init.BlockStart == 0 { - size = bf.Init.Size + if bf.BlockStart == 0 { + size = bf.Size } else { size = bf.BlockSizes[len(bf.BlockSizes)-1] } - fragIndex = bf.Init.FragmentIndex - fragOffset = bf.Init.FragmentOffset + fragIndex = bf.FragmentIndex + fragOffset = bf.FragmentOffset } else { return nil, errors.New("Inode type not supported") } diff --git a/internal/directory/directory.go b/internal/directory/directory.go index 3ff477e..44da60d 100644 --- a/internal/directory/directory.go +++ b/internal/directory/directory.go @@ -13,8 +13,8 @@ type Header struct { InodeNumber uint32 } -//EntryInit is the values that can be easily decoded -type EntryInit struct { +//EntryRaw is the values that can be easily decoded +type EntryRaw struct { Offset uint16 InodeOffset int16 Type uint16 @@ -23,19 +23,19 @@ type EntryInit struct { //Entry is an entry in a directory. type Entry struct { - Init EntryInit - Name string - Header *Header + *Header + Name string + EntryRaw } //NewEntry creates a new directory entry func NewEntry(rdr io.Reader) (Entry, error) { var entry Entry - err := binary.Read(rdr, binary.LittleEndian, &entry.Init) + err := binary.Read(rdr, binary.LittleEndian, &entry.EntryRaw) if err != nil { return Entry{}, err } - tmp := make([]byte, entry.Init.NameSize+1) + tmp := make([]byte, entry.EntryRaw.NameSize+1) err = binary.Read(rdr, binary.LittleEndian, &tmp) if err != nil { return Entry{}, err diff --git a/internal/inode/inodetypes.go b/internal/inode/inodetypes.go index fe679c3..4992f16 100644 --- a/internal/inode/inodetypes.go +++ b/internal/inode/inodetypes.go @@ -5,14 +5,15 @@ import ( "io" ) +//The different types of inodes as defined by inodetype const ( - BasicDirectoryType = iota + 1 - BasicFileType - BasicSymlinkType - BasicBlockDeviceType - BasicCharDeviceType - BasicFifoType - BasicSocketType + DirType = iota + 1 + FileType + SymType + BlockDevType + CharDevType + FifoType + SocketType ExtDirType ExtFileType ExtSymlinkType @@ -32,8 +33,8 @@ type Header struct { Number uint32 } -//BasicDirectory is self explainatory -type BasicDirectory struct { +//Dir is self explainatory +type Dir struct { DirectoryIndex uint32 HardLinks uint32 DirectorySize uint16 @@ -41,8 +42,8 @@ type BasicDirectory struct { ParentInodeNumber uint32 } -//ExtendedDirectoryInit is the information that can be directoy decoded -type ExtendedDirectoryInit struct { +//ExtDirInit is the information that can be directoy decoded +type ExtDirInit struct { HardLinks uint32 DirectorySize uint32 DirectoryIndex uint32 @@ -52,20 +53,20 @@ type ExtendedDirectoryInit struct { XattrIndex uint32 } -//ExtendedDirectory is a directory with extra info -type ExtendedDirectory struct { - Init ExtendedDirectoryInit - Indexes []DirectoryIndex +//ExtDir is a directory with extra info +type ExtDir struct { + Indexes []DirIndex + ExtDirInit } //NewExtendedDirectory creates a new ExtendedDirectory -func NewExtendedDirectory(rdr io.Reader) (ExtendedDirectory, error) { - var inode ExtendedDirectory - err := binary.Read(rdr, binary.LittleEndian, &inode.Init) +func NewExtendedDirectory(rdr io.Reader) (ExtDir, error) { + var inode ExtDir + err := binary.Read(rdr, binary.LittleEndian, &inode.ExtDirInit) if err != nil { return inode, err } - for i := uint16(0); i < inode.Init.IndexCount; i++ { + for i := uint16(0); i < inode.IndexCount; i++ { tmp, err := NewDirectoryIndex(rdr) if err != nil { return inode, err @@ -75,27 +76,27 @@ func NewExtendedDirectory(rdr io.Reader) (ExtendedDirectory, error) { return inode, err } -//DirectoryIndexInit holds the values that can be easily decoded -type DirectoryIndexInit struct { +//DirIndexInit holds the values that can be easily decoded +type DirIndexInit struct { Offset uint32 DirTableOffset uint32 NameSize uint32 } -//DirectoryIndex is a quick lookup provided by an ExtendedDirectory -type DirectoryIndex struct { - Init DirectoryIndexInit +//DirIndex is a quick lookup provided by an ExtendedDirectory +type DirIndex struct { Name string + DirIndexInit } //NewDirectoryIndex return a new DirectoryIndex -func NewDirectoryIndex(rdr io.Reader) (DirectoryIndex, error) { - var index DirectoryIndex - err := binary.Read(rdr, binary.LittleEndian, &index.Init) +func NewDirectoryIndex(rdr io.Reader) (DirIndex, error) { + var index DirIndex + err := binary.Read(rdr, binary.LittleEndian, &index.DirIndexInit) if err != nil { return index, err } - tmp := make([]byte, index.Init.NameSize+1, index.Init.NameSize+1) + tmp := make([]byte, index.NameSize+1, index.NameSize+1) err = binary.Read(rdr, binary.LittleEndian, &tmp) if err != nil { return index, err @@ -104,31 +105,31 @@ func NewDirectoryIndex(rdr io.Reader) (DirectoryIndex, error) { return index, nil } -//BasicFileInit is the information that can be directoy decoded -type BasicFileInit struct { +//FileInit is the information that can be directly decoded +type FileInit struct { BlockStart uint32 FragmentIndex uint32 FragmentOffset uint32 Size uint32 } -//BasicFile is self explainatory -type BasicFile struct { - Init BasicFileInit +//File is self explainatory +type File struct { BlockSizes []uint32 Fragmented bool + FileInit } -//NewBasicFile creates a new BasicFile -func NewBasicFile(rdr io.Reader, blockSize uint32) (BasicFile, error) { - var inode BasicFile - err := binary.Read(rdr, binary.LittleEndian, &inode.Init) +//NewFile creates a new File +func NewFile(rdr io.Reader, blockSize uint32) (File, error) { + var inode File + err := binary.Read(rdr, binary.LittleEndian, &inode.FileInit) if err != nil { return inode, err } - inode.Fragmented = inode.Init.FragmentIndex != 0xFFFFFFFF - blocks := inode.Init.Size / blockSize - if inode.Init.Size%blockSize > 0 { + inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF + blocks := inode.Size / blockSize + if inode.Size%blockSize > 0 { blocks++ } inode.BlockSizes = make([]uint32, blocks, blocks) @@ -136,8 +137,8 @@ func NewBasicFile(rdr io.Reader, blockSize uint32) (BasicFile, error) { return inode, err } -//ExtendedFileInit is the information that can be directly decoded -type ExtendedFileInit struct { +//ExtFileInit is the information that can be directly decoded +type ExtFileInit struct { BlockStart uint32 Size uint32 Sparse uint64 @@ -147,23 +148,23 @@ type ExtendedFileInit struct { XattrIndex uint32 } -//ExtendedFile is a file with more information -type ExtendedFile struct { - Init ExtendedFileInit +//ExtFile is a file with more information +type ExtFile struct { BlockSizes []uint32 Fragmented bool + ExtFileInit } //NewExtendedFile creates a new ExtendedFile -func NewExtendedFile(rdr io.Reader, blockSize uint32) (ExtendedFile, error) { - var inode ExtendedFile - err := binary.Read(rdr, binary.LittleEndian, &inode.Init) +func NewExtendedFile(rdr io.Reader, blockSize uint32) (ExtFile, error) { + var inode ExtFile + err := binary.Read(rdr, binary.LittleEndian, &inode.ExtFileInit) if err != nil { return inode, err } - inode.Fragmented = inode.Init.FragmentIndex != 0xFFFFFFFF - blocks := inode.Init.Size / blockSize - if inode.Init.Size%blockSize > 0 { + inode.Fragmented = inode.FragmentIndex != 0xFFFFFFFF + blocks := inode.Size / blockSize + if inode.Size%blockSize > 0 { blocks++ } inode.BlockSizes = make([]uint32, blocks, blocks) @@ -171,27 +172,27 @@ func NewExtendedFile(rdr io.Reader, blockSize uint32) (ExtendedFile, error) { return inode, err } -//BasicSymlinkInit is all the values that can be directly decoded -type BasicSymlinkInit struct { +//SymInit is all the values that can be directly decoded +type SymInit struct { HardLinks uint32 TargetPathSize uint32 } -//BasicSymlink is a symlink -type BasicSymlink struct { - Init BasicSymlinkInit - targetPath []byte //len is TargetPathSize +//Sym is a symlink +type Sym struct { Path string + targetPath []byte //len is TargetPathSize + SymInit } -//NewBasicSymlink creates a new BasicSymlink -func NewBasicSymlink(rdr io.Reader) (BasicSymlink, error) { - var inode BasicSymlink - err := binary.Read(rdr, binary.LittleEndian, &inode.Init) +//NewSymlink creates a new Symlink +func NewSymlink(rdr io.Reader) (Sym, error) { + var inode Sym + err := binary.Read(rdr, binary.LittleEndian, &inode.SymInit) if err != nil { return inode, err } - inode.targetPath = make([]byte, inode.Init.TargetPathSize, inode.Init.TargetPathSize) + inode.targetPath = make([]byte, inode.TargetPathSize, inode.TargetPathSize) err = binary.Read(rdr, binary.LittleEndian, &inode.targetPath) if err != nil { return inode, err @@ -200,28 +201,28 @@ func NewBasicSymlink(rdr io.Reader) (BasicSymlink, error) { return inode, err } -//ExtendedSymlinkInit is all the values that can be directly decoded -type ExtendedSymlinkInit struct { +//ExtSymInit is all the values that can be directly decoded +type ExtSymInit struct { HardLinks uint32 TargetPathSize uint32 } -//ExtendedSymlink is a symlink with extra information -type ExtendedSymlink struct { - Init ExtendedSymlinkInit - targetPath []uint8 +//ExtSym is a symlink with extra information +type ExtSym struct { Path string + targetPath []uint8 + ExtSymInit XattrIndex uint32 } //NewExtendedSymlink creates a new ExtendedSymlink -func NewExtendedSymlink(rdr io.Reader) (ExtendedSymlink, error) { - var inode ExtendedSymlink - err := binary.Read(rdr, binary.LittleEndian, &inode.Init) +func NewExtendedSymlink(rdr io.Reader) (ExtSym, error) { + var inode ExtSym + err := binary.Read(rdr, binary.LittleEndian, &inode.ExtSymInit) if err != nil { return inode, err } - inode.targetPath = make([]uint8, inode.Init.TargetPathSize, inode.Init.TargetPathSize) + inode.targetPath = make([]uint8, inode.TargetPathSize, inode.TargetPathSize) err = binary.Read(rdr, binary.LittleEndian, &inode.targetPath) if err != nil { return inode, err @@ -231,25 +232,25 @@ func NewExtendedSymlink(rdr io.Reader) (ExtendedSymlink, error) { return inode, err } -//BasicDevice is a device -type BasicDevice struct { +//Device is a device +type Device struct { HardLinks uint32 Device uint32 } -//ExtendedDevice is a device with more info -type ExtendedDevice struct { - BasicDevice +//ExtDevice is a device with more info +type ExtDevice struct { + Device XattrIndex uint32 } -//BasicIPC is a Fifo or Socket device -type BasicIPC struct { +//IPC is a Fifo or Socket device +type IPC struct { HardLink uint32 } -//ExtendedIPC is a IPC device with extra info -type ExtendedIPC struct { - BasicIPC +//ExtIPC is a IPC device with extra info +type ExtIPC struct { + IPC XattrIndex uint32 } diff --git a/internal/inode/process.go b/internal/inode/process.go index 05875d2..d63f4f0 100644 --- a/internal/inode/process.go +++ b/internal/inode/process.go @@ -9,9 +9,9 @@ import ( // //Info holds the actual Inode. Due to each inode type being a different type, it's store as an interface{} type Inode struct { - Header Header - Type int //Type the inode type defined in the header. Here so it's easy to access - Info interface{} //Info is the parsed specific data. It's type is defined by Type. + Info interface{} //Info is the parsed specific data. It's type is defined by Type. + Type int //Type the inode type defined in the header. Here so it's easy to access + Header } //ProcessInode tries to read an inode from the BlockReader @@ -23,48 +23,48 @@ func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) { } var info interface{} switch head.InodeType { - case BasicDirectoryType: - var inode BasicDirectory + case DirType: + var inode Dir err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode - case BasicFileType: - inode, err := NewBasicFile(br, blockSize) + case FileType: + inode, err := NewFile(br, blockSize) if err != nil { return nil, err } info = inode - case BasicSymlinkType: - inode, err := NewBasicSymlink(br) + case SymType: + inode, err := NewSymlink(br) if err != nil { return nil, err } info = inode - case BasicBlockDeviceType: - var inode BasicDevice + case BlockDevType: + var inode Device err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode - case BasicCharDeviceType: - var inode BasicDevice + case CharDevType: + var inode Device err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode - case BasicFifoType: - var inode BasicIPC + case FifoType: + var inode IPC err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode - case BasicSocketType: - var inode BasicIPC + case SocketType: + var inode IPC err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err @@ -89,28 +89,28 @@ func ProcessInode(br io.Reader, blockSize uint32) (*Inode, error) { } info = inode case ExtBlockDeviceType: - var inode ExtendedDevice + var inode ExtDevice err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode case ExtCharDeviceType: - var inode ExtendedDevice + var inode ExtDevice err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode case ExtFifoType: - var inode ExtendedIPC + var inode ExtIPC err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err } info = inode case ExtSocketType: - var inode ExtendedIPC + var inode ExtIPC err = binary.Read(br, binary.LittleEndian, &inode) if err != nil { return nil, err