Skip to content

Commit

Permalink
skipping temp. dir creation when removing temp. files
Browse files Browse the repository at this point in the history
  • Loading branch information
trespasserw committed Mar 28, 2024
1 parent 3065af0 commit e18dd48
Showing 1 changed file with 22 additions and 10 deletions.
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

0 comments on commit e18dd48

Please sign in to comment.