Initial release of bdrclone
This commit is contained in:
37
internal/mount/mount.go
Normal file
37
internal/mount/mount.go
Normal file
@@ -0,0 +1,37 @@
|
||||
//go:build linux
|
||||
|
||||
package mount
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"bazil.org/fuse"
|
||||
"bazil.org/fuse/fs"
|
||||
"gitea.dddbg.com/youbin/bdrclone/internal/baidu"
|
||||
)
|
||||
|
||||
func Mount(ctx context.Context, client *baidu.Client, mountpoint string, options Options) error {
|
||||
if options.Name == "" {
|
||||
options.Name = "bdrclone"
|
||||
}
|
||||
mountOptions := []fuse.MountOption{fuse.FSName(options.Name), fuse.Subtype("bdrclone")}
|
||||
if options.ReadOnly {
|
||||
mountOptions = append(mountOptions, fuse.ReadOnly())
|
||||
}
|
||||
conn, err := fuse.Mount(mountpoint, mountOptions...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mount %s: %w", mountpoint, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
server := fs.New(conn, &fs.Config{})
|
||||
serveErr := make(chan error, 1)
|
||||
go func() { serveErr <- server.Serve(New(client, options.ReadOnly)) }()
|
||||
select {
|
||||
case err := <-serveErr:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
_ = fuse.Unmount(mountpoint)
|
||||
return <-serveErr
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user