serve sftp: extract function refactoring for handling hashsum commands

This commit is contained in:
albertony 2024-12-02 18:51:48 +01:00
parent 53a29fadf5
commit 0eecebfdb5

View file

@ -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
}