@Test
  public void testRemoveAttachment() throws Exception {

    // CREATING ATTACHMENT FOR REMOVE
    List<MultipartFile> files = new ArrayList<>();
    files.add(multipartFile);

    Mockito.when(attachmentFactory.newAttachment()).thenReturn(null);

    attachmentService.setAttachment(new Attachment());
    attachmentService.setMessageResponse(new MessageResponse());

    MessageResponse messageResponse1 = attachmentService.createAttachment(files);

    Mockito.verify(attachmentDao, Mockito.times(1)).createAttachments(argumentCaptor.capture());

    Attachment attachment = argumentCaptor.getValue().get(0);

    // REMOVE ATTACHMENT
    Mockito.when(attachmentDao.removeAttachment(attachment.getAttachmentId()))
        .thenReturn(attachment);

    MessageResponse messageResponse =
        attachmentService.removeAttachment(attachment.getAttachmentId());

    boolean isExistPreview = Files.exists(Paths.get(storagePath + attachment.getPreviewPath()));
    boolean isExistImage = Files.exists(Paths.get(storagePath + attachment.getFilePathInStorage()));

    Assert.assertTrue(!isExistPreview && !isExistImage && messageResponse.getCode() == 1);
  }
  /**
   * Check uploading files and create attachments
   *
   * @throws Exception
   */
  @Test
  public void testCreateAttachment() throws Exception {

    List<MultipartFile> files = new ArrayList<>();
    files.add(multipartFile);

    Mockito.when(attachmentFactory.newAttachment()).thenReturn(null);

    attachmentService.setAttachment(new Attachment());
    attachmentService.setMessageResponse(new MessageResponse());

    MessageResponse messageResponse = attachmentService.createAttachment(files);

    Mockito.verify(attachmentDao, Mockito.times(1)).createAttachments(argumentCaptor.capture());

    Attachment attachment = argumentCaptor.getValue().get(0);

    boolean isExistPreview = Files.exists(Paths.get(storagePath + attachment.getPreviewPath()));
    boolean isExistImage = Files.exists(Paths.get(storagePath + attachment.getFilePathInStorage()));

    Files.delete(Paths.get(storagePath + attachment.getPreviewPath()));
    Files.delete(Paths.get(storagePath + attachment.getFilePathInStorage()));

    Assert.assertTrue(
        attachment.getMimeType().equals("image/png")
            && messageResponse.getCode() == 0
            && isExistPreview
            && isExistImage);
  }
Esempio n. 3
0
 /**
  * we check whether we need to remove the top-level folder while extracting sometimes (e.g.
  * github) the downloaded archive contains a top-level folder which needs to be removed
  */
 private Path findPluginRoot(Path dir) throws IOException {
   if (Files.exists(dir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES))) {
     return dir;
   } else {
     final Path[] topLevelFiles = FileSystemUtils.files(dir);
     if (topLevelFiles.length == 1 && Files.isDirectory(topLevelFiles[0])) {
       Path subdir = topLevelFiles[0];
       if (Files.exists(subdir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES))) {
         return subdir;
       }
     }
   }
   throw new RuntimeException(
       "Could not find plugin descriptor '" + PluginInfo.ES_PLUGIN_PROPERTIES + "' in plugin zip");
 }
 private static boolean exists(Path p) {
   Path base = p.getParent();
   String fileName = p.getFileName().toString();
   return Arrays.asList(extensions)
       .stream()
       .anyMatch(it -> Files.exists(base.resolve(fileName + it)));
 }
Esempio n. 5
0
  public void downloadAndExtract(String name, Terminal terminal) throws IOException {
    if (name == null && url == null) {
      throw new IllegalArgumentException("plugin name or url must be supplied with install.");
    }

    if (!Files.exists(environment.pluginsFile())) {
      terminal.println(
          "Plugins directory [%s] does not exist. Creating...", environment.pluginsFile());
      Files.createDirectory(environment.pluginsFile());
    }

    if (!Environment.isWritable(environment.pluginsFile())) {
      throw new IOException("plugin directory " + environment.pluginsFile() + " is read only");
    }

    PluginHandle pluginHandle;
    if (name != null) {
      pluginHandle = PluginHandle.parse(name);
      checkForForbiddenName(pluginHandle.name);
    } else {
      // if we have no name but url, use temporary name that will be overwritten later
      pluginHandle = new PluginHandle("temp_name" + new Random().nextInt(), null, null);
    }

    Path pluginFile = download(pluginHandle, terminal);
    extract(pluginHandle, terminal, pluginFile);
  }
