From 294f7c3d046ed8aac6e252a55e9ef891e46393ec Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 28 Apr 2026 12:30:13 -0700 Subject: [PATCH] shell: expand `~` in local file path arguments (#9265) * shell: expand `~` in local file path arguments The weed shell parses commands itself instead of going through an OS shell, so a path like `~/Downloads/foo.meta` was passed verbatim to `os.Open`, which fails because no `~` directory exists. Users had to spell out absolute home paths in every command. Add an `expandHomeDir` helper that resolves a leading `~` or `~/...` to the user's home directory, and run user-supplied local file paths in the affected shell commands through it: fs.meta.load (positional file) fs.meta.save (-o) fs.meta.changeVolumeId (-mapping) s3.iam.export (-file) s3.iam.import (-file) s3.policy (-file) s3tables.bucket (-file) s3tables.table (-file, -metadata) volume.fsck (-tempPath) Filer-namespace path flags (`-dir`, `-path`, `-locationPrefix`, etc.) are unaffected; they live in the filer, not on the local FS. * shell: reuse util.ResolvePath instead of a new helper util.ResolvePath already does tilde expansion; drop the local expandHomeDir helper and route every shell call site through it. --- weed/shell/command_fs_meta_change_volume_id.go | 2 +- weed/shell/command_fs_meta_load.go | 2 +- weed/shell/command_fs_meta_save.go | 2 +- weed/shell/command_s3_iam_export.go | 10 ++++++---- weed/shell/command_s3_iam_import.go | 3 ++- weed/shell/command_s3_policy.go | 3 ++- weed/shell/command_s3tables_bucket.go | 3 ++- weed/shell/command_s3tables_table.go | 5 +++-- weed/shell/command_volume_fsck.go | 2 +- 9 files changed, 19 insertions(+), 13 deletions(-) diff --git a/weed/shell/command_fs_meta_change_volume_id.go b/weed/shell/command_fs_meta_change_volume_id.go index 2eded1afd..ef5a9386a 100644 --- a/weed/shell/command_fs_meta_change_volume_id.go +++ b/weed/shell/command_fs_meta_change_volume_id.go @@ -63,7 +63,7 @@ func (c *commandFsMetaChangeVolumeId) Do(args []string, commandEnv *CommandEnv, // load the mapping mapping := make(map[needle.VolumeId]needle.VolumeId) if *mappingFileName != "" { - readMappingFromFile(*mappingFileName, mapping) + readMappingFromFile(util.ResolvePath(*mappingFileName), mapping) } else { if *fromVolumeId == *toVolumeId { return fmt.Errorf("no volume id changes") diff --git a/weed/shell/command_fs_meta_load.go b/weed/shell/command_fs_meta_load.go index 95c31ac55..93426ce51 100644 --- a/weed/shell/command_fs_meta_load.go +++ b/weed/shell/command_fs_meta_load.go @@ -51,7 +51,7 @@ func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io. return nil } - fileName := args[len(args)-1] + fileName := util.ResolvePath(args[len(args)-1]) metaLoadCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.dirPrefix = metaLoadCommand.String("dirPrefix", "", "load entries only with directories matching prefix") diff --git a/weed/shell/command_fs_meta_save.go b/weed/shell/command_fs_meta_save.go index 5f5388dca..be823611c 100644 --- a/weed/shell/command_fs_meta_save.go +++ b/weed/shell/command_fs_meta_save.go @@ -68,7 +68,7 @@ func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io. return parseErr } - fileName := *outputFileName + fileName := util.ResolvePath(*outputFileName) if fileName == "" { t := time.Now() fileName = fmt.Sprintf("%s-%4d%02d%02d-%02d%02d%02d.meta.gz", diff --git a/weed/shell/command_s3_iam_export.go b/weed/shell/command_s3_iam_export.go index b0a1c6d7f..708d59e5b 100644 --- a/weed/shell/command_s3_iam_export.go +++ b/weed/shell/command_s3_iam_export.go @@ -11,6 +11,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb" + "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/grpc" ) @@ -58,8 +59,9 @@ func (c *commandS3IAMExport) Do(args []string, commandEnv *CommandEnv, writer io } var out io.Writer = writer - if *file != "" { - fp, err := os.OpenFile(*file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + outputFile := util.ResolvePath(*file) + if outputFile != "" { + fp, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return fmt.Errorf("create file: %v", err) } @@ -72,8 +74,8 @@ func (c *commandS3IAMExport) Do(args []string, commandEnv *CommandEnv, writer io } fmt.Fprintln(out) - if *file != "" { - fmt.Fprintf(writer, "Exported IAM configuration to %s\n", *file) + if outputFile != "" { + fmt.Fprintf(writer, "Exported IAM configuration to %s\n", outputFile) } return nil }, commandEnv.option.FilerAddress.ToGrpcAddress(), false, commandEnv.option.GrpcDialOption) diff --git a/weed/shell/command_s3_iam_import.go b/weed/shell/command_s3_iam_import.go index 6d4d00c9f..9288497eb 100644 --- a/weed/shell/command_s3_iam_import.go +++ b/weed/shell/command_s3_iam_import.go @@ -11,6 +11,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/filer" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb" + "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/grpc" ) @@ -56,7 +57,7 @@ func (c *commandS3IAMImport) Do(args []string, commandEnv *CommandEnv, writer io return fmt.Errorf("this overwrites the entire IAM configuration; use -apply to confirm") } - data, err := os.ReadFile(*file) + data, err := os.ReadFile(util.ResolvePath(*file)) if err != nil { return fmt.Errorf("read file: %w", err) } diff --git a/weed/shell/command_s3_policy.go b/weed/shell/command_s3_policy.go index 1cd30b187..26276e1bc 100644 --- a/weed/shell/command_s3_policy.go +++ b/weed/shell/command_s3_policy.go @@ -12,6 +12,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine" + "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/grpc" ) @@ -86,7 +87,7 @@ func (c *commandS3Policy) Do(args []string, commandEnv *CommandEnv, writer io.Wr if *file == "" { return fmt.Errorf("-file is required") } - data, err := os.ReadFile(*file) + data, err := os.ReadFile(util.ResolvePath(*file)) if err != nil { return fmt.Errorf("failed to read policy file: %v", err) } diff --git a/weed/shell/command_s3tables_bucket.go b/weed/shell/command_s3tables_bucket.go index c715aebc4..d24a80599 100644 --- a/weed/shell/command_s3tables_bucket.go +++ b/weed/shell/command_s3tables_bucket.go @@ -11,6 +11,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/s3api/s3tables" + "github.com/seaweedfs/seaweedfs/weed/util" ) func init() { @@ -176,7 +177,7 @@ func (c *commandS3TablesBucket) Do(args []string, commandEnv *CommandEnv, writer if *policyFile == "" { return fmt.Errorf("-file is required") } - content, err := os.ReadFile(*policyFile) + content, err := os.ReadFile(util.ResolvePath(*policyFile)) if err != nil { return err } diff --git a/weed/shell/command_s3tables_table.go b/weed/shell/command_s3tables_table.go index 4f056dae1..f0336183d 100644 --- a/weed/shell/command_s3tables_table.go +++ b/weed/shell/command_s3tables_table.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/seaweedfs/seaweedfs/weed/s3api/s3tables" + "github.com/seaweedfs/seaweedfs/weed/util" ) func init() { @@ -108,7 +109,7 @@ func (c *commandS3TablesTable) Do(args []string, commandEnv *CommandEnv, writer case *create: var metadata *s3tables.TableMetadata if *metadataFile != "" { - content, err := os.ReadFile(*metadataFile) + content, err := os.ReadFile(util.ResolvePath(*metadataFile)) if err != nil { return err } @@ -178,7 +179,7 @@ func (c *commandS3TablesTable) Do(args []string, commandEnv *CommandEnv, writer if *policyFile == "" { return fmt.Errorf("-file is required") } - content, err := os.ReadFile(*policyFile) + content, err := os.ReadFile(util.ResolvePath(*policyFile)) if err != nil { return err } diff --git a/weed/shell/command_volume_fsck.go b/weed/shell/command_volume_fsck.go index c74c6033d..d86b2ebf4 100644 --- a/weed/shell/command_volume_fsck.go +++ b/weed/shell/command_volume_fsck.go @@ -145,7 +145,7 @@ func (c *commandVolumeFsck) Do(args []string, commandEnv *CommandEnv, writer io. } // create a temp folder - c.tempFolder, err = os.MkdirTemp(*tempPath, "sw_fsck") + c.tempFolder, err = os.MkdirTemp(util.ResolvePath(*tempPath), "sw_fsck") if err != nil { return fmt.Errorf("failed to create temp folder: %w", err) }