package org.eclipse.core.internal.localstore;
import java.net.URI;
import java.util.LinkedList;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.internal.resources.*;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.NLS;
public class CopyVisitor implements IUnifiedTreeVisitor {
protected IResource rootDestination;
protected SubMonitor monitor;
protected int updateFlags;
protected boolean force;
protected boolean isDeep;
protected int segmentsToDrop;
protected MultiStatus status;
protected RefreshLocalVisitor refreshLocalVisitor;
private FileSystemResourceManager localManager;
public CopyVisitor(IResource rootSource, IResource destination, int updateFlags, IProgressMonitor monitor) {
this.localManager = ((Resource) rootSource).getLocalManager();
this.rootDestination = destination;
this.updateFlags = updateFlags;
this.isDeep = (updateFlags & IResource.SHALLOW) == 0;
this.force = (updateFlags & IResource.FORCE) != 0;
this.monitor = SubMonitor.convert(monitor);
this.segmentsToDrop = rootSource.getFullPath().segmentCount();
this.status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IStatus.INFO, Messages.localstore_copyProblem, null);
}
protected boolean copy(UnifiedTreeNode node) {
Resource source = (Resource) node.getResource();
IPath sufix = source.getFullPath().removeFirstSegments(segmentsToDrop);
Resource destination = getDestinationResource(source, sufix);
if (!copyProperties(source, destination))
return false;
return copyContents(node, source, destination);
}
protected boolean copyContents(UnifiedTreeNode node, Resource source, Resource destination) {
try {
if (source.isVirtual()) {
((Folder) destination).create(IResource.VIRTUAL, true, null);
return true;
}
if ((!isDeep || source.isUnderVirtual()) && source.isLinked()) {
URI sourceLocationURI = getWorkspace().transferVariableDefinition(source, destination, source.getRawLocationURI());
destination.createLink(sourceLocationURI, updateFlags & IResource.ALLOW_MISSING_LOCAL, null);
return false;
}
if (source instanceof Container && ((Container) source).hasFilters()) {
Project sourceProject = (Project) source.getProject();
LinkedList<FilterDescription> originalDescriptions = sourceProject.internalGetDescription().getFilter(source.getProjectRelativePath());
LinkedList<FilterDescription> filterDescriptions = FilterDescription.copy(originalDescriptions, destination);
Project project = (Project) destination.getProject();
project.internalGetDescription().setFilters(destination.getProjectRelativePath(), filterDescriptions);
project.writeDescription(updateFlags);
}
IFileStore sourceStore = node.getStore();
IFileStore destinationStore = destination.getStore();
if (destination == rootDestination)
destinationStore.getParent().mkdir(EFS.NONE, monitor.newChild(0));
sourceStore.copy(destinationStore, EFS.SHALLOW, monitor.newChild(0));
ResourceInfo info = localManager.getWorkspace().createResource(destination, updateFlags);
localManager.updateLocalSync(info, destinationStore.fetchInfo().getLastModified());
getWorkspace().getAliasManager().updateAliases(destination, destinationStore, IResource.DEPTH_ZERO, monitor);
if (destination.getType() == IResource.FILE)
((File) destination).updateMetadataFiles();
} catch (CoreException e) {
status.add(e.getStatus());
}
return true;
}
protected boolean copyProperties(Resource target, Resource destination) {
try {
target.getPropertyManager().copy(target, destination, IResource.DEPTH_ZERO);
return true;
} catch (CoreException e) {
status.add(e.getStatus());
return false;
}
}
protected Resource getDestinationResource(Resource source, IPath suffix) {
if (suffix.segmentCount() == 0)
return (Resource) rootDestination;
IPath destinationPath = rootDestination.getFullPath().append(suffix);
return getWorkspace().newResource(destinationPath, source.getType());
}
protected RefreshLocalVisitor getRefreshLocalVisitor() {
if (refreshLocalVisitor == null)
refreshLocalVisitor = new RefreshLocalVisitor(SubMonitor.convert(null));
return refreshLocalVisitor;
}
public IStatus getStatus() {
return status;
}
protected Workspace getWorkspace() {
return (Workspace) rootDestination.getWorkspace();
}
protected boolean isSynchronized(UnifiedTreeNode node) {
if (node.getResource().isVirtual())
return true;
if (node.isErrorInFileSystem())
return true;
if (!node.existsInWorkspace() || !node.existsInFileSystem())
return false;
if (node.isFolder() && node.getResource().getType() == IResource.FOLDER)
return true;
Resource target = (Resource) node.getResource();
long lastModifed = target.getResourceInfo(false, false).getLocalSyncInfo();
if (lastModifed != node.getLastModified())
return false;
return true;
}
protected void synchronize(UnifiedTreeNode node) throws CoreException {
getRefreshLocalVisitor().visit(node);
}
@Override
public boolean visit(UnifiedTreeNode node) throws CoreException {
monitor.checkCanceled();
int work = 1;
try {
if (node.getStore() == null) {
IPath path = node.getResource().getFullPath();
String message = NLS.bind(Messages.localstore_locationUndefined, path);
status.add(new ResourceStatus(IResourceStatus.FAILED_READ_LOCAL, path, message, null));
return false;
}
boolean wasSynchronized = isSynchronized(node);
if (force && !wasSynchronized) {
synchronize(node);
work = 0;
if (!node.existsInFileSystem()) {
IPath path = node.getResource().getFullPath();
String message = NLS.bind(Messages.resources_mustExist, path);
status.add(new ResourceStatus(IResourceStatus.RESOURCE_NOT_FOUND, path, message, null));
return false;
}
}
if (!force && !wasSynchronized) {
IPath path = node.getResource().getFullPath();
String message = NLS.bind(Messages.localstore_resourceIsOutOfSync, path);
status.add(new ResourceStatus(IResourceStatus.OUT_OF_SYNC_LOCAL, path, message, null));
return true;
}
return copy(node);
} finally {
monitor.worked(work);
}
}
}