Skip to content

Commit d516230

Browse files
committed
Make fusermount optional and --allow-other
1 parent e943952 commit d516230

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

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: 20 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,19 @@ func NewFilesystem(auth *graph.Auth, cacheDir string) *Filesystem {
103115

104116
// ok, ready to start fs
105117
fs := &Filesystem{
118+
// default: whatever user is running the filesystem is the owner
119+
uid: uint32(os.Getuid()),
120+
gid: uint32(os.Getgid()),
121+
106122
RawFileSystem: fuse.NewDefaultRawFileSystem(),
107123
content: content,
108124
db: db,
109125
auth: auth,
110126
opendirs: make(map[uint64][]*Inode),
111127
}
128+
for _, opt := range opts {
129+
opt(fs)
130+
}
112131

113132
rootItem, err := graph.GetItem("root", auth)
114133
root := NewInodeDriveItem(rootItem)

fs/fs.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ 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+
Owner: fuse.Owner{
131+
Uid: f.uid,
132+
Gid: f.gid,
133+
},
134+
}
135+
}
136+
118137
// Statfs returns information about the filesystem. Mainly useful for checking
119138
// quotas and storage limits.
120139
func (f *Filesystem) StatFs(cancel <-chan struct{}, in *fuse.InHeader, out *fuse.StatfsOut) fuse.Status {
@@ -180,7 +199,7 @@ func (f *Filesystem) Mkdir(cancel <-chan struct{}, in *fuse.MkdirIn, name string
180199
newInode.mode = in.Mode | fuse.S_IFDIR
181200

182201
out.NodeId = f.InsertChild(id, newInode)
183-
out.Attr = newInode.makeAttr()
202+
out.Attr = f.makeAttr(newInode)
184203
out.SetAttrTimeout(timeout)
185204
out.SetEntryTimeout(timeout)
186205
return fuse.OK
@@ -305,7 +324,7 @@ func (f *Filesystem) ReadDirPlus(cancel <-chan struct{}, in *fuse.ReadIn, out *f
305324
return fuse.EIO
306325
}
307326
entryOut.NodeId = entry.Ino
308-
entryOut.Attr = inode.makeAttr()
327+
entryOut.Attr = f.makeAttr(inode)
309328
entryOut.SetAttrTimeout(timeout)
310329
entryOut.SetEntryTimeout(timeout)
311330
return fuse.OK
@@ -369,7 +388,7 @@ func (f *Filesystem) Lookup(cancel <-chan struct{}, in *fuse.InHeader, name stri
369388
}
370389

371390
out.NodeId = child.NodeID()
372-
out.Attr = child.makeAttr()
391+
out.Attr = f.makeAttr(child)
373392
out.SetAttrTimeout(timeout)
374393
out.SetEntryTimeout(timeout)
375394
return fuse.OK
@@ -412,7 +431,7 @@ func (f *Filesystem) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string
412431
Str("mode", Octal(in.Mode)).
413432
Msg("Creating inode.")
414433
out.NodeId = f.InsertChild(parentID, inode)
415-
out.Attr = inode.makeAttr()
434+
out.Attr = f.makeAttr(inode)
416435
out.SetAttrTimeout(timeout)
417436
out.SetEntryTimeout(timeout)
418437
return fuse.OK
@@ -721,7 +740,7 @@ func (f *Filesystem) GetAttr(cancel <-chan struct{}, in *fuse.GetAttrIn, out *fu
721740
Str("path", inode.Path()).
722741
Msg("")
723742

724-
out.Attr = inode.makeAttr()
743+
out.Attr = f.makeAttr(inode)
725744
out.SetTimeout(timeout)
726745
return fuse.OK
727746
}
@@ -784,7 +803,7 @@ func (f *Filesystem) SetAttr(cancel <-chan struct{}, in *fuse.SetAttrIn, out *fu
784803
}
785804

786805
i.Unlock()
787-
out.Attr = i.makeAttr()
806+
out.Attr = f.makeAttr(i)
788807
out.SetTimeout(timeout)
789808
return fuse.OK
790809
}

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)