1
1
use std:: fmt;
2
+ use std:: path:: PathBuf ;
2
3
3
4
use super :: * ;
4
5
@@ -58,6 +59,9 @@ pub struct EnvVarsMapping {
58
59
/// Set by the environment variable `GRAPH_IPFS_MAX_ATTEMPTS`. Defaults to 100000.
59
60
pub ipfs_max_attempts : usize ,
60
61
62
+ /// Set by the flag `GRAPH_IPFS_CACHE_LOCATION`.
63
+ pub ipfs_cache_location : Option < PathBuf > ,
64
+
61
65
/// Set by the flag `GRAPH_ALLOW_NON_DETERMINISTIC_IPFS`. Off by
62
66
/// default.
63
67
pub allow_non_deterministic_ipfs : bool ,
@@ -86,6 +90,12 @@ impl TryFrom<InnerMappingHandlers> for EnvVarsMapping {
86
90
type Error = anyhow:: Error ;
87
91
88
92
fn try_from ( x : InnerMappingHandlers ) -> Result < Self , Self :: Error > {
93
+ let ipfs_cache_location = x
94
+ . ipfs_cache_location
95
+ . map ( PathBuf :: from)
96
+ . map ( validate_ipfs_cache_location)
97
+ . transpose ( ) ?;
98
+
89
99
let vars = Self {
90
100
entity_cache_dead_weight : x. entity_cache_dead_weight . 0 ,
91
101
entity_cache_size : x. entity_cache_size_in_kb * 1000 ,
@@ -101,6 +111,7 @@ impl TryFrom<InnerMappingHandlers> for EnvVarsMapping {
101
111
max_ipfs_file_bytes : x. max_ipfs_file_bytes . 0 ,
102
112
ipfs_request_limit : x. ipfs_request_limit ,
103
113
ipfs_max_attempts : x. ipfs_max_attempts ,
114
+ ipfs_cache_location : ipfs_cache_location,
104
115
allow_non_deterministic_ipfs : x. allow_non_deterministic_ipfs . 0 ,
105
116
disable_declared_calls : x. disable_declared_calls . 0 ,
106
117
store_errors_are_nondeterministic : x. store_errors_are_nondeterministic . 0 ,
@@ -137,10 +148,36 @@ pub struct InnerMappingHandlers {
137
148
ipfs_request_limit : u16 ,
138
149
#[ envconfig( from = "GRAPH_IPFS_MAX_ATTEMPTS" , default = "100000" ) ]
139
150
ipfs_max_attempts : usize ,
151
+ #[ envconfig( from = "GRAPH_IPFS_CACHE_LOCATION" ) ]
152
+ ipfs_cache_location : Option < String > ,
140
153
#[ envconfig( from = "GRAPH_ALLOW_NON_DETERMINISTIC_IPFS" , default = "false" ) ]
141
154
allow_non_deterministic_ipfs : EnvVarBoolean ,
142
155
#[ envconfig( from = "GRAPH_DISABLE_DECLARED_CALLS" , default = "false" ) ]
143
156
disable_declared_calls : EnvVarBoolean ,
144
157
#[ envconfig( from = "GRAPH_STORE_ERRORS_ARE_NON_DETERMINISTIC" , default = "false" ) ]
145
158
store_errors_are_nondeterministic : EnvVarBoolean ,
146
159
}
160
+
161
+ fn validate_ipfs_cache_location ( path : PathBuf ) -> Result < PathBuf , anyhow:: Error > {
162
+ let path = path. canonicalize ( ) ?;
163
+ if !path. is_absolute ( ) {
164
+ return Err ( anyhow:: anyhow!(
165
+ "GRAPH_IPFS_CACHE_LOCATION must be an absolute path: {}" ,
166
+ path. display( )
167
+ ) ) ;
168
+ }
169
+ if !path. is_dir ( ) {
170
+ return Err ( anyhow:: anyhow!(
171
+ "GRAPH_IPFS_CACHE_LOCATION must be a directory: {}" ,
172
+ path. display( )
173
+ ) ) ;
174
+ }
175
+ let metadata = path. metadata ( ) ?;
176
+ if metadata. permissions ( ) . readonly ( ) {
177
+ return Err ( anyhow:: anyhow!(
178
+ "GRAPH_IPFS_CACHE_LOCATION must be a writable directory: {}" ,
179
+ path. display( )
180
+ ) ) ;
181
+ }
182
+ Ok ( path)
183
+ }
0 commit comments