|
- package mount
-
- import (
- "os"
- "syscall"
- "time"
-
- "github.com/hanwen/go-fuse/v2/fuse"
- )
-
- type Config struct {
- GID uint32 `json:"gid"`
- UID uint32 `json:"uid"`
- AttrTimeout time.Duration `json:"attrTimeout"`
- }
-
- type Vfs struct {
- fs Fs
- config Config
- }
-
- func translateError(err error) syscall.Errno {
- switch err {
- case nil:
- return 0
-
- case ErrNotExists, os.ErrNotExist:
- return syscall.ENOENT
-
- case ErrExists, os.ErrExist:
- return syscall.EEXIST
-
- case ErrPermission, os.ErrPermission:
- return syscall.EACCES
-
- case ErrNotEmpty:
- return syscall.ENOTEMPTY
-
- default:
- return syscall.EIO
- }
- }
-
- // get the Mode from a vfs Node
- func (v *Vfs) getMode(node FsEntry) uint32 {
- Mode := node.Mode().Perm()
- if node.IsDir() {
- Mode |= fuse.S_IFDIR
- } else {
- Mode |= fuse.S_IFREG
- }
- return uint32(Mode)
- }
-
- // fill in attr from node
- func (v *Vfs) fillAttr(node FsEntry, attr *fuse.Attr) {
- Size := uint64(node.Size())
- const BlockSize = 512
- Blocks := (Size + BlockSize - 1) / BlockSize
- attr.Owner.Gid = v.config.GID
- attr.Owner.Uid = v.config.UID
- attr.Mode = v.getMode(node)
- attr.Size = Size
- attr.Nlink = 1
- attr.Blocks = Blocks
- // attr.Blksize = BlockSize // not supported in freebsd/darwin, defaults to 4k if not set
-
- createTime := node.CreateTime()
- modTime := node.ModTime()
-
- attr.Atime = uint64(modTime.Unix())
- attr.Atimensec = uint32(modTime.Nanosecond())
-
- attr.Mtime = uint64(modTime.Unix())
- attr.Mtimensec = uint32(modTime.Nanosecond())
-
- attr.Ctime = uint64(createTime.Unix())
- attr.Ctimensec = uint32(createTime.Nanosecond())
- }
-
- func (v *Vfs) fillAttrOut(node FsEntry, out *fuse.AttrOut) {
- v.fillAttr(node, &out.Attr)
- out.SetTimeout(v.config.AttrTimeout)
- }
-
- func (v *Vfs) fillEntryOut(node FsEntry, out *fuse.EntryOut) {
- v.fillAttr(node, &out.Attr)
- out.SetAttrTimeout(v.config.AttrTimeout)
- out.SetEntryTimeout(v.config.AttrTimeout)
- }
|