Skip to content

Commit 21b2ede

Browse files
committed
Remote settings storage directory handling fixes
- Fixed code in `components/remote_settings/src/storage.rs` that creates the remote settings data directory. The old code was mixing up the path with the parent directory. - Create the `.cli_data` dir in the remote-settings example CLI code. The remote settings service expects consumers to create at least the parent directory of the the data directory.
1 parent adae414 commit 21b2ede

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

components/remote_settings/src/storage.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ impl Storage {
4242
fn transaction(&mut self) -> Result<Transaction<'_>> {
4343
match &self.conn {
4444
ConnectionCell::Uninitialized => {
45-
if self.path != ":memory:" && std::fs::exists(&self.path)? {
46-
std::fs::create_dir(&self.path)?;
47-
}
45+
self.ensure_dir()?;
4846
self.conn = ConnectionCell::Initialized(open_database_with_flags(
4947
&self.path,
5048
OpenFlags::default(),
@@ -60,6 +58,19 @@ impl Storage {
6058
}
6159
}
6260

61+
pub fn ensure_dir(&self) -> Result<()> {
62+
if self.path == ":memory:" {
63+
return Ok(());
64+
}
65+
let Some(dir) = self.path.parent() else {
66+
return Ok(());
67+
};
68+
if !std::fs::exists(dir)? {
69+
std::fs::create_dir(dir)?;
70+
}
71+
Ok(())
72+
}
73+
6374
pub fn close(&mut self) {
6475
self.conn = ConnectionCell::Closed;
6576
}

examples/cli-support/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ pub fn cli_data_dir() -> String {
3434
data_path(None).to_string_lossy().to_string()
3535
}
3636

37+
pub fn ensure_cli_data_dir_exists() {
38+
let dir = data_path(None);
39+
if !dir.exists() {
40+
std::fs::create_dir(&dir).unwrap_or_else(|_| panic!("Error creating dir: {dir:?}"))
41+
}
42+
}
43+
3744
pub fn cli_data_subdir(relative_path: &str) -> String {
3845
data_path(Some(relative_path)).to_string_lossy().to_string()
3946
}

examples/remote-settings-cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ fn build_service(cli: &Cli) -> Result<RemoteSettingsService> {
118118
bucket_name: cli.bucket.clone(),
119119
app_context: None,
120120
};
121+
cli_support::ensure_cli_data_dir_exists();
121122
let storage_dir = cli
122123
.storage_dir
123124
.clone()

0 commit comments

Comments
 (0)