Finished io/FS interface
This commit is contained in:
@@ -1,516 +1,379 @@
|
||||
// package squashfs
|
||||
package squashfs
|
||||
|
||||
// import (
|
||||
// "errors"
|
||||
// "fmt"
|
||||
// "io"
|
||||
// "os"
|
||||
// "path"
|
||||
// "strings"
|
||||
// "time"
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
// "github.com/CalebQ42/squashfs/internal/directory"
|
||||
// "github.com/CalebQ42/squashfs/internal/inode"
|
||||
// )
|
||||
"github.com/CalebQ42/squashfs/internal/directory"
|
||||
"github.com/CalebQ42/squashfs/internal/inode"
|
||||
)
|
||||
|
||||
// //TODO: implement fs.FS, fs.ReadDirFile, fs.ReadFileFS, fs.StatFS, fs.SubFS with 1.16
|
||||
//File represents a file inside a squashfs archive.
|
||||
type File struct {
|
||||
i *inode.Inode
|
||||
parent *FS
|
||||
r *Reader
|
||||
reader *fileReader
|
||||
name string
|
||||
dirsRead int
|
||||
}
|
||||
|
||||
// var (
|
||||
// //ErrNotDirectory is returned when you're trying to do directory things with a non-directory
|
||||
// errNotDirectory = errors.New("File is not a directory")
|
||||
// //ErrNotFile is returned when you're trying to do file things with a directory
|
||||
// errNotFile = errors.New("File is not a file")
|
||||
// //ErrNotReading is returned when running functions that are only meant to be used when reading a squashfs
|
||||
// errNotReading = errors.New("Function only supported when reading a squashfs")
|
||||
// //ErrBrokenSymlink is returned when using ExtractWithOptions with the unbreakSymlink set to true, but the symlink's file cannot be extracted.
|
||||
// ErrBrokenSymlink = errors.New("Extracted symlink is probably broken")
|
||||
// )
|
||||
//File creates a File from the FileInfo.
|
||||
//*File satisfies fs.File and fs.ReadDirFile.
|
||||
func (f FileInfo) File() (file *File, err error) {
|
||||
file = &File{
|
||||
name: f.name,
|
||||
r: f.r,
|
||||
parent: f.parent,
|
||||
i: f.i,
|
||||
}
|
||||
if file.IsRegular() {
|
||||
file.reader, err = f.r.newFileReader(f.i)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// //File is the main way to interact with files within squashfs, or when putting files into a squashfs.
|
||||
// //File can be either a file or folder. When reading from a squashfs, it reads from the datablocks.
|
||||
// //When writing, this holds the information on WHERE the file will be placed inside the archive.
|
||||
// //
|
||||
// //If copying data from a squashfs, the returned reader from io.Sys() implements io.WriterTo which
|
||||
// //will be significantly faster then calling Read directly.
|
||||
// //Ex: use io.Sys().(io.Reader) for io.Copy instead of using the File directly.
|
||||
// //
|
||||
// //Implements os.FileInfo and io.Reader
|
||||
// type File struct {
|
||||
// reader io.Reader
|
||||
// Parent *File
|
||||
// r *Reader //Underlying reader. When writing, will probably be an os.File. When reading this is kept nil UNTIL reading to save memory.
|
||||
// in *inode.Inode
|
||||
// name string
|
||||
// dir string
|
||||
// filType int //The file's type, using inode types.
|
||||
//File creates a File from the DirEntry.
|
||||
func (d DirEntry) File() (file *File, err error) {
|
||||
return d.r.newFileFromDirEntry(d.en, d.parent)
|
||||
}
|
||||
|
||||
// }
|
||||
func (r Reader) newFileFromDirEntry(en *directory.Entry, parent *FS) (file *File, err error) {
|
||||
file = &File{
|
||||
name: en.Name,
|
||||
r: &r,
|
||||
parent: parent,
|
||||
}
|
||||
file.i, err = r.getInodeFromEntry(en)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if file.IsRegular() {
|
||||
file.reader, err = r.newFileReader(file.i)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// //get a File from a directory.entry
|
||||
// func (r *Reader) newFileFromDirEntry(entry *directory.Entry) (fil *File, err error) {
|
||||
// fil = new(File)
|
||||
// fil.in, err = r.getInodeFromEntry(entry)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// fil.name = entry.Name
|
||||
// fil.r = r
|
||||
// fil.filType = fil.in.Type
|
||||
// return
|
||||
// }
|
||||
//Stat returns the File's fs.FileInfo
|
||||
func (f File) Stat() (fs.FileInfo, error) {
|
||||
return &FileInfo{
|
||||
i: f.i,
|
||||
name: f.name,
|
||||
parent: f.parent,
|
||||
r: f.r,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// //Name is the file's name
|
||||
// func (f *File) Name() string {
|
||||
// return f.name
|
||||
// }
|
||||
//Read reads the data from the file. Only works if file is a normal file.
|
||||
func (f File) Read(p []byte) (int, error) {
|
||||
if f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType {
|
||||
if f.reader == nil {
|
||||
return 0, fs.ErrClosed
|
||||
}
|
||||
return f.reader.Read(p)
|
||||
}
|
||||
return 0, errors.New("Can only read files")
|
||||
}
|
||||
|
||||
// //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.FileType:
|
||||
// return int64(f.in.Info.(inode.File).Size)
|
||||
// case inode.ExtFileType:
|
||||
// return int64(f.in.Info.(inode.ExtFile).Size)
|
||||
// default:
|
||||
// return 0
|
||||
// }
|
||||
// }
|
||||
//WriteTo writes all data from the file to the writer. This is multi-threaded.
|
||||
func (f File) WriteTo(w io.Writer) (int64, error) {
|
||||
if f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType {
|
||||
if f.reader == nil {
|
||||
return 0, fs.ErrClosed
|
||||
}
|
||||
return f.reader.WriteTo(w)
|
||||
}
|
||||
return 0, errors.New("Can only read files")
|
||||
}
|
||||
|
||||
// //ModTime is the time of last modification.
|
||||
// func (f *File) ModTime() time.Time {
|
||||
// return time.Unix(int64(f.in.Header.ModifiedTime), 0)
|
||||
// }
|
||||
//Close simply nils the underlying reader. Here mostly to satisfy fs.File
|
||||
func (f *File) Close() error {
|
||||
f.reader = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// //Sys returns the underlying reader. If the reader isn't initialized, it will initialize it.
|
||||
// //If called on something other then a file, returns nil.
|
||||
// func (f *File) Sys() interface{} {
|
||||
// if !f.IsFile() {
|
||||
// return nil
|
||||
// }
|
||||
// if f.reader == nil && f.r != nil {
|
||||
// var err error
|
||||
// f.reader, err = f.r.newFileReader(f.in)
|
||||
// if err != nil {
|
||||
// return nil
|
||||
// }
|
||||
// }
|
||||
// return f.reader
|
||||
// }
|
||||
//ReadDir returns n fs.DirEntry's that's contained in the File (if it's a directory).
|
||||
//If n <= 0 all fs.DirEntry's are returned.
|
||||
func (f File) ReadDir(n int) ([]fs.DirEntry, error) {
|
||||
if !f.IsDir() {
|
||||
return nil, errors.New("File is not a directory")
|
||||
}
|
||||
ffs, err := f.FS()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var beg, end int
|
||||
if n <= 0 {
|
||||
beg, end = 0, len(ffs.entries)
|
||||
} else {
|
||||
beg, end = f.dirsRead, f.dirsRead+n
|
||||
if end > len(ffs.entries) {
|
||||
end = len(ffs.entries)
|
||||
err = io.EOF
|
||||
}
|
||||
}
|
||||
out := make([]fs.DirEntry, end-beg)
|
||||
for i, ent := range ffs.entries[beg:end] {
|
||||
out[i] = f.r.newDirEntry(ent, ffs)
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
||||
// //TODO: Implement below when 1.16 drops to satisfy fs.File
|
||||
//FS returns the File as a FS.
|
||||
func (f File) FS() (*FS, error) {
|
||||
if !f.IsDir() {
|
||||
return nil, errors.New("File is not a directory")
|
||||
}
|
||||
ents, err := f.r.readDirFromInode(f.i)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FS{
|
||||
entries: ents,
|
||||
parent: f.parent,
|
||||
r: f.r,
|
||||
name: f.name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// //Stat simply returns the file. It's simply here to satisfy fs.File
|
||||
// // func (f *File) Stat() (fs.FileInfo, error) {
|
||||
// // return f, nil
|
||||
// // }
|
||||
//IsDir Yep.
|
||||
func (f File) IsDir() bool {
|
||||
return f.i.Type == inode.DirType || f.i.Type == inode.ExtDirType
|
||||
}
|
||||
|
||||
// //Close does nothing. It's simply here to satisfy fs.File
|
||||
// //TODO: add actual implementation
|
||||
// // func (f *File) Close() error {
|
||||
// // return nil
|
||||
// // }
|
||||
func (f File) path() string {
|
||||
if f.name == "/" {
|
||||
return f.name
|
||||
}
|
||||
return f.parent.path() + "/" + f.name
|
||||
}
|
||||
|
||||
// //GetChildren returns a *squashfs.File slice of every direct child of the directory. If the File is not a directory, will return ErrNotDirectory
|
||||
// func (f *File) GetChildren() (children []*File, err error) {
|
||||
// children = make([]*File, 0)
|
||||
// if f.r == nil {
|
||||
// return nil, errNotReading
|
||||
// }
|
||||
// if !f.IsDir() {
|
||||
// return nil, errNotDirectory
|
||||
// }
|
||||
// dir, err := f.r.readDirFromInode(f.in)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// var fil *File
|
||||
// for _, entry := range dir.Entries {
|
||||
// fil, err = f.r.newFileFromDirEntry(&entry)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// fil.Parent = f
|
||||
// if f.name != "" {
|
||||
// fil.dir = f.Path()
|
||||
// }
|
||||
// children = append(children, fil)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
//IsRegular yep.
|
||||
func (f File) IsRegular() bool {
|
||||
return f.i.Type == inode.FileType || f.i.Type == inode.ExtFileType
|
||||
}
|
||||
|
||||
// //GetChildrenRecursively returns ALL children. Goes down ALL folder paths.
|
||||
// func (f *File) GetChildrenRecursively() (children []*File, err error) {
|
||||
// children = make([]*File, 0)
|
||||
// if f.r == nil {
|
||||
// return nil, errNotReading
|
||||
// }
|
||||
// if !f.IsDir() {
|
||||
// return nil, errNotDirectory
|
||||
// }
|
||||
// children, err = f.GetChildren()
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// var childFolders []*File
|
||||
// for _, child := range children {
|
||||
// if child.IsDir() {
|
||||
// childFolders = append(childFolders, child)
|
||||
// }
|
||||
// }
|
||||
// for _, folds := range childFolders {
|
||||
// var childs []*File
|
||||
// childs, err = folds.GetChildrenRecursively()
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
// children = append(children, childs...)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
//IsSymlink yep.
|
||||
func (f File) IsSymlink() bool {
|
||||
return f.i.Type == inode.SymType || f.i.Type == inode.ExtSymType
|
||||
}
|
||||
|
||||
// //Path returns the path of the file within the archive.
|
||||
// func (f *File) Path() string {
|
||||
// if f.name == "" {
|
||||
// return f.dir
|
||||
// }
|
||||
// return f.dir + "/" + f.name
|
||||
// }
|
||||
//SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
|
||||
func (f File) SymlinkPath() string {
|
||||
switch f.i.Type {
|
||||
case inode.SymType:
|
||||
return f.i.Info.(inode.Sym).Path
|
||||
case inode.ExtSymType:
|
||||
return f.i.Info.(inode.ExtSym).Path
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// //GetFileAtPath tries to return the File at the given path, relative to the file.
|
||||
// //Returns nil if called on something other then a folder, OR if the path goes oustide the archive.
|
||||
// //Allows wildcards supported by path.Match (namely * and ?) and will return the FIRST file that matches.
|
||||
// func (f *File) GetFileAtPath(dirPath string) *File {
|
||||
// dirPath = path.Clean(dirPath)
|
||||
// dirPath = strings.TrimPrefix(dirPath, "/")
|
||||
// if dirPath == "" || dirPath == "." {
|
||||
// return f
|
||||
// }
|
||||
// if dirPath != "." && !f.IsDir() {
|
||||
// return nil
|
||||
// }
|
||||
// split := strings.Split(dirPath, "/")
|
||||
// if split[0] == ".." && f.name == "" {
|
||||
// return nil
|
||||
// } else if split[0] == ".." {
|
||||
// if f.Parent != nil {
|
||||
// return f.Parent.GetFileAtPath(strings.Join(split[1:], "/"))
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
// children, err := f.GetChildren()
|
||||
// if err != nil {
|
||||
// return nil
|
||||
// }
|
||||
// for _, child := range children {
|
||||
// eq, _ := path.Match(split[0], child.name)
|
||||
// if eq {
|
||||
// return child.GetFileAtPath(strings.Join(split[1:], "/"))
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
//GetSymlinkFile returns the File the symlink is pointing to.
|
||||
//If not a symlink, or the target is unobtainable (such as it being outside the archive or it's absolute) returns nil
|
||||
func (f File) GetSymlinkFile() *File {
|
||||
if !f.IsSymlink() {
|
||||
return nil
|
||||
}
|
||||
if strings.HasPrefix(f.SymlinkPath(), "/") {
|
||||
return nil
|
||||
}
|
||||
sym, err := f.parent.Open(f.SymlinkPath())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return sym.(*File)
|
||||
}
|
||||
|
||||
// //TODO: add with 1.16
|
||||
// //Open is the same as GetFileAtPath to implement fs.FS
|
||||
// // func (f *File) Open(name string) (fs.File, error) {
|
||||
// // tmp := f.GetFileAtPath(name)
|
||||
// // if tmp == nil {
|
||||
// // return tmp, fs.ErrNotExist
|
||||
// // }
|
||||
// // return tmp, nil
|
||||
// // }
|
||||
//ExtractionOptions are available options on how to extract.
|
||||
type ExtractionOptions struct {
|
||||
notBase bool
|
||||
DereferenceSymlink bool //Replace symlinks with the target file
|
||||
UnbreakSymlink bool //Try to make sure symlinks remain unbroken when extracted, without changing the symlink
|
||||
Verbose bool //Prints extra info to log on an error
|
||||
FolderPerm fs.FileMode //The permissions used when creating the extraction folder
|
||||
}
|
||||
|
||||
// //IsDir returns if the file is a directory.
|
||||
// func (f *File) IsDir() bool {
|
||||
// return f.filType == inode.DirType || f.filType == inode.ExtDirType
|
||||
// }
|
||||
//DefaultOptions is the default ExtractionOptions.
|
||||
func DefaultOptions() ExtractionOptions {
|
||||
return ExtractionOptions{
|
||||
DereferenceSymlink: false,
|
||||
UnbreakSymlink: false,
|
||||
Verbose: false,
|
||||
FolderPerm: fs.ModePerm,
|
||||
}
|
||||
}
|
||||
|
||||
// //IsSymlink returns if the file is a symlink.
|
||||
// func (f *File) IsSymlink() bool {
|
||||
// return f.filType == inode.SymType || f.filType == inode.ExtSymType
|
||||
// }
|
||||
//ExtractTo extracts the File to the given folder with the default options.
|
||||
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||
func (f File) ExtractTo(folder string) error {
|
||||
return f.ExtractWithOptions(folder, DefaultOptions())
|
||||
}
|
||||
|
||||
// //IsFile returns if the file is a file.
|
||||
// func (f *File) IsFile() bool {
|
||||
// return f.filType == inode.FileType || f.filType == inode.ExtFileType
|
||||
// }
|
||||
//ExtractSymlink extracts the File to the folder with the DereferenceSymlink option.
|
||||
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||
func (f File) ExtractSymlink(folder string) error {
|
||||
return f.ExtractWithOptions(folder, ExtractionOptions{
|
||||
DereferenceSymlink: true,
|
||||
FolderPerm: fs.ModePerm,
|
||||
})
|
||||
}
|
||||
|
||||
// //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.SymType:
|
||||
// return f.in.Info.(inode.Sym).Path
|
||||
// case inode.ExtSymType:
|
||||
// return f.in.Info.(inode.ExtSym).Path
|
||||
// default:
|
||||
// return ""
|
||||
// }
|
||||
// }
|
||||
//ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions.
|
||||
//If the File is a directory, it instead extracts the directory's contents to the folder.
|
||||
func (f File) ExtractWithOptions(folder string, op ExtractionOptions) error {
|
||||
folder = path.Clean(folder)
|
||||
if !op.notBase {
|
||||
err := os.MkdirAll(folder, op.FolderPerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
stat, err := f.Stat()
|
||||
if f.IsDir() {
|
||||
if op.notBase {
|
||||
err = os.Mkdir(folder+"/"+f.name, stat.Mode())
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
op.notBase = true
|
||||
}
|
||||
var ents []fs.DirEntry
|
||||
ents, err = f.ReadDir(0)
|
||||
if err != nil {
|
||||
if op.Verbose {
|
||||
log.Println("Error while reading children of", f.path())
|
||||
}
|
||||
return err
|
||||
}
|
||||
errChan := make(chan error)
|
||||
for i := 0; i < len(ents); i++ {
|
||||
go func(ent *DirEntry) {
|
||||
fil, goErr := ent.File()
|
||||
if goErr != nil {
|
||||
errChan <- goErr
|
||||
fil.Close()
|
||||
return
|
||||
}
|
||||
errChan <- fil.ExtractWithOptions(folder+"/"+f.name, op)
|
||||
fil.Close()
|
||||
return
|
||||
}(ents[i].(*DirEntry))
|
||||
}
|
||||
for i := 0; i < len(ents); i++ {
|
||||
err = <-errChan
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
} else if f.IsRegular() {
|
||||
var fil *os.File
|
||||
fil, err = os.Create(folder + "/" + f.name)
|
||||
if os.IsExist(err) {
|
||||
os.Remove(folder + "/" + f.name)
|
||||
fil, err = os.Create(folder + "/" + f.name)
|
||||
if err != nil {
|
||||
log.Println("Error while creating", folder+"/"+f.name)
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(fil, f)
|
||||
if err != nil {
|
||||
log.Println("Error while copying data to", folder+"/"+f.name)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
} else if f.IsSymlink() {
|
||||
symPath := f.SymlinkPath()
|
||||
if op.DereferenceSymlink {
|
||||
fil := f.GetSymlinkFile()
|
||||
if fil == nil {
|
||||
if op.Verbose {
|
||||
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.name)
|
||||
}
|
||||
return errors.New("Cannot get symlink target")
|
||||
}
|
||||
fil.name = f.name
|
||||
err = fil.ExtractWithOptions(folder, op)
|
||||
if err != nil {
|
||||
if op.Verbose {
|
||||
log.Println("Error while extracting the symlink's file:", folder+"/"+f.name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
} else if op.UnbreakSymlink {
|
||||
fil := f.GetSymlinkFile()
|
||||
if fil == nil {
|
||||
if op.Verbose {
|
||||
log.Println("Symlink path(", symPath, ") is unobtainable:", folder+"/"+f.name)
|
||||
}
|
||||
return errors.New("Cannot get symlink target")
|
||||
}
|
||||
extractLoc := path.Clean(folder + "/" + path.Dir(symPath))
|
||||
err = fil.ExtractWithOptions(extractLoc, op)
|
||||
if err != nil {
|
||||
if op.Verbose {
|
||||
log.Println("Error while extracting ", folder+"/"+f.name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.name)
|
||||
if os.IsExist(err) {
|
||||
os.Remove(folder + "/" + f.name)
|
||||
err = os.Symlink(f.SymlinkPath(), folder+"/"+f.name)
|
||||
}
|
||||
if err != nil {
|
||||
if op.Verbose {
|
||||
log.Println("Error while making symlink:", folder+"/"+f.name)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
|
||||
}
|
||||
|
||||
// //GetSymlinkFile tries to return the squashfs.File associated with the symlink. If the file isn't a symlink
|
||||
// //or the symlink points to a location outside the archive, nil is returned.
|
||||
// func (f *File) GetSymlinkFile() *File {
|
||||
// if !f.IsSymlink() {
|
||||
// return nil
|
||||
// }
|
||||
// if strings.HasSuffix(f.SymlinkPath(), "/") {
|
||||
// return nil
|
||||
// }
|
||||
// return f.Parent.GetFileAtPath(f.SymlinkPath())
|
||||
// }
|
||||
|
||||
// //GetSymlinkFileRecursive tries to return the squasfs.File associated with the symlink. It will recursively
|
||||
// //try to get the symlink's file. This will return either a non-symlink File, or nil.
|
||||
// func (f *File) GetSymlinkFileRecursive() *File {
|
||||
// if !f.IsSymlink() {
|
||||
// return nil
|
||||
// }
|
||||
// if strings.HasSuffix(f.SymlinkPath(), "/") {
|
||||
// return nil
|
||||
// }
|
||||
// sym := f
|
||||
// for {
|
||||
// sym = sym.GetSymlinkFile()
|
||||
// if sym == nil {
|
||||
// return nil
|
||||
// }
|
||||
// if !sym.IsSymlink() {
|
||||
// return sym
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// //Mode returns the os.FileMode of the File. Sets mode bits for directories and symlinks.
|
||||
// func (f *File) Mode() os.FileMode {
|
||||
// mode := os.FileMode(f.in.Header.Permissions)
|
||||
// switch {
|
||||
// case f.IsDir():
|
||||
// mode = mode | os.ModeDir
|
||||
// case f.IsSymlink():
|
||||
// mode = mode | os.ModeSymlink
|
||||
// }
|
||||
// return mode
|
||||
// }
|
||||
|
||||
// //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).
|
||||
// //
|
||||
// //Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
||||
// func (f *File) ExtractTo(path string) []error {
|
||||
// return f.ExtractWithOptions(path, false, false, os.ModePerm, false)
|
||||
// }
|
||||
|
||||
// //ExtractSymlink is similar to ExtractTo, but when it extracts a symlink, it instead extracts the file associated with the symlink in it's place.
|
||||
// //This is the same as ExtractWithOptions(path, true, false, os.ModePerm, false)
|
||||
// func (f *File) ExtractSymlink(path string) []error {
|
||||
// return f.ExtractWithOptions(path, true, false, os.ModePerm, false)
|
||||
// }
|
||||
|
||||
// //ExtractWithOptions will extract the file to the given path, while allowing customization on how it works. ExtractTo is the "default" options.
|
||||
// //Will try it's best to extract all files, and if any errors come up, they will be appended to the error slice that's returned.
|
||||
// //Should only return multiple errors if extracting a folder.
|
||||
// //
|
||||
// //If dereferenceSymlink is set, instead of extracting a symlink, it will extract the file the symlink is pointed to in it's place.
|
||||
// //If both dereferenceSymlink and unbreakSymlink is set, dereferenceSymlink takes precendence.
|
||||
// //
|
||||
// //If unbreakSymlink is set, it will also try to extract the symlink's associated file. WARNING: the symlink's file may have to go up the directory to work.
|
||||
// //If unbreakSymlink is set and the file cannot be extracted, a ErrBrokenSymlink will be appended to the returned error slice.
|
||||
// //
|
||||
// //folderPerm only applies to the folders created to get to path. Folders from the archive are given the correct permissions defined by the archive.
|
||||
// func (f *File) ExtractWithOptions(path string, dereferenceSymlink, unbreakSymlink bool, folderPerm os.FileMode, verbose bool) (errs []error) {
|
||||
// errs = make([]error, 0)
|
||||
// err := os.MkdirAll(path, folderPerm)
|
||||
// if err != nil {
|
||||
// return []error{err}
|
||||
// }
|
||||
// switch {
|
||||
// case f.IsDir():
|
||||
// if f.name != "" {
|
||||
// //TODO: check if folder is present, and if so, try to set it's permission
|
||||
// err = os.Mkdir(path+"/"+f.name, os.ModePerm)
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while making: ", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// }
|
||||
// var fil *os.File
|
||||
// fil, err = os.Open(path + "/" + f.name)
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while opening:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// }
|
||||
// fil.Chown(int(f.r.idTable[f.in.Header.UID]), int(f.r.idTable[f.in.Header.GID]))
|
||||
// //don't mention anything when it fails. Because it fails often. Probably has something to do about uid & gid 0
|
||||
// // if err != nil {
|
||||
// // if verbose {
|
||||
// // fmt.Println("Error while changing owner:", path+"/"+f.Name)
|
||||
// // fmt.Println(err)
|
||||
// // }
|
||||
// // errs = append(errs, err)
|
||||
// // }
|
||||
// err = fil.Chmod(f.Mode())
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while changing owner:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// }
|
||||
// }
|
||||
// var children []*File
|
||||
// children, err = f.GetChildren()
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error getting children for:", f.Path())
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// }
|
||||
// finishChan := make(chan []error)
|
||||
// for _, child := range children {
|
||||
// go func(child *File) {
|
||||
// if f.name == "" {
|
||||
// finishChan <- child.ExtractWithOptions(path, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||
// } else {
|
||||
// finishChan <- child.ExtractWithOptions(path+"/"+f.name, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||
// }
|
||||
// }(child)
|
||||
// }
|
||||
// for range children {
|
||||
// errs = append(errs, (<-finishChan)...)
|
||||
// }
|
||||
// return
|
||||
// case f.IsFile():
|
||||
// var fil *os.File
|
||||
// fil, err = os.Create(path + "/" + f.name)
|
||||
// if os.IsExist(err) {
|
||||
// err = os.Remove(path + "/" + f.name)
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while making:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// }
|
||||
// fil, err = os.Create(path + "/" + f.name)
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while making:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// }
|
||||
// } else if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while making:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// } //Since we will be reading from the file
|
||||
// _, err = io.Copy(fil, f.Sys().(io.Reader))
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while Copying data to:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// return
|
||||
// }
|
||||
// fil.Chown(int(f.r.idTable[f.in.Header.UID]), int(f.r.idTable[f.in.Header.GID]))
|
||||
// //don't mention anything when it fails. Because it fails often. Probably has something to do about uid & gid 0
|
||||
// // if err != nil {
|
||||
// // if verbose {
|
||||
// // fmt.Println("Error while changing owner:", path+"/"+f.Name)
|
||||
// // fmt.Println(err)
|
||||
// // }
|
||||
// // errs = append(errs, err)
|
||||
// // return
|
||||
// // }
|
||||
// err = fil.Chmod(f.Mode())
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while setting permissions for:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// }
|
||||
// return
|
||||
// case f.IsSymlink():
|
||||
// symPath := f.SymlinkPath()
|
||||
// if dereferenceSymlink {
|
||||
// fil := f.GetSymlinkFile()
|
||||
// if fil == nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.name)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// fil.name = f.name
|
||||
// extracSymErrs := fil.ExtractWithOptions(path, dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||
// if len(extracSymErrs) > 0 {
|
||||
// if verbose {
|
||||
// fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.name)
|
||||
// fmt.Println(extracSymErrs)
|
||||
// }
|
||||
// errs = append(errs, extracSymErrs...)
|
||||
// }
|
||||
// return
|
||||
// } else if unbreakSymlink {
|
||||
// fil := f.GetSymlinkFile()
|
||||
// if fil != nil {
|
||||
// symPath = path + "/" + symPath
|
||||
// paths := strings.Split(symPath, "/")
|
||||
// extracSymErrs := fil.ExtractWithOptions(strings.Join(paths[:len(paths)-1], "/"), dereferenceSymlink, unbreakSymlink, folderPerm, verbose)
|
||||
// if len(extracSymErrs) > 0 {
|
||||
// if verbose {
|
||||
// fmt.Println("Error(s) while extracting the symlink's file:", path+"/"+f.name)
|
||||
// fmt.Println(extracSymErrs)
|
||||
// }
|
||||
// errs = append(errs, extracSymErrs...)
|
||||
// }
|
||||
// } else {
|
||||
// if verbose {
|
||||
// fmt.Println("Symlink path(", symPath, ") is outside the archive:"+path+"/"+f.name)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// err = os.Symlink(f.SymlinkPath(), path+"/"+f.name)
|
||||
// if err != nil {
|
||||
// if verbose {
|
||||
// fmt.Println("Error while making symlink:", path+"/"+f.name)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// errs = append(errs, err)
|
||||
// }
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// //Read from the file. Doesn't do anything fancy, just pases it to the underlying io.Reader. If a directory, return io.EOF.
|
||||
// func (f *File) Read(p []byte) (int, error) {
|
||||
// if !f.IsFile() {
|
||||
// return 0, io.EOF
|
||||
// }
|
||||
// var err error
|
||||
// if f.reader == nil && f.r != nil {
|
||||
// f.reader, err = f.r.newFileReader(f.in)
|
||||
// if err != nil {
|
||||
// return 0, err
|
||||
// }
|
||||
// }
|
||||
// return f.reader.Read(p)
|
||||
// }
|
||||
//ReadDirFromInode returns a fully populated Directory from a given Inode.
|
||||
//If the given inode is not a directory it returns an error.
|
||||
func (r *Reader) readDirFromInode(i *inode.Inode) ([]*directory.Entry, error) {
|
||||
var offset uint32
|
||||
var metaOffset uint16
|
||||
var size uint32
|
||||
switch i.Type {
|
||||
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.ExtDir).DirectoryIndex
|
||||
metaOffset = i.Info.(inode.ExtDir).DirectoryOffset
|
||||
size = i.Info.(inode.ExtDir).DirectorySize
|
||||
default:
|
||||
return nil, errors.New("Not a directory inode")
|
||||
}
|
||||
br, err := r.newMetadataReader(int64(r.super.DirTableStart + uint64(offset)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = br.Seek(int64(metaOffset), io.SeekStart)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ents, err := directory.NewDirectory(br, size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ents, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user