Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 332543 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/core/filesystem/provider/FileStore.java (-3 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 IBM Corporation and others.
2
 * Copyright (c) 2005, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.filesystem.provider;
12
package org.eclipse.core.filesystem.provider;
12
13
Lines 76-84 Link Here
76
						String msg = NLS.bind(Messages.failedReadDuringWrite, path);
77
						String msg = NLS.bind(Messages.failedReadDuringWrite, path);
77
						Policy.error(EFS.ERROR_READ, msg, e);
78
						Policy.error(EFS.ERROR_READ, msg, e);
78
					}
79
					}
79
					if (bytesRead == -1)
80
						break;
81
					try {
80
					try {
81
						if (bytesRead == -1) {
82
							destination.close();
83
							break;
84
						}
82
						destination.write(buffer, 0, bytesRead);
85
						destination.write(buffer, 0, bytesRead);
83
					} catch (IOException e) {
86
					} catch (IOException e) {
84
						String msg = NLS.bind(Messages.couldNotWrite, path);
87
						String msg = NLS.bind(Messages.couldNotWrite, path);
(-)src/org/eclipse/core/internal/localstore/Bucket.java (-2 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2009 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.internal.localstore;
12
package org.eclipse.core.internal.localstore;
12
13
Lines 14-19 Link Here
14
import java.util.*;
15
import java.util.*;
15
import org.eclipse.core.internal.resources.ResourceException;
16
import org.eclipse.core.internal.resources.ResourceException;
16
import org.eclipse.core.internal.resources.ResourceStatus;
17
import org.eclipse.core.internal.resources.ResourceStatus;
18
import org.eclipse.core.internal.utils.FileUtil;
17
import org.eclipse.core.internal.utils.Messages;
19
import org.eclipse.core.internal.utils.Messages;
18
import org.eclipse.core.resources.IResourceStatus;
20
import org.eclipse.core.resources.IResourceStatus;
19
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.runtime.*;
Lines 350-357 Link Here
350
					writeEntryKey(destination, entry.getKey());
352
					writeEntryKey(destination, entry.getKey());
351
					writeEntryValue(destination, entry.getValue());
353
					writeEntryValue(destination, entry.getValue());
352
				}
354
				}
353
			} finally {
354
				destination.close();
355
				destination.close();
356
			} finally {
357
				FileUtil.safeClose(destination);
355
			}
358
			}
356
			needSaving = false;
359
			needSaving = false;
357
		} catch (IOException ioe) {
360
		} catch (IOException ioe) {
(-)src/org/eclipse/core/internal/localstore/BucketTree.java (-12 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2008 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.internal.localstore;
12
package org.eclipse.core.internal.localstore;
12
13
Lines 14-19 Link Here
14
import org.eclipse.core.internal.localstore.Bucket.Visitor;
15
import org.eclipse.core.internal.localstore.Bucket.Visitor;
15
import org.eclipse.core.internal.resources.ResourceException;
16
import org.eclipse.core.internal.resources.ResourceException;
16
import org.eclipse.core.internal.resources.Workspace;
17
import org.eclipse.core.internal.resources.Workspace;
18
import org.eclipse.core.internal.utils.FileUtil;
17
import org.eclipse.core.internal.utils.Messages;
19
import org.eclipse.core.internal.utils.Messages;
18
import org.eclipse.core.resources.*;
20
import org.eclipse.core.resources.*;
19
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.runtime.*;
Lines 139-162 Link Here
139
		if (!versionFile.getParentFile().exists())
141
		if (!versionFile.getParentFile().exists())
140
			versionFile.getParentFile().mkdirs();
142
			versionFile.getParentFile().mkdirs();
141
		FileOutputStream stream = null;
143
		FileOutputStream stream = null;
142
		boolean failed = false;
143
		try {
144
		try {
144
			stream = new FileOutputStream(versionFile);
145
			stream = new FileOutputStream(versionFile);
145
			stream.write(current.getVersion());
146
			stream.write(current.getVersion());
147
			stream.close();
146
		} catch (IOException e) {
148
		} catch (IOException e) {
147
			failed = true;
148
			String message = NLS.bind(Messages.resources_writeWorkspaceMeta, versionFile.getAbsolutePath()); 
149
			String message = NLS.bind(Messages.resources_writeWorkspaceMeta, versionFile.getAbsolutePath()); 
149
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, e);
150
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, e);
150
		} finally {
151
		} finally {
151
			try {
152
			FileUtil.safeClose(stream);
152
				if (stream != null)
153
					stream.close();
154
			} catch (IOException e) {
155
				if (!failed) {
156
					String message = NLS.bind(Messages.resources_writeWorkspaceMeta, versionFile.getAbsolutePath());
157
					throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, e);
158
				}
159
			}
160
		}
153
		}
161
	}
154
	}
162
155
(-)src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java (-8 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 9-14 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Martin Oberhuber (Wind River) - [210664] descriptionChanged(): ignore LF style
10
 *     Martin Oberhuber (Wind River) - [210664] descriptionChanged(): ignore LF style
11
 *     Martin Oberhuber (Wind River) - [233939] findFilesForLocation() with symlinks
11
 *     Martin Oberhuber (Wind River) - [233939] findFilesForLocation() with symlinks
12
 *     James Blackburn (Broadcom Corp.) - ongoing development
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.core.internal.localstore;
14
package org.eclipse.core.internal.localstore;
14
15
Lines 1091-1107 Link Here
1091
		try {
1092
		try {
1092
			out = fileStore.openOutputStream(EFS.NONE, null);
1093
			out = fileStore.openOutputStream(EFS.NONE, null);
1093
			new ModelObjectWriter().write(desc, out);
1094
			new ModelObjectWriter().write(desc, out);
1095
			out.close();
1094
		} catch (IOException e) {
1096
		} catch (IOException e) {
1095
			String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath());
1097
			String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath());
1096
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), msg, e);
1098
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), msg, e);
1097
		} finally {
1099
		} finally {
1098
			if (out != null) {
1100
			FileUtil.safeClose(out);
1099
				try {
1100
					out.close();
1101
				} catch (IOException e) {
1102
					// ignore failure to close stream
1103
				}
1104
			}
1105
		}
