1 11 package org.eclipse.team.internal.ccvs.core.util; 12 13 import java.io.*; 14 import java.net.URI ; 15 import java.util.*; 16 17 import org.eclipse.core.filesystem.EFS; 18 import org.eclipse.core.filesystem.IFileStore; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.team.internal.ccvs.core.*; 23 import org.eclipse.team.internal.ccvs.core.resources.CVSEntryLineTag; 24 import org.eclipse.team.internal.ccvs.core.syncinfo.*; 25 26 31 public class SyncFileWriter { 32 33 public static final String CVS_DIRNAME = "CVS"; 36 public static final String REPOSITORY = "Repository"; public static final String ROOT = "Root"; public static final String STATIC = "Entries.Static"; public static final String TAG = "Tag"; public static final String ENTRIES = "Entries"; public static final String ENTRIES_LOG="Entries.Log"; public static final String NOTIFY = "Notify"; public static final String BASE_DIRNAME = "Base"; public static final String BASEREV = "Baserev"; 48 public static final String IGNORE_FILE = ".cvsignore"; 51 private static final String FOLDER_TAG="D"; 55 private static final String ADD_TAG="A "; private static final String REMOVE_TAG="R "; 59 public static final QualifiedName MODSTAMP_KEY = new QualifiedName("org.eclipse.team.cvs.core", "meta-file-modtime"); 62 67 public static byte[][] readAllResourceSync(IContainer parent) throws CVSException { 68 IFolder cvsSubDir = getCVSSubdirectory(parent); 69 70 if (!folderExists(cvsSubDir)){ 71 return null; 72 } 73 74 if (Policy.DEBUG_METAFILE_CHANGES) { 75 System.out.println("Reading Entries file for " + parent.getFullPath()); } 77 78 String [] entries = readLines(cvsSubDir.getFile(ENTRIES)); 80 if (entries == null) return null; 81 Map infos = new TreeMap(); 82 for (int i = 0; i < entries.length; i++) { 83 String line = entries[i]; 84 if(!FOLDER_TAG.equals(line) && !"".equals(line)) { try { 86 ResourceSyncInfo info = new ResourceSyncInfo(line, null); 87 infos.put(info.getName(), info); 88 } catch (CVSException e) { 89 CVSProviderPlugin.log(new CVSStatus(IStatus.ERROR, NLS.bind(CVSMessages.SyncFileWriter_0, new String [] { parent.getFullPath().toString() }), e)); 92 } 93 } 94 } 95 96 String [] entriesLog = readLines(cvsSubDir.getFile(ENTRIES_LOG)); 98 if (entriesLog != null) { 99 for (int i = 0; i < entriesLog.length; i++) { 100 String line = entriesLog[i]; 101 if (line.startsWith(ADD_TAG)) { 102 line = line.substring(ADD_TAG.length()); 103 ResourceSyncInfo info = new ResourceSyncInfo(line, null); 104 infos.put(info.getName(), info); 105 } else if (line.startsWith(REMOVE_TAG)) { 106 line = line.substring(REMOVE_TAG.length()); 107 ResourceSyncInfo info = new ResourceSyncInfo(line, null); 108 infos.remove(info.getName()); 109 } 110 } 111 } 112 113 byte[][] result = new byte[infos.size()][]; 115 int i = 0; 116 for (Iterator iter = infos.values().iterator(); iter.hasNext();) { 117 ResourceSyncInfo info = (ResourceSyncInfo) iter.next(); 118 result[i++] = info.getBytes(); 119 } 120 return result; 121 } 122 123 private static boolean folderExists(IFolder cvsSubDir) throws CVSException { 124 try { 125 URI uri = cvsSubDir.getLocationURI(); 126 if (uri != null){ 127 IFileStore store = EFS.getStore(uri); 128 if (store != null){ 129 return store.fetchInfo().exists(); 130 } 131 } 132 } catch (CoreException e) { 133 throw CVSException.wrapException(e); 134 } 135 return false; 136 } 137 138 public static void writeAllResourceSync(IContainer parent, byte[][] infos) throws CVSException { 139 try { 140 if (Policy.DEBUG_METAFILE_CHANGES) { 141 System.out.println("Writing Entries file for folder " + parent.getFullPath()); } 143 IFolder cvsSubDir = createCVSSubdirectory(parent); 144 145 String [] entries = new String [infos.length]; 147 for (int i = 0; i < infos.length; i++) { 148 byte[] info = infos[i]; 149 entries[i] = new String (info); 150 } 151 152 writeLines(cvsSubDir.getFile(ENTRIES), entries); 154 155 cvsSubDir.getFile(ENTRIES_LOG).delete(IResource.NONE, null); 157 } catch(CoreException e) { 158 throw CVSException.wrapException(e); 159 } 160 } 161 166 public static FolderSyncInfo readFolderSync(IContainer folder) throws CVSException { 167 IFolder cvsSubDir = getCVSSubdirectory(folder); 168 169 if (!folderExists(cvsSubDir)){ 170 return null; 171 } 172 173 if (Policy.DEBUG_METAFILE_CHANGES) { 174 System.out.println("Reading Root/Repository files for " + folder.getFullPath()); } 176 177 if (!cvsSubDir.isTeamPrivateMember() && cvsSubDir.exists()) { 179 try { 180 cvsSubDir.setTeamPrivateMember(true); 181 } catch (CoreException e) { 182 CVSProviderPlugin.log(e); 183 } 184 } 185 186 String root = readFirstLine(cvsSubDir.getFile(ROOT)); 188 if (root == null) return null; 189 190 String repository = readFirstLine(cvsSubDir.getFile(REPOSITORY)); 192 if (repository == null) return null; 193 194 String tag = readFirstLine(cvsSubDir.getFile(TAG)); 196 if (Policy.DEBUG_METAFILE_CHANGES && tag != null) { 197 System.out.println("Reading Tag file for " + folder.getFullPath()); } 199 CVSTag cvsTag = (tag != null) ? new CVSEntryLineTag(tag) : null; 200 201 String staticDir = readFirstLine(cvsSubDir.getFile(STATIC)); 203 if (Policy.DEBUG_METAFILE_CHANGES && staticDir != null) { 204 System.out.println("Reading Static file for " + folder.getFullPath()); } 206 boolean isStatic = (staticDir != null); 207 208 return new FolderSyncInfo(repository, root, cvsTag, isStatic); 210 } 211 212 216 public static void writeFolderSync(IContainer folder, FolderSyncInfo info) throws CVSException { 217 try { 218 if (Policy.DEBUG_METAFILE_CHANGES) { 219 System.out.println("Writing Root/Respository files for " + folder.getFullPath()); } 221 IFolder cvsSubDir = createCVSSubdirectory(folder); 222 223 writeLines(cvsSubDir.getFile(ROOT), new String [] {info.getRoot()}); 225 226 writeLines(cvsSubDir.getFile(REPOSITORY), new String [] {info.getRepository()}); 228 229 IFile tagFile = cvsSubDir.getFile(TAG); 231 if (info.getTag() != null) { 232 if (Policy.DEBUG_METAFILE_CHANGES) { 233 System.out.println("Writing Tag file for " + folder.getFullPath()); } 235 writeLines(tagFile, new String [] {info.getTag().toEntryLineFormat(false)}); 236 } else { 237 if(tagFile.exists()) { 238 if (Policy.DEBUG_METAFILE_CHANGES) { 239 System.out.println("Deleting Tag file for " + folder.getFullPath()); } 241 tagFile.delete(IResource.NONE, null); 242 } 243 } 244 245 IFile staticFile = cvsSubDir.getFile(STATIC); 247 if(info.getIsStatic()) { 248 if (Policy.DEBUG_METAFILE_CHANGES) { 250 System.out.println("Writing Static file for " + folder.getFullPath()); } 252 writeLines(staticFile, new String [] {""}); } else { 254 if(staticFile.exists()) { 255 if (Policy.DEBUG_METAFILE_CHANGES) { 256 System.out.println("Deleting Static file for " + folder.getFullPath()); } 258 staticFile.delete(IResource.NONE, null); 259 } 260 } 261 } catch(CoreException e) { 262 throw CVSException.wrapException(e); 263 } 264 } 265 266 269 public static String [] readCVSIgnoreEntries(IContainer folder) throws CVSException { 270 IFile ignoreFile = folder.getFile(new Path(IGNORE_FILE)); 271 if (ignoreFile != null) { 272 return readLines(ignoreFile); 273 } 274 return null; 275 } 276 277 281 public static void writeCVSIgnoreEntries(IContainer folder, String [] patterns) throws CVSException { 282 IFile ignoreFile = folder.getFile(new Path(IGNORE_FILE)); 283 writeLines(ignoreFile, patterns); 284 } 285 286 289 public static void deleteFolderSync(IContainer folder) throws CVSException { 290 try { 291 if (Policy.DEBUG_METAFILE_CHANGES) { 292 System.out.println("Deleting CVS directory from " + folder.getFullPath()); } 294 getCVSSubdirectory(folder).delete(IResource.NONE, null); 295 } catch(CoreException e) { 296 throw CVSException.wrapException(e); 297 } 298 } 299 300 304 public static NotifyInfo[] readAllNotifyInfo(IContainer parent) throws CVSException { 305 IFolder cvsSubDir = getCVSSubdirectory(parent); 306 307 if (!folderExists(cvsSubDir)){ 308 return null; 309 } 310 311 String [] entries = readLines(cvsSubDir.getFile(NOTIFY)); 313 if (entries == null) return null; 314 Map infos = new TreeMap(); 315 for (int i = 0; i < entries.length; i++) { 316 String line = entries[i]; 317 if(!"".equals(line)) { try { 319 NotifyInfo info = new NotifyInfo(parent, line); 320 infos.put(info.getName(), info); 321 } catch (CVSException e) { 322 CVSProviderPlugin.log(e); 325 } 326 } 327 } 328 329 return (NotifyInfo[])infos.values().toArray(new NotifyInfo[infos.size()]); 330 } 331 332 337 public static void writeAllNotifyInfo(IContainer parent, NotifyInfo[] infos) throws CVSException { 338 IFolder cvsSubDir = getCVSSubdirectory(parent); 340 342 if (infos.length == 0) { 343 try { 345 IFile notifyFile = cvsSubDir.getFile(NOTIFY); 346 if(notifyFile.exists()) { 347 notifyFile.delete(IResource.NONE, null); 348 } 349 } catch (CoreException e) { 350 throw CVSException.wrapException(e); 351 } 352 } else { 353 String [] entries = new String [infos.length]; 355 for (int i = 0; i < infos.length; i++) { 356 NotifyInfo info = infos[i]; 357 entries[i] = info.getNotifyLine(); 358 } 359 360 writeLines(cvsSubDir.getFile(NOTIFY), entries); 362 } 363 } 364 365 370 public static BaserevInfo[] readAllBaserevInfo(IContainer parent) throws CVSException { 371 IFolder cvsSubDir = getCVSSubdirectory(parent); 372 373 if (!folderExists(cvsSubDir)){ 374 return null; 375 } 376 377 String [] entries = readLines(cvsSubDir.getFile(BASEREV)); 379 if (entries == null) return null; 380 Map infos = new TreeMap(); 381 for (int i = 0; i < entries.length; i++) { 382 String line = entries[i]; 383 if(!"".equals(line)) { BaserevInfo info = new BaserevInfo(line); 385 infos.put(info.getName(), info); 386 } 387 } 388 389 return (BaserevInfo[])infos.values().toArray(new BaserevInfo[infos.size()]); 390 } 391 392 397 public static void writeAllBaserevInfo(IContainer parent, BaserevInfo[] infos) throws CVSException { 398 IFolder cvsSubDir = getCVSSubdirectory(parent); 400 402 String [] entries = new String [infos.length]; 404 for (int i = 0; i < infos.length; i++) { 405 BaserevInfo info = infos[i]; 406 entries[i] = info.getEntryLine(); 407 } 408 409 writeLines(cvsSubDir.getFile(BASEREV), entries); 411 } 412 413 416 private static IFolder getCVSSubdirectory(IContainer folder) { 417 return folder.getFolder(new Path(CVS_DIRNAME)); 418 } 419 420 423 private static IFolder createCVSSubdirectory(IContainer folder) throws CVSException { 424 try { 425 final IFolder cvsSubDir = getCVSSubdirectory(folder); 426 if (! cvsSubDir.exists()) { 427 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { 431 public void run(IProgressMonitor monitor) throws CoreException { 432 if (! cvsSubDir.exists()) { 434 if (existsInFileSystem(cvsSubDir)) { 435 cvsSubDir.refreshLocal(IResource.DEPTH_INFINITE, null); 436 cvsSubDir.setTeamPrivateMember(true); 437 } else { 438 cvsSubDir.create(IResource.TEAM_PRIVATE, true , null); 439 } 440 } else { 441 if (!cvsSubDir.isTeamPrivateMember()) { 442 cvsSubDir.setTeamPrivateMember(true); 443 } 444 } 445 } 446 }, folder, 0, null); 447 } 448 return cvsSubDir; 449 } catch (CoreException e) { 450 throw CVSException.wrapException(e); 451 } 452 } 453 454 protected static boolean existsInFileSystem(IFolder cvsSubDir) { 455 URI uri = cvsSubDir.getLocationURI(); 456 if (uri != null) { 457 try { 458 IFileStore store = EFS.getStore(uri); 459 if (store != null) { 460 return store.fetchInfo().exists(); 461 } 462 } catch (CoreException e) { 463 CVSProviderPlugin.log(e); 464 } 465 } 466 return false; 467 } 468 469 473 private static String readFirstLine(IFile file) throws CVSException { 474 try { 475 InputStream in = getInputStream(file); 476 if (in != null) { 477 BufferedReader reader = new BufferedReader(new InputStreamReader(in), 512); 478 try { 479 String line = reader.readLine(); 480 if (line == null) return ""; return line; 482 } finally { 483 reader.close(); 484 } 485 } 486 return null; 487 } catch (IOException e) { 488 throw CVSException.wrapException(e); 489 } catch (CoreException e) { 490 if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND 493 || e.getStatus().getCode() == IResourceStatus.FAILED_READ_LOCAL) 494 return null; 495 throw CVSException.wrapException(e); 496 } 497 } 498 499 private static InputStream getInputStream(IFile file) throws CoreException, FileNotFoundException { 500 if (file.exists()) { 501 return file.getContents(true); 502 } 503 504 URI uri = file.getLocationURI(); 505 if (uri != null) { 506 IFileStore store = EFS.getStore(uri); 507 if (store != null) { 508 return store.openInputStream(EFS.NONE, null); 509 } 510 } 511 512 File ioFile = file.getLocation().toFile(); 513 if (ioFile != null && ioFile.exists()) { 514 return new FileInputStream(ioFile); 515 } 516 517 return null; 518 } 519 520 524 private static String [] readLines(IFile file) throws CVSException { 525 try { 526 InputStream in = getInputStream(file); 527 if (in != null) { 528 BufferedReader reader = new BufferedReader(new InputStreamReader(in), 512); 529 List fileContentStore = new ArrayList(); 530 try { 531 String line; 532 while ((line = reader.readLine()) != null) { 533 fileContentStore.add(line); 534 } 535 return (String []) fileContentStore.toArray(new String [fileContentStore.size()]); 536 } finally { 537 reader.close(); 538 } 539 } 540 return null; 541 } catch (IOException e) { 542 throw CVSException.wrapException(e); 543 } catch (CoreException e) { 544 if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND 547 || e.getStatus().getCode() == IResourceStatus.FAILED_READ_LOCAL) 548 return null; 549 throw CVSException.wrapException(e); 550 } 551 } 552 553 557 private static void writeLines(final IFile file, final String [] contents) throws CVSException { 558 try { 559 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { 564 public void run(IProgressMonitor monitor) throws CoreException { 565 try { 566 ByteArrayOutputStream os = new ByteArrayOutputStream(); 567 writeLinesToStreamAndClose(os, contents); 568 if(!file.exists()) { 569 file.create(new ByteArrayInputStream(os.toByteArray()), IResource.FORCE , null); 570 } else { 571 file.setContents(new ByteArrayInputStream(os.toByteArray()), IResource.FORCE , null); 572 } 573 file.setSessionProperty(MODSTAMP_KEY, new Long (file.getModificationStamp())); 574 } catch(CVSException e) { 575 throw new CoreException(e.getStatus()); 576 } 577 } 578 }, ResourcesPlugin.getWorkspace().getRuleFactory().createRule(file), 0, null); 579 } catch (CoreException e) { 580 throw CVSException.wrapException(e); 581 } 582 } 583 584 private static void writeLinesToStreamAndClose(OutputStream os, String [] contents) throws CVSException { 585 byte[] lineEnd = getLineDelimiter(); 586 try { 587 try { 588 for (int i = 0; i < contents.length; i++) { 589 os.write(contents[i].getBytes()); 590 os.write(lineEnd); 591 } 592 } finally { 593 os.close(); 594 } 595 } catch (IOException e) { 596 throw CVSException.wrapException(e); 597 } 598 } 599 600 606 public static void writeFileToBaseDirectory(IFile file, IProgressMonitor monitor) throws CVSException { 607 monitor = Policy.monitorFor(monitor); 608 monitor.beginTask(null, 100); 609 try { 610 IFolder baseFolder = getBaseDirectory(file); 611 if (!baseFolder.exists()) { 612 baseFolder.create(false , true , Policy.subMonitorFor(monitor, 10)); 613 } 614 IFile target = baseFolder.getFile(new Path(null, file.getName())); 615 if (target.exists()) { 616 setReadOnly(target, false); 619 target.delete(true, Policy.subMonitorFor(monitor, 10)); 620 } 621 file.copy(target.getFullPath(), true , Policy.subMonitorFor(monitor, 80)); 623 } catch (CoreException e) { 624 throw CVSException.wrapException(e); 625 } finally { 626 monitor.done(); 627 } 628 } 629 635 public static void restoreFileFromBaseDirectory(IFile file, IProgressMonitor monitor) throws CVSException { 636 monitor = Policy.monitorFor(monitor); 637 monitor.beginTask(null, 100); 638 try { 639 IFolder baseFolder = getBaseDirectory(file); 640 IFile source = baseFolder.getFile(new Path(null, file.getName())); 641 if (!source.exists()) { 642 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, NLS.bind(CVSMessages.SyncFileWriter_baseNotAvailable, new String [] { file.getFullPath().toString() }), file); 643 throw new CVSException(status); 644 } 645 if (file.exists()) { 646 file.delete(false , true , Policy.subMonitorFor(monitor, 10)); 647 } 648 setReadOnly(source, false); 650 source.move(file.getFullPath(), false , true ,Policy.subMonitorFor(monitor, 100)); 652 } catch (CoreException e) { 653 throw CVSException.wrapException(e); 654 } finally { 655 monitor.done(); 656 } 657 } 658 659 private static void setReadOnly(IFile source, boolean readOnly) { 660 ResourceAttributes attrs = source.getResourceAttributes(); 661 if (attrs != null && attrs.isReadOnly() != readOnly) { 662 attrs.setReadOnly(readOnly); 663 try { 664 source.setResourceAttributes(attrs); 665 } catch (CoreException e) { 666 CVSProviderPlugin.log(e); 668 } 669 } 670 } 671 672 677 public static void deleteFileFromBaseDirectory(IFile file, IProgressMonitor monitor) throws CVSException { 678 monitor = Policy.monitorFor(monitor); 679 monitor.beginTask(null, 100); 680 try { 681 IFolder baseFolder = getBaseDirectory(file); 682 IFile source = baseFolder.getFile(new Path(null, file.getName())); 683 if (source.exists()) { 684 setReadOnly(source, false); 685 source.delete(false, false, Policy.subMonitorFor(monitor, 100)); 686 } 687 } catch (CoreException e) { 688 throw CVSException.wrapException(e); 689 } finally { 690 monitor.done(); 691 } 692 } 693 694 private static IFolder getBaseDirectory(IFile file) { 695 IContainer cvsFolder = getCVSSubdirectory(file.getParent()); 696 IFolder baseFolder = cvsFolder.getFolder(new Path(BASE_DIRNAME)); 697 return baseFolder; 698 } 699 700 706 public static IFile getTemplateFile(IContainer folder) throws CVSException { 707 IFolder cvsFolder = createCVSSubdirectory(folder); 708 return cvsFolder.getFile("Template"); } 710 711 716 public static boolean isEdited(IFile file) { 717 IFolder baseFolder = getBaseDirectory(file); 718 IFile baseFile = baseFolder.getFile(file.getName()); 719 return baseFile.exists(); 720 } 721 722 private static byte[] getLineDelimiter() { 723 if (CVSProviderPlugin.getPlugin().isUsePlatformLineend()) { 724 String property = System.getProperty("line.separator"); if (property != null) return property.getBytes(); 726 } 727 return new byte[] { 0x0A }; 728 } 729 730 } 731 | Popular Tags |