Fixed a race condition with mounts that caused them to fail

This commit is contained in:
Caleb Gardner
2023-01-04 05:41:43 -06:00
parent f61237a1f0
commit f2d86aff96
3 changed files with 32 additions and 8 deletions
+27 -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,38 @@ 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.
// If waiting for the mount to end, simply do <-con.Ready. // Blocks until the arhive is unmounted.
func (r *Reader) Mount(mountpoint string) (con *fuse.Conn, err error) { // Hightly suggested to run in a goroutine.
con, err = fuse.Mount(mountpoint, fuse.ReadOnly()) // 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 { if err != nil {
return return
} }
go fs.Serve(con, &squashFuse{r: r}) err = fs.Serve(r.con, &squashFuse{r: r})
return 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 { type squashFuse struct {
r *Reader r *Reader
} }
+2
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,7 @@ import (
type Reader struct { type Reader struct {
*FS *FS
con *fuse.Conn
d decompress.Decompressor d decompress.Decompressor
r io.ReaderAt r io.ReaderAt
fragEntries []fragEntry fragEntries []fragEntry
+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")
} }