Esempio n. 6
0
 /**
  * Checks if a given file exists and, if not, create it by copying a default template from
  * resources; used to create default conf files.
  *
  * @param file The path of the file that needs to exist
  * @param template The path of the template for the file
  */
 public static void ensureFileExists(final String file, final String template) {
   if (LAUNCHED_FROM_JAR && !Files.exists(Paths.get(file))) {
     if (Debug.on) printDebug("[Meta] " + file + " does not exist: creating a default one.");
     InputStream stream = Meta.class.getResourceAsStream(template);
     if (stream == null) {
       printDebug(
           "[ WARNING ] template for "
               + template
               + " not found. Won't create a default "
               + file
               + ".");
     } else {
       int readBytes;
       byte[] buffer = new byte[4096];
       try (OutputStream outStream = new FileOutputStream(new File(file))) {
         while ((readBytes = stream.read(buffer)) > 0) {
           outStream.write(buffer, 0, readBytes);
         }
         if (Debug.on)
           printDebug("[Meta] created default file " + file + " from " + template + ".");
       } catch (IOException e) {
         e.printStackTrace();
       } finally {
         try {
           stream.close();
         } catch (IOException ignore) {
         }
       }
     }
   } else {
     if (Meta.LAUNCHED_FROM_JAR && Debug.on) printDebug("[Meta] file exists: " + file + ".");
   }
 }
 private CompilationUnit getCompilationUnit(SourceFile sourceFile) {
   CompilationUnit compilationUnit;
   if (Files.exists(sourceFile.getOutputPath())) {
     try {
       compilationUnit = JavaParser.parse(sourceFile.getOutputPath().toFile());
     } catch (Throwable t) {
       throw new RuntimeException(t);
     }
   } else {
     compilationUnit = new CompilationUnit();
     compilationUnit.setComment(
         new LineComment(" Generated by GraphWalker (http://www.graphwalker.org)"));
     if (!"".equals(sourceFile.getPackageName())) {
       compilationUnit.setPackage(createPackageDeclaration(sourceFile));
     }
     compilationUnit.setImports(
         Arrays.asList(
             new ImportDeclaration(
                 new NameExpr("org.graphwalker.java.annotation.Model"), false, false),
             new ImportDeclaration(
                 new NameExpr("org.graphwalker.java.annotation.Vertex"), false, false),
             new ImportDeclaration(
                 new NameExpr("org.graphwalker.java.annotation.Edge"), false, false)));
     ASTHelper.addTypeDeclaration(compilationUnit, getInterfaceName(sourceFile));
   }
   return compilationUnit;
 }
Esempio n. 8
0
  public void removePlugin(String name, Terminal terminal) throws IOException {
    if (name == null) {
      throw new IllegalArgumentException("plugin name must be supplied with remove [name].");
    }
    PluginHandle pluginHandle = PluginHandle.parse(name);
    boolean removed = false;

    checkForForbiddenName(pluginHandle.name);
    Path pluginToDelete = pluginHandle.extractedDir(environment);
    if (Files.exists(pluginToDelete)) {
      terminal.println(VERBOSE, "Removing: %s", pluginToDelete);
      try {
        IOUtils.rm(pluginToDelete);
      } catch (IOException ex) {
        throw new IOException(
            "Unable to remove "
                + pluginHandle.name
                + ". Check file permissions on "
                + pluginToDelete.toString(),
            ex);
      }
      removed = true;
    }
    Path binLocation = pluginHandle.binDir(environment);
    if (Files.exists(binLocation)) {
      terminal.println(VERBOSE, "Removing: %s", binLocation);
      try {
        IOUtils.rm(binLocation);
      } catch (IOException ex) {
        throw new IOException(
            "Unable to remove "
                + pluginHandle.name
                + ". Check file permissions on "
                + binLocation.toString(),
            ex);
      }
      removed = true;
    }

    if (removed) {
      terminal.println("Removed %s", name);
    } else {
      terminal.println(
          "Plugin %s not found. Run \"plugin list\" to get list of installed plugins.", name);
    }
  }
Esempio n. 9
0
  public Path[] getListInstalledPlugins() throws IOException {
    if (!Files.exists(environment.pluginsFile())) {
      return new Path[0];
    }

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(environment.pluginsFile())) {
      return Iterators.toArray(stream.iterator(), Path.class);
    }
  }
 @Override
 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
     throws IOException {
   Path targetPath = toPath.resolve(fromPath.relativize(dir));
   if (!Files.exists(targetPath)) {
     targetPath.toFile().mkdirs();
     // Files.createDirectory(targetPath);
   }
   return FileVisitResult.CONTINUE;
 }
Esempio n. 11
0
  /**
   * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件
   *
   * @param task
   * @return "ok" if download success (else return errmessage);
   */
  static String download(DownloadTask task) {
    if (!openedStatus && show) openStatus();

    URL url;
    HttpURLConnection conn;
    try {
      url = new URL(task.getOrigin());
      conn = (HttpURLConnection) url.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setRequestProperty(
          "User-Agent",
          "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/"
              + Math.random());
      if ("www.imgjav.com".equals(url.getHost())) { // 该网站需要带cookie请求
        conn.setRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");
        // conn.setRequestProperty("Cookie", "__cfduid=d219ea333c7a9b5743b572697b631925a1446093229;
        // cf_clearance=6ae62d843f5d09acf393f9e4eb130d9366840c82-1446093303-28800");
        conn.setRequestProperty(
            "Cookie",
            "__cfduid=d6ee846b378bb7d5d173a05541f8a2b6a1446090548; cf_clearance=ea10e8db31f8b6ee51570b118dd89b7e616d7b62-1446099714-28800");
        conn.setRequestProperty("Host", "www.imgjav.com");
      }
      Path directory = Paths.get(task.getDest()).getParent();
      if (!Files.exists(directory)) Files.createDirectories(directory);
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }

    try (InputStream is = conn.getInputStream();
        BufferedInputStream in = new BufferedInputStream(is);
        FileOutputStream fos = new FileOutputStream(task.getDest());
        OutputStream out = new BufferedOutputStream(fos); ) {
      int length = conn.getContentLength();
      if (length < 1) throw new IOException("length<1");
      byte[] binary = new byte[length];
      byte[] buff = new byte[65536];
      int len;
      int index = 0;
      while ((len = in.read(buff)) != -1) {
        System.arraycopy(buff, 0, binary, index, len);
        index += len;
        allLen += len; // allLen有线程安全的问题 ,可能会不正确。无需精确数据,所以不同步了
        task.setReceivePercent(String.format("%.2f", ((float) index / length) * 100) + "%");
      }
      out.write(binary);
    } catch (IOException e) {
      e.printStackTrace();
      return e.getMessage();
    }
    return "ok";
  }
Esempio n. 12
0
  public static MapGraph createMapGraphFromFile(String filename) throws IOException {
    Path infilePath = Paths.get(filename);
    MapGraph newMap;

    if (Files.exists(infilePath)) {
      newMap = MapGraphCreator.createMapGraphFromText(Files.readAllLines(infilePath));

    } else {
      throw new IOException(String.format("The file {0} does not exist!", filename));
    }
    return newMap;
  }
Esempio n. 13
0
 public static boolean write(String fullyNamedPath, String content) {
   Path file = Paths.get(fullyNamedPath);
   try {
     Files.deleteIfExists(file);
     Files.write(
         file, Arrays.asList(content), Charset.forName("UTF-8"), StandardOpenOption.CREATE_NEW);
   } catch (IOException ioEx) {
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug("", ioEx);
     }
   }
   return Files.exists(file, LinkOption.NOFOLLOW_LINKS);
 }
Esempio n. 14
0
File: IO.java Progetto: nremond/boon
  public static Path createDirectory(Path dir) {

    try {

      if (!Files.exists(dir)) {
        return Files.createDirectory(dir);
      } else {
        return null;
      }

    } catch (Exception ex) {
      return Exceptions.handle(Path.class, ex);
    }
  }
