1 21 22 package org.armedbear.j; 23 24 import gnu.regexp.RE; 25 import gnu.regexp.REException; 26 import gnu.regexp.REMatch; 27 28 public final class SshFile extends File 29 { 30 public static final int DEFAULT_PORT = 22; 31 32 private SshFile() 33 { 34 isRemote = true; 35 protocol = PROTOCOL_SSH; 36 port = DEFAULT_PORT; 37 } 38 39 public SshFile(String hostName, String path, String userName, 40 String password, int port) 41 { 42 isRemote = true; 43 protocol = PROTOCOL_SSH; 44 this.hostName = hostName; 45 this.canonicalPath = path; 46 this.userName = userName; 47 this.password = password; 48 this.port = port; 49 } 50 51 public static SshFile getSshFile(String name) 52 { 53 SshFile file = new SshFile(); 54 if (file.initRemote(name, PREFIX_SSH)) 55 return file; 56 return null; 57 } 58 59 public static SshFile getSshFile(SshFile directory, String name) 60 { 61 SshFile file = new SshFile(); 62 63 file.hostName = directory.hostName; 64 file.userName = directory.userName; 65 file.password = directory.password; 66 file.port = directory.port; 67 68 if (Utilities.isFilenameAbsolute(name)) 69 file.canonicalPath = canonicalize(name, "/"); 70 else 71 file.canonicalPath = canonicalize(appendNameToPath(directory.canonicalPath(), name, '/'), "/"); 72 73 return file; 74 } 75 76 public final File getRoot() 77 { 78 SshFile file = new SshFile(); 79 file.hostName = this.hostName; 80 file.userName = this.userName; 81 file.password = this.password; 82 file.port = this.port; 83 file.canonicalPath = "/"; 84 file.type = TYPE_DIRECTORY; 85 return file; 86 } 87 88 public String netPath() 89 { 90 FastStringBuffer sb = new FastStringBuffer(256); 91 sb.append(PREFIX_SSH); 92 if (userName != null) { 93 sb.append(userName); 94 sb.append('@'); 95 } 96 sb.append(hostName); 97 if (port != DEFAULT_PORT) { 98 sb.append(':'); 99 sb.append(port); 100 } 101 if (canonicalPath != null) 102 sb.append(canonicalPath); 103 return sb.toString(); 104 } 105 106 public File getParentFile() 107 { 108 if (canonicalPath() == null || canonicalPath.equals("/")) 109 return null; int index = canonicalPath.lastIndexOf('/'); 111 if (index < 0) 112 return null; if (index == 0) return new SshFile(hostName, "/", userName, password, port); 115 return new SshFile(hostName, canonicalPath.substring(0, index), 116 userName, password, port); 117 } 118 119 public boolean isDirectory() 120 { 121 if (type == TYPE_LINK) { 122 if (DirectoryCache.getDirectoryCache().getListing(this) != null) 123 return true; 124 SshSession session = SshSession.getSession(this); 125 if (session != null) { 126 Debug.assertTrue(session.isLocked()); 127 boolean result = session.isDirectory(canonicalPath()); 128 session.unlock(); 129 return result; 130 } else 131 return false; 132 } 133 if (type == TYPE_UNKNOWN) { 134 if (DirectoryCache.getDirectoryCache().getListing(this) != null) 135 return true; 136 SshFile parent = (SshFile) getParentFile(); 137 if (parent != null) { 138 String listing = parent.getDirectoryListing(); 139 if (listing != null) { 140 try { 141 String pattern = 142 "\\n[d\\-][^\\n]+ " + getName() + "\\n"; 143 RE re = new RE(pattern); 144 REMatch match = re.getMatch(listing); 145 if (match != null) { 146 String s = match.toString(); 147 if (s.length() > 1) { 148 char c = s.charAt(1); 149 if (c == 'd') 150 return true; 151 if (c == '-') 152 return false; 153 } 155 } 156 } 157 catch (REException e) { 158 Log.error(e); 159 } 160 } 161 } 162 SshSession session = SshSession.getSession(this); 163 if (session != null) { 164 Debug.assertTrue(session.isLocked()); 165 if (session.isDirectory(canonicalPath())) 166 type = TYPE_DIRECTORY; 167 session.unlock(); 168 } 169 } 170 return type == TYPE_DIRECTORY; 171 } 172 173 public boolean isLink() 174 { 175 return type == TYPE_LINK; 176 } 177 178 public boolean exists() 179 { 180 SshSession session = SshSession.getSession(this); 181 if (session == null) 182 return false; 183 Debug.assertTrue(session.isLocked()); 184 boolean result = session.exists(canonicalPath()); 185 session.unlock(); 186 return result; 187 } 188 189 public String getDirectoryListing() 190 { 191 return getDirectoryListing(false); 192 } 193 194 public String getDirectoryListing(boolean forceRefresh) 195 { 196 if (!forceRefresh) { 197 String listing = 198 DirectoryCache.getDirectoryCache().getListing(this); 199 if (listing != null) 200 return listing; 201 } 202 SshSession session = SshSession.getSession(this); 203 if (session == null) 204 return null; 205 if (!session.isLocked()) { 206 Debug.bug(); 207 return null; 208 } 209 String listing = session.retrieveDirectoryListing(this); 210 session.unlock(); 211 DirectoryCache.getDirectoryCache().put(this, listing); 212 return listing; 213 } 214 215 public boolean equals(Object obj) 216 { 217 if (!(obj instanceof SshFile)) 218 return false; 219 SshFile f = (SshFile) obj; 220 if (f.protocol != protocol) 222 return false; 223 if (f.port != port) 225 return false; 226 if (f.hostName == null) { 228 if (hostName != null) 229 return false; 230 } else if (!f.hostName.equals(hostName)) 231 return false; 232 if (f.canonicalPath() == canonicalPath()) 234 return true; 235 if (f.canonicalPath == null || canonicalPath == null) 238 return false; 239 return f.canonicalPath.equals(canonicalPath); 240 } 241 242 public final String getSeparator() 243 { 244 return "/"; 245 } 246 247 public final char getSeparatorChar() 248 { 249 return '/'; 250 } 251 } 252 | Popular Tags |