@@ -25,16 +25,19 @@ import (
2525)
2626
2727func cacheDir () (string , error ) {
28- // Use the checksum to see if it already existsin the modcache
2928 cache := os .Getenv ("SQLCCACHE" )
3029 if cache != "" {
3130 return cache , nil
3231 }
33- home , err := os .UserHomeDir ()
34- if err != nil {
35- return "" , err
32+ cacheHome := os .Getenv ("XDG_CACHE_HOME" )
33+ if cacheHome == "" {
34+ home , err := os .UserHomeDir ()
35+ if err != nil {
36+ return "" , err
37+ }
38+ cacheHome = filepath .Join (home , ".cache" )
3639 }
37- return filepath .Join (home , "sqlc" , "mod " ), nil
40+ return filepath .Join (cacheHome , "sqlc" ), nil
3841}
3942
4043type Runner struct {
@@ -58,17 +61,18 @@ func (r *Runner) loadModule(ctx context.Context, engine *wasmtime.Engine) (*wasm
5861 if err != nil {
5962 return nil , err
6063 }
61-
62- cache , err := cacheDir ()
64+ cacheRoot , err := cacheDir ()
6365 if err != nil {
6466 return nil , err
6567 }
68+ cache := filepath .Join (cacheRoot , "plugins" )
69+ if err := os .MkdirAll (cache , 0755 ); err != nil && ! os .IsExist (err ) {
70+ return nil , fmt .Errorf ("failed to create cache directory: %w" , err )
71+ }
6672
6773 pluginDir := filepath .Join (cache , expected )
68- // TODO: Include os / arch in module name
6974 modPath := filepath .Join (pluginDir , fmt .Sprintf ("plugin_%s_%s.module" , runtime .GOOS , runtime .GOARCH ))
7075 _ , staterr := os .Stat (modPath )
71-
7276 if staterr == nil {
7377 data , err := os .ReadFile (modPath )
7478 if err != nil {
@@ -90,17 +94,15 @@ func (r *Runner) loadModule(ctx context.Context, engine *wasmtime.Engine) (*wasm
9094 }
9195
9296 if staterr != nil {
93- // TODO: What permissions to use?
94- err := os .MkdirAll (pluginDir , 0750 )
97+ err := os .Mkdir (pluginDir , 0755 )
9598 if err != nil && ! os .IsExist (err ) {
9699 return nil , fmt .Errorf ("mkdirall: %w" , err )
97100 }
98101 out , err := module .Serialize ()
99102 if err != nil {
100103 return nil , fmt .Errorf ("serialize: %w" , err )
101104 }
102- // TODO: What permissions to use?
103- if err := os .WriteFile (modPath , out , 0666 ); err != nil {
105+ if err := os .WriteFile (modPath , out , 0444 ); err != nil {
104106 return nil , fmt .Errorf ("cache wasm: %w" , err )
105107 }
106108 }
@@ -160,13 +162,11 @@ func (r *Runner) loadWASM(ctx context.Context, cache string, expected string) ([
160162 }
161163
162164 if staterr != nil {
163- // TODO: What permissions to use?
164- err := os .MkdirAll (pluginDir , 0750 )
165+ err := os .Mkdir (pluginDir , 0755 )
165166 if err != nil && ! os .IsExist (err ) {
166167 return nil , fmt .Errorf ("mkdirall: %w" , err )
167168 }
168- // TODO: What permissions to use?
169- if err := os .WriteFile (pluginPath , wmod , 0666 ); err != nil {
169+ if err := os .WriteFile (pluginPath , wmod , 0444 ); err != nil {
170170 return nil , fmt .Errorf ("cache wasm: %w" , err )
171171 }
172172 }
0 commit comments