001 package railo.commons.io; 002 003 import java.io.IOException; 004 005 public final class ModeUtil { 006 007 public static final int PERM_READ = 04; 008 public static final int PERM_WRITE = 02; 009 public static final int PERM_EXECUTE = 01; 010 011 public static final int ROLE_OWNER = 0100; 012 public static final int ROLE_GROUP = 010; 013 public static final int ROLE_WORLD = 01; 014 015 016 /** 017 * translate a string mode (777 or drwxrwxrwx to a octal value) 018 * @param strMode 019 * @return 020 */ 021 public static int toOctalMode(String strMode) throws IOException { 022 strMode=strMode.trim().toLowerCase(); 023 if(strMode.length()==9 || strMode.length()==10) 024 return _toOctalMode(strMode); 025 if(strMode.length()<=4 && strMode.length()>0) 026 return Integer.parseInt(strMode,8); 027 throw new IOException("can't translate ["+strMode+"] to a mode value"); 028 } 029 030 private static int _toOctalMode(String strMode) { 031 int index; 032 strMode=strMode.trim().toLowerCase(); 033 if(strMode.length()==9)index=0; 034 else index=1; 035 036 int mode=0; 037 038 // owner 039 if("r".equals(strMode.substring(index++,index)))mode+=0400; 040 if("w".equals(strMode.substring(index++,index)))mode+=0200; 041 if("x".equals(strMode.substring(index++,index)))mode+=0100; 042 // group 043 if("r".equals(strMode.substring(index++,index)))mode+=040; 044 if("w".equals(strMode.substring(index++,index)))mode+=020; 045 if("x".equals(strMode.substring(index++,index)))mode+=010; 046 // world 047 if("r".equals(strMode.substring(index++,index)))mode+=04; 048 if("w".equals(strMode.substring(index++,index)))mode+=02; 049 if("x".equals(strMode.substring(index++,index)))mode+=01; 050 return mode; 051 } 052 053 /** 054 * translate a octal mode value (73) to a string representation ("111") 055 * @param strMode 056 * @return 057 */ 058 public static String toStringMode(int octalMode) { 059 String str = Integer.toString(octalMode,8); 060 while(str.length()<3)str="0"+str; 061 return str; 062 } 063 064 /** 065 * update a string mode with a other (111+222=333 or 333+111=333 or 113+202=313) 066 * @param existing 067 * @param update 068 * @return 069 * @throws IOException 070 */ 071 public static String updateMode(String existing, String update) throws IOException { 072 return toStringMode(updateMode(toOctalMode(existing), toOctalMode(update))); 073 } 074 075 /** 076 * update octal mode with a other 077 * @param existingOctal 078 * @param updateOctal 079 * @return 080 */ 081 public static int updateMode(int existingOctal, int updateOctal) { 082 int tmp=existingOctal&updateOctal; 083 return (existingOctal-tmp)+updateOctal; 084 } 085 086 /** 087 * check mode for a specific permission 088 * @param role 089 * @param permission 090 * @param mode 091 * @return 092 */ 093 public static boolean hasPermission(int role, int permission, int mode) { 094 return (mode&(role*permission))>0; 095 } 096 097 /** check if mode is readable for owner 098 * @param octalMode 099 * @return 100 */ 101 public static boolean isReadable(int octalMode) { 102 return hasPermission(ROLE_OWNER,PERM_READ, octalMode); 103 } 104 105 /** check if mode is writeable for owner 106 * @param octalMode 107 * @return 108 */ 109 public static boolean isWritable(int octalMode) { 110 return hasPermission(ROLE_OWNER,PERM_WRITE, octalMode); 111 } 112 113 /** check if mode is executable for owner 114 * @param octalMode 115 * @return 116 */ 117 public static boolean isExecutable(int octalMode) { 118 return hasPermission(ROLE_OWNER,PERM_EXECUTE, octalMode); 119 } 120 121 public static int setReadable(int octalMode, boolean value) { 122 int tmp=octalMode&0444; 123 if(value)return (octalMode-tmp)+0444; 124 return octalMode-tmp; 125 } 126 127 public static int setWritable(int octalMode, boolean value) { 128 int tmp=octalMode&0222; 129 if(value)return (octalMode-tmp)+0222; 130 return octalMode-tmp; 131 } 132 133 public static int setExecutable(int octalMode, boolean value) { 134 int tmp=octalMode&0111; 135 if(value)return (octalMode-tmp)+0111; 136 return octalMode-tmp; 137 } 138 139 public static void main(String[] args) throws IOException { 140 System.out.println(toStringMode(toOctalMode("--xrwxrwx"))); 141 System.out.println(toStringMode(Integer.parseInt("0777",8))); 142 } 143 }