Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a62866aef11aadea2fb6b1ae5cd0ea8aeaac4da4 (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 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.debug.core.sourcelookup.containers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.internal.core.sourcelookup.SourceLookupMessages;
import org.eclipse.debug.internal.core.sourcelookup.SourceLookupUtils;

import com.ibm.icu.text.MessageFormat;

/**
 * An archive in the local file system. Returns instances
 * of <code>ZipEntryStorage</code> as source elements.
 * <p>
 * Clients may instantiate this class.
 * </p>
 * @since 3.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public class ExternalArchiveSourceContainer extends AbstractSourceContainer {

	private boolean fDisposed;
	private boolean fDetectRoots;
	private Set<String> fPotentialRoots;
	private List<String> fRoots = new ArrayList<>();
	private String fArchivePath;
	/**
	 * Unique identifier for the external archive source container type
	 * (value <code>org.eclipse.debug.core.containerType.externalArchive</code>).
	 */
	public static final String TYPE_ID = DebugPlugin.getUniqueIdentifier() + ".containerType.externalArchive";	 //$NON-NLS-1$

	/**
	 * Creates an archive source container on the archive at the
	 * specified location in the local file system.
	 *
	 * @param archivePath path to the archive in the local file system
	 * @param detectRootPaths whether root container paths should be detected. When
	 *   <code>true</code>, searching is performed relative to a root path
	 *   within the archive based on fully qualified file names. A root
	 *   path is automatically determined for when the first
	 *   successful search is performed. For example, when searching for a file
	 *   named <code>a/b/c.d</code>, and an entry in the archive named
	 *   <code>r/a/b/c.d</code> exists, a root path is set to <code>r</code>.
	 *   When searching for an unqualified file name, root containers are not
	 *   considered.
	 *   When <code>false</code>, searching is performed by
	 *   matching file names as suffixes to the entries in the archive.
	 */
	public ExternalArchiveSourceContainer(String archivePath, boolean detectRootPaths) {
		fArchivePath = archivePath;
		fDetectRoots = detectRootPaths;
	}

	@SuppressWarnings("resource")
	@Override
	public Object[] findSourceElements(String name) throws CoreException {
		String newname = name.replace('\\', '/');
		ZipFile file = getArchive();
		if (file == null) {
			return EMPTY;
		}
		// NOTE: archive can be closed between get (above) and synchronized block (below)
		synchronized (file) {
			boolean isQualfied = newname.indexOf('/') > 0;
			if (fDetectRoots && isQualfied) {
				ZipEntry entry = searchRoots(file, newname);
				if (entry != null) {
					return new Object[]{new ZipEntryStorage(file, entry)};
				}
			} else {
				// try exact match
				ZipEntry entry = null;
				try {
					entry = file.getEntry(newname);
				} catch (IllegalStateException e) {
					// archive was closed between retrieving and locking
					throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(),
							e.getMessage(), e));
				}
				if (entry != null) {
					// can't be any duplicates if there is an exact match
					return new Object[]{new ZipEntryStorage(file, entry)};
				}
				// search
				Enumeration<? extends ZipEntry> entries = file.entries();
				List<ZipEntryStorage> matches = null;
				try {
					File zipFile = new File(fArchivePath);
					String zipFileCanonical = zipFile.getCanonicalPath();
					while (entries.hasMoreElements()) {
						entry = entries.nextElement();
						String entryName = entry.getName();
						if (entryName.endsWith(newname)) {
							String zipEntryCanonical = (new File(zipFile, entryName)).getCanonicalPath();
							if (!zipEntryCanonical.startsWith(zipFileCanonical + File.separator)) {
								throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), "Invalid path: " + zipEntryCanonical)); //$NON-NLS-1$
							}
							if (isQualfied || entryName.length() == newname.length() || entryName.charAt(entryName.length() - newname.length() - 1) == '/') {
								if (isFindDuplicates()) {
									if (matches == null) {
										matches = new ArrayList<>();
									}
									matches.add(new ZipEntryStorage(file, entry));
								} else {
									return new Object[] {
											new ZipEntryStorage(file, entry) };
								}
							}
						}
					}
				} catch (IOException e) {
					throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), "Invalid path: " + fArchivePath)); //$NON-NLS-1$
				}
				if (matches != null) {
					return matches.toArray();
				}
			}
		}
		return EMPTY;
	}

	/**
	 * Returns the root path in this archive for the given file name, based
	 * on its type, or <code>null</code> if none. Detects a root if a root has
	 * not yet been detected for the given file type.
	 *
	 * @param file zip file to search in
	 * @param name file name
	 * @return the {@link ZipEntry} with the given name or <code>null</code>
	 * @exception CoreException if an exception occurs while detecting the root
	 */
	private synchronized ZipEntry searchRoots(ZipFile file, String name) throws CoreException {
		if (fDisposed) {
			return null;
		}
		if (fPotentialRoots == null) {
			fPotentialRoots = new HashSet<>();
			fPotentialRoots.add(""); //$NON-NLS-1$
			// all potential roots are the directories
			try {
				Enumeration<? extends ZipEntry> entries = file.entries();
				while (entries.hasMoreElements()) {
					ZipEntry entry = entries.nextElement();
					if (entry.isDirectory()) {
						fPotentialRoots.add(entry.getName());
					} else {
						String entryName = entry.getName();
						int index = entryName.lastIndexOf("/"); //$NON-NLS-1$
						while (index > 0) {
							if (fPotentialRoots.add(entryName.substring(0, index + 1))) {
								entryName = entryName.substring(0, index);
								index = entryName.lastIndexOf("/"); //$NON-NLS-1$
							} else {
								break;
							}
						}
					}
				}
			} catch (IllegalStateException e) {
				// archive was closed between retrieving and locking
				throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(),
					e.getMessage(), e));
			}
		}
		int i = 0;
		while (i < fRoots.size()) {
			String root = fRoots.get(i);
			ZipEntry entry = file.getEntry(root+name);
			if (entry != null) {
				return entry;
			}
			i++;
		}
		if (!fPotentialRoots.isEmpty()) {
			for (String root : fPotentialRoots) {
				ZipEntry entry = file.getEntry(root + name);
				if (entry != null) {
					if (root != null) {
						fRoots.add(root);
						fPotentialRoots.remove(root);
						// remove any roots that begin with the new root, as
						// roots
						// cannot be nested
						Iterator<String> rs = fPotentialRoots.iterator();
						while (rs.hasNext()) {
							String r = rs.next();
							if (r.startsWith(root)) {
								rs.remove();
							}
						}
					}
					return entry;
				}
			}
		}
		return null;
	}

	/**
	 * Returns the archive to search in.
	 * @return the {@link ZipFile} to search in
	 *
	 * @throws CoreException if unable to access the archive
	 */
	private synchronized ZipFile getArchive() throws CoreException {
		if (fDisposed) {
			return null;
		}
		try {
			return SourceLookupUtils.getZipFile(fArchivePath);
		} catch (IOException e) {
			File file = new File(fArchivePath);
			if (file.exists()) {
				abort(MessageFormat.format(SourceLookupMessages.ExternalArchiveSourceContainer_2, new Object[] { fArchivePath }), e);
			} else {
				warn(MessageFormat.format(SourceLookupMessages.ExternalArchiveSourceContainer_1, new Object[] { fArchivePath }), e);
			}
		}
		return null;
	}

	@Override
	public String getName() {
		return fArchivePath;
	}

	@Override
	public ISourceContainerType getType() {
		return getSourceContainerType(TYPE_ID);
	}

	/**
	 * Returns whether root paths are automatically detected in this
	 * archive source container.
	 *
	 * @return whether root paths are automatically detected in this
	 * archive source container
	 */
	public boolean isDetectRoot() {
		return fDetectRoots;
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof ExternalArchiveSourceContainer &&
			((ExternalArchiveSourceContainer)obj).getName().equals(getName());
	}

	@Override
	public int hashCode() {
		return getName().hashCode();
	}

	@Override
	public synchronized void dispose() {
		super.dispose();
		if (fPotentialRoots != null) {
			fPotentialRoots.clear();
		}
		fRoots.clear();
		fDisposed = true;
	}
}

Back to the top