Restructure for 1.16 io/fs interface

This commit is contained in:
Caleb Gardner
2021-01-29 12:55:57 -06:00
parent dd08d3516d
commit 3f1b2a8d1e
7 changed files with 1004 additions and 690 deletions
+96 -97
View File
@@ -8,7 +8,6 @@ import (
"time"
"github.com/CalebQ42/squashfs/internal/compression"
"github.com/CalebQ42/squashfs/internal/inode"
)
const (
@@ -159,117 +158,117 @@ func (r *Reader) ModTime() time.Time {
}
//ExtractTo tries to extract ALL files to the given path. This is the same as getting the root folder and extracting that.
func (r *Reader) ExtractTo(path string) []error {
if r.root == nil {
_, err := r.GetRootFolder()
if err != nil {
return []error{err}
}
}
return r.root.ExtractTo(path)
}
// func (r *Reader) ExtractTo(path string) []error {
// if r.root == nil {
// _, err := r.GetRootFolder()
// if err != nil {
// return []error{err}
// }
// }
// return r.root.ExtractTo(path)
// }
//GetRootFolder returns a squashfs.File that references the root directory of the squashfs archive.
func (r *Reader) GetRootFolder() (*File, error) {
if r.root != nil {
return r.root, nil
}
mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
if err != nil {
return nil, err
}
// mr, err := r.newMetadataReaderFromInodeRef(r.super.RootInodeRef)
// if err != nil {
// return nil, err
// }
var root File
root.in, err = inode.ProcessInode(mr, r.super.BlockSize)
if err != nil {
return nil, err
}
root.dir = "/"
root.filType = root.in.Type
root.r = r
// root.in, err = inode.ProcessInode(mr, r.super.BlockSize)
// if err != nil {
// return nil, err
// }
// root.dir = "/"
// root.filType = root.in.Type
// root.r = r
r.root = &root
return r.root, nil
}
//GetAllFiles returns a slice of ALL files and folders contained in the squashfs.
func (r *Reader) GetAllFiles() (fils []*File, err error) {
if r.root == nil {
_, err := r.GetRootFolder()
if err != nil {
return nil, err
}
}
return r.root.GetChildrenRecursively()
}
// func (r *Reader) GetAllFiles() (fils []*File, err error) {
// if r.root == nil {
// _, err := r.GetRootFolder()
// if err != nil {
// return nil, err
// }
// }
// return r.root.GetChildrenRecursively()
// }
//FindFile returns the first file (in the same order as Reader.GetAllFiles) that the given function returns true for. Returns nil if nothing is found.
func (r *Reader) FindFile(query func(*File) bool) *File {
if r.root == nil {
_, err := r.GetRootFolder()
if err != nil {
return nil
}
}
fils, err := r.root.GetChildren()
if err != nil {
return nil
}
var childrenDirs []*File
for _, fil := range fils {
if query(fil) {
return fil
}
if fil.IsDir() {
childrenDirs = append(childrenDirs, fil)
}
}
for len(childrenDirs) != 0 {
var tmp []*File
for _, dirs := range childrenDirs {
chil, err := dirs.GetChildren()
if err != nil {
return nil
}
for _, child := range chil {
if query(child) {
return child
}
if child.IsDir() {
tmp = append(tmp, child)
}
}
}
childrenDirs = tmp
}
return nil
}
// func (r *Reader) FindFile(query func(*File) bool) *File {
// if r.root == nil {
// _, err := r.GetRootFolder()
// if err != nil {
// return nil
// }
// }
// fils, err := r.root.GetChildren()
// if err != nil {
// return nil
// }
// var childrenDirs []*File
// for _, fil := range fils {
// if query(fil) {
// return fil
// }
// if fil.IsDir() {
// childrenDirs = append(childrenDirs, fil)
// }
// }
// for len(childrenDirs) != 0 {
// var tmp []*File
// for _, dirs := range childrenDirs {
// chil, err := dirs.GetChildren()
// if err != nil {
// return nil
// }
// for _, child := range chil {
// if query(child) {
// return child
// }
// if child.IsDir() {
// tmp = append(tmp, child)
// }
// }
// }
// childrenDirs = tmp
// }
// return nil
// }
//FindAll returns all files where the given function returns true.
func (r *Reader) FindAll(query func(*File) bool) (all []*File) {
if r.root == nil {
_, err := r.GetRootFolder()
if err != nil {
return nil
}
}
fils, err := r.root.GetChildrenRecursively()
if err != nil {
return nil
}
for _, fil := range fils {
if query(fil) {
all = append(all, fil)
}
}
return
}
// func (r *Reader) FindAll(query func(*File) bool) (all []*File) {
// if r.root == nil {
// _, err := r.GetRootFolder()
// if err != nil {
// return nil
// }
// }
// fils, err := r.root.GetChildrenRecursively()
// if err != nil {
// return nil
// }
// for _, fil := range fils {
// if query(fil) {
// all = append(all, fil)
// }
// }
// return
// }
//GetFileAtPath will return the file at the given path. If the file cannot be found, will return nil.
func (r *Reader) GetFileAtPath(filepath string) *File {
if r.root == nil {
_, err := r.GetRootFolder()
if err != nil {
return nil
}
}
return r.root.GetFileAtPath(filepath)
}
// func (r *Reader) GetFileAtPath(filepath string) *File {
// if r.root == nil {
// _, err := r.GetRootFolder()
// if err != nil {
// return nil
// }
// }
// return r.root.GetFileAtPath(filepath)
// }