From 73416072da8d3e210ac42cc1ec2d0070916323eb Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 14 Nov 2023 17:47:19 +0100 Subject: [PATCH] store: drop rootless from arguments drop the rootless argument from DefaultStoreOptions and UpdateStoreOptions since this can be retrieved internally through the unshare package. Signed-off-by: Giuseppe Scrivano --- cmd/containers-storage/main.go | 2 +- pkg/chunked/storage_linux.go | 2 +- store.go | 4 ++-- types/options.go | 27 +++++++++++---------------- types/utils.go | 5 +++-- types/utils_test.go | 4 ++-- utils.go | 9 ++------- 7 files changed, 22 insertions(+), 31 deletions(-) diff --git a/cmd/containers-storage/main.go b/cmd/containers-storage/main.go index 37e221010e..9b1547cd72 100644 --- a/cmd/containers-storage/main.go +++ b/cmd/containers-storage/main.go @@ -73,7 +73,7 @@ func main() { os.Exit(1) } if options.GraphRoot == "" && options.RunRoot == "" && options.GraphDriverName == "" && len(options.GraphDriverOptions) == 0 { - options, _ = types.DefaultStoreOptionsAutoDetectUID() + options, _ = types.DefaultStoreOptions() } args := flags.Args() if len(args) < 1 { diff --git a/pkg/chunked/storage_linux.go b/pkg/chunked/storage_linux.go index 8493a2c19a..fe216a2fff 100644 --- a/pkg/chunked/storage_linux.go +++ b/pkg/chunked/storage_linux.go @@ -254,7 +254,7 @@ func convertTarToZstdChunked(destDirectory string, blobSize int64, iss ImageSour // GetDiffer returns a differ than can be used with ApplyDiffWithDiffer. func GetDiffer(ctx context.Context, store storage.Store, blobSize int64, annotations map[string]string, iss ImageSourceSeekable) (graphdriver.Differ, error) { - storeOpts, err := types.DefaultStoreOptionsAutoDetectUID() + storeOpts, err := types.DefaultStoreOptions() if err != nil { return nil, err } diff --git a/store.go b/store.go index 6753b296ff..2c97015b36 100644 --- a/store.go +++ b/store.go @@ -3545,8 +3545,8 @@ func SetDefaultConfigFilePath(path string) { } // DefaultConfigFile returns the path to the storage config file used -func DefaultConfigFile(rootless bool) (string, error) { - return types.DefaultConfigFile(rootless) +func DefaultConfigFile() (string, error) { + return types.DefaultConfigFile() } // ReloadConfigurationFile parses the specified configuration file and overrides diff --git a/types/options.go b/types/options.go index 1f4dc91750..c9fa7885a9 100644 --- a/types/options.go +++ b/types/options.go @@ -89,7 +89,7 @@ func loadDefaultStoreOptions() { _, err := os.Stat(defaultOverrideConfigFile) if err == nil { - // The DefaultConfigFile(rootless) function returns the path + // The DefaultConfigFile() function returns the path // of the used storage.conf file, by returning defaultConfigFile // If override exists containers/storage uses it by default. defaultConfigFile = defaultOverrideConfigFile @@ -190,7 +190,7 @@ func defaultStoreOptionsIsolated(rootless bool, rootlessUID int, storageConf str // loadStoreOptions returns the default storage ops for containers func loadStoreOptions(rootless bool, rootlessUID int) (StoreOptions, error) { - storageConf, err := DefaultConfigFile(rootless && rootlessUID != 0) + storageConf, err := DefaultConfigFile() if err != nil { return defaultStoreOptions, err } @@ -199,15 +199,16 @@ func loadStoreOptions(rootless bool, rootlessUID int) (StoreOptions, error) { // UpdateOptions should be called iff container engine received a SIGHUP, // otherwise use DefaultStoreOptions -func UpdateStoreOptions(rootless bool, rootlessUID int) (StoreOptions, error) { - storeOptions, storeError = loadStoreOptions(rootless, rootlessUID) +func UpdateStoreOptions() (StoreOptions, error) { + storeOptions, storeError = loadStoreOptions(unshare.IsRootless(), unshare.GetRootlessUID()) return storeOptions, storeError } // DefaultStoreOptions returns the default storage ops for containers -func DefaultStoreOptions(rootless bool, rootlessUID int) (StoreOptions, error) { +func DefaultStoreOptions() (StoreOptions, error) { + uid := unshare.GetRootlessUID() once.Do(func() { - storeOptions, storeError = loadStoreOptions(rootless, rootlessUID) + storeOptions, storeError = loadStoreOptions(uid != 0, uid) }) return storeOptions, storeError } @@ -351,12 +352,6 @@ func getRootlessStorageOpts(rootlessUID int, systemOpts StoreOptions) (StoreOpti return opts, nil } -// DefaultStoreOptionsAutoDetectUID returns the default storage ops for containers -func DefaultStoreOptionsAutoDetectUID() (StoreOptions, error) { - uid := unshare.GetRootlessUID() - return DefaultStoreOptions(uid != 0, uid) -} - var prevReloadConfig = struct { storeOptions *StoreOptions mod time.Time @@ -526,8 +521,8 @@ func Options() (StoreOptions, error) { } // Save overwrites the tomlConfig in storage.conf with the given conf -func Save(conf TomlConfig, rootless bool) error { - configFile, err := DefaultConfigFile(rootless) +func Save(conf TomlConfig) error { + configFile, err := DefaultConfigFile() if err != nil { return err } @@ -545,10 +540,10 @@ func Save(conf TomlConfig, rootless bool) error { } // StorageConfig is used to retrieve the storage.conf toml in order to overwrite it -func StorageConfig(rootless bool) (*TomlConfig, error) { +func StorageConfig() (*TomlConfig, error) { config := new(TomlConfig) - configFile, err := DefaultConfigFile(rootless) + configFile, err := DefaultConfigFile() if err != nil { return nil, err } diff --git a/types/utils.go b/types/utils.go index 8dfc107f3c..a64f07d2cd 100644 --- a/types/utils.go +++ b/types/utils.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/containers/storage/pkg/homedir" + "github.com/containers/storage/pkg/unshare" "github.com/sirupsen/logrus" ) @@ -22,7 +23,7 @@ func expandEnvPath(path string, rootlessUID int) (string, error) { return newpath, nil } -func DefaultConfigFile(rootless bool) (string, error) { +func DefaultConfigFile() (string, error) { if defaultConfigFileSet { return defaultConfigFile, nil } @@ -30,7 +31,7 @@ func DefaultConfigFile(rootless bool) (string, error) { if path, ok := os.LookupEnv(storageConfEnv); ok { return path, nil } - if !rootless { + if unshare.GetRootlessUID() == 0 { if _, err := os.Stat(defaultOverrideConfigFile); err == nil { return defaultOverrideConfigFile, nil } diff --git a/types/utils_test.go b/types/utils_test.go index 62acd54608..2b5eedad05 100644 --- a/types/utils_test.go +++ b/types/utils_test.go @@ -21,7 +21,7 @@ func TestDefaultStoreOpts(t *testing.T) { func TestStorageConfOverrideEnvironmentDefaultConfigFileRootless(t *testing.T) { t.Setenv("CONTAINERS_STORAGE_CONF", "default_override_test.conf") - defaultFile, err := DefaultConfigFile(true) + defaultFile, err := DefaultConfigFile() expectedPath := "default_override_test.conf" @@ -31,7 +31,7 @@ func TestStorageConfOverrideEnvironmentDefaultConfigFileRootless(t *testing.T) { func TestStorageConfOverrideEnvironmentDefaultConfigFileRoot(t *testing.T) { t.Setenv("CONTAINERS_STORAGE_CONF", "default_override_test.conf") - defaultFile, err := DefaultConfigFile(false) + defaultFile, err := DefaultConfigFile() expectedPath := "default_override_test.conf" diff --git a/utils.go b/utils.go index 6e842c5480..5bade6ffe3 100644 --- a/utils.go +++ b/utils.go @@ -11,14 +11,9 @@ func ParseIDMapping(UIDMapSlice, GIDMapSlice []string, subUIDMap, subGIDMap stri return types.ParseIDMapping(UIDMapSlice, GIDMapSlice, subUIDMap, subGIDMap) } -// DefaultStoreOptionsAutoDetectUID returns the default storage options for containers -func DefaultStoreOptionsAutoDetectUID() (types.StoreOptions, error) { - return types.DefaultStoreOptionsAutoDetectUID() -} - // DefaultStoreOptions returns the default storage options for containers -func DefaultStoreOptions(rootless bool, rootlessUID int) (types.StoreOptions, error) { - return types.DefaultStoreOptions(rootless, rootlessUID) +func DefaultStoreOptions() (types.StoreOptions, error) { + return types.DefaultStoreOptions() } func validateMountOptions(mountOptions []string) error {