From 623a84f6f8c5d42243432be39757702f480199ae Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 9 Oct 2018 11:20:38 +0200 Subject: [PATCH] refactor: Require directory instead of file path for cursors The previous change, which makes journaldriver write the cursor position in two steps, requires that journaldriver can write files adjacent to the cursor position file itself. Instead of simply guessing that this is possible (e.g. by changing the file suffix), expect the user to provide a directory that journaldriver can work with. --- src/main.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index ef28ee880..03b57517c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,17 +103,24 @@ lazy_static! { /// information. static ref MONITORED_RESOURCE: Value = determine_monitored_resource(); - /// Path to the file in which journaldriver should persist its - /// cursor state. - static ref CURSOR_FILE: PathBuf = env::var("CURSOR_POSITION_FILE") - .unwrap_or("/var/lib/journaldriver/cursor.pos".into()) + /// Path to the directory in which journaldriver should persist + /// its cursor state. + static ref CURSOR_DIR: PathBuf = env::var("CURSOR_POSITION_DIR") + .unwrap_or("/var/lib/journaldriver".into()) .into(); + /// Path to the cursor position file itself. + static ref CURSOR_FILE: PathBuf = { + let mut path = CURSOR_DIR.clone(); + path.push("cursor.pos"); + path + }; + /// Path to the temporary file used for cursor position writes. static ref CURSOR_TMP_FILE: PathBuf = { - let mut tmp_path = CURSOR_FILE.clone(); - tmp_path.set_extension("pos.tmp"); - tmp_path + let mut path = CURSOR_DIR.clone(); + path.push("cursor.tmp"); + path }; } @@ -506,8 +513,11 @@ fn persist_cursor(cursor: String) -> Result<()> { return Ok(()) } - let mut file = File::create(&*CURSOR_TMP_FILE)?; + let mut file = File::create(&*CURSOR_TMP_FILE) + .context("Failed to create cursor file")?; + write!(file, "{}", cursor).context("Failed to write cursor file")?; + rename(&*CURSOR_TMP_FILE, &*CURSOR_FILE) .context("Failed to move cursor file") .map_err(Into::into) @@ -609,8 +619,13 @@ fn initial_cursor() -> Result { fn main () { env_logger::init(); - // If the cursor file does not yet exist, the directory structure - // leading up to it should be created: + // The directory in which cursor positions are persisted should + // have been created: + if !CURSOR_DIR.exists() { + error!("Cursor directory at '{:?}' does not exist", *CURSOR_DIR); + process::exit(1); + } + let cursor_position_dir = CURSOR_FILE.parent() .expect("Invalid cursor position file path");