Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f2d86aff96 | |||
| f61237a1f0 |
@@ -3,6 +3,7 @@ package squashfs
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/CalebQ42/fuse"
|
||||
@@ -10,17 +11,38 @@ import (
|
||||
"github.com/CalebQ42/squashfs/internal/inode"
|
||||
)
|
||||
|
||||
// Creates a fuse mount, then mounts the archive on a seperate goroutine.
|
||||
// If waiting for the mount to end, simply do <-con.Ready.
|
||||
func (r *Reader) Mount(mountpoint string) (con *fuse.Conn, err error) {
|
||||
con, err = fuse.Mount(mountpoint, fuse.ReadOnly())
|
||||
// Mounts the archive to the given mountpoint using fuse3.
|
||||
// Blocks until the arhive is unmounted.
|
||||
// Hightly suggested to run in a goroutine.
|
||||
// Will take a moment before MountWait and Unmount will work correctly.
|
||||
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 {
|
||||
return
|
||||
}
|
||||
go fs.Serve(con, &squashFuse{r: r})
|
||||
err = fs.Serve(r.con, &squashFuse{r: r})
|
||||
return
|
||||
}
|
||||
|
||||
// Blocks until the mount ends.
|
||||
func (r *Reader) MountWait() {
|
||||
if r.con != nil {
|
||||
<-r.con.Ready
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
r *Reader
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/CalebQ42/fuse"
|
||||
"github.com/CalebQ42/squashfs/internal/decompress"
|
||||
"github.com/CalebQ42/squashfs/internal/directory"
|
||||
"github.com/CalebQ42/squashfs/internal/inode"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
|
||||
type Reader struct {
|
||||
*FS
|
||||
con *fuse.Conn
|
||||
d decompress.Decompressor
|
||||
r io.ReaderAt
|
||||
fragEntries []fragEntry
|
||||
@@ -40,6 +42,10 @@ const (
|
||||
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.
|
||||
func NewReaderFromReader(r io.Reader) (*Reader, error) {
|
||||
rdr, err := toreader.NewReaderAt(r)
|
||||
|
||||
+3
-3
@@ -196,11 +196,11 @@ func TestFuse(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
con, err := rdr.Mount("testing/fuseTest")
|
||||
err = rdr.Mount("testing/fuseTest")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer con.Close()
|
||||
<-con.Ready
|
||||
defer rdr.Unmount()
|
||||
rdr.MountWait()
|
||||
t.Fatal("testing")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user