Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c7e926649 | |||
| 72d85d7810 | |||
| 67df5f40c6 | |||
| 1ae5593e6c | |||
| 653c4a167b | |||
| 9fe17650b8 | |||
| e9e967f085 | |||
| 187da99dd6 | |||
| 75d2a29319 | |||
| ce2e45ceec | |||
| 089ef53c8c | |||
| 658e5c9e0b | |||
| f2d86aff96 | |||
| f61237a1f0 | |||
| 820e06e792 | |||
| 4f8f5f6928 | |||
| 1b5078c7bd |
@@ -17,6 +17,4 @@ Thanks also to [distri's squashfs library](https://github.com/distr1/distri/tree
|
|||||||
|
|
||||||
## Performance
|
## Performance
|
||||||
|
|
||||||
This library, decompressing the Firefox AppImage and using go tests, takes about twice as long as `unsquashfs` on my quad core laptop. (~1 second with the library and about half a second with `unsquashfs`).
|
Testing on a zstd compressed file, my library is anywhere from 5x ~ 7x slower then `unsquashfs`
|
||||||
|
|
||||||
**My recents tests have shown the Firefox AppImage might be an outlier and this library might be considerably slower (4x ~ 6x time slower then `unsquashfs`)**
|
|
||||||
|
|||||||
@@ -0,0 +1,150 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
|
"github.com/seaweedfs/fuse"
|
||||||
|
"github.com/seaweedfs/fuse/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Mounts the archive to the given mountpoint using fuse2. Non-blocking.
|
||||||
|
// If Unmount does not get called, the mount point must be unmounted using umount before the directory can be used again.
|
||||||
|
func (r *Reader) MountFuse2(mountpoint string) (err error) {
|
||||||
|
if r.con != nil {
|
||||||
|
return errors.New("squashfs archive already mounted")
|
||||||
|
}
|
||||||
|
r.con2, err = fuse.Mount(mountpoint, fuse.ReadOnly())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
<-r.con2.Ready
|
||||||
|
r.mount2Done = make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
fs.Serve(r.con2, squashFuse2{r: r})
|
||||||
|
close(r.mount2Done)
|
||||||
|
}()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blocks until the mount ends.
|
||||||
|
// Fuse2 version.
|
||||||
|
func (r *Reader) MountWaitFuse2() {
|
||||||
|
if r.mount2Done != nil {
|
||||||
|
<-r.mount2Done
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmounts the archive.
|
||||||
|
// Fuse2 version.
|
||||||
|
func (r *Reader) UnmountFuse2() error {
|
||||||
|
if r.con != nil {
|
||||||
|
defer func() { r.con = nil }()
|
||||||
|
return r.con.Close()
|
||||||
|
}
|
||||||
|
return errors.New("squashfs archive is not mounted")
|
||||||
|
}
|
||||||
|
|
||||||
|
type squashFuse2 struct {
|
||||||
|
r *Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s squashFuse2) Root() (fs.Node, error) {
|
||||||
|
return fileNode2{File: s.r.FS.File}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileNode2 struct {
|
||||||
|
*File
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) Attr(ctx context.Context, attr *fuse.Attr) error {
|
||||||
|
attr.Blocks = f.r.s.Size / 512
|
||||||
|
if f.r.s.Size%512 > 0 {
|
||||||
|
attr.Blocks++
|
||||||
|
}
|
||||||
|
attr.Gid = f.r.ids[f.i.GidInd]
|
||||||
|
attr.Inode = uint64(f.i.Num)
|
||||||
|
attr.Mode = f.i.Mode()
|
||||||
|
attr.Nlink = f.i.LinkCount()
|
||||||
|
attr.Size = f.i.Size()
|
||||||
|
attr.Uid = f.r.ids[f.i.UidInd]
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) Id() uint64 {
|
||||||
|
return uint64(f.i.Num)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
|
||||||
|
return f.SymlinkPath(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
||||||
|
asFS, err := f.FS()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fuse.ENOTDIR
|
||||||
|
}
|
||||||
|
ret, err := asFS.OpenFile(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fuse.ENOENT
|
||||||
|
}
|
||||||
|
return fileNode2{File: ret}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) ReadAll(ctx context.Context) ([]byte, error) {
|
||||||
|
if f.IsRegular() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err := f.WriteTo(&buf)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
}
|
||||||
|
return nil, ENODATA
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
||||||
|
if f.IsRegular() {
|
||||||
|
buf := make([]byte, req.Size)
|
||||||
|
n, err := f.File.ReadAt(buf, req.Offset)
|
||||||
|
if err == io.EOF {
|
||||||
|
resp.Data = buf[:n]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ENODATA
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f fileNode2) ReadDirAll(ctx context.Context) (out []fuse.Dirent, err error) {
|
||||||
|
asFS, err := f.FS()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fuse.ENOTDIR
|
||||||
|
}
|
||||||
|
var t fuse.DirentType
|
||||||
|
for i := range asFS.e {
|
||||||
|
switch asFS.e[i].Type {
|
||||||
|
case inode.Fil:
|
||||||
|
t = fuse.DT_File
|
||||||
|
case inode.Dir:
|
||||||
|
t = fuse.DT_Dir
|
||||||
|
case inode.Block:
|
||||||
|
t = fuse.DT_Block
|
||||||
|
case inode.Sym:
|
||||||
|
t = fuse.DT_Link
|
||||||
|
case inode.Char:
|
||||||
|
t = fuse.DT_Char
|
||||||
|
case inode.Fifo:
|
||||||
|
t = fuse.DT_FIFO
|
||||||
|
case inode.Sock:
|
||||||
|
t = fuse.DT_Socket
|
||||||
|
default:
|
||||||
|
t = fuse.DT_Unknown
|
||||||
|
}
|
||||||
|
out = append(out, fuse.Dirent{
|
||||||
|
Inode: uint64(asFS.e[i].Num),
|
||||||
|
Type: t,
|
||||||
|
Name: asFS.e[i].Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
+42
-15
@@ -3,6 +3,7 @@ package squashfs
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/CalebQ42/fuse"
|
"github.com/CalebQ42/fuse"
|
||||||
@@ -10,28 +11,54 @@ import (
|
|||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (r *Reader) Mount(mountpoint string) (con *fuse.Conn, err error) {
|
// Mounts the archive to the given mountpoint using fuse3. Non-blocking.
|
||||||
con, err = fuse.Mount(mountpoint, fuse.ReadOnly())
|
// If Unmount does not get called, the mount point must be unmounted using umount before the directory can be used again.
|
||||||
|
func (r *Reader) Mount(mountpoint string) (err error) {
|
||||||
|
if r.con != nil {
|
||||||
|
return errors.New("squashfs archive already mounted")
|
||||||
|
}
|
||||||
|
r.con, err = fuse.Mount(mountpoint, fuse.ReadOnly())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = fs.Serve(con, &squashFuse{r: r})
|
<-r.con.Ready
|
||||||
|
r.mountDone = make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
fs.Serve(r.con, squashFuse{r: r})
|
||||||
|
close(r.mountDone)
|
||||||
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Blocks until the mount ends.
|
||||||
|
func (r *Reader) MountWait() {
|
||||||
|
if r.mountDone != nil {
|
||||||
|
<-r.mountDone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmounts the archive.
|
||||||
|
func (r *Reader) Unmount() error {
|
||||||
|
if r.con != nil {
|
||||||
|
defer func() { r.con = nil }()
|
||||||
|
return r.con.Close()
|
||||||
|
}
|
||||||
|
return errors.New("squashfs archive is not mounted")
|
||||||
|
}
|
||||||
|
|
||||||
type squashFuse struct {
|
type squashFuse struct {
|
||||||
r *Reader
|
r *Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *squashFuse) Root() (fs.Node, error) {
|
func (s squashFuse) Root() (fs.Node, error) {
|
||||||
return &fileNode{File: s.r.FS.File}, nil
|
return fileNode{File: s.r.FS.File}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileNode struct {
|
type fileNode struct {
|
||||||
*File
|
*File
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
func (f fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
||||||
attr.Blocks = f.r.s.Size / 512
|
attr.Blocks = f.r.s.Size / 512
|
||||||
if f.r.s.Size%512 > 0 {
|
if f.r.s.Size%512 > 0 {
|
||||||
attr.Blocks++
|
attr.Blocks++
|
||||||
@@ -45,15 +72,15 @@ func (f *fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fileNode) Id() uint64 {
|
func (f fileNode) Id() uint64 {
|
||||||
return uint64(f.i.Num)
|
return uint64(f.i.Num)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fileNode) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
|
func (f fileNode) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
|
||||||
return f.SymlinkPath(), nil
|
return f.SymlinkPath(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fileNode) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
func (f fileNode) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
||||||
asFS, err := f.FS()
|
asFS, err := f.FS()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fuse.ENOTDIR
|
return nil, fuse.ENOTDIR
|
||||||
@@ -62,19 +89,19 @@ func (f *fileNode) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fuse.ENOENT
|
return nil, fuse.ENOENT
|
||||||
}
|
}
|
||||||
return &fileNode{File: ret}, nil
|
return fileNode{File: ret}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fileNode) ReadAll(ctx context.Context) ([]byte, error) {
|
func (f fileNode) ReadAll(ctx context.Context) ([]byte, error) {
|
||||||
if f.IsRegular() {
|
if f.IsRegular() {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
_, 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 {
|
||||||
if f.IsRegular() {
|
if f.IsRegular() {
|
||||||
buf := make([]byte, req.Size)
|
buf := make([]byte, req.Size)
|
||||||
n, err := f.File.ReadAt(buf, req.Offset)
|
n, err := f.File.ReadAt(buf, req.Offset)
|
||||||
@@ -83,10 +110,10 @@ func (f *fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.R
|
|||||||
}
|
}
|
||||||
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) {
|
||||||
asFS, err := f.FS()
|
asFS, err := f.FS()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fuse.ENOTDIR
|
return nil, fuse.ENOTDIR
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ENODATA = unix.Errno(unix.ENODATA)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
import "github.com/CalebQ42/fuse"
|
||||||
|
|
||||||
|
var ENODATA = fuse.ENODATA
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package squashfs
|
||||||
|
|
||||||
|
var ENODATA = windows.Errno(windows.ENODATA)
|
||||||
@@ -1,14 +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/seaweedfs/fuse v1.2.2
|
||||||
github.com/therootcompany/xz v1.0.1
|
github.com/therootcompany/xz v1.0.1
|
||||||
github.com/ulikunitz/xz v0.5.10
|
github.com/ulikunitz/xz v0.5.11
|
||||||
|
golang.org/x/sys v0.7.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/sys v0.2.0 // indirect
|
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
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=
|
||||||
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e/go.mod h1:9leZcVcItj6m9/CfHY5Em/iBrCz7js8LcRQGTKEEv2M=
|
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e/go.mod h1:9leZcVcItj6m9/CfHY5Em/iBrCz7js8LcRQGTKEEv2M=
|
||||||
|
github.com/seaweedfs/fuse v1.2.2 h1:01l8OjIdyATRNqVc/gDPgFobuC8ubQF3hRKOPColROw=
|
||||||
|
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=
|
||||||
|
|||||||
@@ -52,12 +52,11 @@ func (r FullReader) process(index int, offset int64, out chan outDat) {
|
|||||||
}
|
}
|
||||||
// rdr := io.LimitReader(toreader.NewReader(r.r, offset), int64(size))
|
// rdr := io.LimitReader(toreader.NewReader(r.r, offset), int64(size))
|
||||||
if size == r.sizes[index] {
|
if size == r.sizes[index] {
|
||||||
//Special workaround for zstd for increased performancce.
|
if dec, ok := r.d.(decompress.Decoder); ok {
|
||||||
if zstd, ok := r.d.(*decompress.Zstd); ok {
|
|
||||||
dat = make([]byte, size)
|
dat = make([]byte, size)
|
||||||
_, err = r.r.ReadAt(dat, offset)
|
_, err = r.r.ReadAt(dat, offset)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dat, err = zstd.Decode(dat)
|
dat, err = dec.Decode(dat)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
rdr, err = r.d.Reader(io.LimitReader(toreader.NewReader(r.r, offset), int64(size)))
|
rdr, err = r.d.Reader(io.LimitReader(toreader.NewReader(r.r, offset), int64(size)))
|
||||||
|
|||||||
@@ -53,14 +53,14 @@ func (r *Reader) advance() (err error) {
|
|||||||
} else {
|
} else {
|
||||||
r.cur = io.LimitReader(r.master, int64(size))
|
r.cur = io.LimitReader(r.master, int64(size))
|
||||||
if size == r.blockSizes[0] {
|
if size == r.blockSizes[0] {
|
||||||
if r.d.Resetable() {
|
if rs, ok := r.d.(decompress.Resetable); ok {
|
||||||
if r.comRdr == nil {
|
if r.comRdr == nil {
|
||||||
r.cur, err = r.d.Reader(r.cur)
|
r.cur, err = r.d.Reader(r.cur)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = r.d.Reset(r.comRdr, r.cur)
|
err = rs.Reset(r.comRdr, r.cur)
|
||||||
r.cur = r.comRdr
|
r.cur = r.comRdr
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ func (g GZip) Reader(src io.Reader) (io.ReadCloser, error) {
|
|||||||
return zlib.NewReader(src)
|
return zlib.NewReader(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g GZip) Resetable() bool { return true }
|
|
||||||
|
|
||||||
func (g GZip) Reset(old, src io.Reader) error {
|
func (g GZip) Reset(old, src io.Reader) error {
|
||||||
return old.(zlib.Resetter).Reset(src, nil)
|
return old.(zlib.Resetter).Reset(src, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
package decompress
|
package decompress
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrNotResetable = errors.New("decompressor not resetable")
|
|
||||||
|
|
||||||
type Decompressor interface {
|
type Decompressor interface {
|
||||||
//Creates a new decompressor reading from src.
|
//Creates a new decompressor reading from src.
|
||||||
Reader(src io.Reader) (io.ReadCloser, error)
|
Reader(src io.Reader) (io.ReadCloser, error)
|
||||||
//Reports whether Reset will work or not.
|
}
|
||||||
Resetable() bool
|
|
||||||
|
type Resetable interface {
|
||||||
//Reset attempts to re-use an old decompressor with new data.
|
//Reset attempts to re-use an old decompressor with new data.
|
||||||
//Will return ErrNotResetable if not Resetable().
|
//Will return ErrNotResetable if not Resetable().
|
||||||
//Must ALWAYS be provided with a reader created with Reader.
|
//Must ALWAYS be provided with a reader created with Reader.
|
||||||
Reset(old, src io.Reader) error
|
Reset(old, src io.Reader) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Decoder interface {
|
||||||
|
//Decodes a chunk of data all at once.
|
||||||
|
Decode(in []byte) ([]byte, error)
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ func (l Lz4) Reader(r io.Reader) (io.ReadCloser, error) {
|
|||||||
return io.NopCloser(lz4.NewReader(r)), nil
|
return io.NopCloser(lz4.NewReader(r)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Lz4) Resetable() bool { return true }
|
|
||||||
|
|
||||||
func (l Lz4) Reset(old, src io.Reader) error {
|
func (l Lz4) Reset(old, src io.Reader) error {
|
||||||
old.(*lz4.Reader).Reset(src)
|
old.(*lz4.Reader).Reset(src)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -12,7 +12,3 @@ func (l Lzma) Reader(r io.Reader) (io.ReadCloser, error) {
|
|||||||
rdr, err := lzma.NewReader(r)
|
rdr, err := lzma.NewReader(r)
|
||||||
return io.NopCloser(rdr), err
|
return io.NopCloser(rdr), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Lzma) Resetable() bool { return false }
|
|
||||||
|
|
||||||
func (l Lzma) Reset(old, src io.Reader) error { return ErrNotResetable }
|
|
||||||
|
|||||||
@@ -16,7 +16,3 @@ func (l Lzo) Reader(r io.Reader) (io.ReadCloser, error) {
|
|||||||
}
|
}
|
||||||
return io.NopCloser(bytes.NewReader(cache)), nil
|
return io.NopCloser(bytes.NewReader(cache)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Lzo) Resetable() bool { return false }
|
|
||||||
|
|
||||||
func (l Lzo) Reset(old, src io.Reader) error { return ErrNotResetable }
|
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ func (x Xz) Reader(r io.Reader) (io.ReadCloser, error) {
|
|||||||
return io.NopCloser(rdr), err
|
return io.NopCloser(rdr), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x Xz) Resetable() bool { return true }
|
|
||||||
|
|
||||||
func (x Xz) Reset(old, src io.Reader) error {
|
func (x Xz) Reset(old, src io.Reader) error {
|
||||||
return old.(*xz.Reader).Reset(src)
|
return old.(*xz.Reader).Reset(src)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,11 @@ func (z Zstd) Reader(src io.Reader) (io.ReadCloser, error) {
|
|||||||
return r.IOReadCloser(), err
|
return r.IOReadCloser(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z Zstd) Resetable() bool { return true }
|
|
||||||
|
|
||||||
func (z Zstd) Reset(old, src io.Reader) error {
|
func (z Zstd) Reset(old, src io.Reader) error {
|
||||||
return old.(*zstd.Decoder).Reset(src)
|
return old.(*zstd.Decoder).Reset(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z *Zstd) Decode(in []byte) (out []byte, err error) {
|
func (z Zstd) Decode(in []byte) ([]byte, error) {
|
||||||
if z.writeToReader == nil {
|
if z.writeToReader == nil {
|
||||||
z.writeToReader, _ = zstd.NewReader(nil)
|
z.writeToReader, _ = zstd.NewReader(nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func realSize(siz uint16) uint16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reader) advance() (err error) {
|
func (r *Reader) advance() (err error) {
|
||||||
if !r.d.Resetable() {
|
if _, ok := r.d.(decompress.Resetable); !ok {
|
||||||
if clr, ok := r.cur.(io.Closer); ok {
|
if clr, ok := r.cur.(io.Closer); ok {
|
||||||
clr.Close()
|
clr.Close()
|
||||||
}
|
}
|
||||||
@@ -39,14 +39,14 @@ func (r *Reader) advance() (err error) {
|
|||||||
size := realSize(raw)
|
size := realSize(raw)
|
||||||
r.cur = io.LimitReader(r.master, int64(size))
|
r.cur = io.LimitReader(r.master, int64(size))
|
||||||
if size == raw {
|
if size == raw {
|
||||||
if r.d.Resetable() {
|
if rs, ok := r.d.(decompress.Resetable); ok {
|
||||||
if r.comRdr == nil {
|
if r.comRdr == nil {
|
||||||
r.cur, err = r.d.Reader(r.cur)
|
r.cur, err = r.d.Reader(r.cur)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = r.d.Reset(r.comRdr, r.cur)
|
err = rs.Reset(r.comRdr, r.cur)
|
||||||
r.cur = r.comRdr
|
r.cur = r.comRdr
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package toreader
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type OffsetReader struct {
|
||||||
|
r io.ReaderAt
|
||||||
|
off int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOffsetReader(r io.ReaderAt, off int64) *OffsetReader {
|
||||||
|
return &OffsetReader{
|
||||||
|
r: r,
|
||||||
|
off: off,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r OffsetReader) ReadAt(p []byte, off int64) (n int, e error) {
|
||||||
|
return r.r.ReadAt(p, off+r.off)
|
||||||
|
}
|
||||||
@@ -6,7 +6,8 @@ type ReaderAt struct {
|
|||||||
d []byte
|
d []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReaderAt(r io.Reader) (ra ReaderAt, err error) {
|
func NewReaderAt(r io.Reader) (ra *ReaderAt, err error) {
|
||||||
|
ra = new(ReaderAt)
|
||||||
ra.d, err = io.ReadAll(r)
|
ra.d, err = io.ReadAll(r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,21 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/CalebQ42/fuse"
|
||||||
"github.com/CalebQ42/squashfs/internal/decompress"
|
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||||
"github.com/CalebQ42/squashfs/internal/directory"
|
"github.com/CalebQ42/squashfs/internal/directory"
|
||||||
"github.com/CalebQ42/squashfs/internal/inode"
|
"github.com/CalebQ42/squashfs/internal/inode"
|
||||||
"github.com/CalebQ42/squashfs/internal/metadata"
|
"github.com/CalebQ42/squashfs/internal/metadata"
|
||||||
"github.com/CalebQ42/squashfs/internal/toreader"
|
"github.com/CalebQ42/squashfs/internal/toreader"
|
||||||
|
fuse2 "github.com/seaweedfs/fuse"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
*FS
|
*FS
|
||||||
|
con *fuse.Conn
|
||||||
|
con2 *fuse2.Conn
|
||||||
|
mountDone chan struct{}
|
||||||
|
mount2Done chan struct{}
|
||||||
d decompress.Decompressor
|
d decompress.Decompressor
|
||||||
r io.ReaderAt
|
r io.ReaderAt
|
||||||
fragEntries []fragEntry
|
fragEntries []fragEntry
|
||||||
@@ -40,6 +46,10 @@ const (
|
|||||||
ZSTDCompression
|
ZSTDCompression
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewReaderAtOffset(r io.ReaderAt, off int64) (*Reader, error) {
|
||||||
|
return NewReader(toreader.NewOffsetReader(r, off))
|
||||||
|
}
|
||||||
|
|
||||||
// Creates a new squashfs.Reader from the given io.Reader. NOTE: All data from the io.Reader will be read and stored in memory.
|
// Creates a new squashfs.Reader from the given io.Reader. NOTE: All data from the io.Reader will be read and stored in memory.
|
||||||
func NewReaderFromReader(r io.Reader) (*Reader, error) {
|
func NewReaderFromReader(r io.Reader) (*Reader, error) {
|
||||||
rdr, err := toreader.NewReaderAt(r)
|
rdr, err := toreader.NewReaderAt(r)
|
||||||
|
|||||||
+53
-6
@@ -6,6 +6,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -146,6 +147,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 {
|
||||||
@@ -232,7 +247,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)
|
||||||
@@ -276,8 +292,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) {
|
||||||
@@ -302,8 +317,7 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
case f.IsSymlink():
|
||||||
} else if f.IsSymlink() {
|
|
||||||
symPath := f.SymlinkPath()
|
symPath := f.SymlinkPath()
|
||||||
if op.DereferenceSymlink {
|
if op.DereferenceSymlink {
|
||||||
fil := f.GetSymlinkFile()
|
fil := f.GetSymlinkFile()
|
||||||
@@ -350,7 +364,40 @@ func (f File) realExtract(folder string, op ExtractionOptions) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
case f.isDeviceOrFifo():
|
||||||
|
_, 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
|
||||||
|
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
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
+3
-4
@@ -73,7 +73,6 @@ func TestMisc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkRace(b *testing.B) {
|
func BenchmarkRace(b *testing.B) {
|
||||||
// tmpDir := b.TempDir()
|
|
||||||
tmpDir := "testing"
|
tmpDir := "testing"
|
||||||
fil, err := preTest(tmpDir)
|
fil, err := preTest(tmpDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -197,11 +196,11 @@ func TestFuse(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
con, err := rdr.Mount("testing/fuseTest")
|
err = rdr.Mount("testing/fuseTest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer con.Close()
|
defer rdr.Unmount()
|
||||||
<-con.Ready
|
rdr.MountWait()
|
||||||
t.Fatal("testing")
|
t.Fatal("testing")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user