mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 05:07:55 +08:00
improve maildir and notmuch tests
This commit is contained in:
@@ -76,42 +76,39 @@ impl IdMapper {
|
||||
}
|
||||
|
||||
pub fn append(&mut self, lines: Vec<(String, String)>) -> Result<usize> {
|
||||
let mut entries = String::new();
|
||||
self.extend(lines);
|
||||
|
||||
self.extend(lines.clone());
|
||||
let mut entries = String::new();
|
||||
let mut short_hash_len = self.short_hash_len;
|
||||
|
||||
for (hash, id) in self.iter() {
|
||||
entries.push_str(&format!("{} {}\n", hash, id));
|
||||
}
|
||||
|
||||
for (hash, id) in lines {
|
||||
loop {
|
||||
let short_hash = &hash[0..self.short_hash_len];
|
||||
let conflict_found = self
|
||||
.map
|
||||
.keys()
|
||||
.find(|cached_hash| {
|
||||
cached_hash.starts_with(short_hash) && *cached_hash != &hash
|
||||
})
|
||||
.find(|cached_hash| cached_hash.starts_with(short_hash) && cached_hash != &hash)
|
||||
.is_some();
|
||||
if self.short_hash_len > 32 || !conflict_found {
|
||||
break;
|
||||
}
|
||||
self.short_hash_len += 1;
|
||||
short_hash_len += 1;
|
||||
}
|
||||
entries.push_str(&format!("{} {}\n", hash, id));
|
||||
}
|
||||
|
||||
self.short_hash_len = short_hash_len;
|
||||
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&self.path)
|
||||
.context("cannot open maildir id hash map cache")?
|
||||
.write(format!("{}\n{}", self.short_hash_len, entries).as_bytes())
|
||||
.write(format!("{}\n{}", short_hash_len, entries).as_bytes())
|
||||
.context("cannot write maildir id hash map cache")?;
|
||||
|
||||
Ok(self.short_hash_len)
|
||||
Ok(short_hash_len)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,12 @@ impl<'a> MaildirBackend<'a> {
|
||||
// maildir subdirectory by adding a "." in front
|
||||
// of the name as described in the spec:
|
||||
// https://cr.yp.to/proto/maildir.html
|
||||
let dir = self
|
||||
.account_config
|
||||
.mailboxes
|
||||
.get(dir)
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or(dir);
|
||||
let path = self.mdir.path().join(format!(".{}", dir));
|
||||
self.validate_mdir_path(path)
|
||||
})
|
||||
|
||||
@@ -193,24 +193,16 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> {
|
||||
Ok(envelopes)
|
||||
}
|
||||
|
||||
fn add_msg(&mut self, dir: &str, msg: &[u8], tags: &str) -> Result<Box<dyn ToString>> {
|
||||
fn add_msg(&mut self, _: &str, msg: &[u8], tags: &str) -> Result<Box<dyn ToString>> {
|
||||
info!(">> add notmuch envelopes");
|
||||
debug!("dir: {:?}", dir);
|
||||
debug!("tags: {:?}", tags);
|
||||
|
||||
let mdir = self
|
||||
.mdir
|
||||
.get_mdir_from_dir(dir)
|
||||
.with_context(|| format!("cannot get maildir instance from {:?}", dir))?;
|
||||
let mdir_path_str = mdir
|
||||
.path()
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow!("cannot parse maildir path to string"))?;
|
||||
let dir = &self.notmuch_config.notmuch_database_dir;
|
||||
|
||||
// Adds the message to the maildir folder and gets its hash.
|
||||
let hash = self
|
||||
.mdir
|
||||
.add_msg(mdir_path_str, msg, "seen")
|
||||
.add_msg("inbox", msg, "seen")
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"cannot add notmuch message to maildir {:?}",
|
||||
@@ -222,12 +214,13 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> {
|
||||
|
||||
// Retrieves the file path of the added message by its maildir
|
||||
// identifier.
|
||||
let id = IdMapper::new(mdir.path())
|
||||
.with_context(|| format!("cannot create id mapper instance for {:?}", dir))?
|
||||
let mut mapper = IdMapper::new(dir)
|
||||
.with_context(|| format!("cannot create id mapper instance for {:?}", dir))?;
|
||||
let id = mapper
|
||||
.find(&hash)
|
||||
.with_context(|| format!("cannot find notmuch message from short hash {:?}", hash))?;
|
||||
debug!("id: {:?}", id);
|
||||
let file_path = mdir.path().join("cur").join(format!("{}:2,S", id));
|
||||
let file_path = dir.join("cur").join(format!("{}:2,S", id));
|
||||
debug!("file path: {:?}", file_path);
|
||||
|
||||
// Adds the message to the notmuch database by indexing it.
|
||||
@@ -240,13 +233,6 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> {
|
||||
let hash = format!("{:x}", md5::compute(&id));
|
||||
|
||||
// Appends hash entry to the id mapper cache file.
|
||||
let mut mapper =
|
||||
IdMapper::new(&self.notmuch_config.notmuch_database_dir).with_context(|| {
|
||||
format!(
|
||||
"cannot create id mapper instance for {:?}",
|
||||
self.notmuch_config.notmuch_database_dir
|
||||
)
|
||||
})?;
|
||||
mapper
|
||||
.append(vec![(hash.clone(), id.clone())])
|
||||
.with_context(|| {
|
||||
@@ -264,7 +250,7 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> {
|
||||
Ok(Box::new(hash))
|
||||
}
|
||||
|
||||
fn get_msg(&mut self, _virt_mbox: &str, short_hash: &str) -> Result<Msg> {
|
||||
fn get_msg(&mut self, _: &str, short_hash: &str) -> Result<Msg> {
|
||||
info!(">> add notmuch envelopes");
|
||||
debug!("short hash: {:?}", short_hash);
|
||||
|
||||
@@ -300,7 +286,7 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> {
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
fn copy_msg(&mut self, _mbox_src: &str, _mbox_dst: &str, _id: &str) -> Result<()> {
|
||||
fn copy_msg(&mut self, _dir_src: &str, _dir_dst: &str, _short_hash: &str) -> Result<()> {
|
||||
info!(">> copy notmuch message");
|
||||
info!("<< copy notmuch message");
|
||||
Err(anyhow!(
|
||||
@@ -308,7 +294,7 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> {
|
||||
))
|
||||
}
|
||||
|
||||
fn move_msg(&mut self, _src: &str, _dst: &str, _id: &str) -> Result<()> {
|
||||
fn move_msg(&mut self, _dir_src: &str, _dir_dst: &str, _short_hash: &str) -> Result<()> {
|
||||
info!(">> move notmuch message");
|
||||
info!("<< move notmuch message");
|
||||
Err(anyhow!(
|
||||
|
||||
Reference in New Issue
Block a user