Skip to content
Permalink
Browse files
Cleanup isFileReadOnly check.
  • Loading branch information
leerho committed Nov 9, 2018
1 parent 1925532 commit fbe3e77d5689851f3016d2f27d1c70b678e18667
Showing 6 changed files with 32 additions and 71 deletions.
@@ -9,7 +9,6 @@

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.InvocationTargetException;
@@ -222,50 +221,12 @@ private static long map(final FileChannel fileChannel, final boolean resourceRea
}

static boolean isFileReadOnly(final File file) {
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.close();
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
raf.close();
return false;
} catch (final Exception e) { //could not open for write
return true;
}


// if (System.getProperty("os.name").startsWith("Windows")) {
// return !file.canWrite();
// }
// //All Unix-like OSes
// final Path path = Paths.get(file.getAbsolutePath());
// return !Files.isWritable(path);
// PosixFileAttributes attributes = null;
// try {
// attributes = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();
// } catch (final IOException e) {
// // File presence is guaranteed. Ignore
// e.printStackTrace();
// }
// if (attributes == null) { return false; }
//
// // Most restrictive read-only file status in Linux-derived OSes is when it has 0444 permissions.
// final Set<PosixFilePermission> permissions = attributes.permissions();
// int bits = 0;
// bits |= ((permissions.contains(PosixFilePermission.OWNER_READ)) ? 1 << 8 : 0);
// bits |= ((permissions.contains(PosixFilePermission.OWNER_WRITE)) ? 1 << 7 : 0);
// bits |= ((permissions.contains(PosixFilePermission.OWNER_EXECUTE)) ? 1 << 6 : 0);
// bits |= ((permissions.contains(PosixFilePermission.GROUP_READ)) ? 1 << 5 : 0);
// bits |= ((permissions.contains(PosixFilePermission.GROUP_WRITE)) ? 1 << 4 : 0);
// bits |= ((permissions.contains(PosixFilePermission.GROUP_EXECUTE)) ? 1 << 3 : 0);
// bits |= ((permissions.contains(PosixFilePermission.OTHERS_READ)) ? 1 << 2 : 0);
// bits |= ((permissions.contains(PosixFilePermission.OTHERS_WRITE)) ? 1 << 1 : 0);
// bits |= ((permissions.contains(PosixFilePermission.OTHERS_EXECUTE)) ? 1 : 0);
// // Here we are going to ignore the Owner Write & Execute bits to allow root/owner testing.
// final String filename = file.getName();
// System.err.println(filename + " : " + Integer.toBinaryString(bits));
// if ((filename == "GettysburgAddress.txt") && (bits == 436)) {
// for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
// System.err.println(ste);
// }
// }
// return ((bits & 0477) == 0444);
}

private static final class Deallocator implements Runnable {
@@ -22,14 +22,7 @@ public class AllocateDirectMapMemoryTest {

@BeforeClass
public void setReadOnly() {
UtilTest.setTestFileToReadOnly(this);
// File file =
// new File(getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
// try {
// Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("r--r--r--"));
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
UtilTest.setGettysburgAddressFileToReadOnly(this);
}

@Test
@@ -26,14 +26,7 @@ public class AllocateDirectWritableMapMemoryTest {

@BeforeClass
public void setReadOnly() {
UtilTest.setTestFileToReadOnly(this);
// File file =
// new File(getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
// try {
// Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("r--r--r--"));
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
UtilTest.setGettysburgAddressFileToReadOnly(this);
}

@Test
@@ -173,14 +173,17 @@ public void testByteBufferWrap() {
mem1.putInt(0, 1);
}

@SuppressWarnings("resource")
//@SuppressWarnings("resource")
@Test(expectedExceptions = AssertionError.class)
public void testMapFile() throws IOException {
File tempFile = File.createTempFile("test", "test");
File tempFile = File.createTempFile("test", null);
tempFile.deleteOnExit();
new RandomAccessFile(tempFile, "rw").setLength(8);
try (MapHandle h = Memory.map(tempFile)) {
((WritableMemory) h.get()).putInt(0, 1);
try (RandomAccessFile raf = new RandomAccessFile(tempFile, "rw")) {
raf.setLength(8);
//System.out.println(UtilTest.getFileAttributes(tempFile));
try (MapHandle h = Memory.map(tempFile)) {
((WritableMemory) h.get()).putInt(0, 1);
}
}
}

@@ -25,14 +25,7 @@ public class MemoryTest {

@BeforeClass
public void setReadOnly() {
UtilTest.setTestFileToReadOnly(this);
// File file =
// new File(getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
// try {
// Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("r--r--r--"));
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
UtilTest.setGettysburgAddressFileToReadOnly(this);
}

@Test
@@ -17,6 +17,9 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;

import org.testng.annotations.Test;
@@ -105,7 +108,22 @@ public void checkCodePoint() {
}
}

static final void setTestFileToReadOnly(Object obj) {
static final String getFileAttributes(File file) {
try {
PosixFileAttributes attrs = Files.getFileAttributeView(
file.toPath(), PosixFileAttributeView.class, new LinkOption[0]).readAttributes();
String s = String.format("%s: %s %s %s%n",
file.getPath(),
attrs.owner().getName(),
attrs.group().getName(),
PosixFilePermissions.toString(attrs.permissions()));
return s;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

static final void setGettysburgAddressFileToReadOnly(Object obj) {
File file =
new File(obj.getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
try {

0 comments on commit fbe3e77

Please sign in to comment.