package org.eclipse.core.internal.localstore;
import java.io.*;
import java.net.URI;
import java.util.*;
import org.eclipse.core.filesystem.*;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.internal.refresh.RefreshManager;
import org.eclipse.core.internal.resources.*;
import org.eclipse.core.internal.resources.File;
import org.eclipse.core.internal.utils.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.osgi.util.NLS;
import org.xml.sax.InputSource;
public class FileSystemResourceManager implements ICoreConstants, IManager, Preferences.IPropertyChangeListener {
protected IHistoryStore _historyStore;
protected Workspace workspace;
private volatile boolean lightweightAutoRefreshEnabled;
public FileSystemResourceManager(Workspace workspace) {
this.workspace = workspace;
}
protected ArrayList<IPath> allPathsForLocation(URI inputLocation) {
URI canonicalLocation = FileUtil.canonicalURI(inputLocation);
ArrayList<IPath> results = allPathsForLocationNonCanonical(canonicalLocation);
if (results.isEmpty() && canonicalLocation != inputLocation) {
results = allPathsForLocationNonCanonical(inputLocation);
}
return results;
}
private ArrayList<IPath> allPathsForLocationNonCanonical(URI inputLocation) {
URI location = inputLocation;
final boolean isFileLocation = EFS.SCHEME_FILE.equals(inputLocation.getScheme());
final IWorkspaceRoot root = getWorkspace().getRoot();
final ArrayList<IPath> results = new ArrayList<>();
if (URIUtil.equals(location, locationURIFor(root, true))) {
results.add(Path.ROOT);
return results;
}
for (IProject project : root.getProjects(IContainer.INCLUDE_HIDDEN)) {
if (!project.exists())
continue;
URI testLocation = locationURIFor(project, true);
if (testLocation == null)
continue;
boolean usingAnotherScheme = !inputLocation.getScheme().equals(testLocation.getScheme());
if (isFileLocation && !EFS.SCHEME_FILE.equals(testLocation.getScheme()))
testLocation = getFileURI(testLocation);
if (testLocation == null)
continue;
URI relative = testLocation.relativize(location);
if (!relative.isAbsolute() && !relative.equals(testLocation)) {
IPath suffix = new Path(relative.getPath());
results.add(project.getFullPath().append(suffix));
}
if (usingAnotherScheme) {
ProjectDescription description = ((Project) project).internalGetDescription();
if (description == null)
continue;
HashMap<IPath, LinkDescription> links = description.getLinks();
if (links == null)
continue;
for (LinkDescription link : links.values()) {
IResource resource = project.findMember(link.getProjectRelativePath());
IPathVariableManager pathMan = resource == null ? project.getPathVariableManager() : resource.getPathVariableManager();
testLocation = pathMan.resolveURI(link.getLocationURI());
if (isFileLocation && !EFS.SCHEME_FILE.equals(testLocation.getScheme()))
testLocation = getFileURI(testLocation);
if (testLocation == null)
continue;
relative = testLocation.relativize(location);
if (!relative.isAbsolute() && !relative.equals(testLocation)) {
IPath suffix = new Path(relative.getPath());
results.add(project.getFullPath().append(link.getProjectRelativePath()).append(suffix));
}
}
}
}
try {
findLinkedResourcesPaths(inputLocation, results);
} catch (CoreException e) {
Policy.log(e);
}
return results;
}
private void asyncRefresh(IResource target) {
if (lightweightAutoRefreshEnabled) {
RefreshManager refreshManager = workspace.getRefreshManager();
if (refreshManager != null) {
refreshManager.refresh(target);
}
}
}
private void findLinkedResourcesPaths(URI inputLocation, final ArrayList<IPath> results) throws CoreException {
IPath suffix = null;
IFileStore fileStore = EFS.getStore(inputLocation);
while (fileStore != null) {
IResource[] resources = workspace.getAliasManager().findResources(fileStore);
for (IResource resource : resources) {
if (resource.isLinked()) {
IPath path = resource.getFullPath();
if (suffix != null)
path = path.append(suffix);
if (!results.contains(path))
results.add(path);
}
}
if (suffix == null)
suffix = Path.fromPortableString(fileStore.getName());
else
suffix = Path.fromPortableString(fileStore.getName()).append(suffix);
fileStore = fileStore.getParent();
}
}
private URI getFileURI(URI locationURI) {
try {
IFileStore testLocationStore = EFS.getStore(locationURI);
java.io.File storeAsFile = testLocationStore.toLocalFile(EFS.NONE, null);
if (storeAsFile != null)
return URIUtil.toURI(storeAsFile.getAbsolutePath());
} catch (CoreException e) {
}
return null;
}
@SuppressWarnings({"rawtypes", "unchecked"})
public IResource[] allResourcesFor(URI location, boolean files, int memberFlags) {
ArrayList result = allPathsForLocation(location);
int count = 0;
for (int i = 0, imax = result.size(); i < imax; i++) {
IResource resource = resourceFor((IPath) result.get(i), files);
if (resource == null || ((Resource) resource).isFiltered() || (((memberFlags & IContainer.INCLUDE_HIDDEN) == 0) && resource.isHidden(IResource.CHECK_ANCESTORS)) || (((memberFlags & IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS) == 0) && resource.isTeamPrivateMember(IResource.CHECK_ANCESTORS)))
resource = null;
result.set(i, resource);
if (resource != null)
count++;
}
IResource[] toReturn = files ? (IResource[]) new IFile[count] : (IResource[]) new IContainer[count];
count = 0;
for (Iterator it = result.iterator(); it.hasNext();) {
IResource resource = (IResource) it.next();
if (resource != null)
toReturn[count++] = resource;
}
return toReturn;
}
public ResourceAttributes attributes(IResource resource) {
IFileStore store = getStore(resource);
IFileInfo fileInfo = store.fetchInfo();
if (!fileInfo.exists())
return null;
return FileUtil.fileInfoToAttributes(fileInfo);
}
public IContainer containerForLocation(IPath location) {
return (IContainer) resourceForLocation(location, false);
}
private IResource resourceForLocation(IPath location, boolean files) {
if (workspace.getRoot().getLocation().equals(location)) {
if (!files)
return resourceFor(Path.ROOT, false);
return null;
}
int resultProjectPathSegments = 0;
IResource result = null;
IProject[] projects = getWorkspace().getRoot().getProjects(IContainer.INCLUDE_HIDDEN);
for (IProject project : projects) {
IPath projectLocation = project.getLocation();
if (projectLocation != null && projectLocation.isPrefixOf(location)) {
int segmentsToRemove = projectLocation.segmentCount();
if (segmentsToRemove > resultProjectPathSegments) {
IPath path = project.getFullPath().append(location.removeFirstSegments(segmentsToRemove));
IResource resource = resourceFor(path, files);
if (resource != null && !((Resource) resource).isFiltered()) {
resultProjectPathSegments = segmentsToRemove;
result = resource;
}
}
}
}
return result;
}
public void copy(IResource target, IResource destination, int updateFlags, IProgressMonitor monitor) throws CoreException {
String title = NLS.bind(Messages.localstore_copying, target.getFullPath());
SubMonitor subMonitor = SubMonitor.convert(monitor, title, 100);
IFileStore destinationStore = getStore(destination);
if (destinationStore.fetchInfo().exists()) {
String message = NLS.bind(Messages.localstore_resourceExists, destination.getFullPath());
throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, destination.getFullPath(), message, null);
}
getHistoryStore().copyHistory(target, destination, false);
CopyVisitor visitor = new CopyVisitor(target, destination, updateFlags, subMonitor.split(100));
UnifiedTree tree = new UnifiedTree(target);
tree.accept(visitor, IResource.DEPTH_INFINITE);
IStatus status = visitor.getStatus();
if (!status.isOK()) {
throw new ResourceException(status);
}
}
public void delete(IResource target, int flags, IProgressMonitor monitor) throws CoreException {
Resource resource = (Resource) target;
final int deleteWork = resource.countResources(IResource.DEPTH_INFINITE, false) * 2;
boolean force = (flags & IResource.FORCE) != 0;
int refreshWork = 0;
if (!force) {
refreshWork = Math.min(deleteWork, 100);
}
String title = NLS.bind(Messages.localstore_deleting, resource.getFullPath());
SubMonitor subMonitor = SubMonitor.convert(monitor, title, deleteWork + refreshWork);
MultiStatus status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_DELETE_LOCAL, Messages.localstore_deleteProblem, null);
List<Resource> skipList = null;
UnifiedTree tree = new UnifiedTree(target);
if (!force) {
CollectSyncStatusVisitor refreshVisitor = new CollectSyncStatusVisitor(Messages.localstore_deleteProblem, subMonitor.split(refreshWork));
refreshVisitor.setIgnoreLocalDeletions(true);
tree.accept(refreshVisitor, IResource.DEPTH_INFINITE);
status.merge(refreshVisitor.getSyncStatus());
skipList = refreshVisitor.getAffectedResources();
}
DeleteVisitor deleteVisitor = new DeleteVisitor(skipList, flags, subMonitor.split(deleteWork), deleteWork);
tree.accept(deleteVisitor, IResource.DEPTH_INFINITE);
status.merge(deleteVisitor.getStatus());
if (!status.isOK()) {
throw new ResourceException(status);
}
}
private boolean descriptionChanged(IFile descriptionFile, byte[] newContents) {
int bufsize = newContents.length > 4096 ? 8192 : newContents.length * 2;
try (
InputStream oldStream = new BufferedInputStream(descriptionFile.getContents(true), bufsize);
) {
InputStream newStream = new ByteArrayInputStream(newContents);
int newChar = newStream.read();
int oldChar = oldStream.read();
while (newChar >= 0 && oldChar >= 0) {
if (newChar == oldChar) {
newChar = newStream.read();
oldChar = oldStream.read();
} else if ((newChar == '\r' || newChar == '\n') && (oldChar == '\r' || oldChar == '\n')) {
while (newChar == '\r' || newChar == '\n')
newChar = newStream.read();
while (oldChar == '\r' || oldChar == '\n')
oldChar = oldStream.read();
} else {
return true;
}
}
if (newChar >= 0 || oldChar >= 0)
return true;
return false;
} catch (Exception e) {
Policy.log(e);
}
return true;
}
@Deprecated
public int doGetEncoding(IFileStore store) throws CoreException {
try (
InputStream input = store.openInputStream(EFS.NONE, null);
) {
int first = input.read();
int second = input.read();
if (first == -1 || second == -1)
return IFile.ENCODING_UNKNOWN;
first &= 0xFF;
second &= 0xFF;
if (first == 0xFE && second == 0xFF)
return IFile.ENCODING_UTF_16BE;
if (first == 0xFF && second == 0xFE)
return IFile.ENCODING_UTF_16LE;
int third = (input.read() & 0xFF);
if (third == -1)
return IFile.ENCODING_UNKNOWN;
if (first == 0xEF && second == 0xBB && third == 0xBF)
return IFile.ENCODING_UTF_8;
return IFile.ENCODING_UNKNOWN;
} catch (IOException e) {
String message = NLS.bind(Messages.localstore_couldNotRead, store.toString());
throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, null, message, e);
}
}
public boolean fastIsSynchronized(File target) {
ResourceInfo info = target.getResourceInfo(false, false);
if (target.exists(target.getFlags(info), true)) {
IFileInfo fileInfo = getStore(target).fetchInfo();
if (!fileInfo.isDirectory() && info.getLocalSyncInfo() == fileInfo.getLastModified())
return true;
}
return false;
}
public boolean fastIsSynchronized(Folder target) {
ResourceInfo info = target.getResourceInfo(false, false);
if (target.exists(target.getFlags(info), true)) {
IFileInfo fileInfo = getStore(target).fetchInfo();
if (!fileInfo.exists() && info.getLocalSyncInfo() == fileInfo.getLastModified())
return true;
}
return false;
}
public IFile fileForLocation(IPath location) {
return (IFile) resourceForLocation(location, true);
}
@Deprecated
public int getEncoding(File target) throws CoreException {
IFileStore store = getStore(target);
if (!store.fetchInfo().exists()) {
String message = NLS.bind(Messages.localstore_fileNotFound, store.toString());
throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, target.getFullPath(), message, null);
}
return doGetEncoding(store);
}
public IHistoryStore getHistoryStore() {
if (_historyStore == null) {
IPath location = getWorkspace().getMetaArea().getHistoryStoreLocation();
location.toFile().mkdirs();
IFileStore store = EFS.getLocalFileSystem().getStore(location);
_historyStore = new HistoryStore2(getWorkspace(), store, 256);
}
return _historyStore;
}
public String getLocalName(IFileStore target) {
return target.fetchInfo().getName();
}
protected IPath getProjectDefaultLocation(IProject project) {
return workspace.getRoot().getLocation().append(project.getFullPath());
}
public IFileStore getStore(IResource target) {
try {
return getStoreRoot(target).createStore(target.getFullPath(), target);
} catch (CoreException e) {
return EFS.getNullFileSystem().getStore(target.getFullPath());
}
}
private FileStoreRoot getStoreRoot(IResource target) {
ResourceInfo info = workspace.getResourceInfo(target.getFullPath(), true, false);
FileStoreRoot root;
if (info != null) {
root = info.getFileStoreRoot();
if (root != null && root.isValid())
return root;
if (info.isSet(ICoreConstants.M_VIRTUAL)) {
ProjectDescription description = ((Project) target.getProject()).internalGetDescription();
if (description != null) {
setLocation(target, info, description.getGroupLocationURI(target.getProjectRelativePath()));
return info.getFileStoreRoot();
}
return info.getFileStoreRoot();
}
if (info.isSet(ICoreConstants.M_LINK)) {
ProjectDescription description = ((Project) target.getProject()).internalGetDescription();
if (description != null) {
final URI linkLocation = description.getLinkLocationURI(target.getProjectRelativePath());
if (linkLocation != null) {
setLocation(target, info, linkLocation);
return info.getFileStoreRoot();
}
}
}
}
final IContainer parent = target.getParent();
if (parent == null) {
info = workspace.getResourceInfo(Path.ROOT, false, true);
final IWorkspaceRoot rootResource = workspace.getRoot();
setLocation(rootResource, info, URIUtil.toURI(rootResource.getLocation()));
return info.getFileStoreRoot();
}
root = getStoreRoot(parent);
if (info != null)
info.setFileStoreRoot(root);
return root;
}
protected Workspace getWorkspace() {
return workspace;
}
public boolean hasSavedContent(IProject project) {
return getStore(project).fetchInfo().exists();
}
public boolean hasSavedDescription(IProject project) {
return getStore(project).getChild(IProjectDescription.DESCRIPTION_FILE_NAME).fetchInfo().exists();
}
private IFileStore initializeStore(IResource target, URI location) throws CoreException {
ResourceInfo info = ((Resource) target).getResourceInfo(false, true);
setLocation(target, info, location);
FileStoreRoot root = getStoreRoot(target);
return root.createStore(target.getFullPath(), target);
}
public boolean internalWrite(IProject target, IProjectDescription description, int updateFlags, boolean hasPublicChanges, boolean hasPrivateChanges) throws CoreException {
if (hasPrivateChanges)
getWorkspace().getMetaArea().writePrivateDescription(target);
if (!hasPublicChanges)
return false;
if (description == null)
return false;
ByteArrayOutputStream out = new ByteArrayOutputStream();
IFile descriptionFile = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
try {
new ModelObjectWriter().write(description, out, FileUtil.getLineSeparator(descriptionFile));
} catch (IOException e) {
String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath());
throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), msg, e);
}
byte[] newContents = out.toByteArray();
if (!descriptionFile.exists())
workspace.createResource(descriptionFile, false);
else {
if (!descriptionChanged(descriptionFile, newContents))
return false;
}
ByteArrayInputStream in = new ByteArrayInputStream(newContents);
IFileStore descriptionFileStore = ((Resource) descriptionFile).getStore();
IFileInfo fileInfo = descriptionFileStore.fetchInfo();
if (fileInfo.getAttribute(EFS.ATTRIBUTE_READ_ONLY)) {
IStatus result = getWorkspace().validateEdit(new IFile[] {descriptionFile}, null);
if (!result.isOK())
throw new ResourceException(result);
fileInfo = descriptionFileStore.fetchInfo();
}
write(descriptionFile, in, fileInfo, IResource.FORCE, false, SubMonitor.convert(null));
workspace.getAliasManager().updateAliases(descriptionFile, getStore(descriptionFile), IResource.DEPTH_ZERO, SubMonitor.convert(null));
long lastModified = ((Resource) descriptionFile).getResourceInfo(false, false).getLocalSyncInfo();
ResourceInfo info = ((Resource) target).getResourceInfo(false, true);
updateLocalSync(info, lastModified);
getWorkspace().getMetaArea().clearOldDescription(target);
return true;
}
public boolean isDescriptionSynchronized(IProject target) {
IFile descriptionFile = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
ResourceInfo projectInfo = ((Resource) target).getResourceInfo(false, false);
if (projectInfo == null)
return false;
return projectInfo.getLocalSyncInfo() == getStore(descriptionFile).fetchInfo().getLastModified();
}
public boolean isSynchronized(IResource target, int depth) {
switch (target.getType()) {
case IResource.ROOT :
if (depth == IResource.DEPTH_ZERO)
return true;
depth = depth == IResource.DEPTH_ONE ? IResource.DEPTH_ZERO : depth;
IProject[] projects = ((IWorkspaceRoot) target).getProjects(IContainer.INCLUDE_HIDDEN);
for (IProject project : projects) {
if (!isSynchronized(project, depth)) {
return false;
}
}
return true;
case IResource.PROJECT :
if (!target.isAccessible())
return true;
break;
case IResource.FOLDER :
if (fastIsSynchronized((Folder) target))
return true;
break;
case IResource.FILE :
if (fastIsSynchronized((File) target))
return true;
break;
}
IsSynchronizedVisitor visitor = new IsSynchronizedVisitor(SubMonitor.convert(null));
UnifiedTree tree = new UnifiedTree(target);
try {
tree.accept(visitor, depth);
} catch (CoreException e) {
Policy.log(e);
return false;
} catch (IsSynchronizedVisitor.ResourceChangedException e) {
asyncRefresh(e.target);
return false;
}
return true;
}
public boolean isLightweightAutoRefreshEnabled() {
return lightweightAutoRefreshEnabled;
}
public void link(Resource target, URI location, IFileInfo fileInfo) throws CoreException {
initializeStore(target, location);
ResourceInfo info = target.getResourceInfo(false, true);
long lastModified = fileInfo == null ? 0 : fileInfo.getLastModified();
if (lastModified == 0)
info.clearModificationStamp();
updateLocalSync(info, lastModified);
}
public IPath locationFor(IResource target) {
return locationFor(target, false);
}
public IPath locationFor(IResource target, boolean canonical) {
return getStoreRoot(target).localLocation(target.getFullPath(), target, false);
}
public URI locationURIFor(IResource target) {
return locationURIFor(target, false);
}
public URI locationURIFor(IResource target, boolean canonical) {
return getStoreRoot(target).computeURI(target.getFullPath(), canonical);
}
public void move(IResource source, IFileStore destination, int flags, IProgressMonitor monitor) throws CoreException {
getStore(source).move(destination, EFS.NONE, monitor);
}
@Deprecated
@Override
public void propertyChange(PropertyChangeEvent event) {
if (ResourcesPlugin.PREF_LIGHTWEIGHT_AUTO_REFRESH.equals(event.getProperty()))
lightweightAutoRefreshEnabled = Boolean.valueOf(event.getNewValue().toString());
}
public InputStream read(IFile target, boolean force, IProgressMonitor monitor) throws CoreException {
IFileStore store = getStore(target);
if (lightweightAutoRefreshEnabled || !force) {
final IFileInfo fileInfo = store.fetchInfo();
if (!fileInfo.exists()) {
asyncRefresh(target);
String message = NLS.bind(Messages.localstore_fileNotFound, store.toString());
throw new ResourceException(IResourceStatus.RESOURCE_NOT_FOUND, target.getFullPath(), message, null);
}
ResourceInfo info = ((Resource) target).getResourceInfo(true, false);
int flags = ((Resource) target).getFlags(info);
((Resource) target).checkExists(flags, true);
if (fileInfo.getLastModified() != info.getLocalSyncInfo()) {
asyncRefresh(target);
if (!force) {
String message = NLS.bind(Messages.localstore_resourceIsOutOfSync, target.getFullPath());
throw new ResourceException(IResourceStatus.OUT_OF_SYNC_LOCAL, target.getFullPath(), message, null);
}
}
}
try {
return store.openInputStream(EFS.NONE, monitor);
} catch (CoreException e) {
if (e.getStatus().getCode() == EFS.ERROR_NOT_EXISTS) {
String message = NLS.bind(Messages.localstore_fileNotFound, store.toString());
throw new ResourceException(IResourceStatus.RESOURCE_NOT_FOUND, target.getFullPath(), message, e);
}
throw e;
}
}
public ProjectDescription read(IProject target, boolean creation) throws CoreException {
URI projectLocation = null;
ProjectDescription privateDescription = null;
if (creation) {
privateDescription = new ProjectDescription();
getWorkspace().getMetaArea().readPrivateDescription(target, privateDescription);
projectLocation = privateDescription.getLocationURI();
} else {
IProjectDescription description = ((Project) target).internalGetDescription();
if (description != null && description.getLocationURI() != null) {
projectLocation = description.getLocationURI();
}
}
final boolean isDefaultLocation = projectLocation == null;
if (isDefaultLocation) {
projectLocation = URIUtil.toURI(getProjectDefaultLocation(target));
}
IFileStore projectStore = initializeStore(target, projectLocation);
IFileStore descriptionStore = projectStore.getChild(IProjectDescription.DESCRIPTION_FILE_NAME);
ProjectDescription description = null;
ResourceException error = null;
try (
InputStream in = new BufferedInputStream(descriptionStore.openInputStream(EFS.NONE, SubMonitor.convert(null)));
) {
description = new ProjectDescriptionReader(target).read(new InputSource(in));
} catch (OperationCanceledException e) {
String msg = NLS.bind(Messages.resources_missingProjectMeta, target.getName());
throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, e);
} catch (CoreException e) {
description = getWorkspace().getMetaArea().readOldDescription(target);
if (description != null)
return description;
if (!descriptionStore.fetchInfo().exists()) {
String msg = NLS.bind(Messages.resources_missingProjectMeta, target.getName());
throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, null);
}
String msg = NLS.bind(Messages.resources_readProjectMeta, target.getName());
error = new ResourceException(IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, e);
} catch (IOException ex) {
}
if (error == null && description == null) {
String msg = NLS.bind(Messages.resources_readProjectMeta, target.getName());
error = new ResourceException(IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, null);
}
if (description != null) {
if (!isDefaultLocation)
description.setLocationURI(projectLocation);
if (creation && privateDescription != null)
description.updateDynamicState(privateDescription);
}
long lastModified = descriptionStore.fetchInfo().getLastModified();
IFile descriptionFile = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
ResourceInfo info = ((Resource) descriptionFile).getResourceInfo(false, false);
if (info == null) {
info = getWorkspace().createResource(descriptionFile, false);
updateLocalSync(info, lastModified);
}
if (!creation)
updateLocalSync(info, lastModified);
info = ((Resource) target).getResourceInfo(false, true);
updateLocalSync(info, lastModified);
if (error != null)
throw error;
return description;
}
public boolean refresh(IResource target, int depth, boolean updateAliases, IProgressMonitor monitor) throws CoreException {
switch (target.getType()) {
case IResource.ROOT :
return refreshRoot((IWorkspaceRoot) target, depth, updateAliases, monitor);
case IResource.PROJECT :
if (!target.isAccessible())
return false;
case IResource.FOLDER :
case IResource.FILE :
return refreshResource(target, depth, updateAliases, monitor);
}
return false;
}
protected boolean refreshResource(IResource target, int depth, boolean updateAliases, IProgressMonitor monitor) throws CoreException {
String title = NLS.bind(Messages.localstore_refreshing, target.getFullPath());
SubMonitor subMonitor = SubMonitor.convert(monitor, title, 100);
IFileTree fileTree = null;
if (depth != IResource.DEPTH_ZERO) {
IFileStore fileStore = ((Resource) target).getStore();
fileTree = fileStore.getFileSystem().fetchFileTree(fileStore, subMonitor.newChild(2));
}
UnifiedTree tree = fileTree == null ? new UnifiedTree(target) : new UnifiedTree(target, fileTree);
SubMonitor refreshMonitor = subMonitor.newChild(98);
RefreshLocalVisitor visitor = updateAliases ? new RefreshLocalAliasVisitor(refreshMonitor) : new RefreshLocalVisitor(refreshMonitor);
tree.accept(visitor, depth);
IStatus result = visitor.getErrorStatus();
if (!result.isOK())
throw new ResourceException(result);
return visitor.resourcesChanged();
}
protected boolean refreshRoot(IWorkspaceRoot target, int depth, boolean updateAliases, IProgressMonitor monitor) throws CoreException {
IProject[] projects = target.getProjects(IContainer.INCLUDE_HIDDEN);
String title = Messages.localstore_refreshingRoot;
SubMonitor subMonitor = SubMonitor.convert(monitor, title, projects.length);
if (depth == IResource.DEPTH_ZERO)
return false;
boolean changed = false;
depth = depth == IResource.DEPTH_ONE ? IResource.DEPTH_ZERO : depth;
for (IProject project : projects) {
changed |= refresh(project, depth, updateAliases, subMonitor.newChild(1));
}
return changed;
}
protected IResource resourceFor(IPath path, boolean files) {
int numSegments = path.segmentCount();
if (files && numSegments < ICoreConstants.MINIMUM_FILE_SEGMENT_LENGTH)
return null;
IWorkspaceRoot root = getWorkspace().getRoot();
if (path.isRoot())
return root;
if (numSegments == 1)
return root.getProject(path.segment(0));
return files ? (IResource) root.getFile(path) : (IResource) root.getFolder(path);
}
public long setLocalTimeStamp(IResource target, ResourceInfo info, long value) throws CoreException {
IFileStore store = getStore(target);
IFileInfo fileInfo = store.fetchInfo();
fileInfo.setLastModified(value);
store.putInfo(fileInfo, EFS.SET_LAST_MODIFIED, null);
fileInfo = store.fetchInfo();
long actualValue = fileInfo.getLastModified();
updateLocalSync(info, actualValue);
return actualValue;
}
public void setLocation(IResource target, ResourceInfo info, URI location) {
FileStoreRoot oldRoot = info.getFileStoreRoot();
if (location != null) {
location = FileUtil.realURI(location);
info.setFileStoreRoot(new FileStoreRoot(location, target.getFullPath()));
} else {
info.setFileStoreRoot(null);
}
if (oldRoot != null)
oldRoot.setValid(false);
}
public void setResourceAttributes(IResource resource, ResourceAttributes attributes) throws CoreException {
IFileStore store = getStore(resource);
boolean refresh = false;
if (resource instanceof IContainer && ((store.getFileSystem().attributes() & EFS.ATTRIBUTE_EXECUTABLE) != 0))
refresh = store.fetchInfo().getAttribute(EFS.ATTRIBUTE_EXECUTABLE) != attributes.isExecutable();
store.putInfo(FileUtil.attributesToFileInfo(attributes), EFS.SET_ATTRIBUTES, null);
if (refresh)
workspace.getRefreshManager().refresh(resource);
}
@Override
public void shutdown(IProgressMonitor monitor) throws CoreException {
if (_historyStore != null)
_historyStore.shutdown(monitor);
ResourcesPlugin.getPlugin().getPluginPreferences().removePropertyChangeListener(this);
}
@Override
public void startup(IProgressMonitor monitor) {
Preferences preferences = ResourcesPlugin.getPlugin().getPluginPreferences();
preferences.addPropertyChangeListener(this);
lightweightAutoRefreshEnabled = preferences.getBoolean(ResourcesPlugin.PREF_LIGHTWEIGHT_AUTO_REFRESH);
}
public void updateLocalSync(ResourceInfo info, long localSyncInfo) {
info.setLocalSyncInfo(localSyncInfo);
if (localSyncInfo == I_NULL_SYNC_INFO)
info.clear(M_LOCAL_EXISTS);
else
info.set(M_LOCAL_EXISTS);
}
public void write(IFile target, InputStream content, IFileInfo fileInfo, int updateFlags, boolean append, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 4);
try {
IFileStore store = getStore(target);
if (fileInfo.getAttribute(EFS.ATTRIBUTE_READ_ONLY)) {
String message = NLS.bind(Messages.localstore_couldNotWriteReadOnly, target.getFullPath());
throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, target.getFullPath(), message, null);
}
long lastModified = fileInfo.getLastModified();
if (BitMask.isSet(updateFlags, IResource.FORCE)) {
if (append && !target.isLocal(IResource.DEPTH_ZERO) && !fileInfo.exists()) {
String message = NLS.bind(Messages.resources_mustBeLocal, target.getFullPath());
throw new ResourceException(IResourceStatus.RESOURCE_NOT_LOCAL, target.getFullPath(), message, null);
}
} else {
if (target.isLocal(IResource.DEPTH_ZERO)) {
ResourceInfo info = ((Resource) target).getResourceInfo(true, false);
if (lastModified != info.getLocalSyncInfo()) {
asyncRefresh(target);
String message = NLS.bind(Messages.localstore_resourceIsOutOfSync, target.getFullPath());
throw new ResourceException(IResourceStatus.OUT_OF_SYNC_LOCAL, target.getFullPath(), message, null);
}
if (!fileInfo.exists()) {
asyncRefresh(target);
String message = NLS.bind(Messages.localstore_resourceDoesNotExist, target.getFullPath());
throw new ResourceException(IResourceStatus.NOT_FOUND_LOCAL, target.getFullPath(), message, null);
}
} else {
if (fileInfo.exists()) {
String message = NLS.bind(Messages.localstore_resourceExists, target.getFullPath());
throw new ResourceException(IResourceStatus.EXISTS_LOCAL, target.getFullPath(), message, null);
}
if (append) {
String message = NLS.bind(Messages.resources_mustBeLocal, target.getFullPath());
throw new ResourceException(IResourceStatus.RESOURCE_NOT_LOCAL, target.getFullPath(), message, null);
}
}
}
if (BitMask.isSet(updateFlags, IResource.KEEP_HISTORY) && fileInfo.exists())
getHistoryStore().addState(target.getFullPath(), store, fileInfo, false);
if (!fileInfo.exists()) {
IFileStore parent = store.getParent();
IFileInfo parentInfo = parent.fetchInfo();
if (!parentInfo.exists()) {
parent.mkdir(EFS.NONE, null);
}
}
boolean restoreHiddenAttribute = false;
if (fileInfo.exists() && fileInfo.getAttribute(EFS.ATTRIBUTE_HIDDEN) && Platform.getOS().equals(Platform.OS_WIN32)) {
fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, false);
store.putInfo(fileInfo, EFS.SET_ATTRIBUTES, subMonitor.split(1));
restoreHiddenAttribute = true;
} else {
subMonitor.split(1);
}
int options = append ? EFS.APPEND : EFS.NONE;
OutputStream out = store.openOutputStream(options, subMonitor.split(1));
if (restoreHiddenAttribute) {
fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, true);
store.putInfo(fileInfo, EFS.SET_ATTRIBUTES, subMonitor.split(1));
} else {
subMonitor.split(1);
}
FileUtil.transferStreams(content, out, store.toString(), subMonitor.split(1));
lastModified = store.fetchInfo().getLastModified();
ResourceInfo info = ((Resource) target).getResourceInfo(false, true);
updateLocalSync(info, lastModified);
info.incrementContentId();
info.clear(M_CONTENT_CACHE);
workspace.updateModificationStamp(info);
} finally {
FileUtil.safeClose(content);
}
}
public void write(IFolder target, boolean force, IProgressMonitor monitor) throws CoreException {
IFileStore store = getStore(target);
if (!force) {
IFileInfo fileInfo = store.fetchInfo();
if (fileInfo.isDirectory()) {
String message = NLS.bind(Messages.localstore_resourceExists, target.getFullPath());
throw new ResourceException(IResourceStatus.EXISTS_LOCAL, target.getFullPath(), message, null);
}
if (fileInfo.exists()) {
String message = NLS.bind(Messages.localstore_fileExists, target.getFullPath());
throw new ResourceException(IResourceStatus.OUT_OF_SYNC_LOCAL, target.getFullPath(), message, null);
}
}
store.mkdir(EFS.NONE, monitor);
ResourceInfo info = ((Resource) target).getResourceInfo(false, true);
updateLocalSync(info, store.fetchInfo().getLastModified());
}
public void writeSilently(IProject target) throws CoreException {
IPath location = locationFor(target, false);
if (location == null)
return;
IFileStore projectStore = getStore(target);
projectStore.mkdir(EFS.NONE, null);
IProjectDescription desc = ((Project) target).internalGetDescription();
if (desc == null)
return;
getWorkspace().getMetaArea().writePrivateDescription(target);
IFileStore fileStore = projectStore.getChild(IProjectDescription.DESCRIPTION_FILE_NAME);
try (
OutputStream out = fileStore.openOutputStream(EFS.NONE, null)
) {
IFile file = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
new ModelObjectWriter().write(desc, out, FileUtil.getLineSeparator(file));
} catch (IOException e) {
String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath());
throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), msg, e);
}
getWorkspace().getMetaArea().clearOldDescription(target);
}
}