Skip to content

Commit ce3c881

Browse files
committed
Use correct location for default ~/.pgpass location
Broke with 5e55bb7. Fixes #1294 Closes #1295
1 parent 6d40f13 commit ce3c881

5 files changed

Lines changed: 21 additions & 20 deletions

File tree

connector.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"os"
1313
"path/filepath"
1414
"reflect"
15-
"runtime"
1615
"slices"
1716
"sort"
1817
"strconv"
@@ -800,10 +799,7 @@ func (cfg *Config) fromService() error {
800799
}
801800

802801
if !cfg.isset("PGSERVICEFILE") {
803-
if home := pqutil.Home(); home != "" {
804-
if runtime.GOOS != "windows" {
805-
home = filepath.Dir(home) // Unlike other files this uses ~/ and not ~/.postgresql
806-
}
802+
if home := pqutil.Home(false); home != "" {
807803
cfg.ServiceFile = filepath.Join(home, ".pg_service.conf")
808804
}
809805
}

internal/pqtest/pqtest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ func DSN(conninfo string) string {
8989
func Home(t *testing.T) string {
9090
t.Helper()
9191
t.Setenv("HOME", t.TempDir())
92-
if err := os.MkdirAll(pqutil.Home(), 0o777); err != nil {
92+
if err := os.MkdirAll(pqutil.Home(true), 0o777); err != nil {
9393
t.Fatal(err)
9494
}
95-
return pqutil.Home()
95+
return pqutil.Home(true)
9696
}
9797

9898
// DB connects to the test database and returns the Ping error. The connection

internal/pqutil/path.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
//
2020
// Matches pqGetHomeDirectory() from PostgreSQL.
2121
// https://github.com/postgres/postgres/blob/2b117bb/src/interfaces/libpq/fe-connect.c#L8214
22-
func Home() string {
22+
func Home(subdir bool) string {
2323
if runtime.GOOS == "windows" {
2424
// pq uses SHGetFolderPath(), which is deprecated but x/sys/windows has
2525
// KnownFolderPath(). We don't really want to pull that in though, so
@@ -40,7 +40,12 @@ func Home() string {
4040
}
4141
home = u.HomeDir
4242
}
43-
return filepath.Join(home, ".postgresql")
43+
// libpq reads some files from ~/ and some from ~/.postgresql – on Windows
44+
// it always uses %APPDATA%/postgresql.
45+
if subdir {
46+
home = filepath.Join(home, ".postgresql")
47+
}
48+
return home
4449
}
4550

4651
// ErrNotExists reports if err is a "path doesn't exist" type error.
@@ -62,7 +67,7 @@ var WarnFD io.Writer = os.Stderr
6267
func Pgpass(passfile string) string {
6368
// Get passfile from the options.
6469
if passfile == "" {
65-
home := Home()
70+
home := Home(false)
6671
if home == "" {
6772
return ""
6873
}

ssl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func getTLSConfigClone(key string) *tls.Config {
6565
// in case of sslmode=allow or prefer.
6666
func ssl(cfg Config, mode SSLMode) (func(net.Conn) (net.Conn, error), error) {
6767
var (
68-
home = pqutil.Home()
68+
home = pqutil.Home(true)
6969
// Don't set defaults here, because tlsConf may be overwritten if a
7070
// custom one was registered. Set it after the sslmode switch.
7171
tlsConf = &tls.Config{}

ssl_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,14 @@ func TestSSLDefaults(t *testing.T) {
374374
t.Run(tt.file, func(t *testing.T) {
375375
pqtest.Home(t)
376376

377-
pqtest.Write(t, []byte("invalid data"), pqutil.Home(), tt.file)
377+
pqtest.Write(t, []byte("invalid data"), pqutil.Home(true), tt.file)
378378
if tt.file == "postgresql.crt" {
379-
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.key"), pqutil.Home(), "postgresql.key")
380-
pqtest.Chmod(t, 0o600, pqutil.Home(), "postgresql.key")
379+
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.key"), pqutil.Home(true), "postgresql.key")
380+
pqtest.Chmod(t, 0o600, pqutil.Home(true), "postgresql.key")
381381
}
382382
if tt.file == "postgresql.key" {
383-
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.crt"), pqutil.Home(), "postgresql.crt")
384-
pqtest.Chmod(t, 0o600, pqutil.Home(), "postgresql.key")
383+
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.crt"), pqutil.Home(true), "postgresql.crt")
384+
pqtest.Chmod(t, 0o600, pqutil.Home(true), "postgresql.key")
385385
}
386386

387387
_, err := pqtest.DB(t, "user=pqgossl sslmode=require")
@@ -393,10 +393,10 @@ func TestSSLDefaults(t *testing.T) {
393393

394394
t.Run("work with default paths", func(t *testing.T) {
395395
pqtest.Home(t)
396-
pqtest.Write(t, pqtest.Read(t, "testdata/init/root.crt"), pqutil.Home(), "root.crt")
397-
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.crt"), pqutil.Home(), "postgresql.crt")
398-
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.key"), pqutil.Home(), "postgresql.key")
399-
pqtest.Chmod(t, 0o600, pqutil.Home(), "postgresql.key")
396+
pqtest.Write(t, pqtest.Read(t, "testdata/init/root.crt"), pqutil.Home(true), "root.crt")
397+
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.crt"), pqutil.Home(true), "postgresql.crt")
398+
pqtest.Write(t, pqtest.Read(t, "testdata/init/postgresql.key"), pqutil.Home(true), "postgresql.key")
399+
pqtest.Chmod(t, 0o600, pqutil.Home(true), "postgresql.key")
400400
_ = pqtest.MustDB(t, "host=postgres user=pqgosslcert sslmode=verify-ca")
401401
})
402402
}

0 commit comments

Comments
 (0)