1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Partial fix for 167551, operation to create importable archive.

This commit is contained in:
Markus Schorn 2007-03-14 12:37:10 +00:00
parent 8f0bb1d073
commit 1e65c86123
9 changed files with 338 additions and 56 deletions

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.core.dom;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* @author Doug Schaefer
@ -31,9 +32,19 @@ public interface IPDOMManager {
*/
public void reindex(ICProject project) throws CoreException;
/**
* Export index for usage within a team.
* @param project a project for which the pdom is to be exported.
* @param location the target location for the database.
* @param options currently none are supported.
* @throws CoreException
* @since 4.0
*/
public void export(ICProject project, String location, int options, IProgressMonitor monitor) throws CoreException;
// Getting and setting indexer Ids
public String getDefaultIndexerId();
public void setDefaultIndexerId(String indexerId);
public void setDefaultIndexerId(String indexerId);
public String getIndexerId(ICProject project) throws CoreException;
public void setIndexerId(ICProject project, String indexerId) throws CoreException;

View file

@ -20,9 +20,7 @@ import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
@ -106,7 +104,7 @@ public class Checksums {
* @throws OperationCanceledException
* @since 4.0
*/
public static Map createChecksumMap(ITranslationUnit[] tus, MessageDigest md, IProgressMonitor pm)
public static Map createChecksumMap(IFile[] tus, MessageDigest md, IProgressMonitor pm)
throws OperationCanceledException {
Map result= new HashMap();
putAlgorithm(result, md);
@ -115,10 +113,8 @@ public class Checksums {
if (pm.isCanceled()) {
throw new OperationCanceledException();
}
ITranslationUnit tu = tus[i];
IResource res= tu.getResource();
if (res instanceof IFile) {
IFile file= (IFile) res;
IFile file = tus[i];
if (file != null) {
IPath location= file.getLocation();
if (location != null) {
File f= location.toFile();

View file

@ -22,6 +22,11 @@ public class Messages extends NLS {
public static String PDOMManager_notifyJob_label;
public static String PDOMManager_notifyTask_message;
public static String PDOMManager_StartJob_name;
public static String TeamPDOMExportOperation_errorCreateArchive;
public static String TeamPDOMExportOperation_errorCreatingTempFile;
public static String TeamPDOMExportOperation_errorWriteTempFile;
public static String TeamPDOMExportOperation_subtaskCreateDatabase;
public static String TeamPDOMExportOperation_taskExportIndex;
public static String WritablePDOM_error_unknownLinkage;
static {
// initialize resource bundle

View file

@ -178,7 +178,7 @@ public class PDOM extends PlatformObject implements IIndexFragment, IPDOM {
((IListener)i.next()).handleChange(this);
}
public Database getDB() throws CoreException {
public Database getDB() {
return db;
}

View file

@ -348,8 +348,8 @@ public class PDOMManager implements IPDOMManager, IWritableIndexManager, IListen
}
// perform import
PDOMImporter importer= new PDOMImporter(project);
importer.performImport(pm);
TeamPDOMImportOperation operation= new TeamPDOMImportOperation(project);
operation.run(pm);
synchronized (fIndexerMutex) {
Properties props= IndexerPreferences.getProperties(prj);
@ -357,11 +357,11 @@ public class PDOMManager implements IPDOMManager, IWritableIndexManager, IListen
registerIndexer(project, indexer);
IPDOMIndexerTask task= null;
if (!importer.wasSuccessful()) {
if (!operation.wasSuccessful()) {
task= new PDOMRebuildTask(indexer);
}
else {
ITranslationUnit[] tus= importer.getTranslationUnitsToUpdate();
ITranslationUnit[] tus= operation.getTranslationUnitsToUpdate();
if (tus.length > 0) {
task= indexer.createTask(NO_TUS, tus, NO_TUS);
}
@ -464,7 +464,7 @@ public class PDOMManager implements IPDOMManager, IWritableIndexManager, IListen
Job addProject= new Job(Messages.PDOMManager_StartJob_name) {
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("", 100); //$NON-NLS-1$
if (project.isOpen()) {
if (project.isOpen() && CoreModel.hasCNature(project)) {
ICProject cproject= CoreModel.getDefault().create(project);
if (cproject != null) {
syncronizeProjectSettings(project, new SubProgressMonitor(monitor, 1));
@ -919,4 +919,11 @@ public class PDOMManager implements IPDOMManager, IWritableIndexManager, IListen
pdom.releaseWriteLock();
}
}
public void export(ICProject project, String location, int options, IProgressMonitor monitor) throws CoreException {
TeamPDOMExportOperation operation= new TeamPDOMExportOperation(project);
operation.setTargetLocation(location);
operation.setOptions(options);
operation.run(monitor);
}
}

View file

@ -0,0 +1,247 @@
/*******************************************************************************
* Copyright (c) 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.pdom;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.index.IndexFileLocation;
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
public class TeamPDOMExportOperation implements IWorkspaceRunnable {
private ICProject fProject;
private String fTargetLocation;
private File fTargetLocationFile;
private MessageDigest fMessageDigest;
public TeamPDOMExportOperation(ICProject project) {
fProject= project;
}
public void setTargetLocation(String location) {
fTargetLocation= location;
}
public void setOptions(int options) {
}
public void setAlgorithm(MessageDigest md) {
fMessageDigest= md;
}
public void run(IProgressMonitor monitor) throws CoreException {
getMessageDigest();
getTargetLocation();
File tmpPDOM= null;
File tmpChecksums= null;
try {
tmpPDOM = File.createTempFile("tmp", ".pdom"); //$NON-NLS-1$//$NON-NLS-2$
tmpChecksums= File.createTempFile("checksums", ".dat"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (IOException e) {
throw new CoreException(CCorePlugin.createStatus(Messages.TeamPDOMExportOperation_errorCreatingTempFile, e));
}
try {
PDOMManager pdomManager= CCoreInternals.getPDOMManager();
// wait for indexer
monitor.beginTask(Messages.TeamPDOMExportOperation_taskExportIndex, 100);
pdomManager.joinIndexer(Integer.MAX_VALUE, subMonitor(monitor, 80));
checkMonitor(monitor);
// create index
IIndexLocationConverter converter= new ResourceContainerRelativeLocationConverter(ResourcesPlugin.getWorkspace().getRoot());
monitor.subTask(Messages.TeamPDOMExportOperation_subtaskCreateDatabase);
pdomManager.exportProjectPDOM(fProject, tmpPDOM, converter);
checkMonitor(monitor);
monitor.worked(5);
// create checksums
PDOM pdom= new PDOM(tmpPDOM, converter);
createChecksums(fProject, pdom, tmpChecksums, subMonitor(monitor, 10));
// create archive
createArchive(tmpPDOM, tmpChecksums);
// store preferences
IndexerPreferences.setIndexImportLocation(fProject.getProject(), fTargetLocation.toString());
}
finally {
if (tmpPDOM != null) {
tmpPDOM.delete();
}
if (tmpChecksums != null) {
tmpChecksums.delete();
}
}
}
private void getTargetLocation() throws CoreException {
fTargetLocationFile= TeamPDOMImportOperation.expandLocation(fProject.getProject(), fTargetLocation);
}
private void getMessageDigest() throws CoreException {
if (fMessageDigest == null) {
try {
fMessageDigest= Checksums.getDefaultAlgorithm();
}
catch (NoSuchAlgorithmException e) {
throw new CoreException(CCorePlugin.createStatus(e.getMessage(), e));
}
}
}
private void createChecksums(ICProject cproject, PDOM pdom, File target, IProgressMonitor monitor) throws CoreException {
List locs;
try {
pdom.acquireReadLock();
} catch (InterruptedException e) {
throw new OperationCanceledException();
}
try {
locs = pdom.getAllFileLocations();
}
finally {
pdom.releaseReadLock();
try {
pdom.getDB().close();
} catch (IOException e) {
CCorePlugin.log(e);
}
}
int i=0;
IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
IFile[] files= new IFile[locs.size()];
for (Iterator iterator = locs.iterator(); iterator.hasNext();) {
IndexFileLocation floc = (IndexFileLocation) iterator.next();
String fullPath= floc.getFullPath();
if (fullPath != null) {
files[i++]= root.getFile(new Path(fullPath));
}
}
Map map= Checksums.createChecksumMap(files, fMessageDigest, monitor);
writeChecksums(map, target);
}
private void writeChecksums(Map map, File target) throws CoreException {
ObjectOutputStream out= null;
try {
out= new ObjectOutputStream(new FileOutputStream(target));
out.writeObject(map);
} catch (IOException e) {
throw new CoreException(CCorePlugin.createStatus(Messages.TeamPDOMExportOperation_errorWriteTempFile, e));
}
finally {
close(out);
}
}
private void close(InputStream in) {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
CCorePlugin.log(e);
}
}
private void close(OutputStream out) {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
CCorePlugin.log(e);
}
}
private void createArchive(File tmpPDOM, File tmpChecksums) throws CoreException {
fTargetLocationFile.delete();
ZipOutputStream out= null;
try {
fTargetLocationFile.getParentFile().mkdirs();
out= new ZipOutputStream(new FileOutputStream(fTargetLocationFile));
out.setLevel(Deflater.BEST_COMPRESSION);
writeEntry(out, TeamPDOMImportOperation.INDEX_NAME, tmpPDOM);
writeEntry(out, TeamPDOMImportOperation.CHECKSUMS_NAME, tmpChecksums);
}
catch (IOException e) {
throw new CoreException(CCorePlugin.createStatus(Messages.TeamPDOMExportOperation_errorCreateArchive, e));
}
finally {
close(out);
}
IFile[] wsResource= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(fTargetLocationFile.getAbsolutePath()));
for (int i = 0; i < wsResource.length; i++) {
IFile file = wsResource[i];
file.refreshLocal(0, new NullProgressMonitor());
}
}
private void writeEntry(ZipOutputStream out, String name, File input) throws IOException {
ZipEntry e= new ZipEntry(name);
out.putNextEntry(e);
int read= 0;
byte[] buffer= new byte[4096];
InputStream in= new FileInputStream(input);
try {
while ((read= in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
out.closeEntry();
}
finally {
close(in);
}
}
private SubProgressMonitor subMonitor(IProgressMonitor monitor, int ticks) {
return new SubProgressMonitor(monitor, ticks);
}
private void checkMonitor(IProgressMonitor monitor) {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
}
}

View file

@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
@ -44,6 +45,7 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
@ -51,11 +53,18 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.osgi.util.NLS;
public class PDOMImporter {
private static final String CHECKSUMS_NAME = "checksums.dat"; //$NON-NLS-1$
private static final String INDEX_NAME = "cdt-index.pdom"; //$NON-NLS-1$
public class TeamPDOMImportOperation implements IWorkspaceRunnable {
static final String CHECKSUMS_NAME = "checksums.dat"; //$NON-NLS-1$
static final String INDEX_NAME = "cdt-index.pdom"; //$NON-NLS-1$
private static Pattern PROJECT_VAR_PATTERN= Pattern.compile("\\$\\{(project_[a-zA-Z0-9]*)\\}"); //$NON-NLS-1$
private static final String PROJECT_VAR_REPLACEMENT_BEGIN = "\\${$1:"; //$NON-NLS-1$
private static final String PROJECT_VAR_REPLACEMENT_END = "}"; //$NON-NLS-1$
private static final String DOLLAR_OR_BACKSLASH_REPLACEMENT = "\\\\$0"; //$NON-NLS-1$
private static Pattern DOLLAR_OR_BACKSLASH_PATTERN= Pattern.compile("[\\$\\\\]"); //$NON-NLS-1$
private static final class FileAndChecksum {
public ITranslationUnit fFile;
@ -71,51 +80,23 @@ public class PDOMImporter {
private ITranslationUnit[] fTranslationUnitsToUpdate= new ITranslationUnit[0];
private boolean fShowActivity;
public PDOMImporter(ICProject project) {
public TeamPDOMImportOperation(ICProject project) {
fProject= project;
fShowActivity= PDOMIndexerTask.checkDebugOption(IPDOMIndexerTask.TRACE_ACTIVITY, "true"); //$NON-NLS-1$
}
public void performImport(IProgressMonitor pm) {
public void run(IProgressMonitor pm) {
if (fShowActivity) {
System.out.println("Indexer: PDOMImporter start"); //$NON-NLS-1$
}
IPath importLocation= getImportLocation();
fSuccess= importIndex(importLocation, pm);
if (fShowActivity) {
System.out.println("Indexer: PDOMImporter completed, ok=" + fSuccess); //$NON-NLS-1$
}
}
public boolean wasSuccessful() {
return fSuccess;
}
public ITranslationUnit[] getTranslationUnitsToUpdate() {
return fTranslationUnitsToUpdate;
}
private IPath getImportLocation() {
IProject project= fProject.getProject();
String locationString= IndexerPreferences.getIndexImportLocation(project);
// mstodo support variables
IPath location= new Path(locationString);
if (!location.isAbsolute()) {
location= project.getLocation().append(location);
}
return location;
}
private boolean importIndex(IPath importLocation, IProgressMonitor monitor) {
File importFile= importLocation.toFile();
if (!importFile.exists()) {
return false;
}
fSuccess= false;
Exception ex= null;
try {
doImportIndex(importFile, monitor);
File importFile= getImportLocation();
if (importFile.exists()) {
doImportIndex(importFile, pm);
fSuccess= true;
}
}
catch (InterruptedException e) {
throw new OperationCanceledException();
@ -132,11 +113,41 @@ public class PDOMImporter {
if (ex != null) {
CCorePlugin.log(ex);
return false;
}
return true;
if (fShowActivity) {
System.out.println("Indexer: PDOMImporter completed, ok=" + fSuccess); //$NON-NLS-1$
}
}
public boolean wasSuccessful() {
return fSuccess;
}
public ITranslationUnit[] getTranslationUnitsToUpdate() {
return fTranslationUnitsToUpdate;
}
private File getImportLocation() throws CoreException {
IProject project= fProject.getProject();
String locationString= IndexerPreferences.getIndexImportLocation(project);
return expandLocation(project, locationString);
}
static File expandLocation(IProject project, String loc) throws CoreException {
String replacement= PROJECT_VAR_REPLACEMENT_BEGIN
+ DOLLAR_OR_BACKSLASH_PATTERN.matcher(project.getName()).replaceAll(DOLLAR_OR_BACKSLASH_REPLACEMENT)
+ PROJECT_VAR_REPLACEMENT_END;
loc= PROJECT_VAR_PATTERN.matcher(loc).replaceAll(replacement);
IStringVariableManager varManager= VariablesPlugin.getDefault().getStringVariableManager();
IPath location= new Path(varManager.performStringSubstitution(loc));
if (!location.isAbsolute()) {
location= project.getLocation().append(location);
}
return location.toFile();
}
private void doImportIndex(File importFile, IProgressMonitor monitor) throws CoreException, InterruptedException, IOException {
ZipFile zip= new ZipFile(importFile);
Map checksums= null;

View file

@ -111,10 +111,10 @@ public class WritablePDOM extends PDOM implements IWritableIndexFragment {
String internalFormat = newConverter.toInternalFormat(file.getLocation());
if(internalFormat!=null) {
file.setInternalLocation(internalFormat);
getFileIndex().insert(file.getRecord());
} else {
notConverted.add(file);
}
getFileIndex().insert(file.getRecord());
}
return notConverted;

View file

@ -16,3 +16,8 @@ PDOMManager_notifyTask_message=Notify Listeners
PDOMManager_indexMonitorDetail={0}/{1} sources, {2} headers
PDOMManager_ExistingFileCollides=A pdom already exists at location {0}
Checksums_taskComputeChecksums=Computing checksums
TeamPDOMExportOperation_errorCreatingTempFile=Cannot create temp file
TeamPDOMExportOperation_taskExportIndex=Export team shared index
TeamPDOMExportOperation_subtaskCreateDatabase=Creating database
TeamPDOMExportOperation_errorWriteTempFile=Cannot write to temporary file
TeamPDOMExportOperation_errorCreateArchive=Error creating archive