1101
		}
1106
		//for backwards compatibility, ensure the old .prj file is deleted
1102
		//for backwards compatibility, ensure the old .prj file is deleted
1107
		getWorkspace().getMetaArea().clearOldDescription(target);
1103
		getWorkspace().getMetaArea().clearOldDescription(target);
(-)src/org/eclipse/core/internal/localstore/SafeChunkyOutputStream.java (-2 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2005 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-16 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.internal.localstore;
12
package org.eclipse.core.internal.localstore;
12
13
13
import java.io.*;
14
import java.io.*;
15
import org.eclipse.core.internal.utils.FileUtil;
14
16
15
/**
17
/**
16
 * Appends data, in chunks, to a file. Each chunk is defined by the moment
18
 * Appends data, in chunks, to a file. Each chunk is defined by the moment
Lines 63-71 Link Here
63
	public void succeed() throws IOException {
65
	public void succeed() throws IOException {
64
		try {
66
		try {
65
			endChunk();
67
			endChunk();
68
			close();
66
		} finally {
69
		} finally {
67
			isOpen = false;
70
			isOpen = false;
68
			close();
71
			FileUtil.safeClose(this);
69
		}
72
		}
70
	}
73
	}
71
74
(-)src/org/eclipse/core/internal/localstore/SafeFileOutputStream.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.internal.localstore;
12
package org.eclipse.core.internal.localstore;
12
13
Lines 86-91 Link Here
86
			source = new BufferedInputStream(new FileInputStream(sourceFile));
87
			source = new BufferedInputStream(new FileInputStream(sourceFile));
87
			destination = new BufferedOutputStream(new FileOutputStream(destinationFile));
88
			destination = new BufferedOutputStream(new FileOutputStream(destinationFile));
88
			transferStreams(source, destination);
89
			transferStreams(source, destination);
90
			destination.close();
89
		} finally {
91
		} finally {
90
			FileUtil.safeClose(source);
92
			FileUtil.safeClose(source);
91
			FileUtil.safeClose(destination);
93
			FileUtil.safeClose(destination);
(-)src/org/eclipse/core/internal/resources/FileState.java (-13 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-19 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.internal.resources;
12
package org.eclipse.core.internal.resources;
12
13
13
import java.io.*;
14
import java.io.*;
14
import org.eclipse.core.internal.localstore.IHistoryStore;
15
import org.eclipse.core.internal.localstore.IHistoryStore;
15
import org.eclipse.core.internal.utils.Messages;
16
import org.eclipse.core.internal.utils.*;
16
import org.eclipse.core.internal.utils.UniversalUniqueIdentifier;
17
import org.eclipse.core.resources.*;
17
import org.eclipse.core.resources.*;
18
import org.eclipse.core.runtime.*;
18
import org.eclipse.core.runtime.*;
19
import org.eclipse.core.runtime.content.IContentDescription;
19
import org.eclipse.core.runtime.content.IContentDescription;
Lines 53-75 Link Here
53
		// tries to obtain a description for the file contents
53
		// tries to obtain a description for the file contents
54
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
54
		IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
55
		InputStream contents = new BufferedInputStream(getContents());
55
		InputStream contents = new BufferedInputStream(getContents());
56
		boolean failed = false;
57
		try {
56
		try {
58
			IContentDescription description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] {IContentDescription.CHARSET});
57
			IContentDescription description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] {IContentDescription.CHARSET});
58
			contents.close();
59
			return description == null ? null : description.getCharset();
59
			return description == null ? null : description.getCharset();
60
		} catch (IOException e) {
60
		} catch (IOException e) {
61
			failed = true;
62
			String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());		
61
			String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());		
63
			throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
62
			throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
64
		} finally {
63
		} finally {
65
			try {
64
			FileUtil.safeClose(contents);
66
				contents.close();
67
			} catch (IOException e) {
68
				if (!failed) {
69
					String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());		
70
					throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
71
				}
72
			}
73
		}
65
		}
74
	}
66
	}
75
67
(-)src/org/eclipse/core/internal/resources/LocalMetaArea.java (-5 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-14 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 * Francis Lynch (Wind River) - [301563] Save and load tree snapshots
10
 * Francis Lynch (Wind River) - [301563] Save and load tree snapshots
11
 * Broadcom Corporation - build configurations and references
11
 * Broadcom Corporation - ongoing development
12
 *******************************************************************************/
12
 *******************************************************************************/
13
package org.eclipse.core.internal.resources;
13
package org.eclipse.core.internal.resources;
14
14
Lines 19-26 Link Here
19
import org.eclipse.core.filesystem.URIUtil;
19
import org.eclipse.core.filesystem.URIUtil;
20
import org.eclipse.core.internal.localstore.SafeChunkyInputStream;
20
import org.eclipse.core.internal.localstore.SafeChunkyInputStream;
21
import org.eclipse.core.internal.localstore.SafeChunkyOutputStream;
21
import org.eclipse.core.internal.localstore.SafeChunkyOutputStream;
22
import org.eclipse.core.internal.utils.Messages;
22
import org.eclipse.core.internal.utils.*;
23
import org.eclipse.core.internal.utils.Policy;
24
import org.eclipse.core.resources.*;
23
import org.eclipse.core.resources.*;
25
import org.eclipse.core.runtime.*;
24
import org.eclipse.core.runtime.*;
26
import org.eclipse.osgi.util.NLS;
25
import org.eclipse.osgi.util.NLS;
Lines 480-487 Link Here
480
					}
