@@ -25,16 +25,19 @@ import (
25
25
)
26
26
27
27
func cacheDir () (string , error ) {
28
- // Use the checksum to see if it already existsin the modcache
29
28
cache := os .Getenv ("SQLCCACHE" )
30
29
if cache != "" {
31
30
return cache , nil
32
31
}
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" )
36
39
}
37
- return filepath .Join (home , "sqlc" , "mod " ), nil
40
+ return filepath .Join (cacheHome , "sqlc" ), nil
38
41
}
39
42
40
43
type Runner struct {
@@ -58,17 +61,18 @@ func (r *Runner) loadModule(ctx context.Context, engine *wasmtime.Engine) (*wasm
58
61
if err != nil {
59
62
return nil , err
60
63
}
61
-
62
- cache , err := cacheDir ()
64
+ cacheRoot , err := cacheDir ()
63
65
if err != nil {
64
66
return nil , err
65
67
}
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
+ }
66
72
67
73
pluginDir := filepath .Join (cache , expected )
68
- // TODO: Include os / arch in module name
69
74
modPath := filepath .Join (pluginDir , fmt .Sprintf ("plugin_%s_%s.module" , runtime .GOOS , runtime .GOARCH ))
70
75
_ , staterr := os .Stat (modPath )
71
-
72
76
if staterr == nil {
73
77
data , err := os .ReadFile (modPath )
74
78
if err != nil {
@@ -90,17 +94,15 @@ func (r *Runner) loadModule(ctx context.Context, engine *wasmtime.Engine) (*wasm
90
94
}
91
95
92
96
if staterr != nil {
93
- // TODO: What permissions to use?
94
- err := os .MkdirAll (pluginDir , 0750 )
97
+ err := os .Mkdir (pluginDir , 0755 )
95
98
if err != nil && ! os .IsExist (err ) {
96
99
return nil , fmt .Errorf ("mkdirall: %w" , err )
97
100
}
98
101
out , err := module .Serialize ()
99
102
if err != nil {
100
103
return nil , fmt .Errorf ("serialize: %w" , err )
101
104
}
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 {
104
106
return nil , fmt .Errorf ("cache wasm: %w" , err )
105
107
}
106
108
}
@@ -160,13 +162,11 @@ func (r *Runner) loadWASM(ctx context.Context, cache string, expected string) ([
160
162
}
161
163
162
164
if staterr != nil {
163
- // TODO: What permissions to use?
164
- err := os .MkdirAll (pluginDir , 0750 )
165
+ err := os .Mkdir (pluginDir , 0755 )
165
166
if err != nil && ! os .IsExist (err ) {
166
167
return nil , fmt .Errorf ("mkdirall: %w" , err )
167
168
}
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 {
170
170
return nil , fmt .Errorf ("cache wasm: %w" , err )
171
171
}
172
172
}
0 commit comments