@@ -56,80 +56,80 @@ protected async Task WithCleanupAsync(
5656 // determining the files that exist as there will be no subsequent cleanup process.
5757 if ( this . FileUtilityService == null
5858 || this . DirectoryUtilityService == null
59- || ! this . TryGetCleanupFileDirectory ( processRequest , out var fileParentDirectory ) )
59+ || ! this . TryGetCleanupFileDirectory ( processRequest , out var fileParentDirectory )
60+ || ! cleanupCreatedFiles )
6061 {
6162 await process ( processRequest , detectorArgs , cancellationToken ) . ConfigureAwait ( false ) ;
6263 return ;
6364 }
6465
6566 // Get the files and directories that match the cleanup pattern and exist before the process runs.
66- var ( preExistingFiles , preExistingDirs ) = this . DirectoryUtilityService . GetFilesAndDirectories ( fileParentDirectory , this . CleanupPatterns , DefaultCleanDepth ) ;
67- try
67+ var ( preSuccess , preExistingFiles , preExistingDirs ) = this . TryGetFilesAndDirectories ( fileParentDirectory , this . CleanupPatterns , DefaultCleanDepth ) ;
68+
69+ await process ( processRequest , detectorArgs , cancellationToken ) . ConfigureAwait ( false ) ;
70+ if ( ! preSuccess )
6871 {
69- await process ( processRequest , detectorArgs , cancellationToken ) . ConfigureAwait ( false ) ;
72+ // return early if we failed to get the pre-existing files and directories, since no need for cleanup
73+ return ;
7074 }
71- finally
75+
76+ // Ensure that only one cleanup process is running at a time, helping to prevent conflicts
77+ await this . cleanupSemaphore . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
78+
79+ try
7280 {
73- // Ensure that only one cleanup process is running at a time, helping to prevent conflicts
74- await this . cleanupSemaphore . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
81+ // Clean up any new files or directories created during the scan that match the clean up patterns.
82+ var ( postSuccess , latestFiles , latestDirs ) = this . TryGetFilesAndDirectories ( fileParentDirectory , this . CleanupPatterns , DefaultCleanDepth ) ;
83+ if ( ! postSuccess )
84+ {
85+ // return early if we failed to get the latest files and directories, since we will not be able
86+ // to determine what to clean up
87+ return ;
88+ }
7589
76- try
90+ var createdFiles = latestFiles . Except ( preExistingFiles ) . ToList ( ) ;
91+ var createdDirs = latestDirs . Except ( preExistingDirs ) . ToList ( ) ;
92+
93+ foreach ( var createdDir in createdDirs )
7794 {
78- // Clean up any new files or directories created during the scan that match the clean up patterns.
79- // If the cleanupCreatedFiles flag is set to false, this will be a dry run and will just log the files that it would clean.
80- var dryRun = ! cleanupCreatedFiles ;
81- var dryRunStr = dryRun ? "[DRYRUN] " : string . Empty ;
82- var ( latestFiles , latestDirs ) = this . DirectoryUtilityService . GetFilesAndDirectories ( fileParentDirectory , this . CleanupPatterns , DefaultCleanDepth ) ;
83- var createdFiles = latestFiles . Except ( preExistingFiles ) . ToList ( ) ;
84- var createdDirs = latestDirs . Except ( preExistingDirs ) . ToList ( ) ;
85-
86- foreach ( var createdDir in createdDirs )
95+ if ( createdDir is null || ! this . DirectoryUtilityService . Exists ( createdDir ) )
8796 {
88- if ( createdDir is null || ! this . DirectoryUtilityService . Exists ( createdDir ) )
89- {
90- continue ;
91- }
92-
93- try
94- {
95- this . Logger . LogDebug ( "{DryRun}Cleaning up directory {Dir}" , dryRunStr , createdDir ) ;
96- if ( ! dryRun )
97- {
98- this . DirectoryUtilityService . Delete ( createdDir , true ) ;
99- }
100- }
101- catch ( Exception e )
102- {
103- this . Logger . LogDebug ( e , "{DryRun}Failed to delete directory {Dir}" , dryRunStr , createdDir ) ;
104- }
97+ continue ;
10598 }
10699
107- foreach ( var createdFile in createdFiles )
100+ try
101+ {
102+ this . Logger . LogDebug ( "Cleaning up directory {Dir}" , createdDir ) ;
103+ this . DirectoryUtilityService . Delete ( createdDir , true ) ;
104+ }
105+ catch ( Exception e )
108106 {
109- if ( createdFile is null || ! this . FileUtilityService . Exists ( createdFile ) )
110- {
111- continue ;
112- }
113-
114- try
115- {
116- this . Logger . LogDebug ( "{DryRun}Cleaning up file {File}" , dryRunStr , createdFile ) ;
117- if ( ! dryRun )
118- {
119- this . FileUtilityService . Delete ( createdFile ) ;
120- }
121- }
122- catch ( Exception e )
123- {
124- this . Logger . LogDebug ( e , "{DryRun}Failed to delete file {File}" , dryRunStr , createdFile ) ;
125- }
107+ this . Logger . LogDebug ( e , "Failed to delete directory {Dir}" , createdDir ) ;
126108 }
127109 }
128- finally
110+
111+ foreach ( var createdFile in createdFiles )
129112 {
130- _ = this . cleanupSemaphore . Release ( ) ;
113+ if ( createdFile is null || ! this . FileUtilityService . Exists ( createdFile ) )
114+ {
115+ continue ;
116+ }
117+
118+ try
119+ {
120+ this . Logger . LogDebug ( "Cleaning up file {File}" , createdFile ) ;
121+ this . FileUtilityService . Delete ( createdFile ) ;
122+ }
123+ catch ( Exception e )
124+ {
125+ this . Logger . LogDebug ( e , "Failed to delete file {File}" , createdFile ) ;
126+ }
131127 }
132128 }
129+ finally
130+ {
131+ _ = this . cleanupSemaphore . Release ( ) ;
132+ }
133133 }
134134
135135 protected override async Task OnFileFoundAsync ( ProcessRequest processRequest , IDictionary < string , string > detectorArgs , bool cleanupCreatedFiles , CancellationToken cancellationToken = default ) =>
@@ -151,4 +151,19 @@ private bool TryGetCleanupFileDirectory(ProcessRequest processRequest, out strin
151151
152152 return false ;
153153 }
154+
155+ private ( bool Success , HashSet < string > Files , HashSet < string > Directories ) TryGetFilesAndDirectories ( string root , IList < string > patterns , int depth )
156+ {
157+ try
158+ {
159+ var ( files , directories ) = this . DirectoryUtilityService . GetFilesAndDirectories ( root , patterns , depth ) ;
160+ return ( true , files , directories ) ;
161+ }
162+ catch ( UnauthorizedAccessException e )
163+ {
164+ // log and return false if we are unauthorized to get files and directories
165+ this . Logger . LogDebug ( e , "Unauthorized to get files and directories for {Root}" , root ) ;
166+ return ( false , new HashSet < string > ( ) , new HashSet < string > ( ) ) ;
167+ }
168+ }
154169}
0 commit comments