Skip to content

Commit

Permalink
Merge pull request #146 from ipfs/fix/sync-stderr-windows
Browse files Browse the repository at this point in the history
cli: fix ignoring std{out,err} sync errors on windows
  • Loading branch information
Stebalien authored Feb 25, 2019
2 parents 677c325 + 9d994fb commit a83fda2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
13 changes: 13 additions & 0 deletions cli/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cli

import (
"os"
)

func isSyncNotSupportedErr(err error) bool {
perr, ok := err.(*os.PathError)
if !ok {
return false
}
return perr.Op == "sync" && isErrnoNotSupported(perr.Err)
}
15 changes: 15 additions & 0 deletions cli/error_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//+build !windows

package cli

import (
"syscall"
)

func isErrnoNotSupported(err error) bool {
switch err {
case syscall.EINVAL, syscall.ENOTSUP:
return true
}
return false
}
17 changes: 17 additions & 0 deletions cli/error_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//+build windows

package cli

import (
"syscall"
)

const invalid_file_handle syscall.Errno = 0x6

func isErrnoNotSupported(err error) bool {
switch err {
case syscall.EINVAL, syscall.ENOTSUP, invalid_file_handle:
return true
}
return false
}
20 changes: 6 additions & 14 deletions cli/responseemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"io"
"os"
"sync"
"syscall"

"github.com/ipfs/go-ipfs-cmds"
)

var _ ResponseEmitter = &responseEmitter{}

// NewResponseEmitter constructs a new response emitter that writes results to
// the console.
func NewResponseEmitter(stdout, stderr io.Writer, req *cmds.Request) (ResponseEmitter, error) {
encType, enc, err := cmds.GetEncoder(req, stdout, cmds.TextNewline)

Expand Down Expand Up @@ -101,28 +102,19 @@ func (re *responseEmitter) CloseWithError(err error) error {
re.stderr = nil
}()

// ignore error if the operating system doesn't support syncing std{out,err}
ignoreError := func(err error) bool {
if perr, ok := err.(*os.PathError); ok &&
perr.Op == "sync" && (perr.Err == syscall.EINVAL ||
perr.Err == syscall.ENOTSUP) {
return true
}

return false
}

var errStderr, errStdout error
if f, ok := re.stderr.(*os.File); ok {
errStderr = f.Sync()
}
if f, ok := re.stdout.(*os.File); ok {
errStdout = f.Sync()
}
if errStderr != nil && !ignoreError(errStderr) {

// ignore error if the operating system doesn't support syncing std{out,err}
if errStderr != nil && !isSyncNotSupportedErr(errStderr) {
return errStderr
}
if errStdout != nil && !ignoreError(errStdout) {
if errStdout != nil && !isSyncNotSupportedErr(errStdout) {
return errStdout
}
return nil
Expand Down

0 comments on commit a83fda2

Please sign in to comment.