In a Linux server, my java app is installed on:
/opt/apache-tomcat8/webapps/myApp/
Under the previous directory, I can read/write files easily, however I need to check the existance of some images that are located in a total different directory:
/home/mywebsite/public_html/image/catalog/mainImages/
and I haven't been able to do so. I've even tried giving 777 permissions to the mainImages folder, but Tomcat it's still not able to read from that directory.
I don't think my java code is relevant here, but just in case (I've deleted unnecesary extra code):
filePath = prefix + listImagesArr[i][1];
File f = new File(filePath);
if(f.exists()){
System.out.println("Directory exists!!");
if (f.isFile()) {
//do nothing
System.out.println("File exists");
} else {
System.out.println("File doesn't exist");
}
} else {
System.out.println("Directory doesn't exist");
}
I always get "Directory doesn't exist", but trust me, the directory is there.
Is this related with linux users/groups?
Any help will be really appreciated!!
UPDATE!!
After some research, I'm stuck here.
I've found a way to check permissions for a user/folder.
All the folders of this path have a user & owner called "itlogiccom" The linux Tomcat's user is called "tomcat8"
I've added tomcat8 to the itlogiccom group:
lid -g itlogiccom
itlogiccom(uid=512)
tomcat8(uid=490)
So now, tomcat8 should be able to read directories & files where itlogiccom is the owner and have the necesary permissions.
Using sudo and test I can check if the user tomcat8 is able to read from the specified directory. When the read is successful, the output is "0", if not, it's any other number. In the next code you'll see that the first 4 cases are successful, however, when I try to read from the home directory, the read is not successful (output is 1):
[/home/mywebsite/public_html/image/catalog]# sudo -u tomcat8 test -r ./mainImages/ZKTN3S.jpg; echo $?
0
[/home/mywebsite/public_html/image]# sudo -u tomcat8 test -r ./catalog/mainImages/ZKTN3S.jpg; echo $?
0
[/home/mywebsite/public_html]# sudo -u tomcat8 test -r ./image/catalog/mainImages/ZKTN3S.jpg; echo $?
0
[/home/mywebsite]# sudo -u tomcat8 test -r ./public_html/image/catalog/mainImages/ZKTN3S.jpg; echo $?
0
[/home]# sudo -u tomcat8 test -r ./mywebsite/public_html/image/catalog/mainImages/ZKTN3S.jpg; echo $?
1
mywebsite and public_html have the same group/owner, and the same permissions:
drwxr-xr-x+ 40 itlogiccom itlogiccom 4096 Feb 5 14:21 mywebsite/
drwxr-xr-x+ 12 itlogiccom itlogiccom 4096 Feb 5 16:13 public_html/
So this is why I just don't understand why I'm not able to read from the "home" directory.
By the way, if I perform the same test, using the "itlogiccom" user instead, the the read is successful:
[/home]# sudo -u itlogiccom test -r ./mywebsite/public_html/image/catalog/mainImages/ZKTN3S.jpg; echo $?
0
I don't understand why this behavior is happening. All I need is that the user "tomcat8" can read files from this path:
/home/mywebsite/public_html/image/catalog/mainImages/
Any help will be really appreciated!!
Just to be clear enough, if I try to read from:
/home/mywebsite/ (and every folder down this path), the read is successful.
but when I read from:
/home/
The read is not successful
Even if many of the comments here were very helpful, the main problem in this case was related to some ACL's (Access Control Lists) that were preventing the user "tomcat8" from reading in the directories I was trying to read.
These rules were deleted so now everything works like a charm.