From 43440ff00c7b77ec2af8195cc922a9c15abdcdde Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Mon, 23 Dec 2019 15:08:46 +0300 Subject: - check before erasing unix sock --- util/network.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/network.go b/util/network.go index 3f533023..b9066de7 100644 --- a/util/network.go +++ b/util/network.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net" + "os" "strings" "syscall" ) @@ -19,7 +20,7 @@ func CreateListener(address string) (net.Listener, error) { return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") } - if dsn[0] == "unix" { + 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) @@ -28,3 +29,13 @@ func CreateListener(address string) (net.Listener, error) { 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() +} -- cgit v1.2.3