Skip to content

Commit f31eeca

Browse files
Merge pull request #1171 from sozu-proxy/fix_state_save_path
fix: Resolve path when saving state
2 parents d282447 + a7ecc65 commit f31eeca

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

bin/src/command/requests.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
env,
44
fs::File,
55
io::{ErrorKind, Read},
6+
path::PathBuf,
67
};
78

89
use mio::Token;
@@ -198,18 +199,35 @@ fn list_listeners(server: &mut Server, client: &mut ClientSession) {
198199
}
199200

200201
fn save_state(server: &mut Server, client: &mut ClientSession, path: &str) {
201-
debug!("saving state to file {}", path);
202-
let mut file = match File::create(path) {
202+
let mut path = PathBuf::from(path);
203+
if path.is_relative() {
204+
match std::env::current_dir() {
205+
Ok(cwd) => path = cwd.join(path),
206+
Err(error) => {
207+
client.finish_failure(format!("Cannot get Sōzu working directory: {error}",));
208+
return;
209+
}
210+
}
211+
}
212+
213+
debug!("saving state to file {}", &path.display());
214+
let mut file = match File::create(&path) {
203215
Ok(file) => file,
204216
Err(error) => {
205-
client.finish_failure(format!("Cannot create file at path {path}: {error}"));
217+
client.finish_failure(format!(
218+
"Cannot create file at path {}: {error}",
219+
path.display()
220+
));
206221
return;
207222
}
208223
};
209224

210225
match server.state.write_requests_to_file(&mut file) {
211226
Ok(counter) => {
212-
client.finish_ok(format!("Saved {counter} config messages to {path}"));
227+
client.finish_ok(format!(
228+
"Saved {counter} config messages to {}",
229+
&path.display()
230+
));
213231
}
214232
Err(error) => {
215233
client.finish_failure(format!("Failed writing state to file: {error}"));

bin/src/ctl/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub enum CtlError {
5555
WrongResponse(Response),
5656
#[error("could not setup the logger: {0}")]
5757
SetupLogging(LogError),
58+
#[error("could not resolve path for {0} : {1}")]
59+
ResolvePath(String, std::io::Error),
5860
}
5961

6062
pub struct CommandManager {

bin/src/ctl/request_builder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ impl CommandManager {
3030
pub fn save_state(&mut self, path: String) -> Result<(), CtlError> {
3131
debug!("Saving the state to file {}", path);
3232

33-
self.send_request(RequestType::SaveState(path).into())
33+
let current_directory =
34+
std::env::current_dir().map_err(|err| CtlError::ResolvePath(path.to_owned(), err))?;
35+
36+
let absolute_path = current_directory.join(&path);
37+
self.send_request(
38+
RequestType::SaveState(String::from(absolute_path.to_string_lossy())).into(),
39+
)
3440
}
3541

3642
pub fn load_state(&mut self, path: String) -> Result<(), CtlError> {

0 commit comments

Comments
 (0)