Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.23 KB | None | 0 0
  1. public class PermissionParser {
  2.  
  3.     // ===========================================================
  4.     // Constants
  5.     // ===========================================================
  6.  
  7.     public static final int S_IFMT = 0170000;
  8.  
  9.     public static final int S_IFSOCK = 0140000;
  10.  
  11.     public static final int S_IFLNK = 0120000;
  12.  
  13.     public static final int S_IFREG = 0100000;
  14.  
  15.     public static final int S_IFBLK = 0060000;
  16.  
  17.     public static final int S_IFDIR = 0040000;
  18.  
  19.     public static final int S_IFCHR = 0020000;
  20.  
  21.     public static final int S_IFIFO = 0010000;
  22.  
  23.     public static final int S_IFWHT = 0160000;
  24.  
  25.     public static final int S_ISUID = 0004000;
  26.  
  27.     public static final int S_ISGID = 0002000;
  28.  
  29.     public static final int S_ISVTX = 0001000;
  30.  
  31.     public static final int S_IRWXU = 00700;
  32.  
  33.     public static final int S_IRUSR = 00400;
  34.  
  35.     public static final int S_IWUSR = 00200;
  36.  
  37.     public static final int S_IXUSR = 00100;
  38.  
  39.     public static final int S_IRWXG = 00070;
  40.  
  41.     public static final int S_IRGRP = 00040;
  42.  
  43.     public static final int S_IWGRP = 00020;
  44.  
  45.     public static final int S_IXGRP = 00010;
  46.  
  47.     public static final int S_IRWXO = 00007;
  48.  
  49.     public static final int S_IROTH = 00004;
  50.  
  51.     public static final int S_IWOTH = 00002;
  52.  
  53.     public static final int S_IXOTH = 00001;
  54.  
  55.     // ===========================================================
  56.     // Static Fields
  57.     // ===========================================================
  58.  
  59.     // ===========================================================
  60.     // Static Initializers
  61.     // ===========================================================
  62.  
  63.     // ===========================================================
  64.     // Static Methods
  65.     // ===========================================================
  66.  
  67.     public static String toNumericNotation(int st_mode) {
  68.         int i = 0;
  69.  
  70.         /* owner */
  71.         i += ((st_mode & S_IRUSR) != 0) ? S_IRUSR : 0;
  72.         i += ((st_mode & S_IWUSR) != 0) ? S_IWUSR : 0;
  73.         switch (st_mode & (S_IXUSR | S_ISUID)) {
  74.         case S_IXUSR:
  75.             i += S_IXUSR;
  76.             break;
  77.         case S_ISUID:
  78.             i += S_ISUID;
  79.             break;
  80.         case S_IXUSR | S_ISUID:
  81.             i += S_IXUSR + S_ISUID;
  82.             break;
  83.         }
  84.  
  85.         /* group */
  86.         i += ((st_mode & S_IRGRP) != 0) ? S_IRGRP : 0;
  87.         i += ((st_mode & S_IWGRP) != 0) ? S_IWGRP : 0;
  88.         switch (st_mode & (S_IXGRP | S_ISGID)) {
  89.         case S_IXGRP:
  90.             i += S_IXGRP;
  91.             break;
  92.         case S_ISGID:
  93.             i += S_ISGID;
  94.             break;
  95.         case S_IXGRP | S_ISGID:
  96.             i += S_IXGRP + S_ISGID;
  97.             break;
  98.         }
  99.  
  100.         /* other */
  101.         i += (st_mode & S_IROTH) != 0 ? S_IROTH : 0;
  102.         i += (st_mode & S_IWOTH) != 0 ? S_IWOTH : 0;
  103.         switch (st_mode & (S_IXOTH | S_ISVTX)) {
  104.         case S_IXOTH:
  105.             i += S_IXOTH;
  106.             break;
  107.         case S_ISVTX:
  108.             i += S_ISVTX;
  109.             break;
  110.         case S_IXOTH | S_ISVTX:
  111.             i += S_IXOTH + S_ISVTX;
  112.             break;
  113.         }
  114.  
  115.         return Integer.toOctalString(i);
  116.     }
  117.  
  118.     public static String toSymbolicNotation(int st_mode) {
  119.         String p = "";
  120.         switch (st_mode & S_IFMT) {
  121.         case S_IFDIR: /* directory */
  122.             p += 'd';
  123.             break;
  124.         case S_IFCHR: /* character special */
  125.             p += 'c';
  126.             break;
  127.         case S_IFBLK: /* block special */
  128.             p += 'b';
  129.             break;
  130.         case S_IFREG: /* regular */
  131.             p += '-';
  132.             break;
  133.         case S_IFLNK: /* symbolic link */
  134.             p += 'l';
  135.             break;
  136.         case S_IFSOCK: /* socket */
  137.             p += 's';
  138.             break;
  139.         case S_IFIFO: /* fifo */
  140.             p += 'p';
  141.             break;
  142.         case S_IFWHT: /* whiteout */
  143.             p += 'w';
  144.             break;
  145.         default: /* unknown */
  146.             p += '?';
  147.             break;
  148.         }
  149.  
  150.         /* owner */
  151.         p += ((st_mode & S_IRUSR) != 0) ? 'r' : '-';
  152.         p += ((st_mode & S_IWUSR) != 0) ? 'w' : '-';
  153.         switch (st_mode & (S_IXUSR | S_ISUID)) {
  154.         case 0:
  155.             p += '-';
  156.             break;
  157.         case S_IXUSR:
  158.             p += 'x';
  159.             break;
  160.         case S_ISUID:
  161.             p += 'S';
  162.             break;
  163.         case S_IXUSR | S_ISUID:
  164.             p += 's';
  165.             break;
  166.         }
  167.  
  168.         /* group */
  169.         p += ((st_mode & S_IRGRP) != 0) ? 'r' : '-';
  170.         p += ((st_mode & S_IWGRP) != 0) ? 'w' : '-';
  171.         switch (st_mode & (S_IXGRP | S_ISGID)) {
  172.         case 0:
  173.             p += '-';
  174.             break;
  175.         case S_IXGRP:
  176.             p += 'x';
  177.             break;
  178.         case S_ISGID:
  179.             p += 'S';
  180.             break;
  181.         case S_IXGRP | S_ISGID:
  182.             p += 's';
  183.             break;
  184.         }
  185.  
  186.         /* other */
  187.         p += (st_mode & S_IROTH) != 0 ? 'r' : '-';
  188.         p += (st_mode & S_IWOTH) != 0 ? 'w' : '-';
  189.         switch (st_mode & (S_IXOTH | S_ISVTX)) {
  190.         case 0:
  191.             p += '-';
  192.             break;
  193.         case S_IXOTH:
  194.             p += 'x';
  195.             break;
  196.         case S_ISVTX:
  197.             p += 'T';
  198.             break;
  199.         case S_IXOTH | S_ISVTX:
  200.             p += 't';
  201.             break;
  202.         }
  203.  
  204.         return p;
  205.     }
  206.  
  207.     public static String convertSymbolicToNumericNotation(String permissions) {
  208.         if (!permissions.matches("[rwxSTstdcb-lspw?]+")) {
  209.             throw new IllegalArgumentException("Invalid characters in string " + permissions);
  210.         }
  211.         final int length = permissions.length();
  212.         if (length == 10) {
  213.             // If the value has the character defining the file type, remove that character.
  214.             permissions = permissions.substring(1);
  215.         }
  216.         if (length != 9) {
  217.             throw new IllegalArgumentException("Invalid length. Expected 9 or 10, got " + length);
  218.         }
  219.         final int special = parseSpecialPermissions(permissions);
  220.         final int user = parsePermissions(permissions.substring(0, 3));
  221.         final int group = parsePermissions(permissions.substring(3, 6));
  222.         final int other = parsePermissions(permissions.substring(6, 9));
  223.         return String.format("%d%d%d%d", special, user, group, other);
  224.     }
  225.  
  226.     public static void convertNumericToSymbolicNotation(String mode) {
  227.         // TODO: stuff
  228.     }
  229.  
  230.     private static int parsePermissions(String str) {
  231.         if (!str.matches("[rwxSTstdcb-lspw?]+")) {
  232.             throw new IllegalArgumentException("Invalid characters in string " + str);
  233.         }
  234.         final int length = str.length();
  235.         if (length != 3) {
  236.             throw new IllegalArgumentException("Invalid length. Expected 3, got " + length);
  237.         }
  238.         final String permission = str.toLowerCase(Locale.US);
  239.         int mode = 0;
  240.         if (permission.charAt(0) == 'r') {
  241.             mode += 4;
  242.         }
  243.         if (permission.charAt(1) == 'w') {
  244.             mode += 2;
  245.         }
  246.         if (permission.charAt(2) == 'x') {
  247.             mode += 1;
  248.         }
  249.         return mode;
  250.     }
  251.  
  252.     private static int parseSpecialPermissions(String str) {
  253.         if (!str.matches("[rwxSTstdcb-lspw?]+")) {
  254.             throw new IllegalArgumentException("Invalid characters in string " + str);
  255.         }
  256.         final int length = str.length();
  257.         if (length == 10) {
  258.             // If the value has the character defining the file type, remove that character.
  259.             str = str.substring(1);
  260.         }
  261.         if (length != 9) {
  262.             throw new IllegalArgumentException("Invalid length. Expected 9 or 10, got " + length);
  263.         }
  264.         final String permission = str.toLowerCase(Locale.US);
  265.         int mode = 0;
  266.         if (permission.charAt(2) == 's') {
  267.             mode += 4;
  268.         }
  269.         if (permission.charAt(5) == 's') {
  270.             mode += 2;
  271.         }
  272.         if (permission.charAt(8) == 't') {
  273.             mode += 1;
  274.         }
  275.         return mode;
  276.     }
  277.  
  278.     // ===========================================================
  279.     // Fields
  280.     // ===========================================================
  281.  
  282.     // ===========================================================
  283.     // Initializers
  284.     // ===========================================================
  285.  
  286.     // ===========================================================
  287.     // Constructors
  288.     // ===========================================================
  289.  
  290.     // ===========================================================
  291.     // Getter & Setter
  292.     // ===========================================================
  293.  
  294.     // ===========================================================
  295.     // Methods for/from SuperClass/Interfaces
  296.     // ===========================================================
  297.  
  298.     // ===========================================================
  299.     // Methods
  300.     // ===========================================================
  301.  
  302.     // ===========================================================
  303.     // Inner and Anonymous Classes and/or Interfaces
  304.     // ===========================================================
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement