1

Objective

I want that whenever someone uploads a file (name is Accounts 0998.csv) in /opt/file/incoming directory. Its permissions gets change to chmod 664 i.e rw-rw-r I am using linux.

I want to automate this process so I am writing a java program but its working

package com.reader.file;

import java.io.File;
import java.io.IOException;

public class GrantPermission
{
    public static void main( String[] args )
    {
        try {

          File file = new File("/opt/file/Accounts 0998.csv");

         if(file.exists()){
             System.out.println("File exists.");

            //using PosixFilePermission to set file permissions 664

            Set<PosixFilePermission> perms = new 

            HashSet<PosixFilePermission>();

            //add owners permission
            perms.add(PosixFilePermission.OWNER_READ);
            perms.add(PosixFilePermission.OWNER_WRITE);

            //add group permissions
            perms.add(PosixFilePermission.GROUP_READ);
            perms.add(PosixFilePermission.GROUP_WRITE);

            //add others permissions
            perms.add(PosixFilePermission.OTHERS_READ);

            Files.setPosixFilePermissions(file.toPath(), perms);

            } catch (IOException e) {

                e.printStackTrace();
            }
         }
         else{
              System.out.println("File does not exists.");
         }

        } catch (IOException e) {
          e.printStackTrace();
        }
    }
}

Additional Information

I am using WSO2 ESB it first search for a file then If file exists then I want to change its permission (via using Class mediator i.e JAVA) and then move it to another directory but my GOAL is change file permissions to rw-rw-r

Community
  • 1
  • 1
Mishi
  • 628
  • 4
  • 16
  • 40
  • "whenever someone uploads a file": how is this done? The default file permissions depend on the umask setting of the uploading process. – Henry Feb 23 '18 at 09:58
  • What do u mean by this? umask setting of the uploading process. In linux I have a username "fileuploader" When I upload a file from fileuploader the default permissions are rw-r-r but I want rw-rw-r and I want to automate this process. – Mishi Feb 23 '18 at 10:07
  • 1
    I mean, better than correcting the permissions, make sure they are set correctly when the file is created. https://askubuntu.com/questions/44542/what-is-umask-and-how-does-it-work – Henry Feb 23 '18 at 10:10
  • I am not creating I am uploading a file. Will this link is still valid? – Mishi Feb 23 '18 at 10:12
  • I don't get what you mean by "upload". Some process must accept the file over the network and create it on disk. – Henry Feb 23 '18 at 10:17
  • I connect to filezilla via sftp and I just copy my file from my machine to linux machine – Mishi Feb 23 '18 at 10:20
  • Ok, in this case it is the ssh daemon that creates the file. Then it is not a good idea to change its umask because this would have global effects. – Henry Feb 23 '18 at 10:22
  • Yes. I don't want to change setting of linux server that is why using java – Mishi Feb 23 '18 at 10:29
  • It's better to make sure the destination directory for saving your files is in the right mode(e.g. rwx...) before the uploading process started. It's will be risky to allow the user process to change system settings. – caisil Feb 26 '18 at 11:50

1 Answers1

0

You can use NIO.2 if you are using java7 or later.

See:

How do i programmatically change file permissions?

//if you need rw-rw-r  permissions 
public void setPermission(File file) throws IOException{
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE);

    perms.add(PosixFilePermission.OTHERS_READ);
    perms.add(PosixFilePermission.OTHERS_WRITE);

    perms.add(PosixFilePermission.GROUP_READ);

    Files.setPosixFilePermissions(file.toPath(), perms);
}
Ermal
  • 441
  • 5
  • 19