479
					}
481
				}
480
				}
482
				output.succeed();
481
				output.succeed();
483
			} finally {
484
				dataOut.close();
482
				dataOut.close();
483
			} finally {
484
				FileUtil.safeClose(dataOut);
485
			}
485
			}
486
		} catch (IOException e) {
486
		} catch (IOException e) {
487
			String message = NLS.bind(Messages.resources_exSaveProjectLocation, target.getName());
487
			String message = NLS.bind(Messages.resources_exSaveProjectLocation, target.getName());
(-)src/org/eclipse/core/internal/resources/ModelObjectWriter.java (-4 / +7 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-15 Link Here
10
 *     Serge Beauchamp (Freescale Semiconductor) - [252996] add resource filtering
10
 *     Serge Beauchamp (Freescale Semiconductor) - [252996] add resource filtering
11
 *     Serge Beauchamp (Freescale Semiconductor) - [229633] Group and Project Path Variable Support
11
 *     Serge Beauchamp (Freescale Semiconductor) - [229633] Group and Project Path Variable Support
12
 * Markus Schorn (Wind River) - [306575] Save snapshot location with project
12
 * Markus Schorn (Wind River) - [306575] Save snapshot location with project
13
 * James Blackburn (Broadcom Corp.) - ongoing development
13
 *******************************************************************************/
14
 *******************************************************************************/
14
package org.eclipse.core.internal.resources;
15
package org.eclipse.core.internal.resources;
15
16
Lines 155-163 Link Here
155
		try {
156
		try {
156
			file = new SafeFileOutputStream(location.toOSString(), tempPath);
157
			file = new SafeFileOutputStream(location.toOSString(), tempPath);
157
			write(object, file);
158
			write(object, file);
159
			file.close();
158
		} finally {
160
		} finally {
159
			if (file != null)
161
			FileUtil.safeClose(file);
160
				file.close();
161
		}
162
		}
162
	}
163
	}
163
164
Lines 170-177 Link Here
170
			write(object, writer);
171
			write(object, writer);
171
			writer.flush();
172
			writer.flush();
172
			writer.close();
173
			writer.close();
174
			if (writer.checkError())
175
				throw new IOException();
173
		} finally {
176
		} finally {
174
			output.close();
177
			FileUtil.safeClose(output);
175
		}
178
		}
176
	}
179
	}
177
180
(-)src/org/eclipse/core/internal/resources/ProjectDescriptionReader.java (-5 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 9-14 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Serge Beauchamp (Freescale Semiconductor) - [229633] Project Path Variable Support
10
 *     Serge Beauchamp (Freescale Semiconductor) - [229633] Project Path Variable Support
11
 * Markus Schorn (Wind River) - [306575] Save snapshot location with project
11
 * Markus Schorn (Wind River) - [306575] Save snapshot location with project
12
 * James Blackburn (Broadcom Corp.) - ongoing development
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.core.internal.resources;
14
package org.eclipse.core.internal.resources;
14
15
Lines 20-27 Link Here
20
import org.eclipse.core.filesystem.URIUtil;
21
import org.eclipse.core.filesystem.URIUtil;
21
import org.eclipse.core.internal.events.BuildCommand;
22
import org.eclipse.core.internal.events.BuildCommand;
22
import org.eclipse.core.internal.localstore.SafeFileInputStream;
23
import org.eclipse.core.internal.localstore.SafeFileInputStream;
23
import org.eclipse.core.internal.utils.Messages;
24
import org.eclipse.core.internal.utils.*;
24
import org.eclipse.core.internal.utils.Policy;
25
import org.eclipse.core.resources.*;
25
import org.eclipse.core.resources.*;
26
import org.eclipse.core.runtime.*;
26
import org.eclipse.core.runtime.*;
27
import org.eclipse.osgi.util.NLS;
27
import org.eclipse.osgi.util.NLS;
Lines 946-953 Link Here
946
			file = new BufferedInputStream(new FileInputStream(location.toFile()));
946
			file = new BufferedInputStream(new FileInputStream(location.toFile()));
947
			return read(new InputSource(file));
947
			return read(new InputSource(file));
948
		} finally {
948
		} finally {
949
			if (file != null)
949
			FileUtil.safeClose(file);
950
				file.close();
951
		}
950
		}
952
	}
951
	}
953
952
(-)src/org/eclipse/core/internal/resources/ProjectPreferences.java (-5 / +3 lines)
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Markus Schorn (Wind River) - [108066] Project prefs marked dirty on read
10
 *     Markus Schorn (Wind River) - [108066] Project prefs marked dirty on read
11
 *     James Blackburn (Broadcom Corp.) - ongoing development
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.core.internal.resources;
13
package org.eclipse.core.internal.resources;
13
14
Lines 594-609 Link Here
594
			ByteArrayOutputStream output = new ByteArrayOutputStream();
595
			ByteArrayOutputStream output = new ByteArrayOutputStream();
595
			try {
596
			try {
596
				table.store(output, null);
597
				table.store(output, null);
598
				output.close();
597
			} catch (IOException e) {
599
			} catch (IOException e) {
598
				String message = NLS.bind(Messages.preferences_saveProblems, absolutePath());
600
				String message = NLS.bind(Messages.preferences_saveProblems, absolutePath());
599
				log(new Status(IStatus.ERROR, Platform.PI_RUNTIME, IStatus.ERROR, message, e));
601
				log(new Status(IStatus.ERROR, Platform.PI_RUNTIME, IStatus.ERROR, message, e));
600
				throw new BackingStoreException(message);
602
				throw new BackingStoreException(message);
601
			} finally {
603
			} finally {
602
				try {
604
				FileUtil.safeClose(output);
603
					output.close();
604
				} catch (IOException e) {
605
					// ignore
606
				}
607
			}
605
			}
608
			final InputStream input = new BufferedInputStream(new ByteArrayInputStream(output.toByteArray()));
606
			final InputStream input = new BufferedInputStream(new ByteArrayInputStream(output.toByteArray()));
609
			IWorkspaceRunnable operation = new IWorkspaceRunnable() {
607
			IWorkspaceRunnable operation = new IWorkspaceRunnable() {
(-)src/org/eclipse/core/internal/resources/SafeFileTable.java (-2 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2005 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-18 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.internal.resources;
12
package org.eclipse.core.internal.resources;
12
13
13
import java.io.*;
14
import java.io.*;
14
import java.util.Properties;
15
import java.util.Properties;
15
import java.util.Set;
16
import java.util.Set;
17
import org.eclipse.core.internal.utils.FileUtil;
16
import org.eclipse.core.internal.utils.Messages;
18
import org.eclipse.core.internal.utils.Messages;
17
import org.eclipse.core.resources.IResourceStatus;
19
import org.eclipse.core.resources.IResourceStatus;
18
import org.eclipse.core.resources.ResourcesPlugin;
20
import org.eclipse.core.resources.ResourcesPlugin;
Lines 80-87 Link Here
80
			FileOutputStream output = new FileOutputStream(target);
82
			FileOutputStream output = new FileOutputStream(target);
81
			try {
83
			try {
82
				table.store(output, "safe table"); //$NON-NLS-1$
84
				table.store(output, "safe table"); //$NON-NLS-1$
83
			} finally {
84
				output.close();
85
				output.close();
86
			} finally {
87
				FileUtil.safeClose(output);
85
			}
88
			}
86
		} catch (IOException e) {
89
		} catch (IOException e) {
87
			String message = Messages.resources_exSafeSave;
90
			String message = Messages.resources_exSafeSave;
(-)src/org/eclipse/core/internal/resources/SaveManager.java (-21 / +26 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 9-15 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 * Francis Lynch (Wind River) - [301563] Save and load tree snapshots
10
 * Francis Lynch (Wind River) - [301563] Save and load tree snapshots
11
 * Francis Lynch (Wind River) - [305718] Allow reading snapshot into renamed project
11
 * Francis Lynch (Wind River) - [305718] Allow reading snapshot into renamed project
12
 * Broadcom Corporation - build configurations and references
12
 * Broadcom Corporation - ongoing development
13
 *******************************************************************************/
13
 *******************************************************************************/
14
package org.eclipse.core.internal.resources;
14
package org.eclipse.core.internal.resources;
15
15
Lines 17-23 Link Here
17
import java.net.URI;
17
import java.net.URI;
18
import java.util.*;
18
import java.util.*;
19
import java.util.zip.*;
19
import java.util.zip.*;
20
import org.eclipse.core.filesystem.*;
20
import org.eclipse.core.filesystem.EFS;
21
import org.eclipse.core.filesystem.IFileStore;
21
import org.eclipse.core.internal.events.*;
22
import org.eclipse.core.internal.events.*;
22
import org.eclipse.core.internal.localstore.*;
23
import org.eclipse.core.internal.localstore.*;
23
import org.eclipse.core.internal.utils.*;
24
import org.eclipse.core.internal.utils.*;
Lines 1201-1208 Link Here
1201
			try {
1202
			try {
1202
				masterTable.store(output, "master table"); //$NON-NLS-1$
1203
				masterTable.store(output, "master table"); //$NON-NLS-1$
1203
				output.succeed();
1204
				output.succeed();
1204
			} finally {
1205
				output.close();
1205
				output.close();
1206
			} finally {
1207
				FileUtil.safeClose(output);
1206
			}
1208
			}
1207
		} catch (IOException e) {
1209
		} catch (IOException e) {
1208
			throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, NLS.bind(Messages.resources_exSaveMaster, location.toOSString()), e);
1210
			throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, NLS.bind(Messages.resources_exSaveMaster, location.toOSString()), e);
Lines 1278-1285 Link Here
1278
			try {
1280
			try {
1279
				output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
1281
				output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
1280
				writeTree(project, output, monitor);
1282
				writeTree(project, output, monitor);
1281
			} finally {
1282
				output.close();
1283
				output.close();
1284
			} finally {
1285
				FileUtil.safeClose(output);
1283
			}
1286
			}
1284
			OutputStream snapOut = store.openOutputStream(EFS.NONE, monitor);
1287
			OutputStream snapOut = store.openOutputStream(EFS.NONE, monitor);
1285
			out = new ZipOutputStream(snapOut);
1288
			out = new ZipOutputStream(snapOut);
Lines 1295-1308 Link Here
1295
				}
1298
				}
1296
				out.closeEntry();
1299
				out.closeEntry();
1297
			} finally {
1300
			} finally {
1298
				in.close();
1301
				FileUtil.safeClose(in);
1299
			}
1302
			}
1303
			out.close();
1300
		} catch (IOException e) {
1304
		} catch (IOException e) {
1301
			throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, snapshotPath, Messages.resources_copyProblem, e);
1305
			throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, snapshotPath, Messages.resources_copyProblem, e);
1302
		} finally {
1306
		} finally {
1303
			if (out!=null) {
1307
			FileUtil.safeClose(out);
1304
				try { out.close(); } catch (IOException e) { /*ignore*/ }
1305
			}
1306
			if (tmpTree!=null) tmpTree.delete();
1308
			if (tmpTree!=null) tmpTree.delete();
1307
		}
1309
		}
1308
	}
1310
	}
Lines 1322-1329 Link Here
1322
			try {
1324
			try {
1323
				output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
1325
				output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
1324
				writeTree(computeStatesToSave(contexts, workspace.getElementTree()), output, monitor);
1326
				writeTree(computeStatesToSave(contexts, workspace.getElementTree()), output, monitor);
1325
			} finally {
1326
				output.close();
1327
				output.close();
1328
			} finally {
1329
				FileUtil.safeClose(output);
1327
			}
1330
			}
1328
		} catch (Exception e) {
1331
		} catch (Exception e) {
1329
			String msg = NLS.bind(Messages.resources_writeWorkspaceMeta, treeLocation);
1332
			String msg = NLS.bind(Messages.resources_writeWorkspaceMeta, treeLocation);
Lines 1549-1560 Link Here
1549
			if (root.getType() != IResource.ROOT)
1552
			if (root.getType() != IResource.ROOT)
1550
				o2 = new DataOutputStream(new SafeFileOutputStream(syncInfoLocation.toOSString(), syncInfoTempLocation.toOSString()));
1553
				o2 = new DataOutputStream(new SafeFileOutputStream(syncInfoLocation.toOSString(), syncInfoTempLocation.toOSString()));
1551
		} catch (IOException e) {
1554
		} catch (IOException e) {
1552
			if (o1 != null)
1555
			FileUtil.safeClose(o1);
1553
				try {
1554
					o1.close();
1555
				} catch (IOException e2) {
1556
					// ignore
1557
				}
1558
			message = NLS.bind(Messages.resources_writeMeta, root.getFullPath());
1556
			message = NLS.bind(Messages.resources_writeMeta, root.getFullPath());
1559
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, root.getFullPath(), message, e);
1557
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, root.getFullPath(), message, e);
1560
		}
1558
		}
Lines 1610-1617 Link Here
1610
			removeGarbage(markersOutput, markersLocation, markersTempLocation);
1608
			removeGarbage(markersOutput, markersLocation, markersTempLocation);
1611
			// if we have the workspace root the output stream will be null and we
1609
			// if we have the workspace root the output stream will be null and we
1612
			// don't have to perform cleanup code
1610
			// don't have to perform cleanup code
1613
			if (syncInfoOutput != null)
1611
			if (syncInfoOutput != null) {
1614
				removeGarbage(syncInfoOutput, syncInfoLocation, syncInfoTempLocation);
1612
				removeGarbage(syncInfoOutput, syncInfoLocation, syncInfoTempLocation);
1613
				syncInfoOutput.close();
1614
			}
1615
			markersOutput.close();
1615
		} catch (IOException e) {
1616
		} catch (IOException e) {
1616
			message = NLS.bind(Messages.resources_writeMeta, root.getFullPath());
1617
			message = NLS.bind(Messages.resources_writeMeta, root.getFullPath());
1617
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, root.getFullPath(), message, e);
1618
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, root.getFullPath(), message, e);
Lines 1719-1726 Link Here
1719
				System.out.println("Snap SyncInfo for " + root.getFullPath() + ": " + snapTimes[1] + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1720
				System.out.println("Snap SyncInfo for " + root.getFullPath() + ": " + snapTimes[1] + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1720
			if (markerFileSize != markersOutput.size())
1721
			if (markerFileSize != markersOutput.size())
1721
				safeMarkerStream.succeed();
1722
				safeMarkerStream.succeed();
1722
			if (safeSyncInfoStream != null && syncInfoFileSize != syncInfoOutput.size())
1723
			if (safeSyncInfoStream != null && syncInfoFileSize != syncInfoOutput.size()) {
1723
				safeSyncInfoStream.succeed();
1724
				safeSyncInfoStream.succeed();
1725
				syncInfoOutput.close();
1726
			}
1727
			markersOutput.close();
1724
		} catch (IOException e) {
1728
		} catch (IOException e) {
1725
			message = NLS.bind(Messages.resources_writeMeta, root.getFullPath());
1729
			message = NLS.bind(Messages.resources_writeMeta, root.getFullPath());
1726
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, root.getFullPath(), message, e);
1730
			throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, root.getFullPath(), message, e);
Lines 1969-1977 Link Here
1969
					output.writeUTF((String) it.next());
1973
					output.writeUTF((String) it.next());
1970
				for (Iterator it = additionalConfigNames.iterator(); it.hasNext();)
1974
				for (Iterator it = additionalConfigNames.iterator(); it.hasNext();)
1971
					output.writeUTF((String) it.next());
1975
					output.writeUTF((String) it.next());
1976
				output.close();
1972
			} finally {
1977
			} finally {
1973
				if (output != null)
1978
				FileUtil.safeClose(output);
1974
					output.close();
1975
				if (!wasImmutable)
1979
				if (!wasImmutable)
1976
					workspace.newWorkingTree();
1980
					workspace.newWorkingTree();
1977
			}
1981
			}
Lines 1990-1997 Link Here
1990
				DataOutputStream output = new DataOutputStream(safe);
1994
				DataOutputStream output = new DataOutputStream(safe);
1991
				output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
1995
				output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
1992
				writeTree(project, output, null);
1996
				writeTree(project, output, null);
1993
			} finally {
1994
				safe.close();
1997
				safe.close();
1998
			} finally {
1999
				FileUtil.safeClose(safe);
1995
			}
2000
			}
1996
		} catch (IOException e) {
2001
		} catch (IOException e) {
1997
			String msg = NLS.bind(Messages.resources_writeMeta, project.getFullPath());
2002
			String msg = NLS.bind(Messages.resources_writeMeta, project.getFullPath());
(-)src/org/eclipse/core/internal/utils/FileUtil.java (-20 / +24 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 IBM Corporation and others.
2
 * Copyright (c) 2005, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 * Martin Oberhuber (Wind River) - [44107] Add symbolic links to ResourceAttributes API
10
 * Martin Oberhuber (Wind River) - [44107] Add symbolic links to ResourceAttributes API
11
 * James Blackburn (Broadcom Corp.) - ongoing development
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.core.internal.utils;
13
package org.eclipse.core.internal.utils;
13
14
Lines 175-199 Link Here
175
	 * Closes a stream and ignores any resulting exception. This is useful
176
	 * Closes a stream and ignores any resulting exception. This is useful
176
	 * when doing stream cleanup in a finally block where secondary exceptions
177
	 * when doing stream cleanup in a finally block where secondary exceptions
177
	 * are not worth logging.
178
	 * are not worth logging.
179
	 *
180
	 *<p>
181
	 * <strong>WARNING:</strong>
182
	 * If the API contract requires notifying clients of I/O problems, then you <strong>must</strong>
183
	 * explicitly close() output streams outside of safeClose().
184
	 * Some OutputStreams will defer an IOException from write() to close().  So
185
	 * while the writes may 'succeed', ignoring the IOExcpetion will result in silent
186
	 * data loss.
187
	 * </p>
188
	 * <p>
189
	 * This method should only be used as a fail-safe to ensure resources are not
190
	 * leaked.
191
	 * </p>
192
	 * See also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=332543
178
	 */
193
	 */
179
	public static void safeClose(InputStream in) {
194
	public static void safeClose(Closeable stream) {
180
		try {
181
			if (in != null)
182
				in.close();
183
		} catch (IOException e) {
184
			//ignore
185
		}
186
	}
187
188
	/**
189
	 * Closes a stream and ignores any resulting exception. This is useful
190
	 * when doing stream cleanup in a finally block where secondary exceptions
191
	 * are not worth logging.
192
	 */
193
	public static void safeClose(OutputStream out) {
194
		try {
195
		try {
195
			if (out != null)
196
			if (stream != null)
196
				out.close();
197
				stream.close();
197
		} catch (IOException e) {
198
		} catch (IOException e) {
198
			//ignore
199
			//ignore
199
		}
200
		}
Lines 233-241 Link Here
233
						String msg = NLS.bind(Messages.localstore_failedReadDuringWrite, path);
234
						String msg = NLS.bind(Messages.localstore_failedReadDuringWrite, path);
234
						throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, new Path(path), msg, e);
235
						throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, new Path(path), msg, e);
235
					}
236
					}
236
					if (bytesRead == -1)
237
						break;
238
					try {
237
					try {
238
						if (bytesRead == -1) {
239
							// Bug 332543 - ensure we don't ignore failures on close()
240
							destination.close();
241
							break;
242
						}
239
						destination.write(buffer, 0, bytesRead);
243
						destination.write(buffer, 0, bytesRead);
240
					} catch (IOException e) {
244
					} catch (IOException e) {
241
						String msg = NLS.bind(Messages.localstore_couldNotWrite, path);
245
						String msg = NLS.bind(Messages.localstore_couldNotWrite, path);
(-)src/org/eclipse/core/tests/internal/filesystem/wrapper/WrapperFileStore.java (-7 / +21 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 *  Copyright (c) 2006, 2008 IBM Corporation and others.
2
 *  Copyright (c) 2006, 2011 IBM Corporation and others.
3
 *  All rights reserved. This program and the accompanying materials
3
 *  All rights reserved. This program and the accompanying materials
4
 *  are made available under the terms of the Eclipse Public License v1.0
4
 *  are made available under the terms of the Eclipse Public License v1.0
5
 *  which accompanies this distribution, and is available at
5
 *  which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 * 
7
 * 
8
 *  Contributors:
8
 *  Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
13
Lines 28-33 Link Here
28
		this.baseStore = baseStore;
29
		this.baseStore = baseStore;
29
	}
30
	}
30
31
32
	public static IFileStore newInstance(Class<? extends WrapperFileStore> clazz, IFileStore baseStore) {
33
		try {
34
			return clazz.getConstructor(IFileStore.class).newInstance(baseStore);
35
		} catch (Exception e) {
36
			// Test infrastructure failure...
37
			throw new Error(e);
38
		}
39
	}
40
41
	protected IFileStore createNewWrappedStore(IFileStore store) {
42
		return newInstance(getClass(), store);
43
	}
44
31
	public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
45
	public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
32
		return baseStore.childInfos(options, monitor);
46
		return baseStore.childInfos(options, monitor);
33
	}
47
	}
Lines 40-46 Link Here
40
		IFileStore[] childStores = baseStore.childStores(options, monitor);
54
		IFileStore[] childStores = baseStore.childStores(options, monitor);
41
		for (int i = 0; i < childStores.length; i++)
55
		for (int i = 0; i < childStores.length; i++)
42
			// replace ordinary file store with wrapper version
56
			// replace ordinary file store with wrapper version
43
			childStores[i] = new WrapperFileStore(childStores[i]);
57
			childStores[i] = createNewWrappedStore(childStores[i]);
44
		return childStores;
58
		return childStores;
45
	}
59
	}
46
60
Lines 73-87 Link Here
73
	}
87
	}
74
88
75
	public IFileStore getChild(IPath path) {
89
	public IFileStore getChild(IPath path) {
76
		return new WrapperFileStore(baseStore.getChild(path));
90
		return createNewWrappedStore(baseStore.getChild(path));
77
	}
91
	}
78
	
92
79
	public IFileStore getFileStore(IPath path) {
93
	public IFileStore getFileStore(IPath path) {
80
		return new WrapperFileStore(baseStore.getFileStore(path));
94
		return createNewWrappedStore(baseStore.getFileStore(path));
81
	}
95
	}
82
96
83
	public IFileStore getChild(String name) {
97
	public IFileStore getChild(String name) {
84
		return new WrapperFileStore(baseStore.getChild(name));
98
		return createNewWrappedStore(baseStore.getChild(name));
85
	}
99
	}
