AddUntrustedModule.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.handlers;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.util.Map;
  18. import org.eclipse.cdt.core.templateengine.TemplateCore;
  19. import org.eclipse.cdt.core.templateengine.TemplateEngine;
  20. import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
  21. import org.eclipse.core.commands.ExecutionEvent;
  22. import org.eclipse.core.commands.ExecutionException;
  23. import org.eclipse.core.commands.IHandler;
  24. import org.eclipse.core.commands.IHandlerListener;
  25. import org.eclipse.core.resources.IFolder;
  26. import org.eclipse.core.resources.IProject;
  27. import org.eclipse.core.resources.IResource;
  28. import org.eclipse.core.runtime.CoreException;
  29. import org.eclipse.core.runtime.IPath;
  30. import org.eclipse.core.runtime.IProgressMonitor;
  31. import org.eclipse.core.runtime.IStatus;
  32. import org.eclipse.core.runtime.NullProgressMonitor;
  33. import org.eclipse.core.runtime.Path;
  34. import org.eclipse.jface.dialogs.InputDialog;
  35. import org.eclipse.jface.viewers.ISelection;
  36. import org.eclipse.jface.viewers.IStructuredSelection;
  37. import org.eclipse.swt.widgets.Display;
  38. import org.eclipse.swt.widgets.Shell;
  39. import org.eclipse.ui.handlers.HandlerUtil;
  40. import com.intel.sgx.Activator;
  41. import com.intel.sgx.dialogs.AddUntrustedModuleDialog;
  42. import com.intel.sgx.preferences.PreferenceConstants;
  43. public class AddUntrustedModule extends ModuleCreationBaseHandler implements IHandler {
  44. public String edlFilename = "";
  45. public String libPath = "";
  46. private IPath edlCanonicalFilename;
  47. private IPath libCanonicalPathname;
  48. @Override
  49. public void addHandlerListener(IHandlerListener handlerListener) {
  50. }
  51. @Override
  52. public void dispose() {
  53. }
  54. @Override
  55. public Object execute(ExecutionEvent event) throws ExecutionException {
  56. String edlBasename,edlPath,linuxLibPath,modulePath,enclaveBasename;
  57. IProject project = null;
  58. //Display display = Display.getCurrent();
  59. Shell shell = null;
  60. AddUntrustedModuleDialog dialog = new AddUntrustedModuleDialog(shell, this);
  61. if (dialog.open() != InputDialog.OK) {
  62. return null;
  63. }
  64. if((edlFilename.isEmpty() && libPath.isEmpty())){
  65. System.err.println("No Enclave selected to Import.");
  66. return null;
  67. }
  68. if( edlFilename.isEmpty() ){
  69. System.err.println("Edl File not selected.");
  70. return null;
  71. }
  72. edlCanonicalFilename = Path.fromOSString(edlFilename);
  73. if (!edlCanonicalFilename.getFileExtension().toLowerCase().equals("edl")) {
  74. System.err.println("Error: EDL file extension = "+ edlCanonicalFilename.getFileExtension());
  75. return null;
  76. }
  77. edlBasename = edlCanonicalFilename.removeFileExtension().lastSegment();
  78. ISelection selection = HandlerUtil.getCurrentSelection(event);
  79. Object element = null;
  80. if (selection instanceof IStructuredSelection) {
  81. element = ((IStructuredSelection) selection).getFirstElement();
  82. if (element instanceof IResource) {
  83. project = ((IResource) element).getProject();
  84. }
  85. }
  86. if (!project.exists()) {
  87. System.err.println("Error: Project not found");
  88. return null;
  89. }
  90. IPath targetRelPath = project.getProjectRelativePath().append("sgx").append("untrusted_" + edlBasename);
  91. try {
  92. for (int i = 1; i <= targetRelPath.segmentCount(); i++) {
  93. IFolder subfolder = project.getFolder(targetRelPath.uptoSegment(i));
  94. if (!subfolder.exists()) {
  95. subfolder.create(true, true, null);
  96. }
  97. }
  98. } catch (CoreException e) {
  99. System.err.println("Error: Error creating enclave directory.");
  100. Activator.log(e);
  101. e.printStackTrace();
  102. }
  103. IPath edlRelPath = (Path.fromOSString(edlFilename)).makeRelativeTo(project.getLocation().append("sgx"));
  104. edlPath = edlRelPath.toOSString();
  105. IPath linuxLibRelPath = (Path.fromOSString(libPath)).makeRelativeTo(project.getLocation().append("sgx"));
  106. if(linuxLibRelPath.removeLastSegments(1).lastSegment().toString().equalsIgnoreCase("sgx")){
  107. linuxLibPath = linuxLibRelPath.removeLastSegments(3).toOSString();
  108. modulePath = linuxLibRelPath.removeFirstSegments(linuxLibRelPath.segmentCount()-3).removeLastSegments(1).toOSString();
  109. }
  110. else if(linuxLibRelPath.removeLastSegments(1).lastSegment().toString().equalsIgnoreCase("src")){
  111. linuxLibPath = linuxLibRelPath.removeLastSegments(3).toOSString();
  112. modulePath = linuxLibRelPath.removeFirstSegments(linuxLibRelPath.segmentCount()-3).removeLastSegments(1).toOSString();
  113. }
  114. else{
  115. linuxLibPath = linuxLibRelPath.removeLastSegments(2).toOSString();
  116. modulePath = linuxLibRelPath.removeFirstSegments(linuxLibRelPath.segmentCount()-2).removeLastSegments(1).toOSString();
  117. }
  118. IProgressMonitor monitor = new NullProgressMonitor();
  119. TemplateCore template = null;
  120. if(isCPProject(project))
  121. template = TemplateEngine.getDefault().getTemplateById("SGXUntrustedModuleC++Minimal");
  122. else
  123. template = TemplateEngine.getDefault().getTemplateById("SGXUntrustedModuleCMinimal");
  124. Map<String, String> valueStore = template.getValueStore();
  125. valueStore.put("projectName", project.getName());
  126. valueStore.put("sourcepath",linuxLibPath);
  127. valueStore.put("edlPath", edlPath);
  128. valueStore.put("baseName", edlBasename);
  129. valueStore.put("workspacePath", linuxLibPath);//deprecate
  130. valueStore.put("modPath", modulePath);
  131. valueStore.put("ENCLAVENAME",edlBasename.toUpperCase());
  132. valueStore.put("libPath",libPath);
  133. valueStore.put("SdkPathFromPlugin", Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.SDK_PATH));
  134. IStatus[] statuses = template.executeTemplateProcesses(monitor, false);
  135. try {
  136. copyFile(new File(edlFilename), project.getLocation().append("sgx").append("untrusted_"+edlBasename).append(edlBasename+".edl").toFile());
  137. } catch (IOException e1) {
  138. e1.printStackTrace();
  139. }
  140. ManagedBuildManager.saveBuildInfo(project, true);
  141. try {
  142. project.refreshLocal(IResource.DEPTH_INFINITE, null);
  143. } catch (CoreException e) {
  144. Activator.log(e);
  145. e.printStackTrace();
  146. }
  147. return null;
  148. }
  149. @Override
  150. public boolean isEnabled() {
  151. return true;
  152. }
  153. @Override
  154. public boolean isHandled() {
  155. return true;
  156. }
  157. @Override
  158. public void removeHandlerListener(IHandlerListener handlerListener) {
  159. }
  160. public void setFilename(String filename) {
  161. edlFilename = filename;
  162. }
  163. public static void copyFile(File source, File dest) throws IOException {
  164. byte[] bytes = new byte[4092];
  165. if (source != null && dest != null) {
  166. if (source.isFile()) {
  167. FileInputStream in = null;
  168. FileOutputStream out = null;
  169. try {
  170. in = new FileInputStream(source);
  171. out = new FileOutputStream(dest);
  172. int len;
  173. while ((len = in.read(bytes)) != -1) {
  174. out.write(bytes, 0, len);
  175. }
  176. } catch (Exception e) {
  177. Activator.log(e);
  178. System.err.println("Error: " + e.toString());
  179. } finally {
  180. try {
  181. if (in != null)
  182. in.close();
  183. } finally {
  184. if (out != null)
  185. out.close();
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }