Skip to main content
summaryrefslogtreecommitdiffstats
blob: 944fcb34ba04b8f2955b9352af10fc99359efd36 (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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*******************************************************************************
 * 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 implementation
 ******************************************************************************/
package org.eclipse.team.internal.ccvs.core.resources;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
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.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSStatus;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
import org.eclipse.team.internal.ccvs.core.util.SyncFileWriter;

/**
 * This cache uses session properties to hold the bytes representing the sync
 * info
 */
/*package*/ class SessionPropertySyncInfoCache extends LowLevelSyncInfoCache {
	
	/*package*/ static final String[] NULL_IGNORES = new String[0];
	private static final FolderSyncInfo NULL_FOLDER_SYNC_INFO = new FolderSyncInfo("", "", null, false); //$NON-NLS-1$ //$NON-NLS-2$
	
	private Set changedResourceSync = new HashSet();
	private Set changedFolderSync = new HashSet();
	
	/**
	 * If not already cached, loads and caches the folder ignores sync for the container.
	 * Folder must exist and must not be the workspace root.
	 *
	 * @param container the container
	 * @return the folder ignore patterns, or an empty array if none
	 */
	/*package*/ String[] cacheFolderIgnores(IContainer container) throws CVSException {
		try {
			// don't try to load if the information is already cached
			String[] ignores = (String[])container.getSessionProperty(IGNORE_SYNC_KEY);
			if (ignores == null) {
				// read folder ignores and remember it
				ignores = SyncFileWriter.readCVSIgnoreEntries(container);
				if (ignores == null) ignores = NULL_IGNORES;
				container.setSessionProperty(IGNORE_SYNC_KEY, ignores);
			}
			return ignores;
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}


	/**
	 * If not already cached, loads and caches the folder sync for the container.
	 * Folder must exist and must not be the workspace root.
	 *
	 * @param container the container
	 * @return the folder sync info for the folder, or null if none.
	 */
	/*package*/ FolderSyncInfo cacheFolderSync(IContainer container) throws CVSException {
		if (!container.exists()) return null;
		try {
			// don't try to load if the information is already cached
			FolderSyncInfo info = (FolderSyncInfo)container.getSessionProperty(FOLDER_SYNC_KEY);
			if (info == null) {
				// read folder sync info and remember it
				info = SyncFileWriter.readFolderSync(container);
				if (info == null) {
					container.setSessionProperty(FOLDER_SYNC_KEY, NULL_FOLDER_SYNC_INFO);
				} else {
					container.setSessionProperty(FOLDER_SYNC_KEY, info);
				}
			} else if (info == NULL_FOLDER_SYNC_INFO) {
				info = null;
			}
			return info;
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}


	/**
	 * If not already cached, loads and caches the resource sync for the children of the container.
	 * Folder must exist and must not be the workspace root.
	 *
	 * @param container the container
	 */
	/*package*/ void cacheResourceSyncForChildren(IContainer container) throws CVSException {
		if (!container.exists()) return;
		try {
			// don't try to load if the information is already cached
			byte[][] infos = (byte[][])container.getSessionProperty(RESOURCE_SYNC_KEY);
			if (infos == null) {
				// load the sync info from disk
				infos = SyncFileWriter.readAllResourceSync(container);
				if (infos == null) {
					infos = EMPTY_RESOURCE_SYNC_INFOS;
				}
				container.setSessionProperty(RESOURCE_SYNC_KEY, infos);
			}
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}

	/**
	 * Returns the folder sync info for the container; null if none.
	 * Folder must exist and must not be the workspace root.
	 * The folder sync info for the container MUST ALREADY BE CACHED.
	 *
	 * @param container the container
	 * @return the folder sync info for the folder, or null if none.
	 * @see #cacheFolderSync
	 */
	/*package*/ FolderSyncInfo getCachedFolderSync(IContainer container) throws CVSException {
		if (!container.exists()) return null;
		try {
			FolderSyncInfo info = (FolderSyncInfo)container.getSessionProperty(FOLDER_SYNC_KEY);
			if (info == null) {
				// There should be sync info but it was missing. Report the error
				throw new CVSException(Policy.bind("EclipseSynchronizer.folderSyncInfoMissing", container.getFullPath().toString())); //$NON-NLS-1$
			}
			if (info == NULL_FOLDER_SYNC_INFO) return null;
			return info;
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}

	/**
	 * Returns the resource sync info for all children of the container.
	 * Container must exist and must not be the workspace root.
	 * The resource sync info for the children of the container MUST ALREADY BE CACHED.
	 *
	 * @param container the container
	 * @return a collection of the resource sync info's for all children
	 * @see #cacheResourceSyncForChildren
	 */
	/*package*/ byte[][] getCachedResourceSyncForChildren(IContainer container) throws CVSException {
		if (!container.exists()) return EMPTY_RESOURCE_SYNC_INFOS;
		try {
			byte[][] infos = (byte[][])container.getSessionProperty(RESOURCE_SYNC_KEY);
			// todo: move check to caller
			if (infos == null) {
				// There should be sync info but it was missing. Report the error
				throw new CVSException(Policy.bind("EclipseSynchronizer.folderSyncInfoMissing", container.getFullPath().toString())); //$NON-NLS-1$
			}
			return infos;
		} catch(CoreException e) {
			throw CVSException.wrapException(e);
		}
	}

	/**
	 * Purges the cache recursively for all resources beneath the container.
	 * There must not be any pending uncommitted changes.
	 */
	/*package*/ void purgeCache(IContainer container, boolean deep) throws CVSException {
		if (! container.exists()) return;
		try {
			if (container.getType() != IResource.ROOT) {
				container.setSessionProperty(RESOURCE_SYNC_KEY, null);
				container.setSessionProperty(IGNORE_SYNC_KEY, null);
				container.setSessionProperty(FOLDER_SYNC_KEY, null);
			}
			if(deep) {
				IResource[] members = container.members();
				for (int i = 0; i < members.length; i++) {
					IResource resource = members[i];
					if (resource.getType() != IResource.FILE) {
						purgeCache((IContainer) resource, deep);
					}
				}
			}
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/**
	 * Sets the array of folder ignore patterns for the container, must not be null.
	 * Folder must exist and must not be the workspace root.
	 *
	 * @param container the container
	 * @param ignores the array of ignore patterns
	 */
	/*package*/ void setCachedFolderIgnores(IContainer container, String[] ignores) throws CVSException {
		try {
			container.setSessionProperty(IGNORE_SYNC_KEY, ignores);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}


	/**
	 * Sets the folder sync info for the container; if null, deletes it.
	 * Folder must exist and must not be the workspace root.
	 * The folder sync info for the container need not have previously been cached.
	 *
	 * @param container the container
	 * @param info the new folder sync info
	 */
	/*package*/ void setCachedFolderSync(IContainer container, FolderSyncInfo info) throws CVSException {
		if (!container.exists()) return;
		try {
			if (info == null) info = NULL_FOLDER_SYNC_INFO;
			container.setSessionProperty(FOLDER_SYNC_KEY, info);
			changedFolderSync.add(container);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/**
	 * Sets the resource sync info for the resource; if null, deletes it. Parent
	 * must exist and must not be the workspace root. The resource sync info for
	 * the children of the parent container MUST ALREADY BE CACHED.
	 *
	 * @param resource the resource
	 * @param info the new resource sync info
	 * @see #cacheResourceSyncForChildren
	 */
	/*package*/ void setCachedResourceSyncForChildren(IContainer container, byte[][] infos) throws CVSException {
		if (!container.exists()) return;
		try {
			if (infos == null)
				infos = EMPTY_RESOURCE_SYNC_INFOS;
			container.setSessionProperty(RESOURCE_SYNC_KEY, infos);
			changedResourceSync.add(container);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/**
	 * Commits the cache after a series of operations.
	 *
	 * Will return STATUS_OK unless there were problems writting sync
	 * information to disk. If an error occurs a multistatus is returned
	 * with the list of reasons for the failures. Failures are recovered,
	 * and all changed resources are given a chance to be written to disk.
	 *
	 * @param monitor the progress monitor, may be null
	 */
	/*package*/ IStatus commitCache(IProgressMonitor monitor) {
		List errors = new ArrayList();
		try {
			/*** prepare operation ***/
			// find parents of changed resources

			monitor = Policy.monitorFor(monitor);
			int numDirty = changedResourceSync.size();
			int numResources = changedFolderSync.size() + numDirty;
			monitor.beginTask(null, numResources);
			if(monitor.isCanceled()) {
				monitor.subTask(Policy.bind("EclipseSynchronizer.UpdatingSyncEndOperationCancelled")); //$NON-NLS-1$
			} else {
				monitor.subTask(Policy.bind("EclipseSynchronizer.UpdatingSyncEndOperation")); //$NON-NLS-1$
			}

			/*** write sync info to disk ***/
			// folder sync info changes
			for(Iterator it = changedFolderSync.iterator(); it.hasNext();) {
				IContainer folder = (IContainer) it.next();
				if (folder.exists() && folder.getType() != IResource.ROOT) {
					try {
						FolderSyncInfo info = getCachedFolderSync(folder);
						if (info == null) {
							// deleted folder sync info since we loaded it
							SyncFileWriter.deleteFolderSync(folder);
							changedResourceSync.remove(folder);
						} else {
							// modified or created new folder sync info since we loaded it
							SyncFileWriter.writeFolderSync(folder, info);
						}
					} catch(CVSException e) {
						try {
							purgeCache(folder, true /* deep */);
						} catch(CVSException pe) {
							errors.add(pe.getStatus());
						}
						errors.add(e.getStatus());
					}
				}
				monitor.worked(1);
			}

			// update progress for parents we will skip because they were deleted
			monitor.worked(numDirty - changedResourceSync.size());

			// resource sync info changes
			for (Iterator it = changedResourceSync.iterator(); it.hasNext();) {
				IContainer folder = (IContainer) it.next();
				if (folder.exists() && folder.getType() != IResource.ROOT) {
					// write sync info for all children in one go
					try {
						byte[][] infos = getCachedResourceSyncForChildren(folder);
						SyncFileWriter.writeAllResourceSync(folder, infos);
					} catch(CVSException e) {
						try {
							purgeCache(folder, false /* depth 1 */);
						} catch(CVSException pe) {
							errors.add(pe.getStatus());
						}
						errors.add(e.getStatus());
					}
				}
				monitor.worked(1);
			}

			/*** broadcast events ***/
			changedResourceSync.clear();
			changedFolderSync.clear();
			if ( ! errors.isEmpty()) {
				MultiStatus status = new MultiStatus(CVSProviderPlugin.ID,
											CVSStatus.COMMITTING_SYNC_INFO_FAILED,
											Policy.bind("EclipseSynchronizer.ErrorCommitting"), //$NON-NLS-1$
											null);
				for (int i = 0; i < errors.size(); i++) {
					status.merge((IStatus)errors.get(i));
				}
				return status;
			}
			return STATUS_OK;
		} finally {
			monitor.done();
		}
	}

	/*package*/ void setDirtyIndicator(IResource resource, String indicator) throws CVSException {
		if (resource.getType() == IResource.FILE) {
			internalSetDirtyIndicator((IFile)resource, indicator);
		} else {
			internalSetDirtyIndicator((IContainer)resource, indicator);
		}
	}
	/*package*/ String getDirtyIndicator(IResource resource) throws CVSException {
		if (resource.getType() == IResource.FILE) {
			return internalGetDirtyIndicator((IFile)resource);
		} else {
			return internalGetDirtyIndicator((IContainer)resource);
		}
	}
	private void internalSetDirtyIndicator(IFile file, String indicator) throws CVSException {
		try {
			file.setSessionProperty(IS_DIRTY, indicator);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	private String internalGetDirtyIndicator(IFile file) throws CVSException {
		try {
			return (String)file.getSessionProperty(IS_DIRTY);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	private void internalSetDirtyIndicator(IContainer container, String indicator) throws CVSException {
		try {
			container.setPersistentProperty(IS_DIRTY, indicator);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	private String internalGetDirtyIndicator(IContainer container) throws CVSException {
		try {
			return container.getPersistentProperty(IS_DIRTY);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
		
	/**
	 * Return the dirty count for the given folder. For existing folders, the
	 * dirty count may not have been calculated yet and this method will return
	 * null in that case. For phantom folders, the dirty count is calculated if
	 * it does not exist yet.
	 */
	/*package*/ int getCachedDirtyCount(IContainer container) throws CVSException {
		if (!container.exists()) return -1;
		try {
			Integer dirtyCount = (Integer)container.getSessionProperty(DIRTY_COUNT);
			if (dirtyCount == null) {
				return -1;
			} else {
				return dirtyCount.intValue();
			}
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*package*/ void setCachedDirtyCount(IContainer container, int count) throws CVSException {
		if (!container.exists()) return;
		try {
			container.setSessionProperty(DIRTY_COUNT, new Integer(count));
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/*
	 * Flush all cached info for the container and it's ancestors
	 */
	/*package*/ void flushDirtyCache(IResource resource) throws CVSException {
		if (resource.exists()) {
			try {
				if (resource.getType() == IResource.FILE) {
					resource.setSessionProperty(IS_DIRTY, null);
					resource.setSessionProperty(CLEAN_UPDATE, null);
				} else {
					resource.setSessionProperty(DIRTY_COUNT, null);
					resource.setSessionProperty(DELETED_CHILDREN, null);
					resource.setPersistentProperty(IS_DIRTY, null);
				}
			} catch (CoreException e) {
				throw CVSException.wrapException(e);
			}
		}
	}
	
	/*
	 * Add the deleted child and return true if it didn't exist before
	 */
	/*package*/ boolean addDeletedChild(IContainer container, IFile file) throws CVSException {
		try {
			Set deletedFiles = getDeletedChildren(container);
			if (deletedFiles == null)
				deletedFiles = new HashSet();
			String fileName = file.getName();
			if (deletedFiles.contains(fileName))
				return false;
			deletedFiles.add(fileName);
			setDeletedChildren(container, deletedFiles);
			return true;
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}

	/*package*/ boolean removeDeletedChild(IContainer container, IFile file) throws CVSException {
		try {
			Set deletedFiles = getDeletedChildren(container);
			if (deletedFiles == null || deletedFiles.isEmpty())
				return false;
			String fileName = file.getName();
			if (!deletedFiles.contains(fileName))
				return false;
			deletedFiles.remove(fileName);
			if (deletedFiles.isEmpty()) deletedFiles = null;
			setDeletedChildren(container, deletedFiles);
			return true;
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	protected void setDeletedChildren(IContainer parent, Set deletedFiles) throws CVSException {
		if (!parent.exists()) return;
		try {
			parent.setSessionProperty(DELETED_CHILDREN, deletedFiles);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}

	private Set getDeletedChildren(IContainer parent) throws CoreException {
		if (!parent.exists()) return null;
		return (Set)parent.getSessionProperty(DELETED_CHILDREN);
	}
	
	/**
	 * Method updated flags the objetc as having been modfied by the updated
	 * handler. This flag is read during the resource delta to determine whether
	 * the modification made the file dirty or not.
	 *
	 * @param mFile
	 */
	/*package*/ void markFileAsUpdated(IFile file) throws CVSException {
		try {
			file.setSessionProperty(CLEAN_UPDATE, UPDATED_INDICATOR);
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}

	/*package*/ boolean contentsChangedByUpdate(IFile file) throws CVSException {
		try {
			Object indicator = file.getSessionProperty(CLEAN_UPDATE);
			boolean updated = false;
			if (indicator == UPDATED_INDICATOR) {
				// the file was changed due to a clean update (i.e. no local mods) so skip it
				file.setSessionProperty(CLEAN_UPDATE, null);
				file.setSessionProperty(IS_DIRTY, NOT_DIRTY_INDICATOR);
				updated = true;
			}
			return updated;
		} catch (CoreException e) {
			throw CVSException.wrapException(e);
		}
	}
	
	/**
	 * Method isSyncInfoLoaded returns true if all the sync info for the
	 * provided resources is loaded into the internal cache.
	 *
	 * @param resources
	 * @param i
	 * @return boolean
	 */
	/*package*/ boolean isSyncInfoLoaded(IContainer parent) throws CVSException {
		try {
			if (parent.getFolder(new Path(SyncFileWriter.CVS_DIRNAME)).exists()) {
				if (parent.getSessionProperty(RESOURCE_SYNC_KEY) == null)
					return false;
				if (parent.getSessionProperty(FOLDER_SYNC_KEY) == null)
					return false;
				if (parent.getSessionProperty(IGNORE_SYNC_KEY) == null)
					return false;
			}
		} catch (CoreException e) {
			// let future operations surface the error
			return false;
		}
		return true;
	}
}

Back to the top