Esempio n. 15
0
File: IO.java Progetto: nremond/boon
  public static Path createChildDirectory(Path parentDir, String childDir) {

    try {

      final Path newDir = path(parentDir.toString(), childDir);

      if (!Files.exists(newDir)) {
        Files.createDirectory(newDir);
      }

      return newDir;

    } catch (Exception ex) {
      return Exceptions.handle(Path.class, ex);
    }
  }
  private void loadSharedParameters(Path instanceDir, Map<String, SharedParameters> sharedParams)
      throws Exception {
    String instanceId = instanceDir.getFileName().toString();

    Path sharedParametersPath = Paths.get(instanceDir.toString(), SHARED_PARAMETERS_XML);
    if (Files.exists(sharedParametersPath)) {
      log.trace("Loading shared parameters from {}", sharedParametersPath);

      sharedParams.put(
          instanceId,
          loadParameters(
              sharedParametersPath, SharedParameters.class, this.sharedParameters.get(instanceId)));
    } else {
      log.trace(
          "Not loading shared parameters from {}, " + "file does not exist", sharedParametersPath);
    }
  }
Esempio n. 17
0
  private void initFrame() {
    this.setLocationRelativeTo(null);

    try {
      if (!Files.exists(saveName)) Files.createFile(saveName);

      laden(saveName);
    } catch (IOException e) {
      e.printStackTrace();
    }

    nUebSchr.setSelected(true);
    if (!quellListModel.isEmpty()) quellJList.setSelectedIndex(0);
    if (!zielListModel.isEmpty()) zielJList.setSelectedIndex(0);

    starteLaufwerkspruefung();
  }
  private void loadPrivateParameters(Path instanceDir, Map<String, PrivateParameters> privateParams)
      throws Exception {
    String instanceId = instanceDir.getFileName().toString();

    Path privateParametersPath = Paths.get(instanceDir.toString(), PRIVATE_PARAMETERS_XML);

    if (Files.exists(privateParametersPath)) {
      log.trace("Loading private parameters from {}", privateParametersPath);

      privateParams.put(
          instanceId,
          loadParameters(
              privateParametersPath,
              PrivateParameters.class,
              this.privateParameters.get(instanceId)));
    } else {
      log.trace(
          "Not loading private parameters from {}, " + "file does not exist",
          privateParametersPath);
    }
  }
Esempio n. 19
0
  @Override
  public void parse() throws IOException {
    if (conservedRegionPath == null
        || !Files.exists(conservedRegionPath)
        || !Files.isDirectory(conservedRegionPath)) {
      throw new IOException(
          "Conservation directory whether does not exist, is not a directory or cannot be read");
    }

    Map<String, Path> files = new HashMap<>();
    String chromosome;
    Set<String> chromosomes = new HashSet<>();

    // Reading all files in phastCons folder
    DirectoryStream<Path> directoryStream =
        Files.newDirectoryStream(conservedRegionPath.resolve("phastCons"));
    for (Path path : directoryStream) {
      chromosome = path.getFileName().toString().split("\\.")[0].replace("chr", "");
      chromosomes.add(chromosome);
      files.put(chromosome + "phastCons", path);
    }

    // Reading all files in phylop folder
    directoryStream = Files.newDirectoryStream(conservedRegionPath.resolve("phylop"));
    for (Path path : directoryStream) {
      chromosome = path.getFileName().toString().split("\\.")[0].replace("chr", "");
      chromosomes.add(chromosome);
      files.put(chromosome + "phylop", path);
    }

    /** Now we can iterate over all the chromosomes found and process the files */
    logger.debug("Chromosomes found {}", chromosomes.toString());
    for (String chr : chromosomes) {
      logger.debug("Processing chromosome {}, file {}", chr, files.get(chr + "phastCons"));
      processFile(files.get(chr + "phastCons"), "phastCons");

      logger.debug("Processing chromosome {}, file {}", chr, files.get(chr + "phylop"));
      processFile(files.get(chr + "phylop"), "phylop");
    }
  }
  /**
   * Compresses the specified directory to a zip file
   *
   * @param dir the directory to compress
   * @return the compressed file
   * @throws IOException
   */
  public static Path compress(Path dir) throws IOException {
    Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath());
    Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath());

    Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP);
    try (final ZipOutputStream out =
        new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result.toFile())))) {
      // out.setMethod(ZipOutputStream.DEFLATED);
      final byte data[] = new byte[BUFFER];
      // get a list of files from current directory
      Files.walkFileTree(
          dir,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs)
                throws IOException {
              final File file = path.toFile();

              // compress to relative directory, not absolute
              final String root = StringUtils.substringAfter(file.getParent(), dir.toString());
              try (final BufferedInputStream origin =
                  new BufferedInputStream(new FileInputStream(file), BUFFER)) {
                final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                  out.write(data, 0, count);
                }
              }

              return FileVisitResult.CONTINUE;
            }
          });
    }

    return result;
  }
