1 11 package org.eclipse.core.internal.resources; 12 13 import java.io.*; 14 import java.net.URI ; 15 import org.eclipse.core.filesystem.URIUtil; 16 import org.eclipse.core.internal.localstore.SafeChunkyInputStream; 17 import org.eclipse.core.internal.localstore.SafeChunkyOutputStream; 18 import org.eclipse.core.internal.utils.Messages; 19 import org.eclipse.core.internal.utils.Policy; 20 import org.eclipse.core.resources.*; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.osgi.util.NLS; 23 24 public class LocalMetaArea implements ICoreConstants { 25 static final String F_BACKUP_FILE_EXTENSION = ".bak"; static final String F_DESCRIPTION = ".workspace"; 28 static final String F_HISTORY_STORE = ".history"; static final String F_MARKERS = ".markers"; static final String F_OLD_PROJECT = ".prj"; static final String F_PROJECT_LOCATION = ".location"; static final String F_PROJECTS = ".projects"; static final String F_PROPERTIES = ".properties"; static final String F_ROOT = ".root"; static final String F_SAFE_TABLE = ".safetable"; static final String F_SNAP = ".snap"; static final String F_SNAP_EXTENSION = "snap"; static final String F_SYNCINFO = ".syncinfo"; static final String F_TREE = ".tree"; static final String URI_PREFIX = "URI//"; 42 43 protected final IPath metaAreaLocation; 44 45 48 protected final IPath projectMetaLocation; 49 50 public LocalMetaArea() { 51 super(); 52 metaAreaLocation = ResourcesPlugin.getPlugin().getStateLocation(); 53 projectMetaLocation = metaAreaLocation.append(F_PROJECTS); 54 } 55 56 60 public void clearOldDescription(IProject target) { 61 Workspace.clear(getOldDescriptionLocationFor(target).toFile()); 62 } 63 64 public void create(IProject target) { 65 java.io.File file = locationFor(target).toFile(); 66 Workspace.clear(file); 68 file.mkdirs(); 69 } 70 71 74 public synchronized void createMetaArea() throws CoreException { 75 java.io.File workspaceLocation = metaAreaLocation.toFile(); 76 Workspace.clear(workspaceLocation); 77 if (!workspaceLocation.mkdirs()) { 78 String message = NLS.bind(Messages.resources_writeWorkspaceMeta, workspaceLocation); 79 throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, null); 80 } 81 } 82 83 87 public void delete(IProject target) throws CoreException { 88 IPath path = locationFor(target); 89 if (!Workspace.clear(path.toFile()) && path.toFile().exists()) { 90 String message = NLS.bind(Messages.resources_deleteMeta, target.getFullPath()); 91 throw new ResourceException(IResourceStatus.FAILED_DELETE_METADATA, target.getFullPath(), message, null); 92 } 93 } 94 95 public IPath getBackupLocationFor(IPath file) { 96 return file.removeLastSegments(1).append(file.lastSegment() + F_BACKUP_FILE_EXTENSION); 97 } 98 99 public IPath getHistoryStoreLocation() { 100 return metaAreaLocation.append(F_HISTORY_STORE); 101 } 102 103 107 public IPath getLocation() { 108 return metaAreaLocation; 109 } 110 111 115 public IPath getMarkersLocationFor(IResource resource) { 116 Assert.isNotNull(resource); 117 Assert.isLegal(resource.getType() == IResource.ROOT || resource.getType() == IResource.PROJECT); 118 return locationFor(resource).append(F_MARKERS); 119 } 120 121 125 public IPath getMarkersSnapshotLocationFor(IResource resource) { 126 return getMarkersLocationFor(resource).addFileExtension(F_SNAP_EXTENSION); 127 } 128 129 135 public IPath getOldDescriptionLocationFor(IProject target) { 136 return locationFor(target).append(F_OLD_PROJECT); 137 } 138 139 public IPath getOldWorkspaceDescriptionLocation() { 140 return metaAreaLocation.append(F_DESCRIPTION); 141 } 142 143 public IPath getPropertyStoreLocation(IResource resource) { 144 int type = resource.getType(); 145 Assert.isTrue(type != IResource.FILE && type != IResource.FOLDER); 146 return locationFor(resource).append(F_PROPERTIES); 147 } 148 149 public IPath getSafeTableLocationFor(String pluginId) { 150 IPath prefix = metaAreaLocation.append(F_SAFE_TABLE); 151 if (pluginId.equals(ResourcesPlugin.PI_RESOURCES)) 154 return prefix.append(pluginId); int saveNumber = getWorkspace().getSaveManager().getSaveNumber(pluginId); 156 return prefix.append(pluginId + "." + saveNumber); } 158 159 public IPath getSnapshotLocationFor(IResource resource) { 160 return metaAreaLocation.append(F_SNAP); 161 } 162 163 168 public IPath getSyncInfoLocationFor(IResource resource) { 169 Assert.isNotNull(resource); 170 Assert.isLegal(resource.getType() == IResource.ROOT || resource.getType() == IResource.PROJECT); 171 return locationFor(resource).append(F_SYNCINFO); 172 } 173 174 179 public IPath getSyncInfoSnapshotLocationFor(IResource resource) { 180 return getSyncInfoLocationFor(resource).addFileExtension(F_SNAP_EXTENSION); 181 } 182 183 189 public IPath getTreeLocationFor(IResource target, boolean updateSequenceNumber) { 190 IPath key = target.getFullPath().append(F_TREE); 191 String sequenceNumber = getWorkspace().getSaveManager().getMasterTable().getProperty(key.toString()); 192 if (sequenceNumber == null) 193 sequenceNumber = "0"; if (updateSequenceNumber) { 195 int n = new Integer (sequenceNumber).intValue() + 1; 196 n = n < 0 ? 1 : n; 197 sequenceNumber = new Integer (n).toString(); 198 getWorkspace().getSaveManager().getMasterTable().setProperty(key.toString(), new Integer (sequenceNumber).toString()); 199 } 200 return locationFor(target).append(sequenceNumber + F_TREE); 201 } 202 203 public IPath getWorkingLocation(IResource resource, String id) { 204 return locationFor(resource).append(id); 205 } 206 207 protected Workspace getWorkspace() { 208 return (Workspace) ResourcesPlugin.getWorkspace(); 209 } 210 211 public boolean hasSavedProject(IProject project) { 212 return getOldDescriptionLocationFor(project).toFile().exists() || locationFor(project).append(F_PROJECT_LOCATION).toFile().exists(); 214 } 215 216 public boolean hasSavedWorkspace() { 217 return metaAreaLocation.toFile().exists() || getBackupLocationFor(metaAreaLocation).toFile().exists(); 218 } 219 220 224 public IPath locationFor(IPath resourcePath) { 225 if (Path.ROOT.equals(resourcePath)) 226 return metaAreaLocation.append(F_ROOT); 227 return projectMetaLocation.append(resourcePath.segment(0)); 228 } 229 230 234 public IPath locationFor(IResource resource) { 235 if (resource.getType() == IResource.ROOT) 236 return metaAreaLocation.append(F_ROOT); 237 return projectMetaLocation.append(resource.getProject().getName()); 238 } 239 240 245 public ProjectDescription readOldDescription(IProject project) throws CoreException { 246 IPath path = getOldDescriptionLocationFor(project); 247 if (!path.toFile().exists()) 248 return null; 249 IPath tempPath = getBackupLocationFor(path); 250 ProjectDescription description = null; 251 try { 252 description = new ProjectDescriptionReader(project).read(path, tempPath); 253 } catch (IOException e) { 254 String msg = NLS.bind(Messages.resources_readMeta, project.getName()); 255 throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, project.getFullPath(), msg, e); 256 } 257 if (description == null) { 258 String msg = NLS.bind(Messages.resources_readMeta, project.getName()); 259 throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, project.getFullPath(), msg, null); 260 } 261 return description; 262 } 263 264 268 public WorkspaceDescription readOldWorkspace() { 269 IPath path = getOldWorkspaceDescriptionLocation(); 270 IPath tempPath = getBackupLocationFor(path); 271 try { 272 WorkspaceDescription oldDescription = (WorkspaceDescription) new WorkspaceDescriptionReader().read(path, tempPath); 273 Workspace.clear(path.toFile()); 275 Workspace.clear(tempPath.toFile()); 276 return oldDescription; 277 } catch (IOException e) { 278 return null; 279 } 280 } 281 282 295 public void readPrivateDescription(IProject target, IProjectDescription description) { 296 IPath locationFile = locationFor(target).append(F_PROJECT_LOCATION); 297 java.io.File file = locationFile.toFile(); 298 if (!file.exists()) { 299 locationFile = getBackupLocationFor(locationFile); 300 file = locationFile.toFile(); 301 if (!file.exists()) 302 return; 303 } 304 try { 305 SafeChunkyInputStream input = new SafeChunkyInputStream(file, 500); 306 DataInputStream dataIn = new DataInputStream(input); 307 try { 308 try { 309 String location = dataIn.readUTF(); 310 if (location.length() > 0) { 311 if (location.startsWith(URI_PREFIX)) 314 description.setLocationURI(URI.create(location.substring(URI_PREFIX.length()))); 315 else 316 description.setLocationURI(URIUtil.toURI(Path.fromOSString(location))); 317 } 318 } catch (Exception e) { 319 String msg = NLS.bind(Messages.resources_exReadProjectLocation, target.getName()); 321 Policy.log(new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, e)); 322 } 323 int numRefs = dataIn.readInt(); 325 IProject[] references = new IProject[numRefs]; 326 IWorkspaceRoot root = getWorkspace().getRoot(); 327 for (int i = 0; i < numRefs; i++) 328 references[i] = root.getProject(dataIn.readUTF()); 329 description.setDynamicReferences(references); 330 } finally { 331 dataIn.close(); 332 } 333 } catch (IOException e) { 334 } 337 } 338 339 346 public synchronized void write(WorkspaceDescription description) throws CoreException { 347 IPath path = getOldWorkspaceDescriptionLocation(); 348 path.toFile().getParentFile().mkdirs(); 349 IPath tempPath = getBackupLocationFor(path); 350 try { 351 new ModelObjectWriter().write(description, path, tempPath); 352 } catch (IOException e) { 353 String message = NLS.bind(Messages.resources_writeWorkspaceMeta, path); 354 throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, e); 355 } 356 } 357 358 363 public void writePrivateDescription(IProject target) throws CoreException { 364 IPath location = locationFor(target).append(F_PROJECT_LOCATION); 365 java.io.File file = location.toFile(); 366 Workspace.clear(file); 368 ProjectDescription desc = ((Project) target).internalGetDescription(); 370 if (desc == null) 371 return; 372 final URI projectLocation = desc.getLocationURI(); 373 final IProject[] references = desc.getDynamicReferences(false); 374 final int numRefs = references.length; 375 if (projectLocation == null && numRefs == 0) 376 return; 377 try { 379 SafeChunkyOutputStream output = new SafeChunkyOutputStream(file); 380 DataOutputStream dataOut = new DataOutputStream(output); 381 try { 382 if (projectLocation == null) 383 dataOut.writeUTF(""); else 385 dataOut.writeUTF(URI_PREFIX + projectLocation.toString()); 386 dataOut.writeInt(numRefs); 387 for (int i = 0; i < numRefs; i++) 388 dataOut.writeUTF(references[i].getName()); 389 output.succeed(); 390 } finally { 391 dataOut.close(); 392 } 393 } catch (IOException e) { 394 String message = NLS.bind(Messages.resources_exSaveProjectLocation, target.getName()); 395 throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e); 396 } 397 } 398 } 399 | Popular Tags |