Skip to content

Commit 2b464d1

Browse files
committed
graph: Add an environment variable for a file-based IPFS cache
1 parent 70e4187 commit 2b464d1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

graph/src/env/mappings.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::path::PathBuf;
23

34
use super::*;
45

@@ -58,6 +59,9 @@ pub struct EnvVarsMapping {
5859
/// Set by the environment variable `GRAPH_IPFS_MAX_ATTEMPTS`. Defaults to 100000.
5960
pub ipfs_max_attempts: usize,
6061

62+
/// Set by the flag `GRAPH_IPFS_CACHE_LOCATION`.
63+
pub ipfs_cache_location: Option<PathBuf>,
64+
6165
/// Set by the flag `GRAPH_ALLOW_NON_DETERMINISTIC_IPFS`. Off by
6266
/// default.
6367
pub allow_non_deterministic_ipfs: bool,
@@ -86,6 +90,12 @@ impl TryFrom<InnerMappingHandlers> for EnvVarsMapping {
8690
type Error = anyhow::Error;
8791

8892
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+
8999
let vars = Self {
90100
entity_cache_dead_weight: x.entity_cache_dead_weight.0,
91101
entity_cache_size: x.entity_cache_size_in_kb * 1000,
@@ -101,6 +111,7 @@ impl TryFrom<InnerMappingHandlers> for EnvVarsMapping {
101111
max_ipfs_file_bytes: x.max_ipfs_file_bytes.0,
102112
ipfs_request_limit: x.ipfs_request_limit,
103113
ipfs_max_attempts: x.ipfs_max_attempts,
114+
ipfs_cache_location: ipfs_cache_location,
104115
allow_non_deterministic_ipfs: x.allow_non_deterministic_ipfs.0,
105116
disable_declared_calls: x.disable_declared_calls.0,
106117
store_errors_are_nondeterministic: x.store_errors_are_nondeterministic.0,
@@ -137,10 +148,36 @@ pub struct InnerMappingHandlers {
137148
ipfs_request_limit: u16,
138149
#[envconfig(from = "GRAPH_IPFS_MAX_ATTEMPTS", default = "100000")]
139150
ipfs_max_attempts: usize,
151+
#[envconfig(from = "GRAPH_IPFS_CACHE_LOCATION")]
152+
ipfs_cache_location: Option<String>,
140153
#[envconfig(from = "GRAPH_ALLOW_NON_DETERMINISTIC_IPFS", default = "false")]
141154
allow_non_deterministic_ipfs: EnvVarBoolean,
142155
#[envconfig(from = "GRAPH_DISABLE_DECLARED_CALLS", default = "false")]
143156
disable_declared_calls: EnvVarBoolean,
144157
#[envconfig(from = "GRAPH_STORE_ERRORS_ARE_NON_DETERMINISTIC", default = "false")]
145158
store_errors_are_nondeterministic: EnvVarBoolean,
146159
}
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

Comments
 (0)