mirror of
https://github.com/PR0M3TH3AN/Marlin.git
synced 2025-09-08 07:08:44 +00:00
Update rename handling
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
//! event-debouncing, batch processing and a small state-machine so that the
|
//! event-debouncing, batch processing and a small state-machine so that the
|
||||||
//! watcher can be paused, resumed and shut down cleanly.
|
//! watcher can be paused, resumed and shut down cleanly.
|
||||||
|
|
||||||
use crate::db::{self, Database};
|
use crate::db::Database;
|
||||||
use crate::utils::{canonicalize_lossy, to_db_path};
|
use crate::utils::{canonicalize_lossy, to_db_path};
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use crossbeam_channel::{bounded, Receiver};
|
use crossbeam_channel::{bounded, Receiver};
|
||||||
@@ -293,12 +293,27 @@ impl FileWatcher {
|
|||||||
new_s: &str,
|
new_s: &str,
|
||||||
is_dir: bool,
|
is_dir: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
use rusqlite::params;
|
||||||
|
|
||||||
let mut guard = db_mutex.lock().map_err(|_| anyhow!("db mutex poisoned"))?;
|
let mut guard = db_mutex.lock().map_err(|_| anyhow!("db mutex poisoned"))?;
|
||||||
|
let conn = guard.conn_mut();
|
||||||
|
|
||||||
if is_dir {
|
if is_dir {
|
||||||
db::rename_directory(guard.conn_mut(), old_s, new_s)?;
|
let old_prefix = format!("{}/", old_s.trim_end_matches('/'));
|
||||||
|
let new_prefix = format!("{}/", new_s.trim_end_matches('/'));
|
||||||
|
let like_pattern = format!("{}%", old_prefix);
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE files SET path = REPLACE(path, ?1, ?2) WHERE path LIKE ?3",
|
||||||
|
params![old_prefix, new_prefix, like_pattern],
|
||||||
|
)?;
|
||||||
} else {
|
} else {
|
||||||
db::update_file_path(guard.conn_mut(), old_s, new_s)?;
|
conn.execute(
|
||||||
|
"UPDATE files SET path = ?1 WHERE path = ?2",
|
||||||
|
params![new_s, old_s],
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn.execute("PRAGMA optimize", [])?; // keep FTS in sync
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -74,7 +74,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(250));
|
||||||
let new_file = dir.join("b.txt");
|
let new_file = dir.join("b.txt");
|
||||||
fs::rename(&file, &new_file).unwrap();
|
fs::rename(&file, &new_file).unwrap();
|
||||||
|
|
||||||
@@ -123,7 +123,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(250));
|
||||||
let new = dir.join("newdir");
|
let new = dir.join("newdir");
|
||||||
fs::rename(&sub, &new).unwrap();
|
fs::rename(&sub, &new).unwrap();
|
||||||
let new_canonical = canonicalize_lossy(&new);
|
let new_canonical = canonicalize_lossy(&new);
|
||||||
|
Reference in New Issue
Block a user