86
100
87
	public IFileSystem getFileSystem() {
101
	public IFileSystem getFileSystem() {
Lines 94-100 Link Here
94
108
95
	public IFileStore getParent() {
109
	public IFileStore getParent() {
96
		IFileStore baseParent = baseStore.getParent();
110
		IFileStore baseParent = baseStore.getParent();
97
		return baseParent == null ? null : new WrapperFileStore(baseParent);
111
		return baseParent == null ? null : createNewWrappedStore(baseParent);
98
	}
112
	}
99
113
100
	public int hashCode() {
114
	public int hashCode() {
(-)src/org/eclipse/core/tests/internal/filesystem/wrapper/WrapperFileSystem.java (-8 / +32 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
2
 * Copyright (c) 2006, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
package org.eclipse.core.tests.internal.filesystem.wrapper;
12
13
Lines 22-48 Link Here
22
/**
23
/**
23
 * A simple file system implementation that acts as a wrapper around the
24
 * A simple file system implementation that acts as a wrapper around the
24
 * local file system.
25
 * local file system.
26
 * <p>
27
 * Also allows tests to inject a custom FileStore template class (derived from 
28
 * {@link WrapperFileStore}). Tests can use {@link #setCustomFileStore(Class)}
29
 * to override default {@link WrapperFileStore} behaviour.
30
 * </p>
25
 */
31
 */
26
public class WrapperFileSystem extends FileSystem {
32
public class WrapperFileSystem extends FileSystem {
27
33
28
	private static final IFileStore NULL_ROOT = EFS.getNullFileSystem().getStore(Path.ROOT);
34
	protected static final IFileStore NULL_ROOT = EFS.getNullFileSystem().getStore(Path.ROOT);
29
35
30
	private static final String SCHEME_WRAPPED = "wrapped";
36
	private static final String SCHEME_WRAPPED = "wrapped";
31
37
32
	private static WrapperFileSystem instance;
38
	private static volatile WrapperFileSystem instance;
39
40
	/** Custom file-store wrapper */
41
	private static volatile Class<? extends WrapperFileStore> customFS = WrapperFileStore.class;
33
42
34
	public static URI getBasicURI(URI wrappedURI) {
43
	public static URI getBasicURI(URI wrappedURI) {
35
		Assert.isLegal(SCHEME_WRAPPED.equals(wrappedURI.getScheme()));
44
		Assert.isLegal(SCHEME_WRAPPED.equals(wrappedURI.getScheme()));
36
		return URI.create(wrappedURI.getQuery());
45
		return URI.create(wrappedURI.getQuery());
37
	}
46
	}
38
47
39
	public static WrapperFileSystem getInstance() {
48
	public static synchronized WrapperFileSystem getInstance() {
40
		WrapperFileSystem tmpInstance = instance;
49
		if (instance != null)
41
		if (tmpInstance != null)
50
			return instance;
42
			return tmpInstance;
43
		return instance = new WrapperFileSystem();
51
		return instance = new WrapperFileSystem();
44
	}
52
	}
45
53
54
	/**
55
	 * Use fs as the WrapperFileStore to use in this filesystem.
56
	 * Allows tests to easily override existing IFileStore behaviour.
57
	 * By extending {@link WrapperFileStore} conditions difficult to simulate
58
	 * on the LocalFileSystem can be provoked.
59
	 * 
60
	 * @param fs filestore, or null to use default {@link WrapperFileStore}
61
	 *        based implementation.
62
	 */
63
	public static void setCustomFileStore(Class<? extends WrapperFileStore> fs) {
64
		if (fs == null)
65
			customFS = WrapperFileStore.class;
66
		else
67
			customFS = fs;
68
	}
69
46
	public static URI getWrappedURI(URI baseURI) {
70
	public static URI getWrappedURI(URI baseURI) {
47
		try {
71
		try {
48
			return new URI(SCHEME_WRAPPED, null, baseURI.getPath(), baseURI.toString(), null);
72
			return new URI(SCHEME_WRAPPED, null, baseURI.getPath(), baseURI.toString(), null);
Lines 69-74 Link Here
69
			CoreTest.log(ResourceTest.PI_RESOURCES_TESTS, e);
93
			CoreTest.log(ResourceTest.PI_RESOURCES_TESTS, e);
70
			return NULL_ROOT;
94
			return NULL_ROOT;
71
		}
95
		}
72
		return new WrapperFileStore(baseStore);
96
		return WrapperFileStore.newInstance(customFS, baseStore);
73
	}
97
	}
74
}
98
}
(-)src/org/eclipse/core/tests/resources/ResourceTest.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.tests.resources;
12
package org.eclipse.core.tests.resources;
12
13
Lines 773-778 Link Here
773
			try {
774
			try {
774
				os = new FileOutputStream(osFile);
775
				os = new FileOutputStream(osFile);
775
				os.write(newContent.getBytes("UTF8"));
776
				os.write(newContent.getBytes("UTF8"));
777
				os.close();
776
			} finally {
778
			} finally {
777
				FileUtil.safeClose(os);
779
				FileUtil.safeClose(os);
778
			}
780
			}
(-)src/org/eclipse/core/tests/resources/regression/AllTests.java (+2 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     James Blackburn (Broadcom Corp.) - ongoing development
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.core.tests.resources.regression;
12
package org.eclipse.core.tests.resources.regression;
12
13
Lines 59-64 Link Here
59
		suite.addTest(Bug_288315.suite());
60
		suite.addTest(Bug_288315.suite());
60
		suite.addTest(Bug_329836.suite());
61
		suite.addTest(Bug_329836.suite());
61
		suite.addTest(Bug_331445.suite());
62
		suite.addTest(Bug_331445.suite());
63
		suite.addTest(Bug_332543.suite());
62
		suite.addTest(IFileTest.suite());
64
		suite.addTest(IFileTest.suite());
63
		suite.addTest(IFolderTest.suite());
65
		suite.addTest(IFolderTest.suite());
64
		suite.addTest(IProjectTest.suite());
66
		suite.addTest(IProjectTest.suite());
(-)src/org/eclipse/core/tests/resources/regression/Bug_332543.java (+108 lines)
Added Link Here
1
/*******************************************************************************
2
 *  Copyright (c) 2011 Broadcom 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
 *     James Blackburn (Broadcom Corp.) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.tests.resources.regression;
12
13
import java.io.*;
14
import java.net.URI;
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
17
import org.eclipse.core.filesystem.IFileStore;
18
import org.eclipse.core.filesystem.URIUtil;
19
import org.eclipse.core.resources.*;
20
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.tests.internal.filesystem.wrapper.WrapperFileStore;
22
import org.eclipse.core.tests.internal.filesystem.wrapper.WrapperFileSystem;
23
import org.eclipse.core.tests.resources.ResourceTest;
24
25
/**
26
 * This tests that I/O Exception on OuptuStream#close() after IFile#setContents is correctly reported.
27
 */
28
public class Bug_332543 extends ResourceTest {
29
30
	public static Test suite() {
31
		return new TestSuite(Bug_332543.class);
32
	}
33
34
	/**
35
	 * Wrapper FS which throws an IOException when someone
36
	 * closes an output stream...
37
	 */
38
	public static class IOErrOnCloseFileStore extends WrapperFileStore {
39
		public IOErrOnCloseFileStore(IFileStore store) {
40
			super(store);
41
		}
42
43
		public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException {
44
			OutputStream os = super.openOutputStream(options, monitor);
45
			os = new BufferedOutputStream(os) {
46
				public void close() throws java.io.IOException {
47
					// We close the output stream (so there aren't issues deleting the project during tear-down)
48
					super.close();
49
					// But we also throw IOException as if the operation had failed.
50
					throw new IOException("Whoops I dunno how to close!");
51
				}
52
			};
53
			return os;
54
		}
55
	}
56
57
	@Override
58
	protected void setUp() throws Exception {
59
		super.setUp();
60
	}
61
62
	@Override
63
	protected void tearDown() throws Exception {
64
		WrapperFileSystem.setCustomFileStore(null);
65
		super.tearDown();
66
	}
67
68
	public void testBug() throws Exception {
69
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
70
71
		String proj_name = getUniqueString();
72
		IPath proj_loc = root.getLocation().append(proj_name);
73
		URI proj_uri = WrapperFileSystem.getWrappedURI(URIUtil.toURI(proj_loc));
74
75
		IProjectDescription desc = ResourcesPlugin.getWorkspace().newProjectDescription(proj_name);
76
		desc.setLocationURI(proj_uri);
77
		// Create the project on the wrapped file system
78
		IProject project = root.getProject(desc.getName());
79
		project.create(desc, getMonitor());
80
81
		// Create a file in the project
82
		IFile f = project.getFile("foo.txt");
83
		ensureExistsInFileSystem(f);
84
85
		// Set our evil IOException on close() fs.
86
		WrapperFileSystem.setCustomFileStore(IOErrOnCloseFileStore.class);
87
88
		// Now open the project
89
		project.open(getMonitor());
90
91
		// Try #setContents on an existing file
92
		try {
93
			f.setContents(new ByteArrayInputStream("Random".getBytes()), false, true, getMonitor());
94
			fail("1.0");
95
		} catch (CoreException e) {
96
			// This is expected.
97
		}
98
99
		// Try create on a non-existent file
100
		f = project.getFile("foo1.txt");
101
		try {
102
			f.create(new ByteArrayInputStream("Random".getBytes()), false, getMonitor());
103
			fail("2.0");
104
		} catch (CoreException e) {
105
			// This is expected.
106
		}
107
	}
108
}

Return to bug 332543