Google Groups

Re: [sbt] Re: Bug in IO.delete()

Mark Harrah May 10, 2012 6:48 PM
Posted in group: simple-build-tool
On Wed, 9 May 2012 05:01:12 -0700 (PDT)
Jason Zaugg <> wrote:

> On Wednesday, May 9, 2012 11:42:45 AM UTC+2, Bruce Mitchener wrote:
> >
> > This issue caused data loss and loss of actual work today, so it is fairly
> > annoying.
> >
> > When IO.delete() is called with a directory, it recurses over all contents
> > and deletes them as well.  Unfortunately, if you have a symlink in the
> > directory tree that is getting deleted and that symlink points to something
> > valuable, that valuable data will also be deleted.
> >
> > This happened to me because I have a dist directory that I prepare and run
> > an application from. In that directory is a copy of some web content and
> > instead of editing and re-disting and re-starting the application, I just
> > wanted to symlink to the source files and edit to save me time and effort.
> >  That seems like a pretty reasonable thing to do.  So, my directory
> > structure was:
> >
> >   - dist
> >     - bin
> >     - static-content
> >     - other stuff
> >
> > So, static-content was a symlink to something in my source tree and I was
> > making a lot of changes. When IO.delete happened to nuke the dist directory
> > to do a new build, it iterates through the files in dist, deleting each of
> > them. This results in IO.delete being called on static-content. It then
> > iterates over the contents of that deleting that as well.
> >
> > It would be much better if IO.delete didn't traverse a symlink to another
> > directory, but just deleted the symlink.
> >
> It's a bit tricky pre-Java7 to determine if a file is a symlink, here are a
> few techniques:

The version actually in commons-io always returns false on windows.  It isn't clear why, since symbolic links exist on windows (in several forms, apparently).  There is this bug, which seems to imply the getCanonicalFile hack doesn't work for windows symlinks:


I'm not especially inclined to resort to native hacks, but this almost seems like it would end up being more reliable.  I don't think I would personally put symlinks in a directory that gets cleaned if IO.delete uses the getCanonicalFile hack.  I think I'd put a comment in IO.delete explicitly warning against putting symlinks in and add the hack to try to prevent surprises as much as possible.  (Of course, that has its drawbacks as well, like relying on that behavior.)


> -jason