Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6ae0fdc7086c06926938470845702da882e13b70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial API and implementation
 ******************************************************************************/
package org.eclipse.team.internal.ccvs.core.resources;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSTeamProvider;
import org.eclipse.team.internal.ccvs.core.ICVSFile;
import org.eclipse.team.internal.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.ICVSResource;
import org.eclipse.team.internal.ccvs.core.ICVSResourceVisitor;
import org.eclipse.team.internal.ccvs.core.ICVSRunnable;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.core.util.MoveDeleteHook;

/**
 * Implements the ICVSFolder interface on top of an 
 * instance of the ICVSFolder interface
 * 
 * @see ICVSFolder
 */
class EclipseFolder extends EclipseResource implements ICVSFolder {

	protected EclipseFolder(IContainer container) {
		super(container);		
	}
	
	/**
	 * @see ICVSFolder#members(int)
	 */
	public ICVSResource[] members(int flags) throws CVSException {		
		final List result = new ArrayList();
		IResource[] resources = EclipseSynchronizer.getInstance().members((IContainer)resource);
		boolean includeFiles = (((flags & FILE_MEMBERS) != 0) || ((flags & (FILE_MEMBERS | FOLDER_MEMBERS)) == 0));
		boolean includeFolders = (((flags & FOLDER_MEMBERS) != 0) || ((flags & (FILE_MEMBERS | FOLDER_MEMBERS)) == 0));
		boolean includeManaged = (((flags & MANAGED_MEMBERS) != 0) || ((flags & (MANAGED_MEMBERS | UNMANAGED_MEMBERS | IGNORED_MEMBERS)) == 0));
		boolean includeUnmanaged = (((flags & UNMANAGED_MEMBERS) != 0) || ((flags & (MANAGED_MEMBERS | UNMANAGED_MEMBERS | IGNORED_MEMBERS)) == 0));
		boolean includeIgnored = ((flags & IGNORED_MEMBERS) != 0);
		boolean includeExisting = (((flags & EXISTING_MEMBERS) != 0) || ((flags & (EXISTING_MEMBERS | PHANTOM_MEMBERS)) == 0));
		boolean includePhantoms = (((flags & PHANTOM_MEMBERS) != 0) || ((flags & (EXISTING_MEMBERS | PHANTOM_MEMBERS)) == 0));
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
			if ((includeFiles && (resource.getType()==IResource.FILE)) 
					|| (includeFolders && (resource.getType()==IResource.FOLDER))) {
				boolean isManaged = cvsResource.isManaged();
				boolean isIgnored = cvsResource.isIgnored();
				if ((isManaged && includeManaged)|| (isIgnored && includeIgnored)
						|| ( ! isManaged && ! isIgnored && includeUnmanaged)) {
					boolean exists = cvsResource.exists();
					if ((includeExisting && exists) || (includePhantoms && !exists)) {
						result.add(cvsResource);
					}
				}
						
			}		
		}	
		return (ICVSResource[]) result.toArray(new ICVSResource[result.size()]);
	}

	/**
	 * @see ICVSFolder#createFolder(String)
	 */
	public ICVSFolder getFolder(String name) throws CVSException {
		if ((CURRENT_LOCAL_FOLDER.equals(name)) || ((CURRENT_LOCAL_FOLDER + SEPARATOR).equals(name)))
			return this;
		IPath path = new Path(name);
		if(resource.getType()==IResource.ROOT && path.segmentCount()==1) {
			return new EclipseFolder(((IWorkspaceRoot)resource).getProject(name));
		} else {
			return new EclipseFolder(((IContainer)resource).getFolder(new Path(name)));
		}
	}

	/**
	 * @see ICVSFolder#createFile(String)
	 */
	public ICVSFile getFile(String name) throws CVSException {
		return new EclipseFile(((IContainer)resource).getFile(new Path(name)));
	}

	/**
	 * @see ICVSFolder#mkdir()
	 */
	public void mkdir() throws CVSException {
		try {
			if(resource.getType()==IResource.PROJECT) {
				IProject project = (IProject)resource;
				project.create(null);
				project.open(null);				
			} else {
				((IFolder)resource).create(false /*don't force*/, true /*make local*/, null);
				folderCreated();
			}				
		} catch (CoreException e) {
			throw CVSException.wrapException(resource, Policy.bind("EclipseFolder_problem_creating", resource.getFullPath().toString(), e.getStatus().getMessage()), e); //$NON-NLS-1$
		} 
	}
	/**
	 * Method folderCreated.
	 */
	protected void folderCreated() throws CVSException {
		Integer count = EclipseSynchronizer.getInstance().getDirtyCount((IContainer)getIResource());
		if (count != null) {
			flushWithAncestors();
		}
		EclipseSynchronizer.getInstance().folderCreated((IFolder)getIResource());
	}
		
	/**
	 * @see ICVSResource#isFolder()
	 */
	public boolean isFolder() {
		return true;
	}
		
	/**
	 * @see ICVSFolder#acceptChildren(ICVSResourceVisitor)
	 */
	public void acceptChildren(ICVSResourceVisitor visitor) throws CVSException {
		
		// Visit files and then folders
		ICVSResource[] subFiles = members(FILE_MEMBERS);
		for (int i=0; i<subFiles.length; i++) {
			subFiles[i].accept(visitor);
		}
		ICVSResource[] subFolders = members(FOLDER_MEMBERS);
		for (int i=0; i<subFolders.length; i++) {
			subFolders[i].accept(visitor);
		}
	}

	/**
	 * @see ICVSResource#accept(ICVSResourceVisitor)
	 */
	public void accept(ICVSResourceVisitor visitor) throws CVSException {
		visitor.visitFolder(this);
	}
	
	/**
	 * @see ICVSResource#accept(ICVSResourceVisitor, boolean)
	 */
	public void accept(ICVSResourceVisitor visitor, boolean recurse) throws CVSException {
		visitor.visitFolder(this);
		ICVSResource[] resources;
		if (recurse) {
			resources = members(ICVSFolder.ALL_MEMBERS);
		} else {
			resources = members(ICVSFolder.FILE_MEMBERS);
		}
		for (int i = 0; i < resources.length; i++) {
			resources[i].accept(visitor, recurse);
		}
	}

	/**
	 * @see ICVSResource#getRemoteLocation(ICVSFolder)
	 */
	public String getRemoteLocation(ICVSFolder stopSearching) throws CVSException {
				
		if (getFolderSyncInfo() != null) {
			return getFolderSyncInfo().getRemoteLocation();
		}			

		ICVSFolder parent = getParent();
		if(parent!=null && !equals(stopSearching)) {
			String parentLocation;
			parentLocation = parent.getRemoteLocation(stopSearching);
			if (parentLocation!=null) {
				return parentLocation + SEPARATOR + getName();
			}		
		}
		return null;
	}

	/*
	 * @see ICVSFolder#getFolderInfo()
	 */
	public FolderSyncInfo getFolderSyncInfo() throws CVSException {
		return EclipseSynchronizer.getInstance().getFolderSync((IContainer)resource);
	}

	/*
	 * @see ICVSFolder#setFolderInfo(FolderSyncInfo)
	 */
	public void setFolderSyncInfo(FolderSyncInfo folderInfo) throws CVSException {
		EclipseSynchronizer synchronizer = EclipseSynchronizer.getInstance();
		FolderSyncInfo oldInfo = synchronizer.getFolderSync((IContainer)resource);
		synchronizer.setFolderSync((IContainer)resource, folderInfo);
		// the server won't add directories as sync info, therefore it must be done when
		// a directory is shared with the repository.
		setSyncInfo(new ResourceSyncInfo(getName()));
		// if the sync info changed from null, we may need to adjust the ancestors
		if (oldInfo == null) {
			Integer count = synchronizer.getDirtyCount((IContainer)getIResource());
			if (count == null) {
				// There was no cached count. Flush the ancestors so they are recalculated
				flushWithAncestors();
			} else {
				// There is a count. Decrement the parent's count if the count is zero.
				// Otherwise, the receiver and it's parents remain dirty.
				if (count.intValue() == 0) {
					synchronizer.setDirtyIndicator(getIResource(), EclipseSynchronizer.NOT_DIRTY_INDICATOR);
					((EclipseFolder)getParent()).adjustModifiedCount(false);
				}
			}
		}
	}

	/*
	 * @see ICVSFolder#isCVSFolder()
	 */
	public boolean isCVSFolder() throws CVSException {
		return EclipseSynchronizer.getInstance().getFolderSync((IContainer)resource) != null;
	}

	/*
	 * @see ICVSResource#unmanage()
	 */
	public void unmanage(IProgressMonitor monitor) throws CVSException {
		monitor = Policy.monitorFor(monitor);
		try {
			monitor.beginTask("", 100); //$NON-NLS-1$
			run(new ICVSRunnable() {
				public void run(IProgressMonitor monitor) throws CVSException {
					recursiveUnmanage((IContainer) resource, monitor);				
				}
			}, Policy.subMonitorFor(monitor, 99));
			// unmanaged from parent
			super.unmanage(Policy.subMonitorFor(monitor, 1));
			flushWithAncestors();
		} finally {
			monitor.done();
		}
	}
	
	private static void recursiveUnmanage(IContainer container, IProgressMonitor monitor) throws CVSException {
		try {
			monitor.beginTask(null, 10);
			monitor.subTask(container.getFullPath().toOSString());
			EclipseSynchronizer.getInstance().deleteFolderSync(container);
			EclipseSynchronizer.getInstance().flushModificationCache(container);			
			IResource[] members = container.members(true);
			for (int i = 0; i < members.length; i++) {
				monitor.worked(1);
				IResource resource = members[i];
				if (members[i].getType() == IResource.FILE) {
					EclipseSynchronizer.getInstance().flushModificationCache((IFile)resource);
				} else {
					recursiveUnmanage((IContainer) resource, monitor);
				}
			}
		} catch (CoreException e) {
		} finally {
			monitor.done();
		}
	}
	
	/*
	 * @see ICVSResource#isIgnored()
	 */
	public boolean isIgnored() throws CVSException {
		if(isCVSFolder()) {
			return false;
		}		
		return super.isIgnored();
	}
	
	/*
	 * @see ICVSFolder#getChild(String)
	 */
	public ICVSResource getChild(String namedPath) throws CVSException {
		IPath path = new Path(namedPath);
		if(path.segmentCount()==0) {
			 return this;
		}
		IResource child = ((IContainer)resource).findMember(path, true /* include phantoms */);
		if(child!=null) {
			if(child.getType()==IResource.FILE) {
				return new EclipseFile((IFile)child);
			} else {
				return new EclipseFolder((IContainer)child);
			}
		}
		return null;
	}
	
	/*
	 * @see ICVSFolder#run(ICVSRunnable, IProgressMonitor)
	 */
	public void run(final ICVSRunnable job, IProgressMonitor monitor) throws CVSException {
		final CVSException[] error = new CVSException[1];
		// Remove the registered Move/Delete hook, assuming that the cvs runnable will keep sync info up-to-date
		final MoveDeleteHook hook = CVSTeamProvider.getRegisteredMoveDeleteHook();
		boolean oldSetting = hook.isWithinCVSOperation();
		try {
			ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
				public void run(IProgressMonitor monitor) throws CoreException {
					try {
						hook.setWithinCVSOperation(true);
						EclipseSynchronizer.getInstance().run(job, monitor);
					} catch(CVSException e) {
						error[0] = e; 
					}
				}
			}, monitor);
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		} finally {
			hook.setWithinCVSOperation(oldSetting);
		}
		if(error[0]!=null) {
			throw error[0];
		}
	}
	
	/**
	 * Running with a flag of READ_ONLY will still ensure that only one thread
	 * is accessing the sync info but will not run inside a workspace runnable
	 * in order to avoid doing a build.
	 * 
	 * @see org.eclipse.team.internal.ccvs.core.ICVSFolder#run(ICVSRunnable, int, IProgressMonitor)
	 */
	public void run(final ICVSRunnable job, int flags, IProgressMonitor monitor) throws CVSException {
		if (flags == READ_ONLY)
			EclipseSynchronizer.getInstance().run(job, monitor);
		else
			run(job, monitor);
	}
		
	/**
	 * @see ICVSFolder#fetchChildren(IProgressMonitor)
	 */
	public ICVSResource[] fetchChildren(IProgressMonitor monitor) throws CVSException {
		return members(FILE_MEMBERS | FOLDER_MEMBERS);
	}
	/**
	 * @see org.eclipse.team.internal.ccvs.core.ICVSResource#delete()
	 */
	public void delete() throws CVSException {
		if (!exists()) return;
		if (isCVSFolder()) {
			EclipseSynchronizer.getInstance().prepareForDeletion((IContainer)getIResource());
		}
		super.delete();
	}
	
	/**
	 * Method adjustParentCount.
	 * @param file
	 * @param b
	 */
	protected void adjustModifiedCount(boolean modified) throws CVSException {
		flushWithAncestors();
//		if (EclipseSynchronizer.getInstance().adjustModifiedCount((IContainer)getIResource(), modified)) {
//			((EclipseFolder)getParent()).adjustModifiedCount(modified);
//		}
	}
	
	/*
	 * Flush all cached info for the container and it's ancestors
	 */
	protected void flushModificationCache() throws CVSException {
		EclipseSynchronizer.getInstance().flushModificationCache((IContainer)getIResource());
	}
	
	/*
	 * Either record the deletion (if modified is true) or remove the deletion if
	 * it is already recorded (if modified is false).
	 */
	protected void handleDeletion(IFile file, boolean modified) throws CVSException {
		boolean adjustParent;
		Integer dirtyCount = EclipseSynchronizer.getInstance().getDirtyCount((IContainer)getIResource());
		if (dirtyCount == null) return;
		if (modified) {
			adjustParent = EclipseSynchronizer.getInstance().addDeletedChild((IContainer)getIResource(), file);
		} else {
			adjustParent = EclipseSynchronizer.getInstance().removeDeletedChild((IContainer)getIResource(), file);
		}
		if (adjustParent) {
			adjustModifiedCount(modified);
		}
	}
	
	public boolean isModified() throws CVSException {
		// If it's not a CVS folder, assume it's modified unless it is ignored
		// if (isIgnored()) return false;
		IContainer container = (IContainer)getIResource();
		boolean shared = isCVSFolder();
		Integer count = EclipseSynchronizer.getInstance().getDirtyCount(container);
		if (count == null) {
			String indicator = EclipseSynchronizer.getInstance().getDirtyIndicator(container);
			if (indicator == null) {
				// We have no cached info for the folder. We'll need to check directly,
				// caching as go.
				indicator = determineDirtyCount(indicator, shared);
			} else {
				// the count has not been initialized yet
				if (indicator == EclipseSynchronizer.NOT_DIRTY_INDICATOR) {
					// the folder is not dirty so set the count to zero
					EclipseSynchronizer.getInstance().setDirtyCount(container, 0);
				} else {
					// The folder is dirty
					indicator = determineDirtyCount(indicator, shared);
				}
			}
			return indicator == EclipseSynchronizer.IS_DIRTY_INDICATOR;
		} else {
			return isModified(count.intValue(), shared);
		}
	}
	
	public boolean handleModification(boolean forAddition) throws CVSException {
		if (isIgnored()) return false;
		// For non-additions, we are only interested in sync info changes
		if (!forAddition) return false;
		// the folder is an addition.
		FolderSyncInfo info = getFolderSyncInfo();
		// if the folder has sync info, it was handled is setFolderInfo
		// otherwise, flush the ancestors to recalculate
		if (info == null) {
			flushWithAncestors();
		}
		return true;
		
	}
	
	/**
	 * Method determineDirtyCount.
	 */
	private String determineDirtyCount(String indicator, boolean shared) throws CVSException {
		IContainer container = (IContainer)getIResource();
		ICVSResource[] children = members(ALL_UNIGNORED_MEMBERS);
		int count = 0;
		Set deletedChildren = new HashSet();
		for (int i = 0; i < children.length; i++) {
			ICVSResource resource = children[i];
			if (resource.isModified()) {
				count++;
				if (!resource.isFolder() && !resource.exists()) {
					deletedChildren.add(resource.getName());
				}
			}
		}
		if (!isModified(count, shared) && indicator != EclipseSynchronizer.NOT_DIRTY_INDICATOR) {
			indicator = EclipseSynchronizer.NOT_DIRTY_INDICATOR;
			EclipseSynchronizer.getInstance().setDirtyIndicator(container, indicator);
		} else if ((isModified(count, shared)) && indicator != EclipseSynchronizer.IS_DIRTY_INDICATOR) {
			indicator = EclipseSynchronizer.IS_DIRTY_INDICATOR;
			EclipseSynchronizer.getInstance().setDirtyIndicator(container, indicator);
			if (!deletedChildren.isEmpty()) {
				EclipseSynchronizer.getInstance().setDeletedChildren((IContainer)getIResource(), deletedChildren);
			}
		}
		EclipseSynchronizer.getInstance().setDirtyCount(container, count);
		return indicator;
	}
	
	private boolean isModified(int count, boolean shared) {
		return count > 0 || !shared;
	}
	
	/*
	 * @see org.eclipse.team.internal.ccvs.core.resources.EclipseResource#prepareToBeDeleted()
	 */
	protected void prepareToBeDeleted() throws CVSException {
		if (isCVSFolder()) {
			EclipseSynchronizer.getInstance().prepareForDeletion((IContainer)getIResource());
		}
		super.prepareToBeDeleted();
	}
	
	public void syncInfoChanged() throws CVSException {
		// It hard to deterime the effect of sync info for forlders so just flush the parent info
		String indicator = EclipseSynchronizer.getInstance().getDirtyIndicator(getIResource());
		if (indicator != EclipseSynchronizer.IS_DIRTY_INDICATOR) {
			flushWithAncestors();
		}
		if (isIgnored()) {
			// make sure the folder (or any of it's childen are no longer marked
			EclipseSynchronizer.getInstance().flushModificationCache((IContainer)getIResource(), IResource.DEPTH_INFINITE);
		}
	}
}

Back to the top