0

I am reading files from an input directory, modifying some of their content and writing it to the output directory. The problem is that in the output directory, I am owner of all files. Is there a way to do something with this process, such that the output files have exactly same permissions as originals as well as owner and group.

Thank you,

Alex

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
Alex
  • 1,054
  • 6
  • 25
  • 42

1 Answers1

1

Based on the original file, you can know what permissions you need. For the new file, fiddle around with File.setReadable(boolean readable), setWriteable(), setExecutable() API's to set the permission for the new files to be the exact same as the one for your original file.

EDIT: NOTE. This is for Java >= 7. To set all perms for a file (akin to chmod), move over to Files. There's this method setPosixFilePermissions(..). You will need to import the 3 relevant classes from java.nio.file.*

I had used it like so...

    Set<PosixFilePermission> filePermission = new HashSet<PosixFilePermission>();

    // OWNER rwe --- ---
    filePermission.add(PosixFilePermission.OWNER_READ);
    filePermission.add(PosixFilePermission.OWNER_WRITE);
    filePermission.add(PosixFilePermission.OWNER_EXECUTE);

    // GROUP --- rwe ---
    filePermission.add(PosixFilePermission.GROUP_READ);
    filePermission.add(PosixFilePermission.GROUP_WRITE);
    filePermission.add(PosixFilePermission.GROUP_EXECUTE);

    // GLOBAL --- --- rwe
    filePermission.add(PosixFilePermission.OTHERS_READ);
    filePermission.add(PosixFilePermission.OTHERS_WRITE);
    filePermission.add(PosixFilePermission.OTHERS_EXECUTE);

    Files.setPosixFilePermissions(Paths.get("{PATH_TO_FILE}"), filePermission);
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • I don't think this will work. according to documentation setReadable is "A convenience method to set the owner's read permission for this abstract pathname. " It does not handle group and other permissions. – Alex Mar 21 '16 at 22:29
  • 1
    Isn't that what I said? You need all 3 methods to set the permissions, not just one. Please try it out. – Debosmit Ray Mar 21 '16 at 22:31
  • I am not sure I am getting you. 3 methods for read/write and execute? what about permission for group and others? For example for execute rights from documentation: "Parameters:executable - If true, sets the access permission to allow execute operations; if false to disallow execute operationsownerOnly-If true, the execute permission applies only to the owner's execute permission; otherwise, it applies to everybody. If the underlying file system can not distinguish the owner's execute permission from that of others, then the permission will apply to everybody, regardless of this value." – Alex Mar 21 '16 at 22:49
  • So I can't using this method set rwx--x--- – Alex Mar 21 '16 at 22:50
  • 1
    @Alex i apologize. i misunderstood your question. i think the edit i made to the answer is what you are looking for. – Debosmit Ray Mar 22 '16 at 00:21
  • 1
    @Alex Please let me know if there are any other questions. – Debosmit Ray Mar 22 '16 at 00:22
  • 1
    Prior to Java 6, there was no resources to change file permission. Java 7 saw the introduction of the new IO library `nio`. If you are stuck to Java 6, I suggest [Apache Commons exec library](https://commons.apache.org/proper/commons-exec/) to execute a query to `chmod` it [ [resource](http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code) ] – Debosmit Ray Mar 22 '16 at 00:26