88 "fmt"
99 "log"
1010 "os"
11+ "os/exec"
12+ "strings"
1113 "time"
1214
1315 "github.com/hanwen/go-fuse/v2/fs"
@@ -60,6 +62,7 @@ type BlobFsSystemOpts struct {
6062 Verbose bool
6163 Metadata * BlobCacheMetadata
6264 Config BlobCacheConfig
65+ Client * BlobCacheClient
6366}
6467
6568type BlobFs struct {
@@ -82,37 +85,42 @@ func Mount(ctx context.Context, opts BlobFsSystemOpts) (func() error, <-chan err
8285 }
8386
8487 Logger .Info ("Mount point directory created." )
88+ } else if isFuseMount (mountPoint ) {
89+ if err := forceUnmount (mountPoint ); err != nil {
90+ return nil , nil , fmt .Errorf ("failed to unmount existing FUSE mount: %v" , err )
91+ }
8592 }
8693
8794 blobfs , err := NewFileSystem (ctx , opts )
8895 if err != nil {
8996 return nil , nil , fmt .Errorf ("could not create filesystem: %v" , err )
9097 }
9198
92- for _ , sourceConfig := range opts .Config .BlobFs .Sources {
93- _ , err := NewSource (sourceConfig )
94- if err != nil {
95- Logger .Errorf ("Failed to configure content source: %+v\n " , err )
96- continue
97- }
98-
99- Logger .Infof ("Configured and mounted source: %+v\n " , sourceConfig .FilesystemName )
100- }
101-
10299 root , _ := blobfs .Root ()
103100 attrTimeout := time .Second * 60
104101 entryTimeout := time .Second * 60
105102 fsOptions := & fs.Options {
106103 AttrTimeout : & attrTimeout ,
107104 EntryTimeout : & entryTimeout ,
108105 }
106+
107+ maxReadAheadKB := opts .Config .BlobFs .MaxReadAheadKB
108+ if maxReadAheadKB <= 0 {
109+ maxReadAheadKB = 128
110+ }
111+
112+ maxBackgroundTasks := opts .Config .BlobFs .MaxBackgroundTasks
113+ if maxBackgroundTasks <= 0 {
114+ maxBackgroundTasks = 512
115+ }
116+
109117 server , err := fuse .NewServer (fs .NewNodeFS (root , fsOptions ), mountPoint , & fuse.MountOptions {
110- MaxBackground : 512 ,
118+ MaxBackground : maxBackgroundTasks ,
111119 DisableXAttrs : true ,
112120 EnableSymlinkCaching : true ,
113121 SyncRead : false ,
114122 RememberInodes : true ,
115- MaxReadAhead : 1 << 17 ,
123+ MaxReadAhead : maxReadAheadKB * 1024 ,
116124 })
117125 if err != nil {
118126 return nil , nil , fmt .Errorf ("could not create server: %v" , err )
@@ -142,17 +150,12 @@ func Mount(ctx context.Context, opts BlobFsSystemOpts) (func() error, <-chan err
142150func NewFileSystem (ctx context.Context , opts BlobFsSystemOpts ) (* BlobFs , error ) {
143151 metadata := opts .Metadata
144152
145- client , err := NewBlobCacheClient (ctx , opts .Config )
146- if err != nil {
147- return nil , err
148- }
149-
150153 bfs := & BlobFs {
151- verbose : opts .Verbose ,
152154 ctx : ctx ,
155+ verbose : opts .Verbose ,
153156 Config : opts .Config ,
157+ Client : opts .Client ,
154158 Metadata : metadata ,
155- Client : client ,
156159 }
157160
158161 rootID := GenerateFsID ("/" )
@@ -199,3 +202,20 @@ func (bfs *BlobFs) Root() (fs.InodeEmbedder, error) {
199202 }
200203 return bfs .root , nil
201204}
205+
206+ func isFuseMount (mountPoint string ) bool {
207+ cmd := exec .Command ("findmnt" , "-n" , "-o" , "FSTYPE" , mountPoint )
208+ output , err := cmd .Output ()
209+ if err != nil {
210+ return false
211+ }
212+ return strings .Contains (string (output ), "fuse" )
213+ }
214+
215+ func forceUnmount (mountPoint string ) error {
216+ cmd := exec .Command ("fusermount" , "-uz" , mountPoint )
217+ if _ , err := cmd .CombinedOutput (); err != nil {
218+ return err
219+ }
220+ return nil
221+ }
0 commit comments