From ce109ed9c0753e3b684a00db692228819f876e88 Mon Sep 17 00:00:00 2001 From: Filip Bartodziej Date: Sun, 10 Jun 2018 00:27:42 +0200 Subject: [PATCH] log: password prompt output fixed for unix - partially fixes #2220 --- fs/config/config.go | 7 +++++-- fs/log/redirect_stderr_unix.go | 9 ++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/fs/config/config.go b/fs/config/config.go index 3cdac4fe3..0341ccc54 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -74,6 +74,9 @@ var ( // Key to use for password en/decryption. // When nil, no encryption will be used for saving. configKey []byte + + // output of prompt for password + PasswordPromptOutput = os.Stderr ) func init() { @@ -287,9 +290,9 @@ func checkPassword(password string) (string, error) { // GetPassword asks the user for a password with the prompt given. func GetPassword(prompt string) string { - _, _ = fmt.Fprintln(os.Stderr, prompt) + _, _ = fmt.Fprintln(PasswordPromptOutput, prompt) for { - _, _ = fmt.Fprint(os.Stderr, "password:") + _, _ = fmt.Fprint(PasswordPromptOutput, "password:") password := ReadPassword() password, err := checkPassword(password) if err == nil { diff --git a/fs/log/redirect_stderr_unix.go b/fs/log/redirect_stderr_unix.go index 5c5967d9e..6f5c32a3f 100644 --- a/fs/log/redirect_stderr_unix.go +++ b/fs/log/redirect_stderr_unix.go @@ -8,12 +8,19 @@ import ( "log" "os" + "github.com/ncw/rclone/fs/config" + "golang.org/x/sys/unix" ) // redirectStderr to the file passed in func redirectStderr(f *os.File) { - err := unix.Dup2(int(f.Fd()), int(os.Stderr.Fd())) + passPromptFd, err := unix.Dup(int(os.Stderr.Fd())) + if err != nil { + log.Fatalf("Failed to duplicate stderr: %v", err) + } + config.PasswordPromptOutput = os.NewFile(uintptr(passPromptFd), "passPrompt") + err = unix.Dup2(int(f.Fd()), int(os.Stderr.Fd())) if err != nil { log.Fatalf("Failed to redirect stderr to file: %v", err) }