@@ -362,10 +362,23 @@ protected void deleteOutputs(
362362 }
363363
364364 for (Artifact output : getOutputs ()) {
365- deleteOutput (pathResolver . toPath ( output ), output . getRoot () );
365+ deleteOutput (output , pathResolver );
366366 }
367367 }
368368
369+ /**
370+ * Remove an output artifact.
371+ *
372+ * <p>If the path refers to a directory, recursively removes the contents of the directory.
373+ *
374+ * @param output artifact to remove
375+ */
376+ protected static void deleteOutput (Artifact output , ArtifactPathResolver pathResolver )
377+ throws IOException {
378+ deleteOutput (
379+ pathResolver .toPath (output ), pathResolver .transformRoot (output .getRoot ().getRoot ()));
380+ }
381+
369382 /**
370383 * Helper method to remove an output file.
371384 *
@@ -375,23 +388,22 @@ protected void deleteOutputs(
375388 * @param root the root containing the output. This is used to check that we don't delete
376389 * arbitrary files in the file system.
377390 */
378- public static void deleteOutput (Path path , @ Nullable ArtifactRoot root ) throws IOException {
391+ public static void deleteOutput (Path path , @ Nullable Root root ) throws IOException {
379392 try {
380393 // Optimize for the common case: output artifacts are files.
381394 path .delete ();
382395 } catch (IOException e ) {
383396 // Handle a couple of scenarios where the output can still be deleted, but make sure we're not
384397 // deleting random files on the filesystem.
385398 if (root == null ) {
386- throw new IOException (e );
399+ throw new IOException ("null root" , e );
387400 }
388- Root outputRoot = root .getRoot ();
389- if (!outputRoot .contains (path )) {
390- throw new IOException (e );
401+ if (!root .contains (path )) {
402+ throw new IOException (String .format ("%s not under %s" , path , root ), e );
391403 }
392404
393405 Path parentDir = path .getParentDirectory ();
394- if (!parentDir .isWritable () && outputRoot .contains (parentDir )) {
406+ if (!parentDir .isWritable () && root .contains (parentDir )) {
395407 // Retry deleting after making the parent writable.
396408 parentDir .setWritable (true );
397409 deleteOutput (path , root );
0 commit comments