Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Verify rsync protocol version match prior to proceeding - Rebased on current master. #71

Merged
merged 7 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update local, remote version checks to avoid blanks.
  • Loading branch information
stephenwithav committed Jul 2, 2020
commit 35ee1de3486391990152e541a37fd341c760b358
25 changes: 5 additions & 20 deletions cmd/coder/sync.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"bufio"
"errors"
"bytes"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -34,32 +33,18 @@ func (cmd *syncCmd) RegisterFlags(fl *pflag.FlagSet) {
fl.BoolVarP(&cmd.init, "init", "i", false, "do initial transfer and exit")
}

// See https://lxadm.com/Rsync_exit_codes#List_of_standard_rsync_exit_codes.
var IncompatRsync = errors.New("rsync: exit status 2")
var StreamErrRsync = errors.New("rsync: exit status 12")

// Returns local rsync protocol version as a string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for missing this earlier in the review. These comments should start with the function/method name. So this would be "version returns local rsync protocol version as a string."

(And same for the other Version method)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I appreciate you teaching me the style you prefer prior to the interview next week so if I get the job, I'm already style trained.

func (s *syncCmd) version() string {
func (_ *syncCmd) version() string {
cmd := exec.Command("rsync", "--version")
stdout, err := cmd.StdoutPipe()
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}

if err := cmd.Start(); err != nil {
log.Fatal(err)
}

r := bufio.NewReader(stdout)
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}

firstLine, err := r.ReadString('\n')
firstLine, err := bytes.NewBuffer(out).ReadString('\n')
if err != nil {
return ""
log.Fatal(err)
}

versionString := strings.Split(firstLine, "protocol version ")

return versionString[1]
Expand Down
7 changes: 4 additions & 3 deletions internal/sync/sync.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sync

import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -283,7 +283,8 @@ func (s Sync) Version() (string, error) {
if err != nil {
return "", err
}
r := bufio.NewReader(process.Stdout())
buf := &bytes.Buffer{}
go io.Copy(buf, process.Stdout())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the go here. io.Copy will will terminate as expected once the command completes.

Having a goroutine here can be racy, because there's no guarantees that it runs or completes the copy before we start reading from the buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


err = process.Wait()
if code, ok := err.(wsep.ExitError); ok {
Expand All @@ -293,7 +294,7 @@ func (s Sync) Version() (string, error) {
return "", fmt.Errorf("Server version mismatch")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer returning the error here instead.

}

firstLine, err := r.ReadString('\n')
firstLine, err := buf.ReadString('\n')
if err != nil {
return "", err
}
Expand Down