1 11 package org.eclipse.core.internal.localstore; 12 13 import org.eclipse.core.filesystem.EFS; 14 import org.eclipse.core.filesystem.IFileStore; 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 CopyVisitor implements IUnifiedTreeVisitor { 24 25 26 protected IResource rootDestination; 27 28 29 protected IProgressMonitor monitor; 30 31 32 protected int updateFlags; 33 34 35 protected boolean force; 36 37 38 protected boolean isDeep; 39 40 41 protected int segmentsToDrop; 42 43 44 protected MultiStatus status; 45 46 47 protected RefreshLocalVisitor refreshLocalVisitor; 48 49 private FileSystemResourceManager localManager; 50 51 public CopyVisitor(IResource rootSource, IResource destination, int updateFlags, IProgressMonitor monitor) { 52 this.localManager = ((Resource)rootSource).getLocalManager(); 53 this.rootDestination = destination; 54 this.updateFlags = updateFlags; 55 this.isDeep = (updateFlags & IResource.SHALLOW) == 0; 56 this.force = (updateFlags & IResource.FORCE) != 0; 57 this.monitor = monitor; 58 this.segmentsToDrop = rootSource.getFullPath().segmentCount(); 59 this.status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IStatus.INFO, Messages.localstore_copyProblem, null); 60 } 61 62 protected boolean copy(UnifiedTreeNode node) { 63 Resource source = (Resource) node.getResource(); 64 IPath sufix = source.getFullPath().removeFirstSegments(segmentsToDrop); 65 Resource destination = getDestinationResource(source, sufix); 66 if (!copyProperties(source, destination)) 67 return false; 68 return copyContents(node, source, destination); 69 } 70 71 protected boolean copyContents(UnifiedTreeNode node, Resource source, Resource destination) { 72 try { 73 if (!isDeep && source.isLinked()) { 74 destination.createLink(source.getRawLocation(), updateFlags & IResource.ALLOW_MISSING_LOCAL, null); 75 return false; 76 } 77 IFileStore sourceStore = node.getStore(); 78 IFileStore destinationStore = destination.getStore(); 79 if (destination == rootDestination) 81 destinationStore.getParent().mkdir(EFS.NONE, Policy.subMonitorFor(monitor, 0)); 82 sourceStore.copy(destinationStore, EFS.SHALLOW, Policy.subMonitorFor(monitor, 0)); 83 ResourceInfo info = localManager.getWorkspace().createResource(destination, updateFlags); 85 localManager.updateLocalSync(info, destinationStore.fetchInfo().getLastModified()); 86 getWorkspace().getAliasManager().updateAliases(destination, destinationStore, IResource.DEPTH_ZERO, monitor); 88 } catch (CoreException e) { 89 status.add(e.getStatus()); 90 } 91 return true; 92 } 93 94 protected boolean copyProperties(Resource target, Resource destination) { 95 try { 96 target.getPropertyManager().copy(target, destination, IResource.DEPTH_ZERO); 97 return true; 98 } catch (CoreException e) { 99 status.add(e.getStatus()); 100 return false; 101 } 102 } 103 104 protected Resource getDestinationResource(Resource source, IPath suffix) { 105 if (suffix.segmentCount() == 0) 106 return (Resource)rootDestination; 107 IPath destinationPath = rootDestination.getFullPath().append(suffix); 108 return getWorkspace().newResource(destinationPath, source.getType()); 109 } 110 111 114 protected RefreshLocalVisitor getRefreshLocalVisitor() { 115 if (refreshLocalVisitor == null) 116 refreshLocalVisitor = new RefreshLocalVisitor(Policy.monitorFor(null)); 117 return refreshLocalVisitor; 118 } 119 120 public IStatus getStatus() { 121 return status; 122 } 123 124 protected Workspace getWorkspace() { 125 return (Workspace) rootDestination.getWorkspace(); 126 } 127 128 protected boolean isSynchronized(UnifiedTreeNode node) { 129 130 if (!node.existsInWorkspace() || !node.existsInFileSystem()) 131 return false; 132 133 if (node.isFolder() && node.getResource().getType() == IResource.FOLDER) 134 return true; 135 136 Resource target = (Resource) node.getResource(); 137 long lastModifed = target.getResourceInfo(false, false).getLocalSyncInfo(); 138 if (lastModifed != node.getLastModified()) 139 return false; 140 return true; 141 } 142 143 protected void synchronize(UnifiedTreeNode node) throws CoreException { 144 getRefreshLocalVisitor().visit(node); 145 } 146 147 public boolean visit(UnifiedTreeNode node) throws CoreException { 148 Policy.checkCanceled(monitor); 149 int work = 1; 150 try { 151 if (node.getStore() == null) { 153 IPath path = node.getResource().getFullPath(); 155 String message = NLS.bind(Messages.localstore_locationUndefined, path); 156 status.add(new ResourceStatus(IResourceStatus.FAILED_READ_LOCAL, path, message, null)); 157 return false; 158 } 159 boolean wasSynchronized = isSynchronized(node); 160 if (force && !wasSynchronized) { 161 synchronize(node); 162 work = 0; 165 if (!node.existsInFileSystem()) { 167 IPath path = node.getResource().getFullPath(); 168 String message = NLS.bind(Messages.resources_mustExist, path); 169 status.add(new ResourceStatus(IResourceStatus.RESOURCE_NOT_FOUND, path, message, null)); 170 return false; 171 } 172 } 173 if (!force && !wasSynchronized) { 174 IPath path = node.getResource().getFullPath(); 175 String message = NLS.bind(Messages.localstore_resourceIsOutOfSync, path); 176 status.add(new ResourceStatus(IResourceStatus.OUT_OF_SYNC_LOCAL, path, message, null)); 177 return true; 178 } 179 return copy(node); 180 } finally { 181 monitor.worked(work); 182 } 183 } 184 185 } 186 | Popular Tags |