@@ -56,80 +56,80 @@ protected async Task WithCleanupAsync(
56
56
// determining the files that exist as there will be no subsequent cleanup process.
57
57
if ( this . FileUtilityService == null
58
58
|| this . DirectoryUtilityService == null
59
- || ! this . TryGetCleanupFileDirectory ( processRequest , out var fileParentDirectory ) )
59
+ || ! this . TryGetCleanupFileDirectory ( processRequest , out var fileParentDirectory )
60
+ || ! cleanupCreatedFiles )
60
61
{
61
62
await process ( processRequest , detectorArgs , cancellationToken ) . ConfigureAwait ( false ) ;
62
63
return ;
63
64
}
64
65
65
66
// 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 )
68
71
{
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 ;
70
74
}
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
72
80
{
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
+ }
75
89
76
- try
90
+ var createdFiles = latestFiles . Except ( preExistingFiles ) . ToList ( ) ;
91
+ var createdDirs = latestDirs . Except ( preExistingDirs ) . ToList ( ) ;
92
+
93
+ foreach ( var createdDir in createdDirs )
77
94
{
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 ) )
87
96
{
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 ;
105
98
}
106
99
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 )
108
106
{
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 ) ;
126
108
}
127
109
}
128
- finally
110
+
111
+ foreach ( var createdFile in createdFiles )
129
112
{
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
+ }
131
127
}
132
128
}
129
+ finally
130
+ {
131
+ _ = this . cleanupSemaphore . Release ( ) ;
132
+ }
133
133
}
134
134
135
135
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
151
151
152
152
return false ;
153
153
}
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
+ }
154
169
}
0 commit comments