Sans Browser

Archives Posts

XULRunner File Remove Bug and Work-around

January 12th, 2009 by enefekt

I’m working on a tool based on XULRunner that generates some content. (HTML content, images, etc.) It produces a directory to put the content into, and then offers the user the option of viewing that content right away.

The problem lies in leaving that content open in a another application like a web browser. Upon returning to the tool, and attempting to re-publish the content, an NS_ERROR_FAILURE exception is thrown coming from the nsIFile.remove call to remove the directory recursively. This is because of some sort of file exclusivity the web browser process has already.

So easy, just catch that error, inform the user to close their content in any other open application, rinse and repeat, right? Well unfortunately under XULRunner on Mac OS 10.5, the initial failure gunks up any further attempts. So even reinitializing a new nsIFile instance doesn’t work. Only restarting the app works! Yech.

Well I found that manually doing a recursive loop and removing each file individually, and then removing the parent directory, works. Definitely not efficient, as the native code would be better over JavaScript. (TraceMonkey should help with this right?) But there isn’t a grossly high number of files or that deep of a hiearchy anyways.

So meanwhile over on Windows, that technique throws it’s own lovely exception. But on Windows, catching the initial exception, and warning the user about files open in other application and closing them, works fine, so thats what I’m using over there.