@@ -248,9 +248,10 @@ public static void CleanupOldTempDirectories(TimeSpan? maxAge = null)
248248 var dirName = dirInfo . Name ;
249249
250250 // Parse timestamp from "generalupdate_yyyy-MM-dd-HHmmss-fff_PID_name"
251+ // Splitting by '_': [0]=prefix, [1]=timestamp, [2]=PID, [3..]=name.
251252 // If the PID segment matches the current process, skip it.
252253 var parts = dirName . Split ( '_' ) ;
253- if ( parts . Length >= 4 && int . TryParse ( parts [ 3 ] , out var pid ) && pid == currentPid )
254+ if ( parts . Length >= 3 && int . TryParse ( parts [ 2 ] , out var pid ) && pid == currentPid )
254255 continue ;
255256
256257 if ( dirInfo . CreationTimeUtc < cutoff )
@@ -346,42 +347,58 @@ public static void DeleteDirectory(string targetDir)
346347 // Enumerate then delete with per-item exception handling.
347348 // Between enumeration and deletion, concurrent processes may add/remove
348349 // files — handle these races gracefully instead of crashing.
349- foreach ( var file in Directory . GetFiles ( targetDir ) )
350+ try
350351 {
352+ foreach ( var file in Directory . GetFiles ( targetDir ) )
353+ {
354+ try
355+ {
356+ File . SetAttributes ( file , FileAttributes . Normal ) ;
357+ File . Delete ( file ) ;
358+ }
359+ catch ( FileNotFoundException ) { /* raced away — already deleted */ }
360+ catch ( DirectoryNotFoundException ) { /* raced away */ }
361+ catch ( UnauthorizedAccessException ex )
362+ {
363+ GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot delete file '{ Path . GetFileName ( file ) } ': { ex . Message } ") ;
364+ }
365+ }
366+
351367 try
352368 {
353- File . SetAttributes ( file , FileAttributes . Normal ) ;
354- File . Delete ( file ) ;
369+ foreach ( var dir in Directory . GetDirectories ( targetDir ) )
370+ {
371+ try
372+ {
373+ DeleteDirectory ( dir ) ;
374+ }
375+ catch ( DirectoryNotFoundException ) { /* raced away */ }
376+ catch ( UnauthorizedAccessException ex )
377+ {
378+ GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot delete directory '{ Path . GetFileName ( dir ) } ': { ex . Message } ") ;
379+ }
380+ }
355381 }
356- catch ( FileNotFoundException ) { /* raced away — already deleted */ }
357- catch ( DirectoryNotFoundException ) { /* raced away */ }
382+ catch ( DirectoryNotFoundException ) { /* parent raced away */ }
358383 catch ( UnauthorizedAccessException ex )
359384 {
360- GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot delete file ' { Path . GetFileName ( file ) } ' : { ex . Message } ") ;
385+ GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot enumerate subdirectories : { ex . Message } ") ;
361386 }
362- }
363387
364- foreach ( var dir in Directory . GetDirectories ( targetDir ) )
365- {
366388 try
367389 {
368- DeleteDirectory ( dir ) ;
390+ Directory . Delete ( targetDir , false ) ;
369391 }
370392 catch ( DirectoryNotFoundException ) { /* raced away */ }
371393 catch ( UnauthorizedAccessException ex )
372394 {
373- GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot delete directory '{ Path . GetFileName ( dir ) } ': { ex . Message } ") ;
395+ GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot delete directory '{ Path . GetFileName ( targetDir ) } ': { ex . Message } ") ;
374396 }
375397 }
376-
377- try
378- {
379- Directory . Delete ( targetDir , false ) ;
380- }
381- catch ( DirectoryNotFoundException ) { /* raced away */ }
398+ catch ( DirectoryNotFoundException ) { /* parent raced away before enumeration */ }
382399 catch ( UnauthorizedAccessException ex )
383400 {
384- GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot delete directory ' { Path . GetFileName ( targetDir ) } ': { ex . Message } ") ;
401+ GeneralTracer . Warn ( $ "StorageManager.DeleteDirectory: cannot enumerate ' { targetDir } ': { ex . Message } ") ;
385402 }
386403 }
387404
0 commit comments