SGXSDKDiscoveredPathInfo.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2018 Intel Corporation. //
  3. // //
  4. // All rights reserved. This program and the accompanying materials //
  5. // are made available under the terms of the Eclipse Public License v1.0 //
  6. // which accompanies this distribution, and is available at //
  7. // http://www.eclipse.org/legal/epl-v10.html //
  8. // //
  9. // Contributors: //
  10. // Intel Corporation - initial implementation and documentation //
  11. ///////////////////////////////////////////////////////////////////////////
  12. package com.intel.sgx.discovery;
  13. import java.util.List;
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileReader;
  17. import java.io.IOException;
  18. import java.io.PrintStream;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.Map.Entry;
  23. import org.eclipse.cdt.core.CCorePlugin;
  24. import org.eclipse.cdt.core.model.CoreModel;
  25. import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IDiscoveredPathInfo;
  26. import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IDiscoveredScannerInfoSerializable;
  27. import org.eclipse.core.resources.IFile;
  28. import org.eclipse.core.resources.IProject;
  29. import org.eclipse.core.runtime.CoreException;
  30. import org.eclipse.core.runtime.IPath;
  31. import org.eclipse.core.runtime.IProgressMonitor;
  32. import org.eclipse.core.runtime.Path;
  33. import com.intel.sgx.Activator;
  34. /*
  35. * This code has been taken from the NDK plugin for Linux. If there is an update to this code there, then refactor this code.
  36. */
  37. public class SGXSDKDiscoveredPathInfo implements IDiscoveredPathInfo {
  38. private final IProject mProject;
  39. private long mLastUpdate = IFile.NULL_STAMP;
  40. private IPath[] mIncludePaths;
  41. private Map<String, String> mSymbols;
  42. private boolean mNeedReindexing = false;
  43. private static final IPath LINUX_MK = new Path("sgx/Makefile");
  44. //Keys for preferences
  45. // public static final String LAST_UPDATE = "lastUpdate"; //$NON-NLS-1$
  46. public SGXSDKDiscoveredPathInfo(IProject project) {
  47. this.mProject = project;
  48. load();
  49. }
  50. @Override
  51. public IProject getProject() {
  52. return mProject;
  53. }
  54. @Override
  55. public IPath[] getIncludePaths() {
  56. if (mNeedReindexing) {
  57. CCorePlugin.getIndexManager().reindex(CoreModel.getDefault().create(mProject));
  58. mNeedReindexing = false;
  59. }
  60. return mIncludePaths;
  61. }
  62. void setIncludePaths(List<String> pathStrings) {
  63. mIncludePaths = new IPath[pathStrings.size()];
  64. int i = 0;
  65. for (String path : pathStrings)
  66. mIncludePaths[i++] = new Path(path);
  67. mNeedReindexing = true;
  68. }
  69. @Override
  70. public Map<String, String> getSymbols() {
  71. if (mSymbols == null)
  72. mSymbols = new HashMap<String, String>();
  73. return mSymbols;
  74. }
  75. void setSymbols(Map<String, String> symbols) {
  76. this.mSymbols = symbols;
  77. }
  78. @Override
  79. public IDiscoveredScannerInfoSerializable getSerializable() {
  80. return null;
  81. }
  82. public void update(IProgressMonitor monitor) throws CoreException {
  83. if (!needUpdating())
  84. return;
  85. new SGXSDKDiscoveryUpdater(this).runUpdate(monitor);
  86. if (mIncludePaths != null && mSymbols != null) {
  87. recordUpdate();
  88. save();
  89. }
  90. }
  91. private boolean needUpdating() {
  92. if (mLastUpdate == IFile.NULL_STAMP) {
  93. return true;
  94. }
  95. return mProject.getFile(LINUX_MK).getLocalTimeStamp() > mLastUpdate;
  96. }
  97. private void recordUpdate() {
  98. mLastUpdate = mProject.getFile(LINUX_MK).getLocalTimeStamp();
  99. }
  100. public void delete() {
  101. mLastUpdate = IFile.NULL_STAMP;
  102. }
  103. private File getInfoFile() {
  104. File stateLoc = Activator.getDefault().getStateLocation().toFile();
  105. return new File(stateLoc, mProject.getName() + ".pathInfo"); //$NON-NLS-1$
  106. }
  107. private void save() {
  108. try {
  109. File infoFile = getInfoFile();
  110. infoFile.getParentFile().mkdirs();
  111. PrintStream out = new PrintStream(infoFile);
  112. out.print("t,"); //$NON-NLS-1$
  113. out.print(mLastUpdate);
  114. out.println();
  115. for (IPath include : mIncludePaths) {
  116. out.print("i,"); //$NON-NLS-1$
  117. out.print(include.toPortableString());
  118. out.println();
  119. }
  120. for (Entry<String, String> symbol : mSymbols.entrySet()) {
  121. out.print("d,"); //$NON-NLS-1$
  122. out.print(symbol.getKey());
  123. out.print(","); //$NON-NLS-1$
  124. out.print(symbol.getValue());
  125. out.println();
  126. }
  127. out.close();
  128. } catch (IOException e) {
  129. Activator.log(e);
  130. }
  131. }
  132. private void load() {
  133. try {
  134. File infoFile = getInfoFile();
  135. if (!infoFile.exists())
  136. return;
  137. long timestamp = IFile.NULL_STAMP;
  138. List<IPath> includes = new ArrayList<IPath>();
  139. Map<String, String> defines = new HashMap<String, String>();
  140. BufferedReader reader = new BufferedReader(new FileReader(infoFile));
  141. for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  142. switch (line.charAt(0)) {
  143. case 't':
  144. timestamp = Long.valueOf(line.substring(2));
  145. break;
  146. case 'i':
  147. includes.add(Path.fromPortableString(line.substring(2)));
  148. break;
  149. case 'd':
  150. int n = line.indexOf(',', 2);
  151. if (n == -1)
  152. defines.put(line.substring(2), ""); //$NON-NLS-1$
  153. else
  154. defines.put(line.substring(2, n), line.substring(n + 1));
  155. break;
  156. }
  157. }
  158. reader.close();
  159. mLastUpdate = timestamp;
  160. mIncludePaths = includes.toArray(new IPath[includes.size()]);
  161. mSymbols = defines;
  162. } catch (IOException e) {
  163. Activator.log(e);
  164. }
  165. }
  166. }