Esempio n. 21
0
  @BeforeTest
  @AfterTest
  protected void cleanLogDir() throws IOException {
    Path directory = Paths.get("target/test-logs/");
    if (Files.exists(directory)) {
      Files.walkFileTree(
          directory,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
              Files.delete(file);
              return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                throws IOException {
              Files.delete(dir);
              return FileVisitResult.CONTINUE;
            }
          });
    }
  }
Esempio n. 22
0
    public void cnpStart(Path quellOrdner, Path zielOrdner) {
      try {
        DirectoryStream<Path> qstream = Files.newDirectoryStream(quellOrdner);
        for (Path qfile : qstream) {
          Path target = Paths.get(zielOrdner.toString() + "/" + qfile.getFileName());
          if (abbruch) break;
          if (Files.isDirectory(qfile) && !Files.exists(target)) {
            Files.createDirectory(target);
            textArea.append("Verzeichnis: " + qfile + " wurde erstellt" + System.lineSeparator());
            cnpStart(
                Paths.get(quellOrdner.toString() + "/" + qfile.getFileName()),
                Paths.get(zielOrdner.toString() + "/" + qfile.getFileName()));
          } else if (Files.isDirectory(qfile) && Files.exists(target)) {
            textArea.append("Wechsle in Verzeichnis: " + qfile + System.lineSeparator());
            cnpStart(
                Paths.get(quellOrdner.toString() + "/" + qfile.getFileName()),
                Paths.get(zielOrdner.toString() + "/" + qfile.getFileName()));
          }
          // Wenn die Datei noch nicht existiert
          else if (!Files.exists(target)) {
            textArea.append(
                "Datei " + target.toString() + " wurde erstellt" + System.lineSeparator());
            Files.copy(qfile, target, StandardCopyOption.REPLACE_EXISTING);

          }
          // Wenn Datei im Zielverzeichnis schon existiert
          else if (Files.exists(target)) {
            if (cAUeSchr) {
              textArea.append(
                  "Datei "
                      + target.toString()
                      + " wird absolut überschrieben"
                      + System.lineSeparator());
              Files.copy(qfile, target, StandardCopyOption.REPLACE_EXISTING);
            } else if (cUeSchr) {
              if (checkAlter(
                  Paths.get(quellOrdner.toString() + "/" + qfile.getFileName()),
                  Paths.get(zielOrdner.toString() + "/" + qfile.getFileName()))) {
                textArea.append(
                    target.toString()
                        + " wird mit neuer Datei überschrieben"
                        + System.lineSeparator());
                Files.copy(qfile, target, StandardCopyOption.REPLACE_EXISTING);
              } else {
                textArea.append(
                    target.toString() + " alte Datei bleibt bestehen" + System.lineSeparator());
              }
            } else
              textArea.append(
                  target.toString() + " alte Datei bleibt bestehen" + System.lineSeparator());
          }
          pbCounter++;
          progressBar.setValue(pbCounter);
        }

        qstream.close();
      } catch (IOException e) {

        e.printStackTrace();
      }
    }
