-
Notifications
You must be signed in to change notification settings - Fork 18
Verify rsync protocol version match prior to proceeding - Rebased on current master. #71
Changes from 3 commits
f745670
5538af6
54cecb2
35ee1de
21976c6
d853066
e8abef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
|
@@ -29,6 +34,37 @@ 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
cmd := exec.Command("rsync", "--version") | ||
stdout, err := cmd.StdoutPipe() | ||
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') | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
versionString := strings.Split(firstLine, "protocol version ") | ||
|
||
return versionString[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way this is laid out, I would suggest replacing reading the output of the command with:
And I would replace reading the first line with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
} | ||
|
||
func (cmd *syncCmd) Run(fl *pflag.FlagSet) { | ||
var ( | ||
local = fl.Arg(0) | ||
|
@@ -71,6 +107,16 @@ func (cmd *syncCmd) Run(fl *pflag.FlagSet) { | |
LocalDir: absLocal, | ||
Client: entClient, | ||
} | ||
|
||
localVersion := cmd.version() | ||
remoteVersion, rsyncErr := s.Version() | ||
|
||
if rsyncErr != nil { | ||
flog.Info("Unable to determine remote rsync version. Proceeding cautiously.") | ||
} else if localVersion != remoteVersion { | ||
flog.Fatal(fmt.Sprintf("rsync protocol mismatch. local is %s; remote is %s.", localVersion, remoteVersion)) | ||
} | ||
|
||
for err == nil || err == sync.ErrRestartSync { | ||
err = s.Run() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package sync | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
@@ -10,6 +11,7 @@ import ( | |
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
@@ -261,6 +263,46 @@ const ( | |
maxAcceptableDispatch = time.Millisecond * 50 | ||
) | ||
|
||
// Returns remote protocol version as a string. | ||
// Or, an error if one exists. | ||
func (s Sync) Version() (string, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancel() | ||
|
||
conn, err := s.Client.DialWsep(ctx, s.Env) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer conn.Close(websocket.CloseNormalClosure, "") | ||
|
||
execer := wsep.RemoteExecer(conn) | ||
process, err := execer.Start(ctx, wsep.Command{ | ||
Command: "rsync", | ||
Args: []string{"--version"}, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
r := bufio.NewReader(process.Stdout()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have the same issue here with the reader never reading anything. I think the best solution here would be to replace this reader with:
And There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @scsmithr Do you want the remote check to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer the behavior you had, where we just return the error and warn the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The remote check still has that. |
||
|
||
err = process.Wait() | ||
if code, ok := err.(wsep.ExitError); ok { | ||
return "", fmt.Errorf("Version heck exit status: %v", code) | ||
} | ||
if err != nil { | ||
return "", fmt.Errorf("Server version mismatch") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
versionString := strings.Split(firstLine, "protocol version ") | ||
|
||
return versionString[1], nil | ||
} | ||
|
||
// Run starts the sync synchronously. | ||
// Use this command to debug what wasn't sync'd correctly: | ||
// rsync -e "coder sh" -nicr ~/Projects/cdr/coder-cli/. ammar:/home/coder/coder-cli/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably leftover from a rebase, these were removed in master.