RemoveEnclave.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 org.eclipse.core.commands.ExecutionEvent;
  18. import org.eclipse.core.commands.ExecutionException;
  19. import org.eclipse.core.commands.IHandler;
  20. import org.eclipse.core.commands.IHandlerListener;
  21. import org.eclipse.core.resources.IFolder;
  22. import org.eclipse.core.resources.IProject;
  23. import org.eclipse.core.resources.IResource;
  24. import org.eclipse.core.runtime.CoreException;
  25. import org.eclipse.core.runtime.IPath;
  26. import org.eclipse.core.runtime.Path;
  27. import org.eclipse.jface.dialogs.InputDialog;
  28. import org.eclipse.jface.viewers.ISelection;
  29. import org.eclipse.jface.viewers.IStructuredSelection;
  30. import org.eclipse.swt.widgets.Display;
  31. import org.eclipse.swt.widgets.Shell;
  32. import org.eclipse.ui.handlers.HandlerUtil;
  33. import com.intel.sgx.Activator;
  34. import com.intel.sgx.dialogs.RemoveEnclaveFileDialog;
  35. public class RemoveEnclave implements IHandler {
  36. public String edlFilename = "";
  37. private IPath edlCanonicalFilename;
  38. @Override
  39. public void addHandlerListener(IHandlerListener handlerListener) {
  40. }
  41. @Override
  42. public void dispose() {
  43. }
  44. @Override
  45. public Object execute(ExecutionEvent event) throws ExecutionException {
  46. String edlBasename;
  47. IProject project = null;
  48. // Display display = Display.getCurrent();
  49. // Shell shell = new Shell(display);
  50. RemoveEnclaveFileDialog dialog = new RemoveEnclaveFileDialog(null, this);
  51. if (dialog.open() != InputDialog.OK) {
  52. return null;
  53. }
  54. edlCanonicalFilename = Path.fromOSString(edlFilename);
  55. edlBasename = edlCanonicalFilename.lastSegment();
  56. if(edlBasename.isEmpty()){
  57. return null;
  58. }
  59. ISelection selection = HandlerUtil.getCurrentSelection(event);
  60. Object element = null;
  61. if(selection instanceof IStructuredSelection) {
  62. element = ((IStructuredSelection)selection).getFirstElement();
  63. if (element instanceof IResource) {
  64. project= ((IResource)element).getProject();
  65. }
  66. }
  67. if (!project.exists()) {
  68. System.err.println("Error: Project not found");
  69. return null;
  70. }
  71. IPath targetRelPath = project.getProjectRelativePath().append("sgx").append("enclave_" + edlBasename);
  72. try {
  73. for (int i=1;i<=targetRelPath.segmentCount();i++) {
  74. IFolder subfolder = project.getFolder(targetRelPath.uptoSegment(i));
  75. if (subfolder.exists()){
  76. if(subfolder.getProjectRelativePath().toOSString().contains("enclave_"+edlBasename)){
  77. subfolder.delete(true, true, null);
  78. break;
  79. }
  80. }
  81. }
  82. targetRelPath = project.getProjectRelativePath().append("sgx").append(edlBasename);
  83. for (int i=1;i<=targetRelPath.segmentCount();i++) {
  84. IFolder subfolder = project.getFolder(targetRelPath.uptoSegment(i));
  85. if (subfolder.exists()){
  86. if(subfolder.getProjectRelativePath().toOSString().contains(edlBasename)){
  87. subfolder.delete(true, true, null);
  88. break;
  89. }
  90. }
  91. }
  92. } catch (Exception e) {
  93. Activator.log(e);
  94. }
  95. try {
  96. project.refreshLocal(IResource.DEPTH_INFINITE, null);
  97. } catch (CoreException e) {
  98. Activator.log(e);
  99. } catch (IllegalArgumentException e){
  100. Activator.log(e);
  101. }
  102. return null;
  103. }
  104. @Override
  105. public boolean isEnabled() {
  106. return true;
  107. }
  108. @Override
  109. public boolean isHandled() {
  110. return true;
  111. }
  112. @Override
  113. public void removeHandlerListener(IHandlerListener handlerListener) {
  114. }
  115. public static void copyFile(File source, File dest) throws IOException {
  116. byte[] bytes = new byte[4092];
  117. if (source != null && dest != null) {
  118. if (source.isFile()) {
  119. FileInputStream in = null;
  120. FileOutputStream out = null;
  121. try {
  122. in = new FileInputStream(source);
  123. out = new FileOutputStream(dest);
  124. int len;
  125. while ((len = in.read(bytes)) != -1) {
  126. out.write(bytes, 0, len);
  127. }
  128. } catch (Exception e) {
  129. System.err.println("Error: " + e.toString());
  130. Activator.log(e);
  131. } finally {
  132. try {
  133. if (in != null)
  134. in.close();
  135. } finally {
  136. if (out != null)
  137. out.close();
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }