feat: Allow file and directory creation modes to be configured

The defaults remain the same as before.
For now, the config options are global instead of per-user.
Note also that the BoltDB creation maintains the old default mode of 0640
since it's not really a user-facing filesystem manipulation.
Fixes #5316, #5200
This commit is contained in:
Vincent Lee
2025-07-20 18:21:52 -07:00
committed by Henrique Dias
parent 5b7ea9f95a
commit 21ad653b7e
12 changed files with 73 additions and 41 deletions

View File

@@ -49,6 +49,10 @@ func addConfigFlags(flags *pflag.FlagSet) {
flags.String("branding.files", "", "path to directory with images and custom styles")
flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links")
flags.Bool("branding.disableUsedPercentage", false, "disable used disk percentage graph")
// NB: these are string so they can be presented as octal in the help text
// as that's the conventional representation for modes in Unix.
flags.String("file-mode", fmt.Sprintf("%O", settings.DefaultFileMode), "Mode bits that new files are created with")
flags.String("dir-mode", fmt.Sprintf("%O", settings.DefaultDirMode), "Mode bits that new directories are created with")
}
//nolint:gocyclo
@@ -170,6 +174,8 @@ func printSettings(ser *settings.Server, set *settings.Settings, auther auth.Aut
fmt.Fprintf(w, "\tLocale:\t%s\n", set.Defaults.Locale)
fmt.Fprintf(w, "\tView mode:\t%s\n", set.Defaults.ViewMode)
fmt.Fprintf(w, "\tSingle Click:\t%t\n", set.Defaults.SingleClick)
fmt.Fprintf(w, "\tFile Creation Mode:\t%O\n", set.FileMode)
fmt.Fprintf(w, "\tDirectory Creation Mode:\t%O\n", set.DirMode)
fmt.Fprintf(w, "\tCommands:\t%s\n", strings.Join(set.Defaults.Commands, " "))
fmt.Fprintf(w, "\tSorting:\n")
fmt.Fprintf(w, "\t\tBy:\t%s\n", set.Defaults.Sorting.By)

View File

@@ -43,6 +43,8 @@ override the options.`,
Theme: mustGetString(flags, "branding.theme"),
Files: mustGetString(flags, "branding.files"),
},
FileMode: mustGetMode(flags, "file-mode"),
DirMode: mustGetMode(flags, "dir-mode"),
}
ser := &settings.Server{

View File

@@ -65,6 +65,10 @@ you want to change. Other options will remain unchanged.`,
set.Branding.DisableUsedPercentage = mustGetBool(flags, flag.Name)
case "branding.files":
set.Branding.Files = mustGetString(flags, flag.Name)
case "file-mode":
set.FileMode = mustGetMode(flags, flag.Name)
case "dir-mode":
set.DirMode = mustGetMode(flags, flag.Name)
}
})

View File

@@ -4,9 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/asdine/storm/v3"
@@ -14,12 +16,13 @@ import (
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"
"github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/storage/bolt"
)
const dbPerms = 0640
func checkErr(err error) {
if err != nil {
log.Fatal(err)
@@ -32,6 +35,13 @@ func mustGetString(flags *pflag.FlagSet, flag string) string {
return s
}
func mustGetMode(flags *pflag.FlagSet, flag string) fs.FileMode {
s := mustGetString(flags, flag)
b, err := strconv.ParseUint(s, 0, 32)
checkErr(err)
return fs.FileMode(b)
}
func mustGetBool(flags *pflag.FlagSet, flag string) bool {
b, err := flags.GetBool(flag)
checkErr(err)
@@ -106,7 +116,7 @@ func python(fn pythonFunc, cfg pythonConfig) cobraFunc {
log.Println("Using database: " + absPath)
data.hadDB = exists
db, err := storm.Open(path, storm.BoltOptions(files.PermFile, nil))
db, err := storm.Open(path, storm.BoltOptions(dbPerms, nil))
checkErr(err)
defer db.Close()
data.store, err = bolt.NewStorage(db)