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.
This commit is contained in:
Chris Lu
2026-04-28 12:30:13 -07:00
committed by GitHub
parent e2c8791441
commit 294f7c3d04
9 changed files with 19 additions and 13 deletions
@@ -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")
+1 -1
View File
@@ -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")
+1 -1
View File
@@ -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",
+6 -4
View File
@@ -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)
+2 -1
View File
@@ -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)
}
+2 -1
View File
@@ -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)
}
+2 -1
View File
@@ -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
}
+3 -2
View File
@@ -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
}
+1 -1
View File
@@ -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)
}