1 11 package org.eclipse.core.internal.localstore; 12 13 import java.io.*; 14 import java.io.File ; 15 import org.eclipse.core.internal.resources.*; 16 import org.eclipse.core.internal.utils.Messages; 17 import org.eclipse.core.internal.utils.Policy; 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.osgi.util.NLS; 21 22 public class FileSystemStore implements ILocalStoreConstants { 23 28 private final byte[] buffer = new byte[8192]; 29 30 public FileSystemStore() { 31 super(); 32 } 33 34 public void copy(File source, File destination, int depth, IProgressMonitor monitor) throws CoreException { 35 monitor = Policy.monitorFor(monitor); 36 try { 37 monitor.beginTask(NLS.bind(Messages.localstore_copying, source.getAbsolutePath()), 1); 38 Policy.checkCanceled(monitor); 39 if (source.isDirectory()) 40 copyDirectory(source, destination, depth, Policy.subMonitorFor(monitor, 1)); 41 else 42 copyFile(source, destination, Policy.subMonitorFor(monitor, 1)); 43 } finally { 44 monitor.done(); 45 } 46 } 47 48 protected void copyDirectory(File source, File destination, int depth, IProgressMonitor monitor) throws CoreException { 49 monitor = Policy.monitorFor(monitor); 50 try { 51 String [] children = source.list(); 52 if (children == null) { 53 children = new String [0]; 54 } 55 56 monitor.beginTask(NLS.bind(Messages.localstore_copying, source.getAbsolutePath()), children.length); 57 writeFolder(destination); 59 60 if (depth == IResource.DEPTH_ZERO) 62 return; 63 if (depth == IResource.DEPTH_ONE) 64 depth = IResource.DEPTH_ZERO; 65 66 for (int i = 0; i < children.length; i++) 68 copy(new File (source, children[i]), new File (destination, children[i]), depth, Policy.subMonitorFor(monitor, 1)); 69 } finally { 70 monitor.done(); 71 } 72 } 73 74 protected void copyFile(File target, File destination, IProgressMonitor monitor) throws CoreException { 75 monitor = Policy.monitorFor(monitor); 76 try { 77 int totalWork = 1 + ((int) target.length() / 8192); 78 monitor.beginTask(NLS.bind(Messages.localstore_copying, target.getAbsolutePath()), totalWork); 79 try { 80 write(destination, read(target), false, monitor); 81 } catch (CoreException e) { 82 if (!destination.isDirectory()) 84 destination.delete(); 85 throw e; 86 } 87 long stat = CoreFileSystemLibrary.getStat(target.getAbsolutePath()); 89 long lastModified = CoreFileSystemLibrary.getLastModified(stat); 90 destination.setLastModified(lastModified); 91 CoreFileSystemLibrary.copyAttributes(target.getAbsolutePath(), destination.getAbsolutePath(), false); 93 } finally { 94 monitor.done(); 95 } 96 } 97 98 102 protected OutputStream createStream(File target, boolean append) throws CoreException { 103 String path = target.getAbsolutePath(); 104 try { 105 return new FileOutputStream(path, append); 106 } catch (FileNotFoundException e) { 107 String message; 108 int code = IResourceStatus.FAILED_WRITE_LOCAL; 109 String parent = target.getParent(); 112 if (parent != null && CoreFileSystemLibrary.isReadOnly(parent)) { 113 message = NLS.bind(Messages.localstore_readOnlyParent, path); 114 code = IResourceStatus.PARENT_READ_ONLY; 115 } else if (target.isDirectory()) 116 message = NLS.bind(Messages.localstore_notAFile, path); 117 else 118 message = NLS.bind(Messages.localstore_couldNotWrite, path); 119 throw new ResourceException(code, new Path(path), message, e); 120 } 121 } 122 123 public void delete(File target) throws CoreException { 124 if (!Workspace.clear(target)) { 125 String message = NLS.bind(Messages.localstore_couldnotDelete, target.getAbsolutePath()); 126 throw new ResourceException(IResourceStatus.FAILED_DELETE_LOCAL, new Path(target.getAbsolutePath()), message, null); 127 } 128 } 129 130 134 public boolean delete(File root, MultiStatus status) { 135 return delete(root, root.getAbsolutePath(), status); 136 } 137 138 143 protected boolean delete(File root, String filePath, MultiStatus status) { 144 boolean failedRecursive = false; 145 if (root.isDirectory()) { 146 String [] list = root.list(); 147 if (list != null) { 148 int parentLength = filePath.length(); 149 for (int i = 0, imax = list.length; i < imax; i++) { 150 StringBuffer childBuffer = new StringBuffer (parentLength + list[i].length() + 1); 152 childBuffer.append(filePath); 153 childBuffer.append(File.separatorChar); 154 childBuffer.append(list[i]); 155 String childName = childBuffer.toString(); 156 failedRecursive = !delete(new java.io.File (childName), childName, status) || failedRecursive; 158 } 159 } 160 } 161 boolean failedThis = false; 162 try { 163 if (!failedRecursive && root.exists()) 165 failedThis = !root.delete(); 166 } catch (Exception e) { 167 String message = NLS.bind(Messages.localstore_couldnotDelete, root.getAbsolutePath()); 169 status.add(new ResourceStatus(IResourceStatus.FAILED_DELETE_LOCAL, new Path(root.getAbsolutePath()), message, e)); 170 return false; 171 } 172 if (failedThis) { 173 String message = null; 174 if (CoreFileSystemLibrary.isReadOnly(root.getAbsolutePath())) 175 message = NLS.bind(Messages.localstore_couldnotDeleteReadOnly, root.getAbsolutePath()); 176 else 177 message = NLS.bind(Messages.localstore_couldnotDelete, root.getAbsolutePath()); 178 status.add(new ResourceStatus(IResourceStatus.FAILED_DELETE_LOCAL, new Path(root.getAbsolutePath()), message, null)); 179 } 180 return !(failedRecursive || failedThis); 181 } 182 183 186 public int getEncoding(File target) throws CoreException { 187 InputStream input = null; 188 try { 189 input = read(target); 190 int first = input.read(); 191 int second = input.read(); 192 if (first == -1 || second == -1) 193 return IFile.ENCODING_UNKNOWN; 194 first &= 0xFF; second &= 0xFF; 196 if (first == 0xFE && second == 0xFF) 198 return IFile.ENCODING_UTF_16BE; 199 if (first == 0xFF && second == 0xFE) 200 return IFile.ENCODING_UTF_16LE; 201 int third = (input.read() & 0xFF); 202 if (third == -1) 203 return IFile.ENCODING_UNKNOWN; 204 if (first == 0xEF && second == 0xBB && third == 0xBF) 206 return IFile.ENCODING_UTF_8; 207 return IFile.ENCODING_UNKNOWN; 208 } catch (IOException e) { 209 String message = NLS.bind(Messages.localstore_couldNotRead, target.getAbsolutePath()); 210 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, new Path(target.getAbsolutePath()), message, e); 211 } finally { 212 if (input != null) { 213 try { 214 input.close(); 215 } catch (IOException e) { 216 } 218 } 219 } 220 } 221 222 public void move(File source, File destination, boolean force, IProgressMonitor monitor) throws CoreException { 223 monitor = Policy.monitorFor(monitor); 224 try { 225 monitor.beginTask(NLS.bind(Messages.localstore_moving, source.getAbsolutePath()), 2); 226 boolean sourceEqualsDest = false; 230 try { 231 sourceEqualsDest = source.getCanonicalFile().equals(destination.getCanonicalFile()); 232 } catch (IOException e) { 233 String message = NLS.bind(Messages.localstore_couldNotMove, source.getAbsolutePath()); 234 throw new ResourceException(new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, new Path(source.getAbsolutePath()), message, e)); 235 } 236 if (!sourceEqualsDest && !force && destination.exists()) { 237 String message = NLS.bind(Messages.localstore_resourceExists, destination.getAbsolutePath()); 238 throw new ResourceException(IResourceStatus.EXISTS_LOCAL, new Path(destination.getAbsolutePath()), message, null); 239 } 240 if (source.renameTo(destination)) { 241 if (!sourceEqualsDest && source.exists()) { 244 if (destination.exists()) { 246 Workspace.clear(destination); 250 String message = NLS.bind(Messages.localstore_couldnotDelete, source.getAbsolutePath()); 251 throw new ResourceException(new ResourceStatus(IResourceStatus.FAILED_DELETE_LOCAL, new Path(source.getAbsolutePath()), message, null)); 252 } else { 253 } 255 } else { 256 if (destination.exists()) { 257 return; 259 } else { 260 String message = NLS.bind(Messages.localstore_failedMove, source.getAbsolutePath(), destination.getAbsolutePath()); 262 throw new ResourceException(new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, new Path(source.getAbsolutePath()), message, null)); 263 } 264 } 265 } 266 if (sourceEqualsDest) { 269 String message = NLS.bind(Messages.localstore_couldNotMove, source.getAbsolutePath()); 270 throw new ResourceException(new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, new Path(source.getAbsolutePath()), message, null)); 271 } 272 boolean success = false; 273 boolean canceled = false; 274 try { 275 copy(source, destination, IResource.DEPTH_INFINITE, Policy.subMonitorFor(monitor, 1)); 276 success = true; 277 } catch (OperationCanceledException e) { 278 canceled = true; 279 throw e; 280 } finally { 281 if (success) { 282 String message = Messages.localstore_deleteProblemDuringMove; 284 MultiStatus result = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_DELETE_LOCAL, message, null); 285 if (!delete(source, result)) 286 throw new ResourceException(result); 287 } else { 288 if (!canceled) { 289 String message = NLS.bind(Messages.localstore_couldNotMove, source.getAbsolutePath()); 293 throw new ResourceException(new ResourceStatus(IResourceStatus.FAILED_WRITE_LOCAL, new Path(source.getAbsolutePath()), message, null)); 294 } 295 } 296 } 297 monitor.worked(1); 298 } finally { 299 monitor.done(); 300 } 301 } 302 303 311 public InputStream read(File target) throws CoreException { 312 try { 313 return new FileInputStream(target); 314 } catch (FileNotFoundException e) { 315 String message; 316 if (!target.exists()) 317 message = NLS.bind(Messages.localstore_fileNotFound, target.getAbsolutePath()); 318 else if (target.isDirectory()) 319 message = NLS.bind(Messages.localstore_notAFile, target.getAbsolutePath()); 320 else 321 message = NLS.bind(Messages.localstore_couldNotRead, target.getAbsolutePath()); 322 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, new Path(target.getAbsolutePath()), message, e); 323 } 324 } 325 326 331 public void transferStreams(InputStream source, OutputStream destination, String path, IProgressMonitor monitor) throws CoreException { 332 monitor = Policy.monitorFor(monitor); 333 try { 334 339 synchronized (buffer) { 340 while (true) { 341 int bytesRead = -1; 342 try { 343 bytesRead = source.read(buffer); 344 } catch (IOException e) { 345 String msg = NLS.bind(Messages.localstore_failedReadDuringWrite, path); 346 IPath p = path == null ? null : new Path(path); 347 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, p, msg, e); 348 } 349 if (bytesRead == -1) 350 break; 351 try { 352 destination.write(buffer, 0, bytesRead); 353 } catch (IOException e) { 354 String msg = NLS.bind(Messages.localstore_couldNotWrite, path); 355 IPath p = path == null ? null : new Path(path); 356 throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, p, msg, e); 357 } 358 monitor.worked(1); 359 } 360 } 361 } finally { 362 try { 363 source.close(); 364 } catch (IOException e) { 365 } finally { 367 try { 369 destination.close(); 370 } catch (IOException e) { 371 } 373 } 374 } 375 } 376 377 382 public void write(File target, InputStream content, boolean append, IProgressMonitor monitor) throws CoreException { 383 try { 384 String path = target.getAbsolutePath(); 385 writeFolder(target.getParentFile()); 386 transferStreams(content, createStream(target, append), path, monitor); 387 } finally { 388 try { 389 content.close(); 390 } catch (IOException e) { 391 } 393 } 394 } 395 396 public void writeFolder(File target) throws CoreException { 397 if (!target.exists()) 398 target.mkdirs(); 399 if (!target.isDirectory()) { 400 String path = target.getAbsolutePath(); 401 int code = IResourceStatus.FAILED_WRITE_LOCAL; 402 String message = NLS.bind(Messages.localstore_couldNotCreateFolder, path); 403 String parent = target.getParent(); 406 if (parent != null && CoreFileSystemLibrary.isReadOnly(parent)) { 407 message = NLS.bind(Messages.localstore_readOnlyParent, path); 408 code = IResourceStatus.PARENT_READ_ONLY; 409 } 410 throw new ResourceException(code, new Path(path), message, null); 411 } 412 } 413 414 } 415 | Popular Tags |