-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@TempDir
fails to delete read-only directories on Windows
#3352
Comments
I'm not positive if the read-only directory eventually got cleaned up some how; I can't find the JUnit temp directory in my user's temp directory. Nevertheless we don't want it to throw an exception in any case. |
Can you please provide the implementation of |
It's part of the Java NIO API: In this case |
I agree that this was an oversight. @garretwilson Could you please submit a PR to fix that? |
In theory I would love to, but I'm several layers deep in bug reports, PRs, etc. For the moment I'm just removing the I just wanted to file the ticket so it would at least be known so that it could be addressed some day. I don't think it has super-high severity or priority. If things calm down I would be glad to address it. |
Another note: I don't know if you already have code to do this, but it's not strightforward—it requires detecting Java NIO support for DOS file attributes and POSIX file attributes, the latter of which sets read-only status by manipulating the parent directory permissions, etc. I'm working on this myself in my own library in |
Set<String> supportedFileAttributeViews = tempDir.getFileSystem().supportedFileAttributeViews();
// dos is supported on unix
supportedFileAttributeViews.contains("dos") && !supportedFileAttributeViews.contains("unix") Am I missing something? readAttributes(tempDir, BasicFileAttributes.class) instanceof DosFileAttributes I would not recommend this. This looks like relying on an implementation detail that can change at any point. setAttribute(directory, ATTRIBUTE_DOS_READONLY, false); I find this hard to read. I would do the following Files.getFileAttributeView(directory, DosFileAttributeView.class).setReadOnly(false); |
One thing you're missing is the rest of the sentence which you replaced with ellipsis 😉 regarding the different levels of "read-only" designation in POSIX for directories. My entire sentence taken into context was making the point that there were several gotchas involved, each one in itself perhaps not being too onerous, but taken together meant that the task is not straightforward as a whole.
I didn't say it's hard. I'm just saying it's not something I can dash off in a couple of lines in five minutes. |
Please tell me what the proper parsing here would look like and the possible gotchas. |
Actually I made a mistake hurriedly looking over your code; I thought that |
Thanks for clarifying, I though I missed something or overlooked something. All good. |
I'm using JUnit 5.9.3 with Java 17 on Windows 10 with
@TempDir
. There are several ironies in this ticket. 😄It appears that
@TempDir
knows how to clean up files that are set to read-only, but doesn't know how to clean up directories that are read-only.I'm working on my own ticket, JAVA-312: Option to force deletion of read-only files for file subtrees. This is primarily to clean up Git temporary repositories; see JGit Bug 582051. I of course need to test my library to make sure it cleans things up with a "force" parameter.
These JUnit tests work:
Basically the first test verifies that without the "force" parameter, my deletion method will fail if a file is set to read-only. Note also that this results in a file being set to read-only at the end of the test, but JUnit cleans it up just fine.
Now I do the same test for a read-only directory:
Same as before, except in the first method,
verifyDeleteIfExistsNotForceFailsOnReadOnlyDirectory(…)
, JUnit itself fails when cleaning things up:Stack Trace
The workaround is to add my own cleanup code:
But that to some extent defeats the purpose of
@TempDir
. As JUnit cleans up read-only files on Windows, I'm sure it was just an oversight that it doesn't clean up read-only directories as well.The text was updated successfully, but these errors were encountered: