KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > SyncFileWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.util;
12
13 import java.io.*;
14 import java.net.URI JavaDoc;
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 /*
27  * This is a helper class that knows the format of the CVS metafiles. It
28  * provides a bridge between the CVS metafile formats and location to the
29  * Eclipse CVS client ResourceSyncInfo and FolderSyncInfo types.
30  */

31 public class SyncFileWriter {
32
33     // the famous CVS meta directory name
34
public static final String JavaDoc CVS_DIRNAME = "CVS"; //$NON-NLS-1$
35

36     // CVS meta files located in the CVS subdirectory
37
public static final String JavaDoc REPOSITORY = "Repository"; //$NON-NLS-1$
38
public static final String JavaDoc ROOT = "Root"; //$NON-NLS-1$
39
public static final String JavaDoc STATIC = "Entries.Static"; //$NON-NLS-1$
40
public static final String JavaDoc TAG = "Tag"; //$NON-NLS-1$
41
public static final String JavaDoc ENTRIES = "Entries"; //$NON-NLS-1$
42
//private static final String PERMISSIONS = "Permissions"; //$NON-NLS-1$
43
public static final String JavaDoc ENTRIES_LOG="Entries.Log"; //$NON-NLS-1$
44
public static final String JavaDoc NOTIFY = "Notify"; //$NON-NLS-1$
45
public static final String JavaDoc BASE_DIRNAME = "Base"; //$NON-NLS-1$
46
public static final String JavaDoc BASEREV = "Baserev"; //$NON-NLS-1$
47

48     // the local workspace file that contains pattern for ignored resources
49
public static final String JavaDoc IGNORE_FILE = ".cvsignore"; //$NON-NLS-1$
50

51     // Some older CVS clients may of added a line to the entries file consisting
52
// of only a 'D'. It is safe to ingnore these entries.
53
private static final String JavaDoc FOLDER_TAG="D"; //$NON-NLS-1$
54

55     // Command characters found in the Entries.log file
56
private static final String JavaDoc ADD_TAG="A "; //$NON-NLS-1$
57
private static final String JavaDoc REMOVE_TAG="R "; //$NON-NLS-1$
58

59     // key for saving the mod stamp for each writen meta file
60
public static final QualifiedName MODSTAMP_KEY = new QualifiedName("org.eclipse.team.cvs.core", "meta-file-modtime"); //$NON-NLS-1$ //$NON-NLS-2$
61

62     /**
63      * Reads the CVS/Entries, CVS/Entries.log and CVS/Permissions files from the
64      * specified folder and returns ResourceSyncInfo instances for the data stored therein.
65      * If the folder does not have a CVS subdirectory then <code>null</code> is returned.
66      */

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()); //$NON-NLS-1$
76
}
77
78         // process Entries file contents
79
String JavaDoc[] 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 JavaDoc line = entries[i];
84             if(!FOLDER_TAG.equals(line) && !"".equals(line)) { //$NON-NLS-1$
85
try {
86                     ResourceSyncInfo info = new ResourceSyncInfo(line, null);
87                     infos.put(info.getName(), info);
88                 } catch (CVSException e) {
89                     // There was a problem parsing the entry line.
90
// Log the problem and skip the entry
91
CVSProviderPlugin.log(new CVSStatus(IStatus.ERROR, NLS.bind(CVSMessages.SyncFileWriter_0, new String JavaDoc[] { parent.getFullPath().toString() }), e));
92                 }
93             }
94         }
95         
96         // process Entries.log file contents
97
String JavaDoc[] entriesLog = readLines(cvsSubDir.getFile(ENTRIES_LOG));
98         if (entriesLog != null) {
99             for (int i = 0; i < entriesLog.length; i++) {
100                 String JavaDoc 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         //return (ResourceSyncInfo[])infos.values().toArray(new ResourceSyncInfo[infos.size()]);
114
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 JavaDoc 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()); //$NON-NLS-1$
142
}
143             IFolder cvsSubDir = createCVSSubdirectory(parent);
144
145             // format file contents
146
String JavaDoc[] entries = new String JavaDoc[infos.length];
147             for (int i = 0; i < infos.length; i++) {
148                 byte[] info = infos[i];
149                 entries[i] = new String JavaDoc(info);
150             }
151
152             // write Entries
153
writeLines(cvsSubDir.getFile(ENTRIES), entries);
154
155             // delete Entries.log
156
cvsSubDir.getFile(ENTRIES_LOG).delete(IResource.NONE, null);
157         } catch(CoreException e) {
158             throw CVSException.wrapException(e);
159         }
160     }
161     /**
162      * Reads the CVS/Root, CVS/Repository, CVS/Tag, and CVS/Entries.static files from
163      * the specified folder and returns a FolderSyncInfo instance for the data stored therein.
164      * If the folder does not have a CVS subdirectory then <code>null</code> is returned.
165      */

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()); //$NON-NLS-1$
175
}
176         
177         // check to make sure the the cvs folder is hidden
178
if (!cvsSubDir.isTeamPrivateMember() && cvsSubDir.exists()) {
179             try {
180                 cvsSubDir.setTeamPrivateMember(true);
181             } catch (CoreException e) {
182                 CVSProviderPlugin.log(e);
183             }
184         }
185                 
186         // read CVS/Root
187
String JavaDoc root = readFirstLine(cvsSubDir.getFile(ROOT));
188         if (root == null) return null;
189         
190         // read CVS/Repository
191
String JavaDoc repository = readFirstLine(cvsSubDir.getFile(REPOSITORY));
192         if (repository == null) return null;
193         
194         // read CVS/Tag
195
String JavaDoc tag = readFirstLine(cvsSubDir.getFile(TAG));
196         if (Policy.DEBUG_METAFILE_CHANGES && tag != null) {
197             System.out.println("Reading Tag file for " + folder.getFullPath()); //$NON-NLS-1$
198
}
199         CVSTag cvsTag = (tag != null) ? new CVSEntryLineTag(tag) : null;
200
201         // read Entries.Static
202
String JavaDoc staticDir = readFirstLine(cvsSubDir.getFile(STATIC));
203         if (Policy.DEBUG_METAFILE_CHANGES && staticDir != null) {
204             System.out.println("Reading Static file for " + folder.getFullPath()); //$NON-NLS-1$
205
}
206         boolean isStatic = (staticDir != null);
207         
208         // return folder sync
209
return new FolderSyncInfo(repository, root, cvsTag, isStatic);
210     }
211     
212     /**
213      * Writes the CVS/Root, CVS/Repository, CVS/Tag, and CVS/Entries.static files to the
214      * specified folder using the data contained in the specified FolderSyncInfo instance.
215      */

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()); //$NON-NLS-1$
220
}
221             IFolder cvsSubDir = createCVSSubdirectory(folder);
222     
223             // write CVS/Root
224
writeLines(cvsSubDir.getFile(ROOT), new String JavaDoc[] {info.getRoot()});
225             
226             // write CVS/Repository
227
writeLines(cvsSubDir.getFile(REPOSITORY), new String JavaDoc[] {info.getRepository()});
228             
229             // write CVS/Tag
230
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()); //$NON-NLS-1$
234
}
235                 writeLines(tagFile, new String JavaDoc[] {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()); //$NON-NLS-1$
240
}
241                     tagFile.delete(IResource.NONE, null);
242                 }
243             }
244             
245             // write CVS/Entries.Static
246
IFile staticFile = cvsSubDir.getFile(STATIC);
247             if(info.getIsStatic()) {
248                 // the existance of the file is all that matters
249
if (Policy.DEBUG_METAFILE_CHANGES) {
250                     System.out.println("Writing Static file for " + folder.getFullPath()); //$NON-NLS-1$
251
}
252                 writeLines(staticFile, new String JavaDoc[] {""}); //$NON-NLS-1$
253
} else {
254                 if(staticFile.exists()) {
255                     if (Policy.DEBUG_METAFILE_CHANGES) {
256                         System.out.println("Deleting Static file for " + folder.getFullPath()); //$NON-NLS-1$
257
}
258                     staticFile.delete(IResource.NONE, null);
259                 }
260             }
261         } catch(CoreException e) {
262             throw CVSException.wrapException(e);
263         }
264     }
265
266     /**
267      * Returns all .cvsignore entries for the specified folder.
268      */

269     public static String JavaDoc[] 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     /**
278      * Writes all entries to the specified folder's .cvsignore file, overwriting any
279      * previous edition of the file.
280      */

281     public static void writeCVSIgnoreEntries(IContainer folder, String JavaDoc[] patterns) throws CVSException {
282         IFile ignoreFile = folder.getFile(new Path(IGNORE_FILE));
283         writeLines(ignoreFile, patterns);
284     }
285
286     /**
287      * Delete folder sync is equilavent to removing the CVS subdir.
288      */

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()); //$NON-NLS-1$
293
}
294             getCVSSubdirectory(folder).delete(IResource.NONE, null);
295         } catch(CoreException e) {
296             throw CVSException.wrapException(e);
297         }
298     }
299
300     /**
301      * Reads the CVS/Notify file from the specified folder and returns NotifyInfo instances
302      * for the data stored therein. If the folder does not have a CVS subdirectory then <code>null</code> is returned.
303      */

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         // process Notify file contents
312
String JavaDoc[] 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 JavaDoc line = entries[i];
317             if(!"".equals(line)) { //$NON-NLS-1$
318
try {
319                     NotifyInfo info = new NotifyInfo(parent, line);
320                     infos.put(info.getName(), info);
321                 } catch (CVSException e) {
322                     // We couldn't parse the notify info
323
// Log it and ignore
324
CVSProviderPlugin.log(e);
325                 }
326             }
327         }
328         
329         return (NotifyInfo[])infos.values().toArray(new NotifyInfo[infos.size()]);
330     }
331     
332     /**
333      * Writes the CVS/Notify file to the specified folder using the data contained in the
334      * specified NotifyInfo instances. A CVS subdirectory must already exist (an exception
335      * is thrown if it doesn't).
336      */

337     public static void writeAllNotifyInfo(IContainer parent, NotifyInfo[] infos) throws CVSException {
338         // get the CVS directory
339
IFolder cvsSubDir = getCVSSubdirectory(parent);
340         // write lines will throw an exception if the CVS directoru does not exist
341

342         if (infos.length == 0) {
343             // if there are no notify entries, delete the notify file
344
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             // format file contents
354
String JavaDoc[] entries = new String JavaDoc[infos.length];
355             for (int i = 0; i < infos.length; i++) {
356                 NotifyInfo info = infos[i];
357                 entries[i] = info.getNotifyLine();
358             }
359     
360             // write Notify entries
361
writeLines(cvsSubDir.getFile(NOTIFY), entries);
362         }
363     }
364
365     /**
366      * Reads the CVS/Baserev file from the specified folder and returns
367      * BaserevInfo instances for the data stored therein. If the folder does not
368      * have a CVS subdirectory then <code>null</code> is returned.
369      */

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         // process Notify file contents
378
String JavaDoc[] 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 JavaDoc line = entries[i];
383             if(!"".equals(line)) { //$NON-NLS-1$
384
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     /**
393      * Writes the CVS/Baserev file to the specified folder using the data
394      * contained in the specified BaserevInfo instances. A CVS subdirectory must
395      * already exist (an exception is thrown if it doesn't).
396      */

397     public static void writeAllBaserevInfo(IContainer parent, BaserevInfo[] infos) throws CVSException {
398         // get the CVS directory
399
IFolder cvsSubDir = getCVSSubdirectory(parent);
400         // write lines will throw an exception if the CVS directory does not exist
401

402         // format file contents
403
String JavaDoc[] entries = new String JavaDoc[infos.length];
404         for (int i = 0; i < infos.length; i++) {
405             BaserevInfo info = infos[i];
406             entries[i] = info.getEntryLine();
407         }
408
409         // write Notify entries
410
writeLines(cvsSubDir.getFile(BASEREV), entries);
411     }
412                 
413     /**
414      * Returns the CVS subdirectory for this folder.
415      */

416     private static IFolder getCVSSubdirectory(IContainer folder) {
417         return folder.getFolder(new Path(CVS_DIRNAME));
418     }
419     
420     /**
421      * Creates and makes team-private and returns a CVS subdirectory in this folder.
422      */

423     private static IFolder createCVSSubdirectory(IContainer folder) throws CVSException {
424         try {
425             final IFolder cvsSubDir = getCVSSubdirectory(folder);
426             if (! cvsSubDir.exists()) {
427                 // important to have both the folder creation and setting of team-private in the
428
// same runnable so that the team-private flag is set before other delta listeners
429
// sees the CVS folder creation.
430
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
431                     public void run(IProgressMonitor monitor) throws CoreException {
432                         // Re-check existence in case this method was called without a resource rule
433
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 /*make local*/, 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 JavaDoc 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     /*
470      * Reads the first line of the specified file.
471      * Returns null if the file does not exist, or the empty string if it is blank.
472      */

473     private static String JavaDoc 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 JavaDoc line = reader.readLine();
480                     if (line == null) return ""; //$NON-NLS-1$
481
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 the IFile doesn't exist or the underlying File doesn't exist,
491
// just return null to indicate the absence of the file
492
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 JavaDoc 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     /*
521      * Reads all lines of the specified file.
522      * Returns null if the file does not exist.
523      */

524     private static String JavaDoc[] 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 JavaDoc line;
532                     while ((line = reader.readLine()) != null) {
533                         fileContentStore.add(line);
534                     }
535                     return (String JavaDoc[]) fileContentStore.toArray(new String JavaDoc[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 the IFile doesn't exist or the underlying File doesn't exist,
545
// just return null to indicate the absence of the file
546
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     /*
554      * Writes all lines to the specified file, using linefeed terminators for
555      * compatibility with other CVS clients.
556      */

557     private static void writeLines(final IFile file, final String JavaDoc[] contents) throws CVSException {
558         try {
559             // The creation of sync files has to be in a runnable in order for the resulting delta
560
// to include the MODSTAMP value. If not in a runnable then create/setContents
561
// will trigger a delta and the SyncFileWriter change listener won't know that the delta
562
// was a result of our own creation.
563
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 /*don't keep history but do force*/, null);
570                         } else {
571                             file.setContents(new ByteArrayInputStream(os.toByteArray()), IResource.FORCE /*don't keep history but do force*/, null);
572                         }
573                         file.setSessionProperty(MODSTAMP_KEY, new Long JavaDoc(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 JavaDoc[] 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     /**
601      * Method writeFileToBaseDirectory.
602      *
603      * @param file
604      * @param info
605      */

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 /* force */, true /* local */, Policy.subMonitorFor(monitor, 10));
613             }
614             IFile target = baseFolder.getFile(new Path(null, file.getName()));
615             if (target.exists()) {
616                 // XXX Should ensure that we haven't already copied it
617
// XXX write the revision to the CVS/Baserev file
618
setReadOnly(target, false);
619                 target.delete(true, Policy.subMonitorFor(monitor, 10));
620             }
621             // Copy the file so the timestamp is maintained
622
file.copy(target.getFullPath(), true /* force */, Policy.subMonitorFor(monitor, 80));
623         } catch (CoreException e) {
624             throw CVSException.wrapException(e);
625         } finally {
626             monitor.done();
627         }
628     }
629     /**
630      * Method restoreFileFromBaseDirectory.
631      * @param file
632      * @param info
633      * @param monitor
634      */

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 JavaDoc[] { file.getFullPath().toString() }), file);
643                 throw new CVSException(status);
644             }
645             if (file.exists()) {
646                 file.delete(false /* force */, true /* keep history */, Policy.subMonitorFor(monitor, 10));
647             }
648             // Make the source writtable to avoid problems on some file systems (bug 109308)
649
setReadOnly(source, false);
650             // Copy the file so the timestamp is maintained
651
source.move(file.getFullPath(), false /* force */, true /* keep history */,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                 // Just log the failure since the move may succeed anyway
667
CVSProviderPlugin.log(e);
668             }
669         }
670     }
671     
672     /**
673      * Method deleteFileFromBaseDirectory.
674      * @param file
675      * @param monitor
676      */

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     /**
701      * Return a handle to the CVS/Template file for the given folder
702      * @param folder
703      * @return IFile
704      * @throws CVSException
705      */

706     public static IFile getTemplateFile(IContainer folder) throws CVSException {
707         IFolder cvsFolder = createCVSSubdirectory(folder);
708         return cvsFolder.getFile("Template"); //$NON-NLS-1$
709
}
710     
711     /**
712      * Method isEdited.
713      * @param resource
714      * @return boolean
715      */

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 JavaDoc property = System.getProperty("line.separator"); //$NON-NLS-1$
725
if (property != null) return property.getBytes();
726         }
727         return new byte[] { 0x0A };
728     }
729
730 }
731
Popular Tags