package org.eclipse.core.internal.resources;
import java.net.URI;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
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.core.runtime.jobs.ISchedulingRule;
import org.eclipse.osgi.util.NLS;
public class Folder extends Container implements IFolder {
protected Folder(IPath path, Workspace container) {
super(path, container);
}
protected void assertCreateRequirements(IFileStore store, IFileInfo localInfo, int updateFlags) throws CoreException {
checkDoesNotExist();
Container parent = (Container) getParent();
ResourceInfo info = parent.getResourceInfo(false, false);
parent.checkAccessible(getFlags(info));
checkValidGroupContainer(parent, false, false);
final boolean force = (updateFlags & IResource.FORCE) != 0;
if (!force && localInfo.exists()) {
if (!Workspace.caseSensitive) {
String name = getLocalManager().getLocalName(store);
if (name != null && !store.getName().equals(name)) {
String msg = NLS.bind(Messages.resources_existsLocalDifferentCase, new Path(store.toString()).removeLastSegments(1).append(name).toOSString());
throw new ResourceException(IResourceStatus.CASE_VARIANT_EXISTS, getFullPath(), msg, null);
}
}
String msg = NLS.bind(Messages.resources_fileExists, store.toString());
throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, getFullPath(), msg, null);
}
}
public IFile changeToFile() throws CoreException {
getPropertyManager().deleteProperties(this, IResource.DEPTH_INFINITE);
IFile result = workspace.getRoot().getFile(path);
if (isLinked()) {
URI location = getRawLocationURI();
delete(IResource.NONE, null);
result.createLink(location, IResource.ALLOW_MISSING_LOCAL, null);
} else {
workspace.deleteResource(this);
workspace.createResource(result, false);
}
return result;
}
@Override
public void create(int updateFlags, boolean local, IProgressMonitor monitor) throws CoreException {
if ((updateFlags & IResource.VIRTUAL) == IResource.VIRTUAL) {
createLink(LinkDescription.VIRTUAL_LOCATION, updateFlags, monitor);
return;
}
final boolean force = (updateFlags & IResource.FORCE) != 0;
String message = NLS.bind(Messages.resources_creating, getFullPath());
SubMonitor subMonitor = SubMonitor.convert(monitor, message, 100);
checkValidPath(path, FOLDER, true);
final ISchedulingRule rule = workspace.getRuleFactory().createRule(this);
try {
workspace.prepareOperation(rule, subMonitor.newChild(1));
IFileStore store = getStore();
IFileInfo localInfo = store.fetchInfo();
assertCreateRequirements(store, localInfo, updateFlags);
workspace.beginOperation(true);
if (force && !Workspace.caseSensitive && localInfo.exists()) {
String name = getLocalManager().getLocalName(store);
if (name == null || localInfo.getName().equals(name)) {
delete(true, null);
} else {
String msg = NLS.bind(Messages.resources_existsLocalDifferentCase,
new Path(store.toString()).removeLastSegments(1).append(name).toOSString());
throw new ResourceException(IResourceStatus.CASE_VARIANT_EXISTS, getFullPath(), msg, null);
}
}
internalCreate(updateFlags, local, subMonitor.newChild(98));
workspace.getAliasManager().updateAliases(this, getStore(), IResource.DEPTH_ZERO, subMonitor.newChild(1));
} catch (OperationCanceledException e) {
workspace.getWorkManager().operationCanceled();
throw e;
} finally {
subMonitor.done();
workspace.endOperation(rule, true);
}
}
@Override
public void create(boolean force, boolean local, IProgressMonitor monitor) throws CoreException {
create((force ? IResource.FORCE : IResource.NONE), local, monitor);
}
public void ensureExists(IProgressMonitor monitor) throws CoreException {
ResourceInfo info = getResourceInfo(false, false);
int flags = getFlags(info);
if (exists(flags, true))
return;
if (exists(flags, false)) {
String message = NLS.bind(Messages.resources_folderOverFile, getFullPath());
throw new ResourceException(IResourceStatus.RESOURCE_WRONG_TYPE, getFullPath(), message, null);
}
Container parent = (Container) getParent();
if (parent.getType() == PROJECT) {
info = parent.getResourceInfo(false, false);
parent.checkExists(getFlags(info), true);
} else
((Folder) parent).ensureExists(monitor);
if (getType() == FOLDER && isUnderVirtual())
create(IResource.VIRTUAL | IResource.FORCE, true, monitor);
else
internalCreate(IResource.FORCE, true, monitor);
}
@Override
public String getDefaultCharset(boolean checkImplicit) {
if (!exists())
return checkImplicit ? workspace.getCharsetManager().getCharsetFor(getFullPath().removeLastSegments(1), true) : null;
return workspace.getCharsetManager().getCharsetFor(getFullPath(), checkImplicit);
}
@Override
public int getType() {
return FOLDER;
}
public void internalCreate(int updateFlags, boolean local, IProgressMonitor monitor) throws CoreException {
monitor = Policy.monitorFor(monitor);
try {
String message = NLS.bind(Messages.resources_creating, getFullPath());
monitor.beginTask(message, Policy.totalWork);
workspace.createResource(this, updateFlags);
if (local) {
try {
final boolean force = (updateFlags & IResource.FORCE) != 0;
getLocalManager().write(this, force, Policy.subMonitorFor(monitor, Policy.totalWork));
} catch (CoreException e) {
workspace.deleteResource(this);
throw e;
}
}
internalSetLocal(local, DEPTH_ZERO);
if (!local)
getResourceInfo(true, true).clearModificationStamp();
} finally {
monitor.done();
}
}
}