This is going to be very short. Actually does not entail a blog post. Anyways, here goes.
So, when I was trying to setup some Junit tests on an EC2 instance, I stumble into this ridiculous failure where, file.canWrite() simply returns true, no matter what. A code like below, keeps printing the "file is writable"
Turns out, the culprit is that I am running as "root". Since root can do-it-all, it simply returns true even though the file permissions read
Kind of subtle, since I would expect the permissions to be the ultimate source of truth.
So, when I was trying to setup some Junit tests on an EC2 instance, I stumble into this ridiculous failure where, file.canWrite() simply returns true, no matter what. A code like below, keeps printing the "file is writable"
import java.io.File;
public class Test1 {
public static void main(String[] args) throws Exception {
File file = new File("hello.txt");
file.createNewFile();
file.setReadOnly();
if (file.canWrite()) {
System.out.println("File is writable!");
} else {
System.out.println("File is in read only mode!");
}
}
}
Turns out, the culprit is that I am running as "root". Since root can do-it-all, it simply returns true even though the file permissions read
[root@myhost ~]# ls -al hello.txt
-r--r--r--. 1 root root 0 Jan 30 18:52 hello.txt
Kind of subtle, since I would expect the permissions to be the ultimate source of truth.
Comments
Post a Comment