Esempio n. 23
0
  private void copyBinDirectory(
      Path sourcePluginBinDirectory,
      Path destPluginBinDirectory,
      String pluginName,
      Terminal terminal)
      throws IOException {
    boolean canCopyFromSource =
        Files.exists(sourcePluginBinDirectory)
            && Files.isReadable(sourcePluginBinDirectory)
            && Files.isDirectory(sourcePluginBinDirectory);
    if (canCopyFromSource) {
      terminal.println(VERBOSE, "Found bin, moving to %s", destPluginBinDirectory.toAbsolutePath());
      if (Files.exists(destPluginBinDirectory)) {
        IOUtils.rm(destPluginBinDirectory);
      }
      try {
        Files.createDirectories(destPluginBinDirectory.getParent());
        FileSystemUtils.move(sourcePluginBinDirectory, destPluginBinDirectory);
      } catch (IOException e) {
        throw new IOException(
            "Could not move [" + sourcePluginBinDirectory + "] to [" + destPluginBinDirectory + "]",
            e);
      }
      if (Environment.getFileStore(destPluginBinDirectory)
          .supportsFileAttributeView(PosixFileAttributeView.class)) {
        final PosixFileAttributes parentDirAttributes =
            Files.getFileAttributeView(
                    destPluginBinDirectory.getParent(), PosixFileAttributeView.class)
                .readAttributes();
        // copy permissions from parent bin directory
        final Set<PosixFilePermission> filePermissions = new HashSet<>();
        for (PosixFilePermission posixFilePermission : parentDirAttributes.permissions()) {
          switch (posixFilePermission) {
            case OWNER_EXECUTE:
            case GROUP_EXECUTE:
            case OTHERS_EXECUTE:
              break;
            default:
              filePermissions.add(posixFilePermission);
          }
        }
        // add file execute permissions to existing perms, so execution will work.
        filePermissions.add(PosixFilePermission.OWNER_EXECUTE);
        filePermissions.add(PosixFilePermission.GROUP_EXECUTE);
        filePermissions.add(PosixFilePermission.OTHERS_EXECUTE);
        Files.walkFileTree(
            destPluginBinDirectory,
            new SimpleFileVisitor<Path>() {
              @Override
              public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                  throws IOException {
                if (attrs.isRegularFile()) {
                  setPosixFileAttributes(
                      file,
                      parentDirAttributes.owner(),
                      parentDirAttributes.group(),
                      filePermissions);
                }
                return FileVisitResult.CONTINUE;
              }

              @Override
              public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                  throws IOException {
                setPosixFileAttributes(
                    dir,
                    parentDirAttributes.owner(),
                    parentDirAttributes.group(),
                    parentDirAttributes.permissions());
                return FileVisitResult.CONTINUE;
              }
            });
      } else {
        terminal.println(
            VERBOSE, "Skipping posix permissions - filestore doesn't support posix permission");
      }
      terminal.println(
          VERBOSE, "Installed %s into %s", pluginName, destPluginBinDirectory.toAbsolutePath());
    }
  }
