(state: &PersistedState)
| 57 | } |
| 58 | |
| 59 | fn garbage_collect_document_files(state: &PersistedState) { |
| 60 | let valid_paths: std::collections::HashSet<_> = state.documents.iter().map(|doc| document_content_path(&doc.id)).collect(); |
| 61 | |
| 62 | let directory = crate::dirs::app_autosave_documents_dir(); |
| 63 | let entries = match std::fs::read_dir(&directory) { |
| 64 | Ok(entries) => entries, |
| 65 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return, |
| 66 | Err(e) => { |
| 67 | tracing::error!("Failed to read autosave documents directory: {e}"); |
| 68 | return; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | for entry in entries.flatten() { |
| 73 | let path = entry.path(); |
| 74 | if path.is_file() && !valid_paths.contains(&path) { |
| 75 | if let Err(e) = std::fs::remove_file(&path) { |
| 76 | tracing::error!("Failed to remove orphaned document file {path:?}: {e}"); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn state_file_path() -> std::path::PathBuf { |
| 83 | let mut path = crate::dirs::app_data_dir(); |
no test coverage detected