diff options
author | Wolfy-J <[email protected]> | 2019-12-23 15:08:46 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-12-23 15:08:46 +0300 |
commit | 43440ff00c7b77ec2af8195cc922a9c15abdcdde (patch) | |
tree | 657dd3af8e2d6c9dcd8d2c817dd90d3cbba4f061 /util/network.go | |
parent | 7f2909ffb746880a380dbc8b9d7f4257a5abca6c (diff) |
- check before erasing unix sock
Diffstat (limited to 'util/network.go')
-rw-r--r-- | util/network.go | 13 |
1 files changed, 12 insertions, 1 deletions
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() +} |