Skip to content

Commit d70ba19

Browse files
author
JojiiOfficial
committed
implement fuse functions
improve mounter
1 parent a7a37ed commit d70ba19

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

dmfs/dmfs.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
11
package dmfs
22

33
import (
4+
"context"
5+
"fmt"
6+
"syscall"
7+
8+
"github.com/DataManager-Go/libdatamanager"
9+
dmConfig "github.com/DataManager-Go/libdatamanager/config"
410
"github.com/hanwen/go-fuse/v2/fs"
511
)
612

713
type dmanagerFilesystem struct {
814
fs.Inode
15+
16+
mounter *Mounter
17+
config *dmConfig.Config
18+
libdm *libdatamanager.LibDM
19+
}
20+
21+
// Ensure that we implement NodeOnAdder
22+
var _ = (fs.NodeOnAdder)((*dmanagerFilesystem)(nil))
23+
var _ = (fs.NodeRenamer)((*dmanagerFilesystem)(nil))
24+
var _ = (fs.NodeUnlinker)((*dmanagerFilesystem)(nil))
25+
26+
// OnAdd is called on mounting the file system. Use it to populate
27+
// the file system tree.
28+
func (root *dmanagerFilesystem) OnAdd(ctx context.Context) {
29+
root.initFiles()
30+
}
31+
32+
// Unlink if virtual file was unlinked
33+
func (root *dmanagerFilesystem) Unlink(ctx context.Context, name string) syscall.Errno {
34+
fmt.Println("rm", name)
35+
return 0
36+
}
37+
38+
// Rename if virtual file was renamed
39+
func (root *dmanagerFilesystem) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
40+
fmt.Println("renome")
41+
return 0
42+
}
43+
44+
func (root *dmanagerFilesystem) debug(arg ...interface{}) {
45+
if root.mounter.Debug {
46+
fmt.Println(arg...)
47+
}
948
}

dmfs/initFiles.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dmfs
2+
3+
func (root *dmanagerFilesystem) initFiles() {
4+
root.debug("Init files")
5+
}

dmfs/mount.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ import (
1313
"github.com/hanwen/go-fuse/v2/fuse"
1414
)
1515

16-
// MountOptions options to mount
17-
type MountOptions struct {
16+
// Mounter options to mount
17+
type Mounter struct {
1818
MountPoint string
1919
Config *dmConfig.Config
2020
Libdm *libdatamanager.LibDM
2121
Debug bool
2222
DebugFS bool
23+
24+
server *fuse.Server
25+
doneChan chan bool
2326
}
2427

2528
// Mount the fs
26-
func (mopt *MountOptions) Mount() {
27-
mountDir := filepath.Clean(mopt.MountPoint)
29+
func (mounter *Mounter) Mount() {
30+
mountDir := filepath.Clean(mounter.MountPoint)
2831
fmt.Printf("Mounting on %s\n", mountDir)
2932

3033
// Create mount dir if not exists
@@ -33,52 +36,65 @@ func (mopt *MountOptions) Mount() {
3336
}
3437

3538
// Test server availability
36-
if !mopt.testServer() {
39+
if !mounter.testServer() {
3740
return
3841
}
3942

40-
root := &dmanagerFilesystem{}
43+
// Init exit channels
44+
exitChan := make(chan bool, 1)
45+
mounter.doneChan = make(chan bool, 1)
46+
47+
// Create the fs
48+
root := &dmanagerFilesystem{
49+
mounter: mounter,
50+
config: mounter.Config,
51+
libdm: mounter.Libdm,
52+
}
53+
54+
var err error
4155

4256
// Mount fs
43-
server, err := fs.Mount(mountDir, root, mopt.getMountOptions())
57+
mounter.server, err = fs.Mount(mountDir, root, mounter.getMountOptions())
4458
if err != nil {
4559
log.Fatal(err)
4660
}
4761

4862
// Umount fs on interrupt
49-
exitChan := make(chan bool, 1)
50-
doneChan := make(chan bool, 1)
5163
sigChan := make(chan os.Signal, 1)
5264
go (func() {
5365
signal.Notify(sigChan, os.Interrupt, os.Kill)
66+
5467
// Await signal
5568
sig := <-sigChan
5669

5770
// Debug & Umount
5871
fmt.Println("\rReceived", sig) // Print \r to overwrite the ugly ^C
5972

6073
exitChan <- true
61-
62-
server.Unmount()
63-
64-
fmt.Println("Umounted")
65-
doneChan <- true
74+
mounter.umount()
6675
})()
6776

6877
// Exit if mountpoint was
6978
// unmounted or process was interrupted
70-
server.Wait()
79+
mounter.server.Wait()
7180
select {
7281
case <-exitChan:
73-
<-doneChan
82+
<-mounter.doneChan
7483
default:
7584
fmt.Println("umounted externally\nexiting")
7685
}
7786
}
7887

88+
// Umount fs
89+
func (mounter *Mounter) umount() {
90+
mounter.server.Unmount()
91+
fmt.Println("Umounted")
92+
mounter.doneChan <- true
93+
}
94+
7995
// tests if server can be accessed and user is authorized
80-
func (mopt *MountOptions) testServer() bool {
81-
_, err := mopt.Libdm.GetNamespaces()
96+
func (mounter *Mounter) testServer() bool {
97+
_, err := mounter.Libdm.GetNamespaces()
8298
if err != nil {
8399
fmt.Println("Can't mount:", err)
84100
return false
@@ -88,10 +104,10 @@ func (mopt *MountOptions) testServer() bool {
88104
}
89105

90106
// Get the mountoptions for the mount operation
91-
func (mopt *MountOptions) getMountOptions() *fs.Options {
107+
func (mounter *Mounter) getMountOptions() *fs.Options {
92108
return &fs.Options{
93109
MountOptions: fuse.MountOptions{
94-
Debug: mopt.DebugFS,
110+
Debug: mounter.DebugFS,
95111
AllowOther: false,
96112
FsName: "Datamanager mount",
97113
Name: "dmanager",

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func main() {
5959
}
6060

6161
// Create mount options
62-
options := dmfs.MountOptions{
62+
options := dmfs.Mounter{
6363
Libdm: libdatamanager.NewLibDM(config.MustGetRequestConfig()),
6464
MountPoint: *appMountpoint,
6565
Config: config,

0 commit comments

Comments
 (0)