Skip to content

Commit 623a84f

Browse files
tazjinVincent Ambo
authored andcommitted
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.
1 parent 61b2577 commit 623a84f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/main.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,24 @@ lazy_static! {
103103
/// information.
104104
static ref MONITORED_RESOURCE: Value = determine_monitored_resource();
105105

106-
/// Path to the file in which journaldriver should persist its
107-
/// cursor state.
108-
static ref CURSOR_FILE: PathBuf = env::var("CURSOR_POSITION_FILE")
109-
.unwrap_or("/var/lib/journaldriver/cursor.pos".into())
106+
/// Path to the directory in which journaldriver should persist
107+
/// its cursor state.
108+
static ref CURSOR_DIR: PathBuf = env::var("CURSOR_POSITION_DIR")
109+
.unwrap_or("/var/lib/journaldriver".into())
110110
.into();
111111

112+
/// Path to the cursor position file itself.
113+
static ref CURSOR_FILE: PathBuf = {
114+
let mut path = CURSOR_DIR.clone();
115+
path.push("cursor.pos");
116+
path
117+
};
118+
112119
/// Path to the temporary file used for cursor position writes.
113120
static ref CURSOR_TMP_FILE: PathBuf = {
114-
let mut tmp_path = CURSOR_FILE.clone();
115-
tmp_path.set_extension("pos.tmp");
116-
tmp_path
121+
let mut path = CURSOR_DIR.clone();
122+
path.push("cursor.tmp");
123+
path
117124
};
118125
}
119126

@@ -506,8 +513,11 @@ fn persist_cursor(cursor: String) -> Result<()> {
506513
return Ok(())
507514
}
508515

509-
let mut file = File::create(&*CURSOR_TMP_FILE)?;
516+
let mut file = File::create(&*CURSOR_TMP_FILE)
517+
.context("Failed to create cursor file")?;
518+
510519
write!(file, "{}", cursor).context("Failed to write cursor file")?;
520+
511521
rename(&*CURSOR_TMP_FILE, &*CURSOR_FILE)
512522
.context("Failed to move cursor file")
513523
.map_err(Into::into)
@@ -609,8 +619,13 @@ fn initial_cursor() -> Result<JournalSeek> {
609619
fn main () {
610620
env_logger::init();
611621

612-
// If the cursor file does not yet exist, the directory structure
613-
// leading up to it should be created:
622+
// The directory in which cursor positions are persisted should
623+
// have been created:
624+
if !CURSOR_DIR.exists() {
625+
error!("Cursor directory at '{:?}' does not exist", *CURSOR_DIR);
626+
process::exit(1);
627+
}
628+
614629
let cursor_position_dir = CURSOR_FILE.parent()
615630
.expect("Invalid cursor position file path");
616631

0 commit comments

Comments
 (0)