Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting file permissions returns FALSE always

The code:

File dir = new File(path);
boolean rc1 = dir.setExecutable(true, false);
boolean rc2 = dir.setReadable(true, false);
boolean rc3 = dir.setWritable(true, false);
if (!rc1 || !rc2 || !rc3){
    logger.warn("One of the permissions set returned false: rc1="+rc1+" rc2="+rc2+" rc3="+rc3 + " [for dir '"+dir+"']");
}

On Ubuntu all 3 calls return false. On my Windows only the 3rd call to setWritable returns false.

The target is to create the file/dir so the user (tomcat) and the group will be able to read/write.
BUT the file created on Ubuntu without permissions for the group to write.

like image 677
urir Avatar asked Dec 27 '12 09:12

urir


1 Answers

from javadocs

setExecutable(): Returns

true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname. If executable is false and the underlying file system does not implement an execute permission, then the operation will fail.

Also,

File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. It creates a file instance. It does not create a new file.

To create a new file

File f;
  f=new File("myfile.txt");
  if(!f.exists()){
  f.createNewFile();
  System.out.println("New file \"myfile.txt\" has been created 
  to the current directory");
  }
like image 124
Bhavik Shah Avatar answered Sep 22 '22 12:09

Bhavik Shah