Delete on directory with symlinks considered harmful

(Olaf Flebbe) #1

Hi,

If one uses a delete action to remove a directory containing symlinks, the files the symlink are pointing to a removed, too. This violates the principle of least surprise.

Please see this build.gradle. It removes testDir/file. Please consider what is happening, if one places a symlink pointing to “/” into toRemoveDir instead? (Hint: Don’t try).

task demo << {
   delete('testDir')
   delete('toRemoveDir')

   mkdir('testDir')
   new File('testDir/file').write('anythingi\n')


   mkdir('toRemoveDir')
   exec {
     workingDir '.'
     commandLine "ln -s ../testDir toRemoveDir/testLink".split()
   }

   // show testDir
   exec {
     workingDir '.'
     commandLine "ls -lR testDir".split()
   }

   delete('toRemoveDir')

   // expected: 'toRemoveDir is removed (o.k)
   // but actually testDir/file has been removed !!
   exec {
     workingDir '.'
     commandLine "ls -lR testDir".split()
   }
}

Looking at the source code of ./subprojects/core/src/main/groovy/org/gradle/api/internal/file/copy/DeleteActionImpl.java
(doDelete): It follows symlinks and deletes everything on its way. Gradle should use the java.nio.file.Files isSymlink() method to detect symlinks.

(Mark Vieira) #2

There is an open pull request to change the default behavior here to not follow symlinks. The intent is to get this included in Gradle 2.13.

(Olaf Flebbe) #3

Sorry, the patch is IMHO plain overkill.
Nobody wants to restore the broken behavior of deleting Files following Symlinks, since everything can happen.

Anyway, I worked around this issue at this project: https://issues.apache.org/jira/secure/attachment/12789292/BIGTOP-2346.1.patch

Thanks