Compare commits

..

9 Commits

Author SHA1 Message Date
Caleb Gardner 94b45c8402 Added IgnorePerm to ExtractionOptions 2023-04-12 07:57:57 -05:00
Caleb Gardner 01de43a5ae Added ErrReadNotFile to ReatAt, WriteTo 2023-04-11 00:34:43 -05:00
Caleb Gardner 5b29f4d029 Updated README 2023-04-09 21:09:53 -05:00
Caleb Gardner 6c7e926649 Updated deps 2023-04-09 20:41:45 -05:00
Caleb Gardner 72d85d7810 Added (expiremental) support for device files and Fifo 2023-04-09 20:41:00 -05:00
Caleb Gardner 67df5f40c6 Fix #21 and update deps 2023-03-29 12:45:21 -05:00
Caleb Gardner 1ae5593e6c Merge pull request #20 from x1unix/main
fix: use correct ENODATA value on different platforms
2023-03-18 06:57:45 -05:00
ds.hiveon 653c4a167b fix: remove redundant build tag 2023-03-18 07:46:29 +01:00
ds.hiveon 9fe17650b8 fix: use correct ENODATA value on different platforms 2023-03-18 07:44:18 +01:00
9 changed files with 159 additions and 39 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
# squashfs (WIP) # squashfs
[![PkgGoDev](https://pkg.go.dev/badge/github.com/CalebQ42/squashfs)](https://pkg.go.dev/github.com/CalebQ42/squashfs) [![Go Report Card](https://goreportcard.com/badge/github.com/CalebQ42/squashfs)](https://goreportcard.com/report/github.com/CalebQ42/squashfs) [![PkgGoDev](https://pkg.go.dev/badge/github.com/CalebQ42/squashfs)](https://pkg.go.dev/github.com/CalebQ42/squashfs) [![Go Report Card](https://goreportcard.com/badge/github.com/CalebQ42/squashfs)](https://goreportcard.com/report/github.com/CalebQ42/squashfs)
A PURE Go library to read and write squashfs. A PURE Go library to read squashfs. There is currently no plans to add archive creation support as it will almost always be better to just call `mksquashfs`. I could see some possible use cases, but probably won't spend time on it unless it's requested (open a discussion fi you want this feature).
Currently has support for reading squashfs files and extracting files and folders. Currently has support for reading squashfs files and extracting files and folders.
+2 -2
View File
@@ -100,7 +100,7 @@ func (f fileNode2) ReadAll(ctx context.Context) ([]byte, error) {
_, err := f.WriteTo(&buf) _, err := f.WriteTo(&buf)
return buf.Bytes(), err return buf.Bytes(), err
} }
return nil, fuse.ENODATA return nil, ENODATA
} }
func (f fileNode2) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { func (f fileNode2) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
@@ -112,7 +112,7 @@ func (f fileNode2) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.R
} }
return nil return nil
} }
return fuse.ENODATA return ENODATA
} }
func (f fileNode2) ReadDirAll(ctx context.Context) (out []fuse.Dirent, err error) { func (f fileNode2) ReadDirAll(ctx context.Context) (out []fuse.Dirent, err error) {
+2 -2
View File
@@ -98,7 +98,7 @@ func (f fileNode) ReadAll(ctx context.Context) ([]byte, error) {
_, err := f.WriteTo(&buf) _, err := f.WriteTo(&buf)
return buf.Bytes(), err return buf.Bytes(), err
} }
return nil, fuse.ENODATA return nil, ENODATA
} }
func (f fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { func (f fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
@@ -110,7 +110,7 @@ func (f fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.Re
} }
return nil return nil
} }
return fuse.ENODATA return ENODATA
} }
func (f fileNode) ReadDirAll(ctx context.Context) (out []fuse.Dirent, err error) { func (f fileNode) ReadDirAll(ctx context.Context) (out []fuse.Dirent, err error) {
+7
View File
@@ -0,0 +1,7 @@
package squashfs
import (
"golang.org/x/sys/unix"
)
var ENODATA = unix.Errno(unix.ENODATA)
+5
View File
@@ -0,0 +1,5 @@
package squashfs
import "github.com/CalebQ42/fuse"
var ENODATA = fuse.ENODATA
+3
View File
@@ -0,0 +1,3 @@
package squashfs
var ENODATA = windows.Errno(windows.ENODATA)
+5 -8
View File
@@ -1,17 +1,14 @@
module github.com/CalebQ42/squashfs module github.com/CalebQ42/squashfs
go 1.19 go 1.20
require ( require (
github.com/CalebQ42/fuse v0.1.0 github.com/CalebQ42/fuse v0.1.0
github.com/klauspost/compress v1.15.12 github.com/klauspost/compress v1.16.4
github.com/pierrec/lz4/v4 v4.1.17 github.com/pierrec/lz4/v4 v4.1.17
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e
github.com/therootcompany/xz v1.0.1
github.com/ulikunitz/xz v0.5.10
)
require (
github.com/seaweedfs/fuse v1.2.2 github.com/seaweedfs/fuse v1.2.2
golang.org/x/sys v0.2.0 // indirect github.com/therootcompany/xz v1.0.1
github.com/ulikunitz/xz v0.5.11
golang.org/x/sys v0.7.0
) )
+10 -6
View File
@@ -1,7 +1,9 @@
github.com/CalebQ42/fuse v0.1.0 h1:KLCNjun7zcd2kBNVFfH+SWJyhuwJdE0nhw5/q8K8HGQ= github.com/CalebQ42/fuse v0.1.0 h1:KLCNjun7zcd2kBNVFfH+SWJyhuwJdE0nhw5/q8K8HGQ=
github.com/CalebQ42/fuse v0.1.0/go.mod h1:pJpoKG03HJKVhsp8o0YQYqmfbFsr3Eowt90yQGQVO+4= github.com/CalebQ42/fuse v0.1.0/go.mod h1:pJpoKG03HJKVhsp8o0YQYqmfbFsr3Eowt90yQGQVO+4=
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM= github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.16.4 h1:91KN02FnsOYhuunwU4ssRe8lc2JosWmizWa91B5v1PU=
github.com/klauspost/compress v1.16.4/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc=
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e h1:dCWirM5F3wMY+cmRda/B1BiPsFtmzXqV9b0hLWtVBMs= github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e h1:dCWirM5F3wMY+cmRda/B1BiPsFtmzXqV9b0hLWtVBMs=
@@ -10,7 +12,9 @@ github.com/seaweedfs/fuse v1.2.2 h1:01l8OjIdyATRNqVc/gDPgFobuC8ubQF3hRKOPColROw=
github.com/seaweedfs/fuse v1.2.2/go.mod h1:iwbDQv5BZACY54r6AO/6xsLNuMaYcBKSkLTZVfmK594= github.com/seaweedfs/fuse v1.2.2/go.mod h1:iwbDQv5BZACY54r6AO/6xsLNuMaYcBKSkLTZVfmK594=
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw= github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY= github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8= github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+121 -17
View File
@@ -6,7 +6,9 @@ import (
"io/fs" "io/fs"
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
@@ -58,6 +60,21 @@ func (f File) Stat() (fs.FileInfo, error) {
return newFileInfo(f.e, f.i), nil return newFileInfo(f.e, f.i), nil
} }
// Mode returns the file's fs.FileMode
func (f File) Mode() fs.FileMode {
switch f.e.Type {
case inode.Dir:
return fs.FileMode(f.i.Perm) | fs.ModeDir
case inode.Char:
return fs.FileMode(f.i.Perm) | fs.ModeCharDevice
case inode.Block:
return fs.FileMode(f.i.Perm) | fs.ModeDevice
case inode.Sym:
return fs.FileMode(f.i.Perm) | fs.ModeSymlink
}
return fs.FileMode(f.i.Perm)
}
// Read reads the data from the file. Only works if file is a normal file. // Read reads the data from the file. Only works if file is a normal file.
func (f File) Read(p []byte) (int, error) { func (f File) Read(p []byte) (int, error) {
if f.i.Type != inode.Fil && f.i.Type != inode.EFil { if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
@@ -70,16 +87,22 @@ func (f File) Read(p []byte) (int, error) {
} }
func (f File) ReadAt(p []byte, off int64) (int, error) { func (f File) ReadAt(p []byte, off int64) (int, error) {
if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
return 0, ErrReadNotFile
}
return f.fullRdr.ReadAt(p, off) return f.fullRdr.ReadAt(p, off)
} }
// WriteTo writes all data from the file to the writer. This is multi-threaded. // WriteTo writes all data from the file to the writer. This is multi-threaded.
// The underlying reader is seperate from the one used with Read and can be reused. // The underlying reader is seperate from the one used with Read and can be reused.
func (f File) WriteTo(w io.Writer) (int64, error) { func (f File) WriteTo(w io.Writer) (int64, error) {
if f.i.Type != inode.Fil && f.i.Type != inode.EFil {
return 0, ErrReadNotFile
}
return f.fullRdr.WriteTo(w) return f.fullRdr.WriteTo(w)
} }
// Close simply nils the underlying reader. Here mostly to satisfy fs.File // Close simply nils the underlying reader.
func (f *File) Close() error { func (f *File) Close() error {
f.rdr = nil f.rdr = nil
return nil return nil
@@ -89,7 +112,7 @@ func (f *File) Close() error {
// If n <= 0 all fs.DirEntry's are returned. // If n <= 0 all fs.DirEntry's are returned.
func (f *File) ReadDir(n int) (out []fs.DirEntry, err error) { func (f *File) ReadDir(n int) (out []fs.DirEntry, err error) {
if !f.IsDir() { if !f.IsDir() {
return nil, errors.New("File is not a directory") return nil, errors.New("file is not a directory")
} }
ents, err := f.r.readDirectory(f.i) ents, err := f.r.readDirectory(f.i)
if err != nil { if err != nil {
@@ -146,6 +169,20 @@ func (f File) IsSymlink() bool {
return f.i.Type == inode.Sym || f.i.Type == inode.ESym return f.i.Type == inode.Sym || f.i.Type == inode.ESym
} }
func (f File) isDeviceOrFifo() bool {
return f.i.Type == inode.Char || f.i.Type == inode.Block || f.i.Type == inode.EChar || f.i.Type == inode.EBlock || f.i.Type == inode.Fifo || f.i.Type == inode.EFifo
}
func (f File) deviceDevices() (maj uint32, min uint32) {
var dev uint32
if f.i.Type == inode.Char || f.i.Type == inode.Block {
dev = f.i.Data.(inode.Device).Dev
} else if f.i.Type == inode.EChar || f.i.Type == inode.EBlock {
dev = f.i.Data.(inode.EDevice).Dev
}
return dev >> 8, dev & 0x000FF
}
// SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string. // SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
func (f File) SymlinkPath() string { func (f File) SymlinkPath() string {
switch f.i.Type { switch f.i.Type {
@@ -183,10 +220,11 @@ func (f File) GetSymlinkFile() *File {
// ExtractionOptions are available options on how to extract. // ExtractionOptions are available options on how to extract.
type ExtractionOptions struct { type ExtractionOptions struct {
LogOutput io.Writer //Where error log should write. If nil, uses os.Stdout. Has no effect if verbose is false. LogOutput io.Writer //Where error log should write. If nil, uses os.Stdout. Has no effect if verbose is false.
DereferenceSymlink bool //Replace symlinks with the target file DereferenceSymlink bool //Replace symlinks with the target file.
UnbreakSymlink bool //Try to make sure symlinks remain unbroken when extracted, without changing the symlink 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 Verbose bool //Prints extra info to log on an error.
FolderPerm fs.FileMode //The permissions used when creating the extraction folder IgnorePerm bool //ignore the file's permission and instead use FolderPerm.
FolderPerm fs.FileMode //The permissions used when creating the extraction folder. Defaults to 0755.
} }
// DefaultOptions is the default ExtractionOptions. // DefaultOptions is the default ExtractionOptions.
@@ -205,10 +243,9 @@ func (f File) ExtractTo(folder string) error {
// ExtractSymlink extracts the File to the folder with the DereferenceSymlink option. // 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. // If the File is a directory, it instead extracts the directory's contents to the folder.
func (f File) ExtractSymlink(folder string) error { func (f File) ExtractSymlink(folder string) error {
return f.ExtractWithOptions(folder, ExtractionOptions{ op := DefaultOptions()
DereferenceSymlink: true, op.DereferenceSymlink = true
FolderPerm: 0755, return f.ExtractWithOptions(folder, op)
})
} }
// ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions. // ExtractWithOptions extracts the File to the given folder with the given ExtrationOptions.
@@ -232,7 +269,8 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
} }
return err return err
} }
if f.IsDir() { switch {
case f.IsDir():
filFS, _ := f.FS() filFS, _ := f.FS()
var ents []directory.Entry var ents []directory.Entry
ents, err = f.r.readDirectory(f.i) ents, err = f.r.readDirectory(f.i)
@@ -254,8 +292,11 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
return return
} }
if fil.IsDir() { if fil.IsDir() {
info, _ := fil.Stat() perm := f.Mode()
err = os.Mkdir(filepath.Join(folder, fil.e.Name), info.Mode()) if op.IgnorePerm {
perm = (op.FolderPerm & fs.ModePerm) | (perm & fs.ModeType)
}
err = os.Mkdir(filepath.Join(folder, fil.e.Name), perm)
if err != nil { if err != nil {
if op.Verbose { if op.Verbose {
log.Println("Error while creating", filepath.Join(folder, fil.e.Name)) log.Println("Error while creating", filepath.Join(folder, fil.e.Name))
@@ -276,8 +317,7 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
return err return err
} }
} }
return nil case f.IsRegular():
} else if f.IsRegular() {
var fil *os.File var fil *os.File
fil, err = os.Create(folder + "/" + f.e.Name) fil, err = os.Create(folder + "/" + f.e.Name)
if os.IsExist(err) { if os.IsExist(err) {
@@ -295,6 +335,7 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
} }
return err return err
} }
defer fil.Close()
_, err = io.Copy(fil, f) _, err = io.Copy(fil, f)
if err != nil { if err != nil {
if op.Verbose { if op.Verbose {
@@ -302,8 +343,12 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
} }
return err return err
} }
return nil if op.IgnorePerm {
} else if f.IsSymlink() { os.Chmod(fil.Name(), op.FolderPerm)
} else {
os.Chmod(fil.Name(), f.Mode())
}
case f.IsSymlink():
symPath := f.SymlinkPath() symPath := f.SymlinkPath()
if op.DereferenceSymlink { if op.DereferenceSymlink {
fil := f.GetSymlinkFile() fil := f.GetSymlinkFile()
@@ -350,7 +395,66 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
} }
return err return err
} }
if op.IgnorePerm {
os.Chmod(folder+"/"+f.e.Name, op.FolderPerm)
} else {
os.Chmod(folder+"/"+f.e.Name, f.Mode())
}
case f.isDeviceOrFifo():
if runtime.GOOS == "windows" {
if op.Verbose {
log.Println(folder+"/"+f.e.Name, "ignored since it's a device link and can't be created on Windows.")
}
return nil return nil
} }
_, err = exec.LookPath("mknod")
if err != nil {
if op.Verbose {
log.Println("Extracting Fifo IPC or Device and mknod is not in PATH")
}
return err
}
var typ string
if f.i.Type == inode.Char || f.i.Type == inode.EChar {
typ = "c"
} else if f.i.Type == inode.Block || f.i.Type == inode.EBlock {
typ = "b"
} else { //Fifo IPC
if runtime.GOOS == "darwin" {
if op.Verbose {
log.Println(folder+"/"+f.e.Name, "ignored since it's a Fifo file and can't be created on Darwin.")
}
return nil
}
typ = "p"
}
cmd := exec.Command("mknod", folder+"/"+f.e.Name, typ)
if typ != "p" {
maj, min := f.deviceDevices()
cmd.Args = append(cmd.Args, strconv.Itoa(int(maj)), strconv.Itoa(int(min)))
}
if op.Verbose {
cmd.Stdout = op.LogOutput
cmd.Stderr = op.LogOutput
}
err = cmd.Run()
if err != nil {
if op.Verbose {
log.Println("Error while running mknod for", folder+"/"+f.e.Name)
}
return err
}
if op.IgnorePerm {
os.Chmod(folder+"/"+f.e.Name, op.FolderPerm)
} else {
os.Chmod(folder+"/"+f.e.Name, f.Mode())
}
case f.e.Type == inode.Sock:
if op.Verbose {
log.Println(folder+"/"+f.e.Name, "ignored since it's a socket file.")
}
default:
return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type))) return errors.New("Unsupported file type. Inode type: " + strconv.Itoa(int(f.i.Type)))
}
return nil
} }