Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PermissionParser {
- // ===========================================================
- // Constants
- // ===========================================================
- public static final int S_IFMT = 0170000;
- public static final int S_IFSOCK = 0140000;
- public static final int S_IFLNK = 0120000;
- public static final int S_IFREG = 0100000;
- public static final int S_IFBLK = 0060000;
- public static final int S_IFDIR = 0040000;
- public static final int S_IFCHR = 0020000;
- public static final int S_IFIFO = 0010000;
- public static final int S_IFWHT = 0160000;
- public static final int S_ISUID = 0004000;
- public static final int S_ISGID = 0002000;
- public static final int S_ISVTX = 0001000;
- public static final int S_IRWXU = 00700;
- public static final int S_IRUSR = 00400;
- public static final int S_IWUSR = 00200;
- public static final int S_IXUSR = 00100;
- public static final int S_IRWXG = 00070;
- public static final int S_IRGRP = 00040;
- public static final int S_IWGRP = 00020;
- public static final int S_IXGRP = 00010;
- public static final int S_IRWXO = 00007;
- public static final int S_IROTH = 00004;
- public static final int S_IWOTH = 00002;
- public static final int S_IXOTH = 00001;
- // ===========================================================
- // Static Fields
- // ===========================================================
- // ===========================================================
- // Static Initializers
- // ===========================================================
- // ===========================================================
- // Static Methods
- // ===========================================================
- public static String toNumericNotation(int st_mode) {
- int i = 0;
- /* owner */
- i += ((st_mode & S_IRUSR) != 0) ? S_IRUSR : 0;
- i += ((st_mode & S_IWUSR) != 0) ? S_IWUSR : 0;
- switch (st_mode & (S_IXUSR | S_ISUID)) {
- case S_IXUSR:
- i += S_IXUSR;
- break;
- case S_ISUID:
- i += S_ISUID;
- break;
- case S_IXUSR | S_ISUID:
- i += S_IXUSR + S_ISUID;
- break;
- }
- /* group */
- i += ((st_mode & S_IRGRP) != 0) ? S_IRGRP : 0;
- i += ((st_mode & S_IWGRP) != 0) ? S_IWGRP : 0;
- switch (st_mode & (S_IXGRP | S_ISGID)) {
- case S_IXGRP:
- i += S_IXGRP;
- break;
- case S_ISGID:
- i += S_ISGID;
- break;
- case S_IXGRP | S_ISGID:
- i += S_IXGRP + S_ISGID;
- break;
- }
- /* other */
- i += (st_mode & S_IROTH) != 0 ? S_IROTH : 0;
- i += (st_mode & S_IWOTH) != 0 ? S_IWOTH : 0;
- switch (st_mode & (S_IXOTH | S_ISVTX)) {
- case S_IXOTH:
- i += S_IXOTH;
- break;
- case S_ISVTX:
- i += S_ISVTX;
- break;
- case S_IXOTH | S_ISVTX:
- i += S_IXOTH + S_ISVTX;
- break;
- }
- return Integer.toOctalString(i);
- }
- public static String toSymbolicNotation(int st_mode) {
- String p = "";
- switch (st_mode & S_IFMT) {
- case S_IFDIR: /* directory */
- p += 'd';
- break;
- case S_IFCHR: /* character special */
- p += 'c';
- break;
- case S_IFBLK: /* block special */
- p += 'b';
- break;
- case S_IFREG: /* regular */
- p += '-';
- break;
- case S_IFLNK: /* symbolic link */
- p += 'l';
- break;
- case S_IFSOCK: /* socket */
- p += 's';
- break;
- case S_IFIFO: /* fifo */
- p += 'p';
- break;
- case S_IFWHT: /* whiteout */
- p += 'w';
- break;
- default: /* unknown */
- p += '?';
- break;
- }
- /* owner */
- p += ((st_mode & S_IRUSR) != 0) ? 'r' : '-';
- p += ((st_mode & S_IWUSR) != 0) ? 'w' : '-';
- switch (st_mode & (S_IXUSR | S_ISUID)) {
- case 0:
- p += '-';
- break;
- case S_IXUSR:
- p += 'x';
- break;
- case S_ISUID:
- p += 'S';
- break;
- case S_IXUSR | S_ISUID:
- p += 's';
- break;
- }
- /* group */
- p += ((st_mode & S_IRGRP) != 0) ? 'r' : '-';
- p += ((st_mode & S_IWGRP) != 0) ? 'w' : '-';
- switch (st_mode & (S_IXGRP | S_ISGID)) {
- case 0:
- p += '-';
- break;
- case S_IXGRP:
- p += 'x';
- break;
- case S_ISGID:
- p += 'S';
- break;
- case S_IXGRP | S_ISGID:
- p += 's';
- break;
- }
- /* other */
- p += (st_mode & S_IROTH) != 0 ? 'r' : '-';
- p += (st_mode & S_IWOTH) != 0 ? 'w' : '-';
- switch (st_mode & (S_IXOTH | S_ISVTX)) {
- case 0:
- p += '-';
- break;
- case S_IXOTH:
- p += 'x';
- break;
- case S_ISVTX:
- p += 'T';
- break;
- case S_IXOTH | S_ISVTX:
- p += 't';
- break;
- }
- return p;
- }
- public static String convertSymbolicToNumericNotation(String permissions) {
- if (!permissions.matches("[rwxSTstdcb-lspw?]+")) {
- throw new IllegalArgumentException("Invalid characters in string " + permissions);
- }
- final int length = permissions.length();
- if (length == 10) {
- // If the value has the character defining the file type, remove that character.
- permissions = permissions.substring(1);
- }
- if (length != 9) {
- throw new IllegalArgumentException("Invalid length. Expected 9 or 10, got " + length);
- }
- final int special = parseSpecialPermissions(permissions);
- final int user = parsePermissions(permissions.substring(0, 3));
- final int group = parsePermissions(permissions.substring(3, 6));
- final int other = parsePermissions(permissions.substring(6, 9));
- return String.format("%d%d%d%d", special, user, group, other);
- }
- public static void convertNumericToSymbolicNotation(String mode) {
- // TODO: stuff
- }
- private static int parsePermissions(String str) {
- if (!str.matches("[rwxSTstdcb-lspw?]+")) {
- throw new IllegalArgumentException("Invalid characters in string " + str);
- }
- final int length = str.length();
- if (length != 3) {
- throw new IllegalArgumentException("Invalid length. Expected 3, got " + length);
- }
- final String permission = str.toLowerCase(Locale.US);
- int mode = 0;
- if (permission.charAt(0) == 'r') {
- mode += 4;
- }
- if (permission.charAt(1) == 'w') {
- mode += 2;
- }
- if (permission.charAt(2) == 'x') {
- mode += 1;
- }
- return mode;
- }
- private static int parseSpecialPermissions(String str) {
- if (!str.matches("[rwxSTstdcb-lspw?]+")) {
- throw new IllegalArgumentException("Invalid characters in string " + str);
- }
- final int length = str.length();
- if (length == 10) {
- // If the value has the character defining the file type, remove that character.
- str = str.substring(1);
- }
- if (length != 9) {
- throw new IllegalArgumentException("Invalid length. Expected 9 or 10, got " + length);
- }
- final String permission = str.toLowerCase(Locale.US);
- int mode = 0;
- if (permission.charAt(2) == 's') {
- mode += 4;
- }
- if (permission.charAt(5) == 's') {
- mode += 2;
- }
- if (permission.charAt(8) == 't') {
- mode += 1;
- }
- return mode;
- }
- // ===========================================================
- // Fields
- // ===========================================================
- // ===========================================================
- // Initializers
- // ===========================================================
- // ===========================================================
- // Constructors
- // ===========================================================
- // ===========================================================
- // Getter & Setter
- // ===========================================================
- // ===========================================================
- // Methods for/from SuperClass/Interfaces
- // ===========================================================
- // ===========================================================
- // Methods
- // ===========================================================
- // ===========================================================
- // Inner and Anonymous Classes and/or Interfaces
- // ===========================================================
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement