Bug 578725 - fix [autobuild] Random startup issues

if subMonitor.split throws OperationCanceledException the
"prepareOperation" was not execuded but "endOperation" which caused
IllegalArgumentException: endRule without matching beginRule

Change-Id: I03eb712f766f3129272bef86ab8fb59a054a85eb
Signed-off-by: Joerg Kubitz <jkubitz-eclipse@gmx.de>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.resources/+/190761
Tested-by: Andrey Loskutov <loskutov@gmx.de>
Reviewed-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/events/AutoBuildJob.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/events/AutoBuildJob.java
index 12f2d66..dfe0a9a 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/events/AutoBuildJob.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/events/AutoBuildJob.java
@@ -149,8 +149,9 @@
 	private void doBuild(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
 		SubMonitor subMonitor = SubMonitor.convert(monitor, Policy.opWork + 1);
 		final ISchedulingRule rule = workspace.getRuleFactory().buildRule();
+		SubMonitor split = subMonitor.split(1); // will throw OperationCanceledException if autobuild canceled
 		try {
-			workspace.prepareOperation(rule, subMonitor.split(1));
+			workspace.prepareOperation(rule, split);
 			workspace.beginOperation(true);
 			final int trigger = IncrementalProjectBuilder.AUTO_BUILD;
 			workspace.broadcastBuildEvent(workspace, IResourceChangeEvent.PRE_BUILD, trigger);
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Resource.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Resource.java
index 61df415..1c70c20 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Resource.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Resource.java
@@ -530,8 +530,9 @@
 		checkValidPath(destination, getType(), false);
 		Resource destResource = workspace.newResource(destination, getType());
 		final ISchedulingRule rule = workspace.getRuleFactory().copyRule(this, destResource);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			// The following assert method throws CoreExceptions as stated in the IResource.copy API
 			// and assert for programming errors. See checkCopyRequirements for more information.
 			assertCopyRequirements(destination, getType(), updateFlags);
@@ -561,8 +562,9 @@
 		Assert.isNotNull(destDesc);
 		String message = NLS.bind(Messages.resources_copying, getFullPath());
 		SubMonitor progress = SubMonitor.convert(monitor, message, 100).checkCanceled();
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(workspace.getRoot(), progress.split(1));
+			workspace.prepareOperation(workspace.getRoot(), split);
 			// The following assert method throws CoreExceptions as stated in the IResource.copy API
 			// and assert for programming errors. See checkCopyRequirements for more information.
 			IPath destPath = new Path(destDesc.getName()).makeAbsolute();
@@ -631,8 +633,9 @@
 		SubMonitor progress = SubMonitor.convert(monitor, message, 100).checkCanceled();
 		checkValidPath(path, FOLDER, true);
 		final ISchedulingRule rule = workspace.getRuleFactory().createRule(this);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			IFileInfo fileInfo = assertLinkRequirements(localLocation, updateFlags);
 			workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_LINK_CREATE, this));
 			workspace.beginOperation(true);
@@ -738,8 +741,9 @@
 		SubMonitor progress = SubMonitor.convert(monitor, 100).checkCanceled();
 		progress.subTask(message);
 		final ISchedulingRule rule = workspace.getRuleFactory().deleteRule(this);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			// If there is no resource then there is nothing to delete so just return.
 			if (!exists())
 				return;
@@ -1484,8 +1488,9 @@
 		checkValidPath(destination, getType(), false);
 		Resource destResource = workspace.newResource(destination, getType());
 		final ISchedulingRule rule = workspace.getRuleFactory().moveRule(this, destResource);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			// The following assert method throws CoreExceptions as stated in the IResource.move API
 			// and assert for programming errors. See checkMoveRequirements for more information.
 			assertMoveRequirements(destination, getType(), updateFlags);
@@ -1550,8 +1555,9 @@
 		progress.subTask(message);
 		boolean build = false;
 		final ISchedulingRule rule = workspace.getRuleFactory().refreshRule(this);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			if (!isRoot && !getProject().isAccessible())
 				return;
 			if (!exists() && isFiltered())
@@ -1613,8 +1619,9 @@
 		String message = NLS.bind(Messages.resources_settingDerivedFlag, getFullPath());
 		SubMonitor progress = SubMonitor.convert(monitor, message, 100).checkCanceled();
 		final ISchedulingRule rule = workspace.getRuleFactory().derivedRule(this);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			ResourceInfo info = getResourceInfo(false, false);
 			checkAccessible(getFlags(info));
 			// Ignore attempts to set derived flag on anything except files and folders.
@@ -1657,8 +1664,9 @@
 	public void setLocal(boolean flag, int depth, IProgressMonitor monitor) throws CoreException {
 		String message = Messages.resources_setLocal;
 		SubMonitor progress = SubMonitor.convert(monitor, message, 100).checkCanceled();
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(null, progress.split(1));
+			workspace.prepareOperation(null, split);
 			workspace.beginOperation(true);
 			internalSetLocal(flag, depth);
 			progress.split(98);
@@ -1749,8 +1757,9 @@
 		String message = NLS.bind(Messages.resources_touch, getFullPath());
 		SubMonitor progress = SubMonitor.convert(monitor, message, 100).checkCanceled();
 		final ISchedulingRule rule = workspace.getRuleFactory().modifyRule(this);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			ResourceInfo info = checkAccessibleAndLocal(DEPTH_ZERO);
 
 			workspace.beginOperation(true);
@@ -2004,8 +2013,9 @@
 		String message = NLS.bind(Messages.links_setLocation, getFullPath());
 		SubMonitor progress = SubMonitor.convert(monitor, message, 100).checkCanceled();
 		final ISchedulingRule rule = workspace.getRuleFactory().createRule(this);
+		SubMonitor split = progress.split(1);
 		try {
-			workspace.prepareOperation(rule, progress.split(1));
+			workspace.prepareOperation(rule, split);
 			workspace.broadcastEvent(LifecycleEvent.newEvent(LifecycleEvent.PRE_LINK_CHANGE, this));
 			workspace.beginOperation(true);