diff options
author | Vincent Ambo <mail@tazj.in> | 2018-10-09T09·20+0200 |
---|---|---|
committer | Vincent Ambo <github@tazj.in> | 2018-10-09T09·38+0200 |
commit | 623a84f6f8c5d42243432be39757702f480199ae (patch) | |
tree | fcfc30945797bbc9474cbaf20ed60a175ff17ad6 | |
parent | 61b2577a19715901048dd519d732caf3da90ea07 (diff) |
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.
-rw-r--r-- | src/main.rs | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs index ef28ee880465..03b57517cc28 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<JournalSeek> { 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"); |