Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e81337326fd790da5b40b12b2303e3a4870d8c8f (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.core.util;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.ICVSFile;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.resources.EclipseSynchronizer;
import org.eclipse.team.internal.ccvs.core.syncinfo.DeferredResourceChangeHandler;

/*
 * Listens to CVS meta-file changes and notifies the EclipseSynchronizer of changes made to sync files 
 * by 3rd parties.
 * 
 * If CVS meta-directories are created outside of the CVS plugin their team-private state will be set
 * by this listener however this change won't be known to other plugins because it does not generate 
 * a delta. As a result views, such as the navigator, may show CVS team-private directories. There
 * are some common scenarios where a user may (depending on the order of delta traversal)  see 
 * this behavior:
 * 
 * 1. A user has an existing CVS project outside of Eclipse. By creating the project in Eclipse to point
 * to the existing location the project's contents will be brought into Eclipse and the CVS folders
 * will be marlked as team-private but other delta listeners that have handled the event already won't receive
 * notification that the resource is now team-private. As a result, the user may have to close views or 
 * restart the workbench to have the CVS folders filtered.
 * 
 * 2. A user performs CVS command line operations outside of Eclipse that result in new CVS folders.
 * From Eclipse the refresh local will bring in the new folders and they will be marked as team-private.
 * But as in 1, they may not appear in the UI.
 * 
 * See: http://dev.eclipse.org/bugs/show_bug.cgi?id=12386
 */
public class SyncFileChangeListener implements IResourceChangeListener {
	
	// consider the following changes types and ignore the others (e.g. marker and description changes are ignored)
	protected int INTERESTING_CHANGES = 	IResourceDelta.CONTENT | 
																	IResourceDelta.MOVED_FROM | 
																	IResourceDelta.MOVED_TO |
																	IResourceDelta.OPEN | 
																	IResourceDelta.REPLACED |
																	IResourceDelta.TYPE;
	
	protected boolean isProjectOpening = false;
	
	protected static DeferredResourceChangeHandler deferredHandler = new DeferredResourceChangeHandler();
	
	/**
	 * This accessor is for use by test cases only.
	 * 
	 * @return Returns the deferredHandler.
	 */
	public static DeferredResourceChangeHandler getDeferredHandler() {
		return deferredHandler;
	}
	
	/*
	 * When a resource changes this method will detect if the changed resources is a meta file that has changed 
	 * by a 3rd party. For example, if the command line tool was run and then the user refreshed from local. To
	 * distinguish changes made by this class and thoses made by others a modification stamp is persisted with each
	 * metafile.
	 * 
	 * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
	 */
	public void resourceChanged(IResourceChangeEvent event) {
		try {
			final Set changedContainers = new HashSet();
			final Set externalDeletions = new HashSet();
			
			setProjectOpening(false);
			
			event.getDelta().accept(new IResourceDeltaVisitor() {

				public boolean visit(IResourceDelta delta) {
					IResource resource = delta.getResource();
					
					if(resource.getType()==IResource.ROOT) {
						// continue with the delta
						return true;
					}
					
					if (resource.getType() == IResource.PROJECT) {
						// If the project is not accessible, don't process it
						if (!resource.isAccessible()) return false;
						setProjectOpening((delta.getFlags() & IResourceDelta.OPEN) != 0);
					}
															
					String name = resource.getName();
					int kind = delta.getKind();
					
					// if the file has changed but not in a way that we care
					// then ignore the change (e.g. marker changes to files).
					if(kind == IResourceDelta.CHANGED && 
						(delta.getFlags() & INTERESTING_CHANGES) == 0) {
							return true;
					}
										
					if(name.equals(SyncFileWriter.CVS_DIRNAME)) {
						handleCVSDir((IContainer)resource, kind);
						// if the project is opening there is no need to notify about chagned CVs/ meta files
						// they will all be read from disk.
						if(isProjectOpening()) return false;
					} else {
						// Inform the synchronizer about folder creations
						if(isProjectOpening()) return true;
					}
					
					if(isMetaFile(resource)) {
						IResource[] toBeNotified = handleChangedMetaFile(resource);
						if(toBeNotified.length>0 && isModifiedBy3rdParty(resource)) {
							for (IResource t : toBeNotified) {
								changedContainers.add(t);							
							}
							if(Policy.DEBUG_METAFILE_CHANGES) {
								System.out.println("[cvs] metafile changed by 3rd party: " + resource.getFullPath()); //$NON-NLS-1$
							}
							return false; /*don't visit any children we have all the information we need*/
						}
					} else if(isIgnoreFile(resource) && isModifiedBy3rdParty(resource)) {
						deferredHandler.ignoreFileChanged((IFile)resource);
					} else if (isExternalDeletion(resource, kind)) {
						externalDeletions.add(resource);
					} else if (kind == IResourceDelta.ADDED && isRecreation(resource)) {
						deferredHandler.recreated(resource);
					}
					return true;
				}
			}, IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS);
				
			if(!changedContainers.isEmpty() || !externalDeletions.isEmpty()) {
				EclipseSynchronizer.getInstance().syncFilesChangedExternally(
					(IContainer[])changedContainers.toArray(new IContainer[changedContainers.size()]),
					(IFile[]) externalDeletions.toArray(new IFile[externalDeletions.size()]));
			}			
		} catch(CoreException e) {
			CVSProviderPlugin.log(e);
		}
	}

	/**
	 * Treat a resource as an external deletion if 
	 *   - it is a file 
	 *   - the delta says the file was removed
	 *   - the file is not managed but its parent is a CVS folder
	 * 
	 * There will be some false positives but the reaction to this situation
	 * is to purge the cahced CVS meta-information so nothing bad will happen
	 * for the false positives.
	 * 
	 * @param resource
	 * @param kind
	 * @return
	 */
	protected boolean isExternalDeletion(IResource resource, int kind) {
		if (kind != IResourceDelta.REMOVED) return false;
		if (resource.getType() != IResource.FILE) return false;
		ICVSFile file = CVSWorkspaceRoot.getCVSFileFor((IFile)resource);
		try {
			return (!file.isManaged() && file.getParent().isCVSFolder() && file.getParent().exists());
		} catch (CVSException e) {
			CVSProviderPlugin.log(e);
			return false;
		}
	}

	/*
	 * Consider non-existing resources as being recently deleted and thus modified, and resources
	 * with modification stamps that differ from when the CVS plugin last modified the meta-file.
	 */
	protected boolean isModifiedBy3rdParty(IResource resource) {
		if(!resource.exists()) return true;
		long modStamp = resource.getModificationStamp();
		Long whenWeWrote;
		try {
			whenWeWrote = (Long)resource.getSessionProperty(SyncFileWriter.MODSTAMP_KEY);
		} catch(CoreException e) {
			CVSProviderPlugin.log(e);
			whenWeWrote = null;
		}
		return (whenWeWrote==null || whenWeWrote.longValue() != modStamp);
	}
	
	/*
	 * If it's a new CVS directory with the canonical child metafiles then mark it as team-private. Otherwise
	 * if changed or deleted
	 */	
	protected void handleCVSDir(IContainer cvsDir, int kind) {
		if((kind & IResourceDelta.ALL_WITH_PHANTOMS)!=0) {
			if(kind==IResourceDelta.ADDED) {
				// should this dir be made team-private? If it contains CVS/Root and CVS/Repository then yes!
				IFile rootFile = cvsDir.getFile(new Path(SyncFileWriter.ROOT));
				IFile repositoryFile = cvsDir.getFile(new Path(SyncFileWriter.REPOSITORY));
				if(rootFile.exists() && repositoryFile.exists() && !cvsDir.isTeamPrivateMember()) {
					try {
						// TODO: Is this considered a tree modification?
						cvsDir.setTeamPrivateMember(true);			
						if(Policy.DEBUG_METAFILE_CHANGES) {
							System.out.println("[cvs] found a new CVS meta folder, marking as team-private: " + cvsDir.getFullPath()); //$NON-NLS-1$
						}
					} catch(CoreException e) {
						CVSProviderPlugin.log(CVSException.wrapException(cvsDir, CVSMessages.SyncFileChangeListener_errorSettingTeamPrivateFlag, e)); 
					}
				}
			}
		}
	}
	
	protected boolean isIgnoreFile(IResource resource) {
		return resource.getType() == IResource.FILE &&
			resource.getName().equals(SyncFileWriter.IGNORE_FILE);
	}
	
	private boolean isRecreation(IResource resource) {
		return EclipseSynchronizer.getInstance().wasPhantom(resource);
	}
	
	/*
	 * It's a meta file if it's parent is a team-private CVS folder.
	 */
	protected boolean isMetaFile(IResource resource) {
		IContainer parent = resource.getParent();		
		return resource.getType() == IResource.FILE &&
					parent!=null && 
					parent.getName().equals(SyncFileWriter.CVS_DIRNAME) &&
					(parent.isTeamPrivateMember() || !parent.exists());
	}
	
	/*
	 * This is a meta file (e.g. folder/CVS/Entries), notify that 'folder' and it's immediate children 
	 * may have their CVS sync state changed. If the 'folder' is deleted than no notification is
	 * required.
	 */
	protected IContainer[] handleChangedMetaFile(IResource resource) {		
		IContainer changedContainer = resource.getParent().getParent();
		if(changedContainer.exists()) {
			return new IContainer[] {changedContainer};
		} else {
			return new IContainer[0];
		}
	}
	
	/**
	 * @return boolean
	 */
	public boolean isProjectOpening() {
		return isProjectOpening;
	}

	/**
	 * Sets the isProjectOpening.
	 * @param isProjectOpening The isProjectOpening to set
	 */
	public void setProjectOpening(boolean isProjectOpening) {
		this.isProjectOpening = isProjectOpening;
	}
}

Back to the top