Skip to content

Commit 8af008f

Browse files
committed
Make fusermount optional and --allow-other
1 parent 2bd7b3c commit 8af008f

4 files changed

Lines changed: 52 additions & 29 deletions

File tree

cmd/onedriver/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func main() {
5858
versionFlag := flag.BoolP("version", "v", false, "Display program version.")
5959
debugOn := flag.BoolP("debug", "d", false, "Enable FUSE debug logging. "+
6060
"This logs communication between onedriver and the kernel.")
61+
allowOther := flag.BoolP("allow-other", "", false,
62+
"Allow access to the mount point for other users.")
63+
uid := flag.Uint32P("uid", "", uint32(os.Getuid()), "Owner uid of the mount point.")
64+
gid := flag.Uint32P("gid", "", uint32(os.Getgid()), "Owner gid of the mount point.")
6165
help := flag.BoolP("help", "h", false, "Displays this help message.")
6266
flag.Usage = usage
6367
flag.Parse()
@@ -123,13 +127,15 @@ func main() {
123127
// create the filesystem
124128
log.Info().Msgf("onedriver %s", common.Version())
125129
auth := graph.Authenticate(config.AuthConfig, authPath, *headless)
126-
filesystem := fs.NewFilesystem(auth, cachePath)
130+
filesystem := fs.NewFilesystem(auth, cachePath, fs.OptionOwner(*uid, *gid))
127131
go filesystem.DeltaLoop(10 * time.Minute)
128132
xdgVolumeInfo(filesystem, auth)
129133

130134
server, err := fuse.NewServer(filesystem, mountpoint, &fuse.MountOptions{
131135
Name: "onedriver",
132136
FsName: "onedriver",
137+
DirectMount: true,
138+
AllowOther: *allowOther,
133139
DisableXAttrs: true,
134140
MaxBackground: 1024,
135141
Debug: *debugOn,

fs/cache.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
type Filesystem struct {
2222
fuse.RawFileSystem
2323

24+
uid uint32
25+
gid uint32
26+
2427
metadata sync.Map
2528
db *bolt.DB
2629
content *LoopbackCache
@@ -51,8 +54,17 @@ var (
5154
// so we can tell what format the db has
5255
const fsVersion = "1"
5356

57+
type Option func(fs *Filesystem)
58+
59+
func OptionOwner(uid, gid uint32) Option {
60+
return func(fs *Filesystem) {
61+
fs.uid = uid
62+
fs.gid = gid
63+
}
64+
}
65+
5466
// NewFilesystem creates a new filesystem
55-
func NewFilesystem(auth *graph.Auth, cacheDir string) *Filesystem {
67+
func NewFilesystem(auth *graph.Auth, cacheDir string, opts ...Option) *Filesystem {
5668
// prepare cache directory
5769
if _, err := os.Stat(cacheDir); err != nil {
5870
if err = os.Mkdir(cacheDir, 0700); err != nil {
@@ -103,12 +115,18 @@ func NewFilesystem(auth *graph.Auth, cacheDir string) *Filesystem {
103115

104116
// ok, ready to start fs
105117
fs := &Filesystem{
118+
uid: uint32(os.Getuid()),
119+
gid: uint32(os.Getgid()),
120+
106121
RawFileSystem: fuse.NewDefaultRawFileSystem(),
107122
content: content,
108123
db: db,
109124
auth: auth,
110125
opendirs: make(map[uint64][]*Inode),
111126
}
127+
for _, opt := range opts {
128+
opt(fs)
129+
}
112130

113131
rootItem, err := graph.GetItem("root", auth)
114132
root := NewInodeDriveItem(rootItem)

fs/fs.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ func isNameRestricted(name string) bool {
115115
return disallowedRexp.FindStringIndex(name) != nil
116116
}
117117

118+
// makeattr is a convenience function to create a set of filesystem attrs for
119+
// use with syscalls that use or modify attrs.
120+
func (f *Filesystem) makeAttr(i *Inode) fuse.Attr {
121+
mtime := i.ModTime()
122+
return fuse.Attr{
123+
Ino: i.NodeID(),
124+
Size: i.Size(),
125+
Nlink: i.NLink(),
126+
Ctime: mtime,
127+
Mtime: mtime,
128+
Atime: mtime,
129+
Mode: i.Mode(),
130+
// whatever user is running the filesystem is the owner
131+
Owner: fuse.Owner{
132+
Uid: f.uid,
133+
Gid: f.gid,
134+
},
135+
}
136+
}
137+
118138
// Statfs returns information about the filesystem. Mainly useful for checking
119139
// quotas and storage limits.
120140
func (f *Filesystem) StatFs(cancel <-chan struct{}, in *fuse.InHeader, out *fuse.StatfsOut) fuse.Status {
@@ -180,7 +200,7 @@ func (f *Filesystem) Mkdir(cancel <-chan struct{}, in *fuse.MkdirIn, name string
180200
newInode.mode = in.Mode | fuse.S_IFDIR
181201

182202
out.NodeId = f.InsertChild(id, newInode)
183-
out.Attr = newInode.makeAttr()
203+
out.Attr = f.makeAttr(newInode)
184204
out.SetAttrTimeout(timeout)
185205
out.SetEntryTimeout(timeout)
186206
return fuse.OK
@@ -305,7 +325,7 @@ func (f *Filesystem) ReadDirPlus(cancel <-chan struct{}, in *fuse.ReadIn, out *f
305325
return fuse.EIO
306326
}
307327
entryOut.NodeId = entry.Ino
308-
entryOut.Attr = inode.makeAttr()
328+
entryOut.Attr = f.makeAttr(inode)
309329
entryOut.SetAttrTimeout(timeout)
310330
entryOut.SetEntryTimeout(timeout)
311331
return fuse.OK
@@ -369,7 +389,7 @@ func (f *Filesystem) Lookup(cancel <-chan struct{}, in *fuse.InHeader, name stri
369389
}
370390

371391
out.NodeId = child.NodeID()
372-
out.Attr = child.makeAttr()
392+
out.Attr = f.makeAttr(child)
373393
out.SetAttrTimeout(timeout)
374394
out.SetEntryTimeout(timeout)
375395
return fuse.OK
@@ -412,7 +432,7 @@ func (f *Filesystem) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string
412432
Str("mode", Octal(in.Mode)).
413433
Msg("Creating inode.")
414434
out.NodeId = f.InsertChild(parentID, inode)
415-
out.Attr = inode.makeAttr()
435+
out.Attr = f.makeAttr(inode)
416436
out.SetAttrTimeout(timeout)
417437
out.SetEntryTimeout(timeout)
418438
return fuse.OK
@@ -721,7 +741,7 @@ func (f *Filesystem) GetAttr(cancel <-chan struct{}, in *fuse.GetAttrIn, out *fu
721741
Str("path", inode.Path()).
722742
Msg("")
723743

724-
out.Attr = inode.makeAttr()
744+
out.Attr = f.makeAttr(inode)
725745
out.SetTimeout(timeout)
726746
return fuse.OK
727747
}
@@ -784,7 +804,7 @@ func (f *Filesystem) SetAttr(cancel <-chan struct{}, in *fuse.SetAttrIn, out *fu
784804
}
785805

786806
i.Unlock()
787-
out.Attr = i.makeAttr()
807+
out.Attr = f.makeAttr(i)
788808
out.SetTimeout(timeout)
789809
return fuse.OK
790810
}

fs/inode.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fs
33
import (
44
"encoding/json"
55
"math/rand"
6-
"os"
76
"strconv"
87
"strings"
98
"sync"
@@ -211,26 +210,6 @@ func (i *Inode) HasChildren() bool {
211210
return len(i.children) > 0
212211
}
213212

214-
// makeattr is a convenience function to create a set of filesystem attrs for
215-
// use with syscalls that use or modify attrs.
216-
func (i *Inode) makeAttr() fuse.Attr {
217-
mtime := i.ModTime()
218-
return fuse.Attr{
219-
Ino: i.NodeID(),
220-
Size: i.Size(),
221-
Nlink: i.NLink(),
222-
Ctime: mtime,
223-
Mtime: mtime,
224-
Atime: mtime,
225-
Mode: i.Mode(),
226-
// whatever user is running the filesystem is the owner
227-
Owner: fuse.Owner{
228-
Uid: uint32(os.Getuid()),
229-
Gid: uint32(os.Getgid()),
230-
},
231-
}
232-
}
233-
234213
// IsDir returns if it is a directory (true) or file (false).
235214
func (i *Inode) IsDir() bool {
236215
// 0 if the dir bit is not set

0 commit comments

Comments
 (0)