aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/sharefs/fuse.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-12-27 22:09:55 +0900
committerOphestra <cat@gensokyo.uk>2025-12-27 22:14:33 +0900
commite7982b4ee9cc935d6b89f291087524cdee229d09 (patch)
tree2a6a20431cd18c2c0ca73c0a9a454734e5866a5e /cmd/sharefs/fuse.go
parentef1ebf12d98359c0d75ce48a1b05737f7e4e93c0 (diff)
cmd/sharefs: create directory as root
This optional behaviour is required on NixOS as it is otherwise impossible to set this up: systemd.mounts breaks startup order somehow even though my unit looks identical to generated ones, fileSystems does not support any kind of initialisation or ordering other than against other mount points. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd/sharefs/fuse.go')
-rw-r--r--cmd/sharefs/fuse.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/cmd/sharefs/fuse.go b/cmd/sharefs/fuse.go
index bbbc1446..9a0a0149 100644
--- a/cmd/sharefs/fuse.go
+++ b/cmd/sharefs/fuse.go
@@ -53,6 +53,9 @@ type (
// Whether sharefs_init failed.
initFailed bool
+ // Whether to create source directory as root.
+ mkdir bool
+
// Open file descriptor to fuse.
Fuse int
@@ -157,6 +160,9 @@ func parseOpts(args *fuseArgs, setup *setupState, log *log.Logger) (ok bool) {
// Pathname to writable source directory.
source *C.char
+ // Whether to create source directory as root.
+ mkdir C.int
+
// Decimal string representation of uid to set when running as root.
setuid *C.char
// Decimal string representation of gid to set when running as root.
@@ -169,6 +175,7 @@ func parseOpts(args *fuseArgs, setup *setupState, log *log.Logger) (ok bool) {
if C.fuse_opt_parse(args, unsafe.Pointer(&unsafeOpts), &[]C.struct_fuse_opt{
{templ: C.CString("source=%s"), offset: C.ulong(unsafe.Offsetof(unsafeOpts.source)), value: 0},
+ {templ: C.CString("mkdir"), offset: C.ulong(unsafe.Offsetof(unsafeOpts.mkdir)), value: 1},
{templ: C.CString("setuid=%s"), offset: C.ulong(unsafe.Offsetof(unsafeOpts.setuid)), value: 0},
{templ: C.CString("setgid=%s"), offset: C.ulong(unsafe.Offsetof(unsafeOpts.setgid)), value: 0},
@@ -223,6 +230,7 @@ func parseOpts(args *fuseArgs, setup *setupState, log *log.Logger) (ok bool) {
} else {
setup.Source = a
}
+ setup.mkdir = unsafeOpts.mkdir != 0
if unsafeOpts.setuid == nil {
setup.Setuid = -1
@@ -339,6 +347,9 @@ func _main(s ...string) (exitCode int) {
} else if setup.Fuse < 3 && (setup.Setuid > 0 || setup.Setgid > 0) {
log.Println("setuid and setgid has no effect when not starting as root")
return 1
+ } else if setup.mkdir {
+ log.Println("mkdir has no effect when not starting as root")
+ return 1
}
op := C.struct_fuse_operations{
@@ -387,6 +398,20 @@ func _main(s ...string) (exitCode int) {
log.Println("setuid and setgid must not be 0")
return 5
}
+
+ if setup.mkdir {
+ if err := os.MkdirAll(setup.Source.String(), 0700); err != nil {
+ if !errors.Is(err, os.ErrExist) {
+ log.Println(err)
+ return 5
+ }
+ // skip setup for existing source directory
+ } else if err = os.Chown(setup.Source.String(), setup.Setuid, setup.Setgid); err != nil {
+ log.Println(err)
+ return 5
+ }
+ }
+
if err := syscall.Setresgid(setup.Setgid, setup.Setgid, setup.Setgid); err != nil {
log.Printf("cannot set gid: %v", err)
return 5