Skip to content
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

skipping temp. dir creation when removing temp. files #1598

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/com/sun/jna/Native.java
Expand Up @@ -1315,11 +1315,17 @@ static void markTemporaryFile(File file) {
Override with <code>jna.tmpdir</code>
*/
static File getTempDir() throws IOException {
return getTempDir(true);
}

private static File getTempDir(boolean create) throws IOException {
File jnatmp;
String prop = System.getProperty("jna.tmpdir");
if (prop != null) {
jnatmp = new File(prop);
jnatmp.mkdirs();
if (create) {
jnatmp.mkdirs();
}
}
else {
File tmp = new File(System.getProperty("java.io.tmpdir"));
Expand All @@ -1344,23 +1350,29 @@ static File getTempDir() throws IOException {
jnatmp = new File(tmp, "jna-" + System.getProperty("user.name").hashCode());
}

jnatmp.mkdirs();
if (!jnatmp.exists() || !jnatmp.canWrite()) {
jnatmp = tmp;
if (create) {
jnatmp.mkdirs();
if (!jnatmp.exists() || !jnatmp.canWrite()) {
jnatmp = tmp;
}
}
}
if (!jnatmp.exists()) {
throw new IOException("JNA temporary directory '" + jnatmp + "' does not exist");
}
if (!jnatmp.canWrite()) {
throw new IOException("JNA temporary directory '" + jnatmp + "' is not writable");

if (create) {
if (!jnatmp.exists()) {
throw new IOException("JNA temporary directory '" + jnatmp + "' does not exist");
}
if (!jnatmp.canWrite()) {
throw new IOException("JNA temporary directory '" + jnatmp + "' is not writable");
}
}

return jnatmp;
}

/** Remove all marked temporary files in the given directory. */
static void removeTemporaryFiles() throws IOException {
File dir = getTempDir();
File dir = getTempDir(false);
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
Expand Down