Skip to content

Commit

Permalink
Merge remote-tracking branch 'bertm/fileutil-reflection' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Feb 16, 2025
2 parents a5d7232 + 429798a commit ba22ebd
Showing 1 changed file with 6 additions and 35 deletions.
41 changes: 6 additions & 35 deletions src/freenet/support/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -670,41 +669,13 @@ public static boolean setOwnerRWX(File f) {
** Set owner-only permissions on the given file.
*/
public static boolean setOwnerPerm(File f, boolean r, boolean w, boolean x) {
/* JDK6 replace when we upgrade
boolean b = f.setReadable(false, false);
b &= f.setWritable(false, false);
b &= f.setExecutable(false, false);
b &= f.setReadable(r, true);
b &= f.setWritable(w, true);
b &= f.setExecutable(x, true);
return b;
*/

boolean success = true;
try {

String[] methods = {"setReadable", "setWritable", "setExecutable"};
boolean[] perms = {r, w, x};

for (int i=0; i<methods.length; ++i) {
Method m = File.class.getDeclaredMethod(methods[i], boolean.class, boolean.class);
if (m != null) {
success &= (Boolean)m.invoke(f, false, false);
success &= (Boolean)m.invoke(f, perms[i], true);
}
}

} catch (NoSuchMethodException e) {
success = false;
} catch (java.lang.reflect.InvocationTargetException e) {
success = false;
} catch (IllegalAccessException e) {
success = false;
} catch (ExceptionInInitializerError e) {
success = false;
} catch (RuntimeException e) {
success = false;
}
success &= f.setReadable(false, false);
success &= f.setReadable(r, true);
success &= f.setWritable(false, false);
success &= f.setWritable(w, true);
success &= f.setExecutable(false, false);
success &= f.setExecutable(x, true);
return success;
}

Expand Down

0 comments on commit ba22ebd

Please sign in to comment.