package org.eclipse.core.internal.localstore;
import org.eclipse.core.internal.resources.*;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.internal.utils.Policy;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.NLS;
public class RefreshLocalVisitor implements IUnifiedTreeVisitor, ILocalStoreConstants {
protected static final int RL_UNKNOWN = 0;
protected static final int RL_IN_SYNC = 1;
protected static final int RL_NOT_IN_SYNC = 2;
public static final int TOTAL_WORK = 1000;
protected MultiStatus errors;
protected SubMonitor monitor;
protected boolean resourceChanged;
protected Workspace workspace;
public RefreshLocalVisitor(IProgressMonitor monitor) {
this.monitor = SubMonitor.convert(monitor);
workspace = (Workspace) ResourcesPlugin.getWorkspace();
resourceChanged = false;
String msg = Messages.resources_errorMultiRefresh;
errors = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_READ_LOCAL, msg, null);
}
protected void contentAdded(UnifiedTreeNode node, Resource target) {
resourceChanged(node, target);
}
protected void createResource(UnifiedTreeNode node, Resource target) throws CoreException {
ResourceInfo info = target.getResourceInfo(false, false);
int flags = target.getFlags(info);
if (target.exists(flags, false))
return;
IContainer parent = target.getParent();
if (parent.getType() == IResource.FOLDER)
((Folder) target.getParent()).ensureExists(monitor);
info = workspace.createResource(target, false);
info.set(ICoreConstants.M_CHILDREN_UNKNOWN);
target.getLocalManager().updateLocalSync(info, node.getLastModified());
}
protected void deleteResource(UnifiedTreeNode node, Resource target) throws CoreException {
ResourceInfo info = target.getResourceInfo(false, false);
int flags = target.getFlags(info);
if (ResourceInfo.isSet(flags, ICoreConstants.M_LINK)) {
info = target.getResourceInfo(false, true);
if (info != null) {
info.clearModificationStamp();
target.getLocalManager().updateLocalSync(info, node.getLastModified());
}
return;
}
if (target.exists(flags, false))
target.deleteResource(true, errors);
node.setExistsWorkspace(false);
}
protected void fileToFolder(UnifiedTreeNode node, Resource target) throws CoreException {
ResourceInfo info = target.getResourceInfo(false, false);
int flags = target.getFlags(info);
if (target.exists(flags, true)) {
target = (Folder) ((File) target).changeToFolder();
} else {
if (!target.exists(flags, false)) {
target = (Resource) workspace.getRoot().getFolder(target.getFullPath());
workspace.createResource(target, false);
}
}
node.setResource(target);
info = target.getResourceInfo(false, true);
target.getLocalManager().updateLocalSync(info, node.getLastModified());
}
protected void folderToFile(UnifiedTreeNode node, Resource target) throws CoreException {
ResourceInfo info = target.getResourceInfo(false, false);
int flags = target.getFlags(info);
if (target.exists(flags, true))
target = (File) ((Folder) target).changeToFile();
else {
if (!target.exists(flags, false)) {
target = (Resource) workspace.getRoot().getFile(target.getFullPath());
workspace.createResource(target, false);
}
}
node.setResource(target);
info = target.getResourceInfo(false, true);
target.getLocalManager().updateLocalSync(info, node.getLastModified());
}
public IStatus getErrorStatus() {
return errors;
}
protected void makeLocal(UnifiedTreeNode node, Resource target) {
ResourceInfo info = target.getResourceInfo(false, true);
if (info != null)
target.getLocalManager().updateLocalSync(info, node.getLastModified());
}
protected void refresh(Container parent) throws CoreException {
parent.getLocalManager().refresh(parent, IResource.DEPTH_ZERO, false, null);
}
protected void resourceChanged(UnifiedTreeNode node, Resource target) {
ResourceInfo info = target.getResourceInfo(false, true);
if (info == null)
return;
target.getLocalManager().updateLocalSync(info, node.getLastModified());
info.incrementContentId();
info.clear(ICoreConstants.M_CONTENT_CACHE);
workspace.updateModificationStamp(info);
}
public boolean resourcesChanged() {
return resourceChanged;
}
protected int synchronizeExistence(UnifiedTreeNode node, Resource target) throws CoreException {
if (node.existsInWorkspace()) {
if (!node.existsInFileSystem()) {
if (target.isLocal(IResource.DEPTH_ZERO) && target.getModificationStamp() != IResource.NULL_STAMP) {
deleteResource(node, target);
resourceChanged = true;
return RL_NOT_IN_SYNC;
}
return RL_IN_SYNC;
}
} else {
IResource genderVariant = workspace.getRoot().findMember(target.getFullPath());
if (genderVariant != null)
return RL_UNKNOWN;
if (node.existsInFileSystem()) {
Container parent = (Container) target.getParent();
if (!parent.exists()) {
refresh(parent);
if (!parent.exists())
return RL_NOT_IN_SYNC;
}
if (!target.getName().equals(node.getLocalName()))
return RL_IN_SYNC;
if (!Workspace.caseSensitive && node.getLevel() == 0) {
IResource variant = target.findExistingResourceVariant(target.getFullPath());
if (variant != null) {
deleteResource(node, ((Resource) variant));
createResource(node, target);
resourceChanged = true;
return RL_NOT_IN_SYNC;
}
}
createResource(node, target);
resourceChanged = true;
return RL_NOT_IN_SYNC;
}
}
return RL_UNKNOWN;
}
protected boolean synchronizeGender(UnifiedTreeNode node, Resource target) throws CoreException {
if (!node.existsInWorkspace()) {
IResource genderVariant = workspace.getRoot().findMember(target.getFullPath());
if (genderVariant != null)
target = (Resource) genderVariant;
}
if (target.getType() == IResource.FILE) {
if (node.isFolder()) {
fileToFolder(node, target);
resourceChanged = true;
return false;
}
} else {
if (!node.isFolder()) {
folderToFile(node, target);
resourceChanged = true;
return false;
}
}
return true;
}
protected void synchronizeLastModified(UnifiedTreeNode node, Resource target) {
if (target.isLocal(IResource.DEPTH_ZERO))
resourceChanged(node, target);
else
contentAdded(node, target);
resourceChanged = true;
}
@Override
public boolean visit(UnifiedTreeNode node) throws CoreException {
Policy.checkCanceled(monitor);
try {
if (node.isErrorInFileSystem())
return false;
Resource target = (Resource) node.getResource();
int targetType = target.getType();
if (targetType == IResource.PROJECT)
return true;
if (node.existsInWorkspace() && node.existsInFileSystem()) {
if (targetType == IResource.FOLDER && node.isFolder()) {
if (!target.isLocal(IResource.DEPTH_ZERO))
makeLocal(node, target);
ResourceInfo info = target.getResourceInfo(false, false);
if (info != null && info.getModificationStamp() != IResource.NULL_STAMP)
return true;
}
if (targetType == IResource.FILE && !node.isFolder()) {
ResourceInfo info = target.getResourceInfo(false, false);
if (info != null && info.getModificationStamp() != IResource.NULL_STAMP && info.getLocalSyncInfo() == node.getLastModified())
return true;
}
} else {
if (node.existsInFileSystem() && !Path.EMPTY.isValidSegment(node.getLocalName())) {
String message = NLS.bind(Messages.resources_invalidResourceName, node.getLocalName());
errors.merge(new ResourceStatus(IResourceStatus.INVALID_RESOURCE_NAME, message));
return false;
}
int state = synchronizeExistence(node, target);
if (state == RL_IN_SYNC || state == RL_NOT_IN_SYNC) {
if (targetType == IResource.FILE) {
try {
((File) target).updateMetadataFiles();
} catch (CoreException e) {
errors.merge(e.getStatus());
}
}
return true;
}
}
if (node.isSymbolicLink() && !node.existsInFileSystem())
return true;
if (synchronizeGender(node, target))
synchronizeLastModified(node, target);
if (targetType == IResource.FILE) {
try {
((File) target).updateMetadataFiles();
} catch (CoreException e) {
errors.merge(e.getStatus());
}
}
return true;
} finally {
monitor.setWorkRemaining(TOTAL_WORK).worked(1);
}
}
}