Compare commits

...

3 Commits

Author SHA1 Message Date
Caleb Gardner 658e5c9e0b Mount is non-blocking again 2023-01-04 06:01:12 -06:00
Caleb Gardner f2d86aff96 Fixed a race condition with mounts that caused them to fail 2023-01-04 05:41:43 -06:00
Caleb Gardner f61237a1f0 Added ReaderAtOffset 2022-12-22 02:00:42 -06:00
5 changed files with 61 additions and 9 deletions
+30 -5
View File
@@ -3,6 +3,7 @@ package squashfs
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"io" "io"
"github.com/CalebQ42/fuse" "github.com/CalebQ42/fuse"
@@ -10,17 +11,41 @@ import (
"github.com/CalebQ42/squashfs/internal/inode" "github.com/CalebQ42/squashfs/internal/inode"
) )
// Creates a fuse mount, then mounts the archive on a seperate goroutine. // Mounts the archive to the given mountpoint using fuse3. Non-blocking.
// If waiting for the mount to end, simply do <-con.Ready. // 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) (con *fuse.Conn, err error) { func (r *Reader) Mount(mountpoint string) (err error) {
con, err = fuse.Mount(mountpoint, fuse.ReadOnly()) 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
} }
go 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
} }
+19
View File
@@ -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)
}
+2 -1
View File
@@ -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
View File
@@ -7,6 +7,7 @@ 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"
@@ -16,6 +17,8 @@ import (
type Reader struct { type Reader struct {
*FS *FS
con *fuse.Conn
mountDone chan struct{}
d decompress.Decompressor d decompress.Decompressor
r io.ReaderAt r io.ReaderAt
fragEntries []fragEntry fragEntries []fragEntry
@@ -40,6 +43,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)
+3 -3
View File
@@ -196,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")
} }