Skip to content

Commit

Permalink
Add special-handling for Octal fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Apr 26, 2024
1 parent 0311a4e commit 3d903b0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
48 changes: 45 additions & 3 deletions internal/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import (
"fmt"
"os"
"path"
"reflect"
"strconv"
"strings"
"time"

"github.com/googlecloudplatform/gcsfuse/v2/internal/util"
"github.com/mitchellh/mapstructure"

flag "github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -134,6 +137,11 @@ type GCSConnectionConfig struct {
}

type OctalInt int

func (oi OctalInt) String() string {
return fmt.Sprintf("%o", oi)
}

type FileSystemConfig struct {
TempDir string `mapstructure:"temp-dir"`
RenameDirLimit int `mapstructure:"rename-dir-limit"`
Expand Down Expand Up @@ -173,6 +181,35 @@ type Config struct {
Monitoring MonitoringConfig
}

// DotSeparatedStringList is a string list with a mapstructure decode hook
// that decodes a dot separated string list.
type DotSeparatedStringList []string

// DotSeparatedStringListHookFunc returns a DecodeHookFunc that converts
// strings to string slices, when the target type is DotSeparatedStringList.
func octalIntHookFunc() mapstructure.DecodeHookFuncType {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}

if t != reflect.TypeOf(OctalInt(0)) {
return data, nil
}

val, err := strconv.ParseInt(data.(string), 8, 32)
if err != nil {
err = fmt.Errorf("parsing as octal: %w", err)
return 0, err
}
return val, nil
}
}

var Cfg Config

var flagNames = make([]string, 0, 100)
Expand Down Expand Up @@ -242,7 +279,12 @@ func ParseConfig() (Config, error) {
}
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
err = v.Unmarshal(&cfg)
err = v.Unmarshal(&cfg, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
mapstructure.TextUnmarshallerHookFunc(),
octalIntHookFunc(),
mapstructure.StringToTimeDurationHookFunc(), // default hook
mapstructure.StringToSliceHookFunc(","), // default hook
)))
if err != nil {
return cfg, err
}
Expand Down Expand Up @@ -316,8 +358,8 @@ func addFlags(flagSet *flag.FlagSet) {
addIntParam(flagSet, "file-system.rename-dir-limit", 0, "Allow rename a directory containing fewer descendants than this limit.")
addIntParam(flagSet, "file-system.gid", -1, "GID owner of all inodes.")
addIntParam(flagSet, "file-system.uid", -1, "UID owner of all inodes.")
addIntParam(flagSet, "file-system.file-mode", 0644, "Permission bits for files, in octal.")
addIntParam(flagSet, "file-system.dir-mode", 0755, "Permissions bits for directories, in octal.")
addStringParam(flagSet, "file-system.file-mode", "0644", "Permission bits for files, in octal.")
addStringParam(flagSet, "file-system.dir-mode", "0755", "Permissions bits for directories, in octal.")
addStringSliceParamP(flagSet, "file-system.mount-options", "o", []string{}, "Additional system-specific mount options. Multiple options can be passed as comma separated. For readonly, use -o ro")

// Write
Expand Down
16 changes: 16 additions & 0 deletions internal/cfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ func TestFlags(t *testing.T) {
return config
},
},
{
name: "Set file-mode without 0 prefix",
osArgs: []string{"/m", "--file-system.file-mode=721"},
updateFunc: func(config Config) Config {
config.FileSystem.FileMode = 0721
return config
},
},
{
name: "Set dir-mode",
osArgs: []string{"/m", "--file-system.dir-mode=0751"},
Expand All @@ -388,6 +396,14 @@ func TestFlags(t *testing.T) {
return config
},
},
{
name: "Set dir-mode without 0 prefix",
osArgs: []string{"/m", "--file-system.dir-mode=751"},
updateFunc: func(config Config) Config {
config.FileSystem.DirMode = 0751
return config
},
},
{
name: "Set mount-options with a single option",
osArgs: []string{"/m", "--file-system.mount-options=a"},
Expand Down
4 changes: 2 additions & 2 deletions internal/cfg/testdata/all_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ file-system:
rename-dir-limit: 123
gid: 24
uid: 52
file-mode: 0614 # Octal
dir-mode: 0725 # Octal
file-mode: "614" # Octal
dir-mode: "725" # Octal
mount-options:
- a
- b=c
Expand Down

0 comments on commit 3d903b0

Please sign in to comment.