Twitter github

Tales from the Crypt

Sometimes, while browsing source code, you come across things that make you think twice. While working on an enhancement recently, I came across this snippet:

public static boolean canWrite(File installDir) {
if (installDir.canWrite() == false)
return false;

if (!installDir.isDirectory())
return false;

File fileTest = null;
try {
// we use the .dll suffix to properly test on Vista virtual directories
// on Vista you are not allowed to write executable files on virtual directories like "Program Files"
fileTest = File.createTempFile("writtableArea", ".dll", installDir); //$NON-NLS-1$ //$NON-NLS-2$
} catch (IOException e) {
//If an exception occured while trying to create the file, it means that it is not writtable
return false;
} finally {
if (fileTest != null)
fileTest.delete();
}
return true;
}

It seems Vista has a really stupid problem in regards to writing executable files. I was aware of the new virtual directory madness in Vista, I didn’t know of this particular workaround. I guess you learn something new everyday πŸ˜‰

  • Wassim Melhem

    >I guess you learn something new >everyday πŸ˜‰

    That is precisely why we discourage learning on the PDE team.

    Knowledge leads to freedom πŸ™‚

  • Wassim Melhem

    >I guess you learn something new >everyday ;)That is precisely why we discourage learning on the PDE team. Knowledge leads to freedom πŸ™‚

  • Brad Reynolds

    How does that differ from OS X? On OS X you can’t write to the Applications folder unless you’re either an administrator or you’ve authenticated yourself. My guess, and since I don’t run Vista this is only a guess, is that if the code would authenticate itself it could write to the Program Files directory.

  • Brad Reynolds

    How does that differ from OS X? On OS X you can’t write to the Applications folder unless you’re either an administrator or you’ve authenticated yourself. My guess, and since I don’t run Vista this is only a guess, is that if the code would authenticate itself it could write to the Program Files directory.

  • Chris Aniszczyk (zx)

    it’s different because if you used java.io.File’s canWrite() on say anything, particularly a DLL it will ALWAYS return true, because Vista virtualizes everything.

    The method should be renamed to canReallyWrite() πŸ™‚

  • Chris Aniszczyk (zx)

    it’s different because if you used java.io.File’s canWrite() on say anything, particularly a DLL it will ALWAYS return true, because Vista virtualizes everything. The method should be renamed to canReallyWrite() πŸ™‚

  • Brad Reynolds

    I didn’t see the canWrite(), thanks. But there’s got to be a bug in the VM though, right?

  • Brad Reynolds

    I didn’t see the canWrite(), thanks. But there’s got to be a bug in the VM though, right?

  • Tom

    You gotta love the spelling on “writtable”

  • Tom

    You gotta love the spelling on “writtable”