newUnixSocketMount parses the configuration and returns the path to the unix socket, or an error if that path is not valid.
(inst InstanceConnConfig, unixSocketDir string, postgres bool)
| 942 | // newUnixSocketMount parses the configuration and returns the path to the unix |
| 943 | // socket, or an error if that path is not valid. |
| 944 | func newUnixSocketMount(inst InstanceConnConfig, unixSocketDir string, postgres bool) (string, error) { |
| 945 | var ( |
| 946 | // the path to the unix socket |
| 947 | address string |
| 948 | // the parent directory of the unix socket |
| 949 | dir string |
| 950 | ) |
| 951 | |
| 952 | if inst.UnixSocketPath != "" { |
| 953 | // When UnixSocketPath is set |
| 954 | address = inst.UnixSocketPath |
| 955 | |
| 956 | // If UnixSocketPath ends .s.PGSQL.5432, remove it for consistency |
| 957 | if postgres && path.Base(address) == ".s.PGSQL.5432" { |
| 958 | address = path.Dir(address) |
| 959 | } |
| 960 | |
| 961 | dir = path.Dir(address) |
| 962 | } else { |
| 963 | // When UnixSocket is set |
| 964 | dir = unixSocketDir |
| 965 | if dir == "" { |
| 966 | dir = inst.UnixSocket |
| 967 | } |
| 968 | address = UnixAddress(dir, inst.Name) |
| 969 | } |
| 970 | |
| 971 | // if base directory does not exist, fail |
| 972 | if _, err := os.Stat(dir); err != nil { |
| 973 | return "", err |
| 974 | } |
| 975 | |
| 976 | // When setting up a listener for Postgres, create address as a |
| 977 | // directory, and use the Postgres-specific socket name |
| 978 | // .s.PGSQL.5432. |
| 979 | if postgres { |
| 980 | // Make the directory only if it hasn't already been created. |
| 981 | if _, err := os.Stat(address); err != nil { |
| 982 | if err = os.Mkdir(address, 0777); err != nil { |
| 983 | return "", err |
| 984 | } |
| 985 | } |
| 986 | address = UnixAddress(address, ".s.PGSQL.5432") |
| 987 | } |
| 988 | |
| 989 | return address, nil |
| 990 | } |
| 991 | |
| 992 | func (s *socketMount) Addr() net.Addr { |
| 993 | return s.listener.Addr() |
no test coverage detected