@@ -3,8 +3,7 @@ use std::{
33 collections:: BTreeSet ,
44 ffi:: OsStr ,
55 fs,
6- path:: Component ,
7- path:: { Path , PathBuf } ,
6+ path:: { Component , Path , PathBuf } ,
87 rc:: Rc ,
98} ;
109
@@ -17,8 +16,6 @@ use log::{debug, error, warn};
1716use mountpoints:: mountpaths;
1817use walkdir:: WalkDir ;
1918
20- use crate :: Config ;
21-
2219use super :: items:: TrashItemInfos ;
2320
2421/// Name of the trash directory
@@ -42,26 +39,20 @@ pub static ALWAYS_EXCLUDE_DIRS: &[&str] = &[
4239 "/var/lib/docker" ,
4340] ;
4441
45- /// Determine path to the trash directory for a given item and create it if required
46- pub fn determine_trash_dir_for ( item : & Path , config : & Config ) -> Result < PathBuf > {
47- debug ! ( "Determining trasher directory for item: {}" , item. display( ) ) ;
48-
49- let home_dir = dirs:: home_dir ( ) . context ( "Failed to determine path to user's home directory" ) ?;
42+ /// Compute the list of directories to exclude
43+ pub fn compute_exclusions ( exclude_dirs : & [ PathBuf ] ) -> Result < Vec < PathBuf > > {
44+ debug ! ( "Computing directories to exclude..." ) ;
5045
51- let mut exclude = config
52- . exclude
46+ let mut exclude = exclude_dirs
5347 . iter ( )
54- . filter_map ( |dir| {
55- if !dir. is_dir ( ) {
56- None
57- } else {
58- Some ( fs:: canonicalize ( dir) . with_context ( || {
59- format ! (
60- "Failed to canonicalize excluded directory: {}" ,
61- dir. display( )
62- )
63- } ) )
64- }
48+ . filter ( |dir| dir. is_dir ( ) )
49+ . map ( |dir| {
50+ fs:: canonicalize ( dir) . with_context ( || {
51+ format ! (
52+ "Failed to canonicalize excluded directory: {}" ,
53+ dir. display( )
54+ )
55+ } )
6556 } )
6657 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
6758
@@ -73,14 +64,23 @@ pub fn determine_trash_dir_for(item: &Path, config: &Config) -> Result<PathBuf>
7364 . map ( Path :: to_owned) ,
7465 ) ;
7566
67+ Ok ( exclude)
68+ }
69+
70+ /// Determine path to the trash directory for a given item and create it if required
71+ pub fn determine_trash_dir_for ( item : & Path , exclude_dirs : & [ PathBuf ] ) -> Result < PathBuf > {
72+ debug ! ( "Determining trasher directory for item: {}" , item. display( ) ) ;
73+
74+ let home_dir = dirs:: home_dir ( ) . context ( "Failed to determine path to user's home directory" ) ?;
75+
7676 // Don't canonicalize excluded item paths
7777 // NOTE: Only works if item path is absolute
78- if exclude . iter ( ) . any ( |dir| item. starts_with ( dir) ) {
78+ if exclude_dirs . iter ( ) . any ( |dir| item. starts_with ( dir) ) {
7979 return Ok ( home_dir. join ( TRASH_DIR_NAME ) ) ;
8080 }
8181
8282 let item = fs:: canonicalize ( item)
83- . with_context ( || format ! ( "Failed to canonicalize item path: {}\n \n Tip: you can exclude this directory using --exclude. " , item. display( ) ) ) ?;
83+ . with_context ( || format ! ( "Failed to canonicalize item path: {}" , item. display( ) ) ) ?;
8484
8585 let mut mountpoints = mountpaths ( ) . context ( "Failed to list system mountpoints" ) ?;
8686
@@ -125,7 +125,7 @@ pub fn determine_trash_dir_for(item: &Path, config: &Config) -> Result<PathBuf>
125125 continue ;
126126 }
127127
128- if exclude . iter ( ) . any ( |parent| item. starts_with ( parent) ) {
128+ if exclude_dirs . iter ( ) . any ( |parent| item. starts_with ( parent) ) {
129129 found = None ;
130130 break ;
131131 }
@@ -140,25 +140,28 @@ pub fn determine_trash_dir_for(item: &Path, config: &Config) -> Result<PathBuf>
140140}
141141
142142/// List all trash directories
143- pub fn list_trash_dirs ( config : & Config ) -> Result < BTreeSet < PathBuf > > {
143+ pub fn list_trash_dirs ( exclude_dirs : & [ PathBuf ] ) -> Result < BTreeSet < PathBuf > > {
144144 let canon_root = fs:: canonicalize ( "/" ) . context ( "Failed to canonicalize the root directory" ) ?;
145145
146146 let trash_dirs = mountpaths ( )
147147 . context ( "Failed to list system mountpoints" ) ?
148148 . iter ( )
149149 . chain ( [ canon_root] . iter ( ) )
150- . filter ( |path| match fs:: metadata ( path) {
151- Ok ( _) => true ,
152-
153- Err ( err) => {
154- warn ! ( "Warning: Skipping mountpoint {}: {err}" , path. display( ) ) ;
155- false
150+ . filter ( |dir| {
151+ !exclude_dirs
152+ . iter ( )
153+ . any ( |excluded| dir. starts_with ( excluded) )
154+ } )
155+ . filter_map ( |dir| match fs:: metadata ( dir) {
156+ Ok ( _) => Some ( dir. join ( TRASH_DIR_NAME ) ) ,
157+ Err ( _) => {
158+ warn ! ( "Skipping unavailable directory: {}" , dir. display( ) ) ;
159+ None
156160 }
157161 } )
158- . map ( |path| determine_trash_dir_for ( path, config) )
159- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
162+ . collect ( ) ;
160163
161- Ok ( trash_dirs. into_iter ( ) . filter ( |dir| dir . is_dir ( ) ) . collect ( ) )
164+ Ok ( trash_dirs)
162165}
163166
164167/// List and parse all items in the trash
@@ -213,8 +216,8 @@ pub fn list_trash_items(trash_dir: &Path) -> Result<Vec<TrashedItem>> {
213216}
214217
215218/// List all trash items
216- pub fn list_all_trash_items ( config : & Config ) -> Result < Vec < TrashedItem > > {
217- let all_trash_items = list_trash_dirs ( config ) ?
219+ pub fn list_all_trash_items ( exclude_dirs : & [ PathBuf ] ) -> Result < Vec < TrashedItem > > {
220+ let all_trash_items = list_trash_dirs ( exclude_dirs ) ?
218221 . into_iter ( )
219222 . map ( |trash_dir| list_trash_items ( & trash_dir) )
220223 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
@@ -229,9 +232,9 @@ pub fn list_all_trash_items(config: &Config) -> Result<Vec<TrashedItem>> {
229232pub fn expect_trash_item (
230233 filename : & str ,
231234 id : Option < & str > ,
232- config : & Config ,
235+ exclude_dirs : & [ PathBuf ] ,
233236) -> Result < FoundTrashItems > {
234- let mut candidates = list_all_trash_items ( config ) ?
237+ let mut candidates = list_all_trash_items ( exclude_dirs ) ?
235238 . into_iter ( )
236239 . filter ( |trashed| trashed. data . filename == filename)
237240 . collect :: < Vec < _ > > ( ) ;
@@ -257,9 +260,9 @@ pub fn expect_trash_item(
257260pub fn expect_single_trash_item (
258261 filename : & str ,
259262 id : Option < & str > ,
260- config : & Config ,
263+ exclude_dirs : & [ PathBuf ] ,
261264) -> Result < TrashedItem > {
262- match expect_trash_item ( filename, id, config ) ? {
265+ match expect_trash_item ( filename, id, exclude_dirs ) ? {
263266 FoundTrashItems :: Single ( item) => Ok ( item) ,
264267 FoundTrashItems :: Multi ( candidates) => bail ! (
265268 "Multiple items with this filename were found in the trash:\n \n {}" ,
0 commit comments