Setting file permissions returns FALSE always

11,696

Solution 1

I found the solution and will answer my own question:
When setting permissions on file or directory, you first MUST actually create the directory or write the file and only then set the permissions.
So, what I was doing at start was wrong:

File dir = new File(path);
boolean rc1 = dir.setExecutable(true, false);

While actually need to:

File dir = new File(path);
dir.mkdirs();
boolean rc1 = dir.setExecutable(true, false);
boolean rc2 = dir.setReadable(true, false);
boolean rc3 = dir.setWritable(true, false);

or

    File f = new File(uploadedFileLocation);
    ImageIO.write(image, "jpg", f);
    boolean rc1 = f.setExecutable(true, false);
    boolean rc2 = f.setReadable(true, false);
    boolean rc3 = f.setWritable(true, false);

Then it will work :)

Solution 2

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");
  }

Solution 3

Maybe you are not running this as a Super User in Linux. It can be the case that you(Logged in as) itself don't have rights to give file permissions.

Share:
11,696
urir
Author by

urir

Updated on June 03, 2022

Comments

  • urir
    urir almost 2 years

    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.

  • urir
    urir over 11 years
    Correct, this is not super user. This is 'tomcat' user for running Tomcat. User is part of other Group X. While creating directory and file we want to give permission to other users in the group X to this file.
  • urir
    urir over 11 years
    If I login with this user then in command line I have permission to change it.
  • urir
    urir over 11 years
    Also: same is correct both for creating the dir and same call for creating the file in dir.
  • Bhavik Shah
    Bhavik Shah over 11 years
    also try changing the umask on ubuntu