package com.bootstraponline;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.HashSet;
import java.util.Set;

/*

default OS X permissions:

-rw-r--r--   1 user  wheel    1 Jul  6 12:18 ok.txt --- file   - 644
drwxr-xr-x   2 user  wheel   68 Jul  6 12:17 tmp    --- folder - 755

*/

public class Visitor extends SimpleFileVisitor<Path> {

    private static Set<PosixFilePermission> dirPermission;
    private static Set<PosixFilePermission> filePermission;

    static {

        // Directory 755
        dirPermission = new HashSet<>();
        // Owner - RWE - 7
        dirPermission.add(PosixFilePermission.OWNER_READ); // 4
        dirPermission.add(PosixFilePermission.OWNER_WRITE); // 2
        dirPermission.add(PosixFilePermission.OWNER_EXECUTE); // 1
        // Group - RE - 5
        dirPermission.add(PosixFilePermission.GROUP_READ); // 4
        dirPermission.add(PosixFilePermission.GROUP_EXECUTE); // 1
        // Other - RE - 5
        dirPermission.add(PosixFilePermission.OTHERS_READ); // 4
        dirPermission.add(PosixFilePermission.OTHERS_EXECUTE); // 1

        // File 644
        filePermission = new HashSet<>();
        // Owner - RW - 6
        filePermission.add(PosixFilePermission.OWNER_READ); // 4
        filePermission.add(PosixFilePermission.OWNER_WRITE); // 2
        // Group - R - 4
        filePermission.add(PosixFilePermission.GROUP_READ); // 4
        // Other - R - 4
        filePermission.add(PosixFilePermission.OTHERS_READ); // 4
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Files.setPosixFilePermissions(dir, dirPermission);

        return super.preVisitDirectory(dir, attrs);
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.setPosixFilePermissions(file, filePermission);

        // todo: optionally set executable permission if it's already set on the file
        // allows preserving executable binaries as executable files although sometimes
        // it's desirable to fix permissions and always set 644.

        return super.visitFile(file, attrs);
    }
}

@@ -345,34 +345,8 @@
 ns;%0A
-import java.util.HashSet;%0A
 impo
@@ -367,18 +367,17 @@
 et;%0A%0A/*%0A
-%0Ad
+D
 efault O
@@ -398,22 +398,23 @@
 s:%0A%0A
--rw-r--r--   1
+%0Adrwxr-xr-x   2
  use
@@ -424,18 +424,18 @@
 wheel   
- 1
+68
  Jul  6 
@@ -442,48 +442,48 @@
 12:1
-8 ok.txt --- file   - 644%0Adrwxr-xr-x   2
+7 tmp    --- folder - 755%0A-rw-r--r--   1
  use
@@ -485,34 +485,34 @@
 1 user  wheel   
-68
+ 1
  Jul  6 12:17 tm
@@ -511,34 +511,33 @@
 12:1
-7 tmp    --- folder - 755%0A
+8 ok.txt --- file   - 644
 %0A*/%0A
@@ -592,17 +592,16 @@
 Path%3E %7B%0A
-%0A
     priv
@@ -725,17 +725,16 @@
 tatic %7B%0A
-%0A
         
@@ -740,310 +740,68 @@
  // 
-Directory 755%0A        dirPermission = new HashSet%3C%3E();%0A        // Owner - RWE - 7%0A        dirPermission.add(PosixFilePermission.OWNER_READ); // 4%0A        dirPermission.add(PosixFilePermission.OWNER_WRITE); // 2%0A        dirPermission.add(PosixFilePermission.OWNER_EXECUTE); // 1%0A        // Group - RE - 
+r = 4, w = 2, x = 1%0A%0A        // Directory %7C rwx r-x r-x %7C 75
 5%0A  
@@ -811,37 +811,35 @@
    dirPermission
-.add(
+ = 
 PosixFilePermiss
@@ -845,405 +845,80 @@
 sion
-.GROUP_READ); // 4%0A        dirPermission.add(PosixFilePermission.GROUP_EXECUTE); // 1%0A        // Other - RE - 5%0A        dirPermission.add(PosixFilePermission.OTHERS_READ); // 4%0A        dirPermission.add(PosixFilePermission.OTHERS_EXECUTE); // 1%0A%0A        // File 644%0A        filePermission = new HashSet%3C%3E();%0A        // Owner - RW - 6%0A        filePermission.add(PosixFilePermission.OWNER_READ); // 
+s.fromString(%22rwx%22 + %22r-x%22 + %22r-x%22);%0A%0A        // File %7C rw- r-- r-- %7C 64
 4%0A  
@@ -929,37 +929,35 @@
   filePermission
-.add(
+ = 
 PosixFilePermiss
@@ -963,208 +963,44 @@
 sion
-.OWNER_WRITE); // 2%0A        // Group - R - 4%0A        filePermission.add(PosixFilePermission.GROUP_READ); // 4%0A        // Other - R - 4%0A        filePermission.add(PosixFilePermission.OTHERS_READ); // 4
+s.fromString(%22rw-%22 + %22r--%22 + %22r--%22);
 %0A   

Use posix from string