Esempio n. 24
0
  private void extract(PluginHandle pluginHandle, Terminal terminal, Path pluginFile)
      throws IOException {
    // unzip plugin to a staging temp dir, named for the plugin
    Path tmp = Files.createTempDirectory(environment.tmpFile(), null);
    Path root = tmp.resolve(pluginHandle.name);
    unzipPlugin(pluginFile, root);

    // find the actual root (in case its unzipped with extra directory wrapping)
    root = findPluginRoot(root);

    // read and validate the plugin descriptor
    PluginInfo info = PluginInfo.readFromProperties(root);
    terminal.println(VERBOSE, "%s", info);

    // update name in handle based on 'name' property found in descriptor file
    pluginHandle = new PluginHandle(info.getName(), pluginHandle.version, pluginHandle.user);
    final Path extractLocation = pluginHandle.extractedDir(environment);
    if (Files.exists(extractLocation)) {
      throw new IOException(
          "plugin directory "
              + extractLocation.toAbsolutePath()
              + " already exists. To update the plugin, uninstall it first using 'remove "
              + pluginHandle.name
              + "' command");
    }

    // check for jar hell before any copying
    if (info.isJvm()) {
      jarHellCheck(root, info.isIsolated());
    }

    // install plugin
    FileSystemUtils.copyDirectoryRecursively(root, extractLocation);
    terminal.println("Installed %s into %s", pluginHandle.name, extractLocation.toAbsolutePath());

    // cleanup
    tryToDeletePath(terminal, tmp, pluginFile);

    // take care of bin/ by moving and applying permissions if needed
    Path sourcePluginBinDirectory = extractLocation.resolve("bin");
    Path destPluginBinDirectory = pluginHandle.binDir(environment);
    boolean needToCopyBinDirectory = Files.exists(sourcePluginBinDirectory);
    if (needToCopyBinDirectory) {
      if (Files.exists(destPluginBinDirectory) && !Files.isDirectory(destPluginBinDirectory)) {
        tryToDeletePath(terminal, extractLocation);
        throw new IOException(
            "plugin bin directory " + destPluginBinDirectory + " is not a directory");
      }

      try {
        copyBinDirectory(
            sourcePluginBinDirectory, destPluginBinDirectory, pluginHandle.name, terminal);
      } catch (IOException e) {
        // rollback and remove potentially before installed leftovers
        terminal.printError(
            "Error copying bin directory [%s] to [%s], cleaning up, reason: %s",
            sourcePluginBinDirectory, destPluginBinDirectory, ExceptionsHelper.detailedMessage(e));
        tryToDeletePath(terminal, extractLocation, pluginHandle.binDir(environment));
        throw e;
      }
    }

    Path sourceConfigDirectory = extractLocation.resolve("config");
    Path destConfigDirectory = pluginHandle.configDir(environment);
    boolean needToCopyConfigDirectory = Files.exists(sourceConfigDirectory);
    if (needToCopyConfigDirectory) {
      if (Files.exists(destConfigDirectory) && !Files.isDirectory(destConfigDirectory)) {
        tryToDeletePath(terminal, extractLocation, destPluginBinDirectory);
        throw new IOException(
            "plugin config directory " + destConfigDirectory + " is not a directory");
      }

      try {
        terminal.println(
            VERBOSE, "Found config, moving to %s", destConfigDirectory.toAbsolutePath());
        moveFilesWithoutOverwriting(sourceConfigDirectory, destConfigDirectory, ".new");

        if (Environment.getFileStore(destConfigDirectory)
            .supportsFileAttributeView(PosixFileAttributeView.class)) {
          // We copy owner, group and permissions from the parent ES_CONFIG directory, assuming they
          // were properly set depending
          // on how es was installed in the first place: can be root:elasticsearch (750) if es was
          // installed from rpm/deb packages
          // or most likely elasticsearch:elasticsearch if installed from tar/zip. As for
          // permissions we don't rely on umask.
          final PosixFileAttributes parentDirAttributes =
              Files.getFileAttributeView(
                      destConfigDirectory.getParent(), PosixFileAttributeView.class)
                  .readAttributes();
          // for files though, we make sure not to copy execute permissions from the parent dir and
          // leave them untouched
          final Set<PosixFilePermission> baseFilePermissions = new HashSet<>();
          for (PosixFilePermission posixFilePermission : parentDirAttributes.permissions()) {
            switch (posixFilePermission) {
              case OWNER_EXECUTE:
              case GROUP_EXECUTE:
              case OTHERS_EXECUTE:
                break;
              default:
                baseFilePermissions.add(posixFilePermission);
            }
          }
          Files.walkFileTree(
              destConfigDirectory,
              new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                    throws IOException {
                  if (attrs.isRegularFile()) {
                    Set<PosixFilePermission> newFilePermissions =
                        new HashSet<>(baseFilePermissions);
                    Set<PosixFilePermission> currentFilePermissions =
                        Files.getPosixFilePermissions(file);
                    for (PosixFilePermission posixFilePermission : currentFilePermissions) {
                      switch (posixFilePermission) {
                        case OWNER_EXECUTE:
                        case GROUP_EXECUTE:
                        case OTHERS_EXECUTE:
                          newFilePermissions.add(posixFilePermission);
                      }
                    }
                    setPosixFileAttributes(
                        file,
                        parentDirAttributes.owner(),
                        parentDirAttributes.group(),
                        newFilePermissions);
                  }
                  return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                    throws IOException {
                  setPosixFileAttributes(
                      dir,
                      parentDirAttributes.owner(),
                      parentDirAttributes.group(),
                      parentDirAttributes.permissions());
                  return FileVisitResult.CONTINUE;
                }
              });
        } else {
          terminal.println(
              VERBOSE, "Skipping posix permissions - filestore doesn't support posix permission");
        }

        terminal.println(
            VERBOSE,
            "Installed %s into %s",
            pluginHandle.name,
            destConfigDirectory.toAbsolutePath());
      } catch (IOException e) {
        terminal.printError(
            "Error copying config directory [%s] to [%s], cleaning up, reason: %s",
            sourceConfigDirectory, destConfigDirectory, ExceptionsHelper.detailedMessage(e));
        tryToDeletePath(terminal, extractLocation, destPluginBinDirectory, destConfigDirectory);
        throw e;
      }
    }
  }
Esempio n. 25
0
  @Override
  public void run() {
    logger.debug("Register root " + startDir.toString());

    try {
      registerAll(startDir);
    } catch (IOException ex) {
      logger.error(ex.getMessage());
      return;
    }

    if (isInterrupted()) return;

    VOSync.debug("Sync local db with drive");

    DbPool.goSql(
        "Synching the local db with drive",
        "select NAME from FILES",
        new SqlWorker<Boolean>() {
          @Override
          public Boolean go(Connection conn, PreparedStatement stmt) throws SQLException {
            ResultSet resSet = stmt.executeQuery();
            while (resSet.next()) {
              try {
                String fileName = resSet.getString(1);
                Path filePath =
                    FileSystems.getDefault().getPath(startDir.toString(), fileName.substring(1));
                if (!filePath.toFile().exists()) {
                  logger.debug(
                      "Deleting file " + fileName + " existing in DB and not present on disk");
                  api.delete(fileName);
                  MetaHandler.delete(fileName);
                }
              } catch (DropboxException ex) {
              }
            }
            resSet.close();
            return true;
          }
        });

    if (isInterrupted()) return;

    VOSync.debug("Sync storage");

    syncStorage();

    logger.debug("Start watching");

    while (!isInterrupted()) {
      WatchKey key;
      try {
        key = watcher.take();
      } catch (InterruptedException x) {
        return;
      }

      Path dir = keys.get(key);
      if (dir == null) {
        System.err.println("WatchKey " + key.toString() + " not recognized!");
        continue;
      }

      for (WatchEvent<?> event : key.pollEvents()) {
        Kind<?> kind = event.kind();

        // TBD - provide example of how OVERFLOW event is handled
        if (kind == OVERFLOW) {
          continue;
        }

        // Context for directory entry event is the file name of entry
        WatchEvent<Path> ev = cast(event);
        Path name = ev.context();
        Path child = dir.resolve(name);
        Path relativeDir = startDir.relativize(child);
        String fileRelPath = "/" + fixPath(relativeDir.toString());

        // print out event
        logger.debug(event.kind().name() + ":" + child + " " + name + " " + key);

        try {
          if (Files.exists(child, new LinkOption[] {}) && Files.isHidden(child)) {
            logger.error(
                "Skipping hidden file " + child.getFileName()); // skip OS generated catalog files
          } else {
            if (event.kind() == ENTRY_CREATE) {
              if (Files.isRegularFile(child, NOFOLLOW_LINKS)) { // file modified
                uploadFile(fileRelPath, child);
              } else if (Files.isDirectory(child, NOFOLLOW_LINKS)) { // directory contents changed
                registerAll(child);
              }
            } else if (event.kind() == ENTRY_DELETE) {
              logger.debug("Deleting " + fileRelPath);
              api.delete(fileRelPath);
              MetaHandler.delete(fileRelPath);
              logger.debug("Deleted!");
            } else if (event.kind() == ENTRY_MODIFY) {
              if (Files.isRegularFile(child, NOFOLLOW_LINKS)) { // file modified
                uploadFile(fileRelPath, child);
              } else if (Files.isDirectory(child, NOFOLLOW_LINKS)) { // directory contents changed
                // logger.debug("Renewing dir: "+relativeDir.toString());
                // TODO update folder date
                // MetaHandler.setFile(fileRelPath, child, rev);
              }
            }
          }
        } catch (IOException ex) {
          ex.printStackTrace();
          logger.error(ex.getMessage());
        } catch (DropboxException ex) {
          ex.printStackTrace();
          logger.error(ex.getMessage());
        }
      }

      boolean valid = key.reset();

      if (!valid) keys.remove(key);
    }
  }