@@ -16,15 +16,18 @@ package auth
1616
1717import (
1818 "os"
19+ "path/filepath"
1920 "sync"
2021 "sync/atomic"
2122
2223 "github.com/dolthub/dolt/go/libraries/doltcore/env"
2324 "github.com/dolthub/dolt/go/libraries/utils/filesys"
25+
26+ doltgresservercfg "github.com/dolthub/doltgresql/servercfg"
2427)
2528
2629// authFileName is the name of the file that contains all authorization-related data.
27- const authFileName = "auth.db"
30+ var authFileName = "auth.db"
2831
2932var (
3033 globalDatabase Database
@@ -128,7 +131,7 @@ func LockWrite(f func()) {
128131
129132// dbInit handle the global database initialization. Panics if an error occurs, since it points to something going
130133// terribly wrong.
131- func dbInit (dEnv * env.DoltEnv ) {
134+ func dbInit (dEnv * env.DoltEnv , cfg * doltgresservercfg. DoltgresConfig ) {
132135 globalDatabase = Database {
133136 rolesByName : make (map [string ]RoleID ),
134137 rolesByID : make (map [RoleID ]Role ),
@@ -140,10 +143,16 @@ func dbInit(dEnv *env.DoltEnv) {
140143 globalLock = & sync.RWMutex {}
141144 if dEnv != nil {
142145 if _ , ok := dEnv .FS .(* filesys.InMemFS ); ! ok {
146+ if cfg != nil && cfg .AuthFile != nil && len (* cfg .AuthFile ) > 0 {
147+ authFileName = * cfg .AuthFile
148+ }
143149 fileSystem = dEnv .FS
144150 authData , err := fileSystem .ReadFile (authFileName )
145151 if os .IsNotExist (err ) {
146152 dbInitDefault ()
153+ if err = dbInitCreateAuthDirectory (authFileName ); err != nil {
154+ panic (err )
155+ }
147156 if err = fileSystem .WriteFile (authFileName , globalDatabase .serialize (), 0644 ); err != nil {
148157 panic (err )
149158 }
@@ -179,3 +188,20 @@ func dbInitDefault() {
179188 }
180189 SetRole (superUser )
181190}
191+
192+ // dbInitCreateAuthDirectory creates the directory structure pointed to by the auth file if it does not already exist.
193+ func dbInitCreateAuthDirectory (authFileName string ) error {
194+ dir , _ := filepath .Split (authFileName )
195+ // If there's no directory, then we have nothing to create
196+ if len (dir ) == 0 {
197+ return nil
198+ }
199+ fullDir , err := fileSystem .Abs (dir )
200+ if err != nil {
201+ return err
202+ }
203+ if exists , _ := fileSystem .Exists (fullDir ); ! exists {
204+ return fileSystem .MkDirs (fullDir )
205+ }
206+ return nil
207+ }
0 commit comments