forked from TrueCloudLab/rclone
serve sftp: extract function refactoring for handling hashsum commands
This commit is contained in:
parent
53a29fadf5
commit
0eecebfdb5
1 changed files with 56 additions and 50 deletions
|
@ -100,6 +100,48 @@ func (c *conn) execCommand(ctx context.Context, out io.Writer, command string) (
|
|||
if binary == "sha1sum" {
|
||||
ht = hash.SHA1
|
||||
}
|
||||
return c.handleHashsumCommand(ctx, out, ht, args)
|
||||
case "echo":
|
||||
// Special cases for legacy rclone command detection.
|
||||
// Before rclone v1.49.0 the sftp backend used "echo 'abc' | md5sum" when
|
||||
// detecting hash support, but was then changed to instead just execute
|
||||
// md5sum/sha1sum (without arguments), which is handled above. The following
|
||||
// code is therefore only necessary to support rclone versions older than
|
||||
// v1.49.0 using a sftp remote connected to a rclone serve sftp instance
|
||||
// running a newer version of rclone (e.g. latest).
|
||||
switch args {
|
||||
case "'abc' | md5sum":
|
||||
if c.vfs.Fs().Hashes().Contains(hash.MD5) {
|
||||
_, err = fmt.Fprintf(out, "0bee89b07a248e27c83fc3d5951213c1 -\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
} else {
|
||||
return errors.New("md5 hash not supported")
|
||||
}
|
||||
case "'abc' | sha1sum":
|
||||
if c.vfs.Fs().Hashes().Contains(hash.SHA1) {
|
||||
_, err = fmt.Fprintf(out, "03cfd743661f07975fa2f1220c5194cbaff48451 -\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
} else {
|
||||
return errors.New("sha1 hash not supported")
|
||||
}
|
||||
default:
|
||||
_, err = fmt.Fprintf(out, "%s\n", args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("%q not implemented", command)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleHashsumCommand is a helper to execCommand for common functionality of hashsum related commands
|
||||
func (c *conn) handleHashsumCommand(ctx context.Context, out io.Writer, ht hash.Type, args string) (err error) {
|
||||
if !c.vfs.Fs().Hashes().Contains(ht) {
|
||||
return fmt.Errorf("%v hash not supported", ht)
|
||||
}
|
||||
|
@ -150,42 +192,6 @@ func (c *conn) execCommand(ctx context.Context, out io.Writer, command string) (
|
|||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
case "echo":
|
||||
// Special cases for legacy rclone command detection.
|
||||
// Before rclone v1.49.0 the sftp backend used "echo 'abc' | md5sum" when
|
||||
// detecting hash support, but was then changed to instead just execute
|
||||
// md5sum/sha1sum (without arguments), which is handled above. The following
|
||||
// code is therefore only necessary to support rclone versions older than
|
||||
// v1.49.0 using a sftp remote connected to a rclone serve sftp instance
|
||||
// running a newer version of rclone (e.g. latest).
|
||||
switch args {
|
||||
case "'abc' | md5sum":
|
||||
if c.vfs.Fs().Hashes().Contains(hash.MD5) {
|
||||
_, err = fmt.Fprintf(out, "0bee89b07a248e27c83fc3d5951213c1 -\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
} else {
|
||||
return errors.New("md5 hash not supported")
|
||||
}
|
||||
case "'abc' | sha1sum":
|
||||
if c.vfs.Fs().Hashes().Contains(hash.SHA1) {
|
||||
_, err = fmt.Fprintf(out, "03cfd743661f07975fa2f1220c5194cbaff48451 -\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
} else {
|
||||
return errors.New("sha1 hash not supported")
|
||||
}
|
||||
default:
|
||||
_, err = fmt.Fprintf(out, "%s\n", args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("send output failed: %w", err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("%q not implemented", command)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue