From e68637fb1b1547b87a28f3d1f2e5be858792758f Mon Sep 17 00:00:00 2001 From: Johann150 Date: Tue, 5 Jan 2021 10:57:14 +0100 Subject: [PATCH] fix check_outdated --- README.md | 2 +- src/metadata.rs | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1ef1597..fe050e7 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ All of the command-line arguments are optional. Run `agate --help` to see the d When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If any segment of the requested path starts with a dot, agate will respond with a status code 52, even if the file does not exist (this behaviour can be disabled with `--serve-secret`). If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory. If there is no such file, but a file named `.directory-listing-ok` exists inside that directory, a basic directory listing is displayed. Files or directories whose name starts with a dot (e.g. the `.directory-listing-ok` file itself) are omitted from the list. -Agate will look for a file called `.lang` in the same directory as the file currently being served. If this file exists and has an entry for the current file, the respective data will be used to formulate the response header. +Agate will look for a file called `.mime` in the same directory as the file currently being served. If this file exists and has an entry for the current file, the respective data will be used to formulate the response header. The lines of the file should have this format: ```text diff --git a/src/metadata.rs b/src/metadata.rs index 16f021d..3c1dfba 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -26,12 +26,14 @@ pub(crate) struct FileOptions { default: String, } +static SIDECAR_FILENAME: &str = ".mime"; + impl FileOptions { - pub(crate) fn new(default: &String) -> Self { + pub(crate) fn new(default: &str) -> Self { Self { databases_read: BTreeMap::new(), file_meta: BTreeMap::new(), - default: default.clone(), + default: default.to_string(), } } @@ -41,7 +43,7 @@ impl FileOptions { /// return false if there is no database file in the specified directory. fn check_outdated(&self, db_dir: &PathBuf) -> bool { let mut db = db_dir.clone(); - db.push(".lang"); + db.push(SIDECAR_FILENAME); let db = db.as_path(); if let Ok(metadata) = db.metadata() { @@ -49,11 +51,11 @@ impl FileOptions { // it exists, but it is a directory false } else if let (Ok(modified), Some(last_read)) = - (metadata.modified(), self.databases_read.get(db)) + (metadata.modified(), self.databases_read.get(db_dir)) { // check that it was last modified before the read // if the times are the same, we might have read the old file - &modified < last_read + &modified >= last_read } else { // either the filesystem does not support last modified // metadata, so we have to read it again every time; or the @@ -71,8 +73,9 @@ impl FileOptions { /// alterations "on the fly". /// This function will allways try to read the file, even if it is current. fn read_database(&mut self, db_dir: &PathBuf) { + log::trace!("reading database for {:?}", db_dir); let mut db = db_dir.clone(); - db.push(".lang"); + db.push(SIDECAR_FILENAME); let db = db.as_path(); if let Ok(file) = std::fs::File::open(db) { @@ -103,12 +106,12 @@ impl FileOptions { /// The file path should consistenly be either absolute or relative to the /// working/content directory. If inconsisten file paths are used, this can /// lead to loading and storing sidecar files multiple times. - pub fn get(&mut self, file: PathBuf) -> &str { + pub fn get(&mut self, file: &PathBuf) -> &str { let dir = file.parent().expect("no parent directory").to_path_buf(); if self.check_outdated(&dir) { self.read_database(&dir); } - self.file_meta.get(&file).unwrap_or(&self.default) + self.file_meta.get(file).unwrap_or(&self.default) } }