diff options
author | Paramtamtam <[email protected]> | 2019-12-24 10:48:49 +0500 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-24 10:48:49 +0500 |
commit | fbd2022729ab7ffe4eee8ba8b7357ba179e4d010 (patch) | |
tree | fa6b66b18259282b2efe00a41c445a3fa40aa701 /util/network.go | |
parent | 3f7d8bc007a4efdd48bb16253c2686006f0f9cda (diff) | |
parent | 75a817edcff22e721dcc3a7fa5590b866f630403 (diff) |
Merge branch 'master' into master
Diffstat (limited to 'util/network.go')
-rw-r--r-- | util/network.go | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/util/network.go b/util/network.go index 70e38fdb..b9066de7 100644 --- a/util/network.go +++ b/util/network.go @@ -2,7 +2,9 @@ package util import ( "errors" + "fmt" "net" + "os" "strings" "syscall" ) @@ -11,16 +13,29 @@ import ( func CreateListener(address string) (net.Listener, error) { dsn := strings.Split(address, "://") if len(dsn) != 2 { - return nil, errors.New("Invalid DSN (tcp://:6001, unix://file.sock)") + return nil, errors.New("invalid DSN (tcp://:6001, unix://file.sock)") } if dsn[0] != "unix" && dsn[0] != "tcp" { - return nil, errors.New("Invalid Protocol (tcp://:6001, unix://file.sock)") + return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") } - if dsn[0] == "unix" { - syscall.Unlink(dsn[1]) + if dsn[0] == "unix" && fileExists(dsn[1]) { + err := syscall.Unlink(dsn[1]) + if err != nil { + return nil, fmt.Errorf("error during the unlink syscall: error %v", err) + } } return net.Listen(dsn[0], dsn[1]) } + +// fileExists checks if a file exists and is not a directory before we +// try using it to prevent further errors. +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} |