5656import java .io .InputStream ;
5757import java .io .OutputStream ;
5858import java .io .OutputStreamWriter ;
59+ import java .io .UncheckedIOException ;
5960import java .io .Writer ;
6061import java .nio .charset .Charset ;
6162import java .nio .charset .StandardCharsets ;
@@ -469,18 +470,23 @@ public static void deleteDirWithRetry(File dir) throws Exception
469470 // Sometimes on Windows the directory could be locked, maybe by an external process, or the child directory is
470471 // readonly. Use a retry mechanism to set the writeable flag and then try to delete the parent directory.
471472 for (int attempt = 1 ; attempt <= 10 ; attempt ++) {
472- try {
473+ try
474+ {
473475 dir .setWritable (true , false );
474476
475477 // Wrap in a try to close the stream.
476- try (Stream <Path > files = Files .walk (dir .toPath ())) {
478+ try (Stream <Path > files = Files .walk (dir .toPath ()))
479+ {
477480 files .forEach (p -> p .toFile ().setWritable (true , false ));
478481 }
479482
480483 FileUtils .deleteDirectory (dir );
481484 LOG .info (String .format ("Deletion of directory %s was successful." , dir ));
482485 break ;
483- } catch (IOException ioException ) {
486+ } catch (AccessDeniedException e )
487+ {
488+ throw e ;
489+ } catch (IOException | UncheckedIOException ioException ) {
484490 LOG .warn (String .format ("IOException trying to delete directory %s. Error: %s. Waiting 10s and retrying. Attempt %d of 10." ,
485491 dir , ioException .getMessage (), attempt ));
486492 if (attempt == 10 ) {
@@ -502,8 +508,6 @@ public static void deleteDirWithRetry(File dir) throws Exception
502508 LOG .info ("Lock diagnostic..." );
503509 pb .redirectErrorStream (true );
504510
505- // Tried to use "try (Process p = pb.start()) {" without the finally block but our build
506- // system didn't like that and complained that Process doesn't implement closeable (it does).
507511 Process p = pb .start ();
508512 try (InputStream is = p .getInputStream ()) {
509513 String output = new String (is .readAllBytes (), StringUtilsLabKey .DEFAULT_CHARSET );
@@ -515,7 +519,7 @@ public static void deleteDirWithRetry(File dir) throws Exception
515519 }
516520
517521 } catch (IOException diagnosticException ) {
518- LOG .warn ("Failed to run lock diagnostic: " + diagnosticException );
522+ LOG .warn ("Failed to run lock diagnostic: " + diagnosticException . getMessage (), diagnosticException );
519523 }
520524 throw ioException ;
521525 }
0 commit comments