1 12 package org.eclipse.team.internal.ccvs.core.resources; 13 14 import java.io.File ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 import java.util.Date ; 18 import java.util.List ; 19 20 import org.eclipse.core.resources.*; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.team.core.RepositoryProvider; 24 import org.eclipse.team.core.TeamException; 25 import org.eclipse.team.internal.ccvs.core.*; 26 import org.eclipse.team.internal.ccvs.core.client.Session; 27 import org.eclipse.team.internal.ccvs.core.syncinfo.*; 28 29 33 public class EclipseFile extends EclipseResource implements ICVSFile { 34 35 private static final String TEMP_FILE_EXTENSION = ".tmp"; private static final IPath PROJECT_META_DATA_PATH = new Path(".project"); 38 41 protected EclipseFile(IFile file) { 42 super(file); 43 } 44 45 48 public void delete() throws CVSException { 49 try { 50 ((IFile)resource).delete(false , true , null); 51 } catch(CoreException e) { 52 throw CVSException.wrapException(resource, NLS.bind(CVSMessages.EclipseFile_Problem_deleting_resource, new String [] { resource.getFullPath().toString(), e.getStatus().getMessage() }), e); } 54 } 55 56 public long getSize() { 57 return getIOFile().length(); 58 } 59 60 public InputStream getContents() throws CVSException { 61 try { 62 return getIFile().getContents(); 63 } catch (CoreException e) { 64 throw CVSException.wrapException(resource, NLS.bind(CVSMessages.EclipseFile_Problem_accessing_resource, new String [] { resource.getFullPath().toString(), e.getStatus().getMessage() }), e); } 66 } 67 68 71 public Date getTimeStamp() { 72 long timestamp = getIFile().getLocalTimeStamp(); 73 if( timestamp == IResource.NULL_STAMP) { 74 return new Date (0L); 76 } 77 return new Date ((timestamp/1000)*1000); 78 } 79 80 83 public void setTimeStamp(Date date) throws CVSException { 84 long time; 85 if (date == null) { 86 time = System.currentTimeMillis(); 87 } else { 88 time = date.getTime(); 89 } 90 EclipseSynchronizer.getInstance().setTimeStamp(this, time); 91 } 92 93 96 public boolean isFolder() { 97 return false; 98 } 99 100 103 public boolean isModified(IProgressMonitor monitor) throws CVSException { 104 105 108 if (!exists()) { 109 return getSyncBytes() != null; 110 } 111 int state = EclipseSynchronizer.getInstance().getModificationState(getIFile()); 112 113 if (state != UNKNOWN) { 114 boolean dirty = state != CLEAN; 115 if (dirty == isDirty()) { 118 return dirty; 119 } 120 } 121 122 byte[] syncBytes = getSyncBytes(); 124 if (syncBytes == null && isIgnored()) return false; 125 return EclipseSynchronizer.getInstance().setModified(this, UNKNOWN); 127 } 128 129 132 public void accept(ICVSResourceVisitor visitor) throws CVSException { 133 visitor.visitFile(this); 134 } 135 136 139 public void accept(ICVSResourceVisitor visitor, boolean recurse) throws CVSException { 140 visitor.visitFile(this); 141 } 142 143 146 public void copyTo(String filename) throws CVSException { 147 try { 148 IPath targetPath = new Path(null, filename); 149 IFile targetFile = getIFile().getParent().getFile(targetPath); 150 if (targetFile.exists()) { 151 targetFile.delete(false , true , null); 154 } 155 getIFile().copy(targetPath, true , null); 156 } catch(CoreException e) { 157 throw new CVSException(e.getStatus()); 158 } 159 } 160 161 164 public String getRemoteLocation(ICVSFolder stopSearching) throws CVSException { 165 return getParent().getRemoteLocation(stopSearching) + SEPARATOR + getName(); 166 } 167 168 171 public void setContents(InputStream stream, int responseType, boolean keepLocalHistory, IProgressMonitor monitor) throws CVSException { 172 try { 173 IFile file = getIFile(); 174 if (PROJECT_META_DATA_PATH.equals(file.getFullPath().removeFirstSegments(1))) { 175 responseType = UPDATED; 176 } 177 switch (responseType) { 178 case UPDATED: 179 if (resource.exists()) { 180 file.setContents(stream, false , true , monitor); 181 break; 182 } 183 case CREATED: file.create(stream, false , monitor); 185 break; 186 case MERGED: IFile tempFile = file.getParent().getFile(new Path(null, file.getName() + TEMP_FILE_EXTENSION)); 189 monitor.beginTask(null, 100); 190 if (tempFile.exists()) 191 tempFile.delete(true , Policy.subMonitorFor(monitor, 25)); 192 tempFile.create(stream, true , Policy.subMonitorFor(monitor, 25)); 193 file.delete(false , true , Policy.subMonitorFor(monitor, 25)); 194 tempFile.move(new Path(null, file.getName()), false , true , Policy.subMonitorFor(monitor, 25)); 195 monitor.done(); 196 break; 197 case UPDATE_EXISTING: file.setContents(stream, false , true , monitor); 199 break; 200 } 201 } catch(CoreException e) { 202 String message = null; 203 if (e.getStatus().getCode() == IResourceStatus.FAILED_READ_LOCAL) { 204 Throwable t = e.getStatus().getException(); 207 if (t != null) message = t.getMessage(); 208 } 209 if (message == null) message = e.getMessage(); 210 throw CVSException.wrapException(resource, NLS.bind(CVSMessages.EclipseFile_Problem_writing_resource, new String [] { resource.getFullPath().toString(), message }), e); 211 } 212 } 213 214 217 public void setReadOnly(boolean readOnly) throws CVSException { 218 ResourceAttributes attributes = resource.getResourceAttributes(); 219 if (attributes != null) { 220 attributes.setReadOnly(readOnly); 221 try { 222 resource.setResourceAttributes(attributes); 223 } catch (CoreException e) { 224 throw CVSException.wrapException(e); 225 } 226 } 227 } 228 229 232 public boolean isReadOnly() throws CVSException { 233 return getIFile().isReadOnly(); 234 } 235 236 239 public void setExecutable(boolean executable) throws CVSException { 240 ResourceAttributes attributes = resource.getResourceAttributes(); 241 if (attributes != null) { 242 attributes.setExecutable(executable); 243 try { 244 resource.setResourceAttributes(attributes); 245 } catch (CoreException e) { 246 throw CVSException.wrapException(e); 247 } 248 } 249 } 250 251 254 public boolean isExecutable() throws CVSException { 255 ResourceAttributes attributes = resource.getResourceAttributes(); 256 if (attributes != null) { 257 return attributes.isExecutable(); 258 } else { 259 return false; 260 } 261 } 262 263 266 public IFile getIFile() { 267 return (IFile)resource; 268 } 269 270 273 private File getIOFile() { 274 IPath location = resource.getLocation(); 275 if(location!=null) { 276 return location.toFile(); 277 } 278 return null; 279 } 280 283 public ILogEntry[] getLogEntries(IProgressMonitor monitor) throws TeamException { 284 285 if (getIResource() == null 288 || !getIResource().getProject().isAccessible()) 289 return new ILogEntry[0]; 290 291 byte[] syncBytes = getSyncBytes(); 292 if(syncBytes != null && !ResourceSyncInfo.isAddition(syncBytes)) { 293 ICVSRemoteResource remoteFile = CVSWorkspaceRoot.getRemoteResourceFor(resource); 294 return ((ICVSRemoteFile)remoteFile).getLogEntries(monitor); 295 } 296 return new ILogEntry[0]; 297 } 298 301 public void setNotifyInfo(NotifyInfo info) throws CVSException { 302 if (isManaged()) { 303 EclipseSynchronizer.getInstance().setNotifyInfo(resource, info); 304 } 308 } 309 310 313 public NotifyInfo getNotifyInfo() throws CVSException { 314 if (isManaged()) { 315 return EclipseSynchronizer.getInstance().getNotifyInfo(resource); 316 } 317 return null; 318 } 319 320 323 public void setBaserevInfo(BaserevInfo info) throws CVSException { 324 if (isManaged()) { 325 if (info == null) { 326 EclipseSynchronizer.getInstance().deleteBaserevInfo(resource); 327 EclipseSynchronizer.getInstance().deleteFileFromBaseDirectory(getIFile(), null); 328 } else 329 EclipseSynchronizer.getInstance().setBaserevInfo(resource, info); 330 } 331 } 332 335 public BaserevInfo getBaserevInfo() throws CVSException { 336 if (isManaged()) { 337 return EclipseSynchronizer.getInstance().getBaserevInfo(resource); 338 } 339 return null; 340 } 341 342 345 public void edit(final int notifications, boolean notifyForWritable, IProgressMonitor monitor) throws CVSException { 346 if (!notifyForWritable && !isReadOnly()) return; 347 run(new ICVSRunnable() { 348 public void run(IProgressMonitor monitor) throws CVSException { 349 byte[] syncBytes = getSyncBytes(); 350 if (syncBytes == null || ResourceSyncInfo.isAddition(syncBytes)) return; 351 352 char[] internalFormat; 354 if (notifications == NO_NOTIFICATION) { 355 internalFormat = null; 356 } else if (notifications == NOTIFY_ON_ALL) { 357 internalFormat = NotifyInfo.ALL; 358 } else { 359 List notificationCharacters = new ArrayList (); 360 if ((notifications & NOTIFY_ON_EDIT) >0) 361 notificationCharacters.add(new Character (NotifyInfo.EDIT)); 362 if ((notifications & NOTIFY_ON_UNEDIT) >0) 363 notificationCharacters.add(new Character (NotifyInfo.UNEDIT)); 364 if ((notifications & NOTIFY_ON_COMMIT) >0) 365 notificationCharacters.add(new Character (NotifyInfo.COMMIT)); 366 internalFormat = new char[notificationCharacters.size()]; 367 for (int i = 0; i < internalFormat.length; i++) { 368 internalFormat[i] = ((Character )notificationCharacters.get(i)).charValue(); 369 } 370 } 371 372 NotifyInfo notifyInfo = new NotifyInfo(getName(), NotifyInfo.EDIT, new Date (), internalFormat); 374 setNotifyInfo(notifyInfo); 375 376 if (!isModified(null)) { 378 EclipseSynchronizer.getInstance().copyFileToBaseDirectory(getIFile(), monitor); 379 setBaserevInfo(new BaserevInfo(getName(), ResourceSyncInfo.getRevision(syncBytes))); 380 } 381 382 try { 383 setReadOnly(false); 385 } catch (CVSException e) { 386 CVSProviderPlugin.log(e); 388 } 389 } 390 }, monitor); 391 392 } 393 394 397 public void unedit(IProgressMonitor monitor) throws CVSException { 398 if (isReadOnly()) return; 399 run(new ICVSRunnable() { 400 public void run(IProgressMonitor monitor) throws CVSException { 401 NotifyInfo info = getNotifyInfo(); 403 if (info != null && info.getNotificationType() == NotifyInfo.EDIT) { 404 info = null; 405 } else { 406 info = new NotifyInfo(getName(), NotifyInfo.UNEDIT, new Date (), null); 407 } 408 setNotifyInfo(info); 409 410 if (isModified(null)) { 411 ResourceSyncInfo syncInfo = getSyncInfo(); 412 BaserevInfo baserevInfo = getBaserevInfo(); 413 EclipseSynchronizer.getInstance().restoreFileFromBaseDirectory(getIFile(), monitor); 414 if (!syncInfo.getRevision().equals(baserevInfo.getRevision())) { 416 MutableResourceSyncInfo newInfo = syncInfo.cloneMutable(); 417 newInfo.setRevision(baserevInfo.getRevision()); 418 newInfo.setTimeStamp(getTimeStamp()); 419 newInfo.setDeleted(false); 420 setSyncInfo(newInfo, ICVSFile.CLEAN); 421 } else { 422 EclipseSynchronizer.getInstance().setModified(EclipseFile.this, CLEAN); 424 } 425 } else { 426 setSyncBytes(getSyncBytes(), ICVSFile.CLEAN); 428 } 429 setBaserevInfo(null); 430 431 try { 432 setReadOnly(true); 434 } catch (CVSException e) { 435 CVSProviderPlugin.log(e); 437 } 438 } 439 }, monitor); 440 } 441 442 445 public void notificationCompleted() throws CVSException { 446 EclipseSynchronizer.getInstance().deleteNotifyInfo(resource); 447 } 448 449 452 public NotifyInfo getPendingNotification() throws CVSException { 453 return getNotifyInfo(); 454 } 455 456 459 public void checkedIn(String entryLine, boolean commit) throws CVSException { 460 ResourceSyncInfo oldInfo = getSyncInfo(); 461 ResourceSyncInfo newInfo = null; 462 int modificationState = ICVSFile.CLEAN; 463 if (entryLine == null) { 464 if (oldInfo == null) return; 466 if(! oldInfo.isAdded()) { 469 MutableResourceSyncInfo mutable = oldInfo.cloneMutable(); 470 mutable.setTimeStamp(getTimeStamp(), true ); 471 newInfo = mutable; 472 } 473 } else if (oldInfo == null) { 475 newInfo = new ResourceSyncInfo(entryLine, null); 477 modificationState = ICVSFile.DIRTY; 479 } else { 480 Date timeStamp; 483 if (commit) { 484 timeStamp = getTimeStamp(); 486 } else { 487 timeStamp = oldInfo.getTimeStamp(); 490 if (timeStamp == null) { 491 timeStamp = getTimeStamp(); 492 } else { 493 setTimeStamp(timeStamp); 497 timeStamp = getTimeStamp(); 500 } 501 } 502 newInfo = new ResourceSyncInfo(entryLine, timeStamp); 503 504 } 505 if (newInfo != null){ 507 CVSTag tag = newInfo.getTag(); 508 if(tag != null && CVSEntryLineTag.BASE.getName().equals(tag.getName())){ 509 newInfo = newInfo.cloneMutable(); 510 ((MutableResourceSyncInfo)newInfo).setTag(oldInfo.getTag()); 511 } 512 setSyncInfo(newInfo, modificationState); 513 } 514 clearCachedBase(); 515 } 516 517 private void clearCachedBase() throws CVSException { 518 BaserevInfo base = getBaserevInfo(); 519 if (base != null) { 520 setBaserevInfo(null); 521 try { 522 setReadOnly(true); 523 } catch (CVSException e) { 524 CVSProviderPlugin.log(e); 526 } 527 } else { 528 CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()); 530 if (provider != null && provider.isWatchEditEnabled()) { 531 try { 532 setReadOnly(true); 533 } catch (CVSException e) { 534 CVSProviderPlugin.log(e); 536 } 537 } 538 } 539 } 540 541 544 public void unmanage(IProgressMonitor monitor) throws CVSException { 545 run(new ICVSRunnable() { 546 public void run(IProgressMonitor monitor) throws CVSException { 547 EclipseFile.super.unmanage(monitor); 548 clearCachedBase(); 549 } 550 }, monitor); 551 } 552 553 556 public boolean isEdited() throws CVSException { 557 return EclipseSynchronizer.getInstance().isEdited(getIFile()); 558 } 559 560 563 public void setSyncInfo(ResourceSyncInfo info, int modificationState) throws CVSException { 564 setSyncBytes(info.getBytes(), info, modificationState); 565 } 566 567 570 public void setSyncBytes(byte[] syncBytes, int modificationState) throws CVSException { 571 setSyncBytes(syncBytes, null, modificationState); 572 } 573 574 577 private void setSyncBytes(byte[] syncBytes, ResourceSyncInfo info, int modificationState) throws CVSException { 578 Assert.isNotNull(syncBytes); 579 setSyncBytes(syncBytes); 580 EclipseSynchronizer.getInstance().setModified(this, modificationState); 581 } 582 583 public void handleModification(boolean forAddition) throws CVSException { 584 if (isIgnored()) { 585 if(! resource.isDerived()) { 592 EclipseSynchronizer.getInstance().setModified(this, CLEAN); 593 } 594 return; 595 } 596 EclipseSynchronizer.getInstance().setModified(this, UNKNOWN); 598 } 599 600 603 public String getRepositoryRelativePath() throws CVSException { 604 if (!isManaged()) return null; 605 String parentPath = getParent().getRepositoryRelativePath(); 606 if (parentPath == null) return null; 607 return parentPath + Session.SERVER_SEPARATOR + getName(); 608 } 609 610 protected boolean isDirty() throws CVSException { 611 boolean dirty; 612 byte[] syncBytes = getSyncBytes(); 613 if (syncBytes == null) { 614 dirty = exists(); 615 } else { 616 if(ResourceSyncInfo.isAddition(syncBytes) || ResourceSyncInfo.isMerge(syncBytes) || !exists()) { 620 dirty = true; 621 } else { 622 ResourceSyncInfo info = new ResourceSyncInfo(syncBytes); 624 dirty = !getTimeStamp().equals(info.getTimeStamp()); 625 } 626 } 627 return dirty; 628 } 629 630 } 631 632 633 | Popular Tags |