Skip to content

Commit b7ffbd3

Browse files
committed
Bugfix: use filepath package when connecting to UNIX sockets
The code before this commit is non-portable: it only works on UNIX systems where path.Join() is close enough to filepath.Join(). However, path.Join() is the wrong choice: UNIX domain sockets are identified via their file system name, so — conceptually and in practice — we need to use the filepath package instead. This commit fixes connecting to a PostgreSQL database that is listening on a UNIX domain socket on Windows. If you are surprised to see UNIX domain sockets on Windows, they were introduced with Windows 10, i.e. are widely available now: https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ filepath.IsAbs() is equivalent to strings.HasPrefix(s, "/") on UNIX, but also works on Windows, where absolute paths start with e.g. C:\ Similarly, filepath.Join() uses the correct path separator. Also recognize UNIX domain sockets in the abstract name space (represented by a leading @ character) while here. related to zombiezen/postgrestest#3
1 parent 3d61320 commit b7ffbd3

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

conn.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"net"
1616
"os"
1717
"os/user"
18-
"path"
1918
"path/filepath"
2019
"strconv"
2120
"strings"
@@ -437,8 +436,10 @@ func dial(ctx context.Context, d Dialer, o values) (net.Conn, error) {
437436
func network(o values) (string, string) {
438437
host := o["host"]
439438

440-
if strings.HasPrefix(host, "/") {
441-
sockPath := path.Join(host, ".s.PGSQL."+o["port"])
439+
// UNIX domain sockets are either represented by an (absolute) file system
440+
// path or they live in the abstract name space (starting with an @).
441+
if filepath.IsAbs(host) || strings.HasPrefix(host, "@") {
442+
sockPath := filepath.Join(host, ".s.PGSQL."+o["port"])
442443
return "unix", sockPath
443444
}
444445

0 commit comments

Comments
 (0)