Пример #1
0
  public static void main(String[] args) throws Exception {
    Path dir = Paths.get(args[0]);

    Files.walkFileTree(
        dir,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            check(dir);
            if (skip(dir)) return FileVisitResult.SKIP_SIBLINGS;
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            check(file);
            if (skip(file)) return FileVisitResult.SKIP_SIBLINGS;
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException x) {
            if (x != null) throw new RuntimeException(x);
            check(dir);
            return FileVisitResult.CONTINUE;
          }
        });
  }
  private void compareBinaryFolder(String path, boolean res) throws BrutException, IOException {

    String tmp = "";
    if (res) {
      tmp = File.separatorChar + "res" + File.separatorChar;
    }

    Files.walkFileTree(
        Paths.get(sTestOrigDir.toPath() + tmp + path),
        new SimpleFileVisitor<Path>() {

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {

            // hacky fix - load test by changing name of control
            File control = file.toFile();
            File test = new File(file.toString().replace("testapp-orig", "testapp-new"));

            if (test.isFile()) {
              if (control.hashCode() != test.hashCode()) {
                sResult = false;
                return FileVisitResult.TERMINATE;
              }
            } else {
              sResult = false;
              return FileVisitResult.TERMINATE;
            }
            return FileVisitResult.CONTINUE;
          }
        });
  }
  @After
  public void tearDown() throws Exception {

    // Удаляем тестовый фаил
    Files.delete(Paths.get(pathTempFile));

    // Удаляем тестовые папки и файлы
    Files.walkFileTree(
        Paths.get(storagePath),
        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;
          }
        });
  }
Пример #4
0
 private List<Path> getPathsFromFile(String file, List<Path> filesToCombine) throws IOException {
   Path filePath = Paths.get(file);
   File fileLister = filePath.toFile();
   BufferedReader br = new BufferedReader(new FileReader(fileLister));
   String line;
   PathMatcher matcher;
   Finder finder = new Finder();
   String parentFolder;
   Path filePathToCombine;
   while ((line = br.readLine()) != null) {
     if (line.isEmpty()) {
       continue;
     }
     parentFolder = "";
     filePathToCombine = Paths.get(line);
     if (filePathToCombine.getParent() != null) {
       parentFolder = filePathToCombine.getParent().toString();
     }
     matcher =
         FileSystems.getDefault()
             .getPathMatcher("glob:" + filePathToCombine.getFileName().toString());
     finder.setMatcher(matcher);
     Files.walkFileTree(
         Paths.get(fileLister.getAbsoluteFile().getParent() + parentFolder), finder);
   }
   filesToCombine.addAll(finder.getMatchPath());
   return filesToCombine;
 }
Пример #5
0
  /** Register the given directory, and all its sub-directories, with the WatchService.f */
  private void registerAll(final Path start) throws IOException {
    Files.walkFileTree(
        start,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            logger.debug("postVisitDir " + dir.toString());
            WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            logger.debug("Adding key: " + key.toString() + " " + dir);
            keys.put(key, dir);
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult preVisitDirectory(Path fullDir, BasicFileAttributes attrs)
              throws IOException {
            logger.debug("preVisitDirectory " + fullDir.toString());
            String dir = "/" + fixPath(startDir.relativize(fullDir).toString());
            if (!MetaHandler.isStored(dir) || MetaHandler.isModified(dir, fullDir.toFile())) {
              try {
                Entry folderEntry = null;
                try {
                  logger.debug("Creating folder: " + dir);
                  folderEntry = api.createFolder(dir);
                } catch (DropboxServerException ex) {
                  if (ex.error == DropboxServerException._403_FORBIDDEN) {
                    folderEntry = api.metadata(dir, 0, null, false, null);
                  } else {
                    logger.error(ex.getMessage());
                  }
                }

                MetaHandler.setFile(dir, fullDir.toFile(), folderEntry.rev);
              } catch (DropboxException ex) {
                ex.printStackTrace();
              }
            }

            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path fullPath, BasicFileAttributes attrs)
              throws IOException {
            logger.debug("visitFile " + fullPath.toString());
            String file = "/" + fixPath(startDir.relativize(fullPath).toString());
            if (!MetaHandler.isStored(file) || MetaHandler.isModified(file, fullPath.toFile())) {
              logger.debug("Not stored or updated: " + file);
              try {
                uploadFile(file, fullPath);
              } catch (Exception ex) {
                ex.printStackTrace();
              }
            }
            return FileVisitResult.CONTINUE;
          }
        });
  }
Пример #6
0
  public static void main(String[] args) throws Exception {
    boolean followLinks = false;
    boolean printCycles = false;
    int i = 0;
    while (i < (args.length - 1)) {
      switch (args[i]) {
        case "-follow":
          followLinks = true;
          break;
        case "-printCycles":
          printCycles = true;
          break;
        default:
          throw new RuntimeException(args[i] + " not recognized");
      }
      i++;
    }
    Path dir = Paths.get(args[i]);

    Set<FileVisitOption> options = new HashSet<FileVisitOption>();
    if (followLinks) options.add(FileVisitOption.FOLLOW_LINKS);

    final boolean follow = followLinks;
    final boolean reportCycles = printCycles;
    Files.walkFileTree(
        dir,
        options,
        Integer.MAX_VALUE,
        new FileVisitor<Path>() {
          @Override
          public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            System.out.println(dir);
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            System.out.println(file);
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc != null) throw exc;
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            if (follow && (exc instanceof FileSystemLoopException)) {
              if (reportCycles) System.out.println(file);
              return FileVisitResult.CONTINUE;
            } else {
              throw exc;
            }
          }
        });
  }
Пример #7
0
 public int countFiles(String root) {
   CountFile fileProcessor = new CountFile();
   try {
     Files.walkFileTree(Paths.get(root), fileProcessor);
   } catch (IOException e) {
     e.printStackTrace();
   }
   return fileProcessor.getTotal();
 }
Пример #8
0
 public static void main(String[] args) {
   // Path startingDir = FileSystems.getDefault().getPath("/home/neko/NetBeansProjects/Default");
   Path startingDir = FileSystems.getDefault().getPath(args[0]);
   ex_4_3_13_FileTree pf = new ex_4_3_13_FileTree();
   try {
     Files.walkFileTree(startingDir, pf);
   } catch (IOException ex) {
     System.out.println(ex);
   }
 }
  /** Prints the structure for a given directory or file */
  public static void main(String[] args) throws IOException {
    if (args.length != 1) {
      printUsage();
      System.exit(-1);
    }

    String pathName = args[0];
    Path startingDir = Paths.get(pathName);

    PrintDirectoryStructureWithVisitor pdsv = new PrintDirectoryStructureWithVisitor();
    Files.walkFileTree(startingDir, pdsv);
  }
Пример #10
0
 /** Регистрация директории для WatchService. */
 private void registerAll(final Path start) throws IOException {
   // Регистрируем директорию
   Files.walkFileTree(
       start,
       new SimpleFileVisitor<Path>() {
         @Override
         public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
             throws IOException {
           register(dir);
           return FileVisitResult.CONTINUE;
         }
       });
 }
Пример #11
0
 public static void load(Collection<FileDesc> files, Path root, int blocSize, Pattern pattern)
     throws IOException {
   root = root.toAbsolutePath().normalize();
   Visitor visitor = new Visitor(root, blocSize, pattern);
   Files.walkFileTree(root, visitor);
   for (Future<FileDesc> future : visitor.futures()) {
     try {
       files.add(future.get());
     } catch (Exception e) {
       log.error("", e);
     }
   }
 }
 private void registerAll(final Path start) throws IOException {
   // register directory and sub-directories
   System.out.println("Traversing all directories..");
   Files.walkFileTree(
       start,
       new SimpleFileVisitor<Path>() {
         @Override
         public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
             throws IOException {
           register(dir);
           return FileVisitResult.CONTINUE;
         }
       });
   System.out.println("Done.");
 }
 /** Register the given directory, and all its sub-directories, with the WatchService. */
 public void watchDirectory(final Path start) {
   // register directory and sub-directories
   try {
     Files.walkFileTree(
         start,
         new SimpleFileVisitor<Path>() {
           @Override
           public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
               throws IOException {
             register(dir);
             return FileVisitResult.CONTINUE;
           }
         });
   } catch (IOException e) {
     log.error("Failed to register the directory '{}'", start);
   }
 }
Пример #14
0
  public void backup(String wildCardPath) {
    try {
      if (wildCardPath.contains("*") || wildCardPath.contains("?")) {
        // Extract the path so we know where to start and get the name which contains the
        // wildcard so we know what to store:

        File wildCardFile = new File(wildCardPath);
        String parentDirectory = wildCardFile.getParent();
        String wildCardPattern = wildCardFile.getName();
        _pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + wildCardPattern);
        Files.walkFileTree(Paths.get(parentDirectory), this);
      } else storeFile(new File(wildCardPath));
    } catch (FileNotFoundException exception) {
      Console.printError("File/Directory not found: " + wildCardPath);
    } catch (IOException exception) {
      Console.printError(
          "File/Directory '%s' not stored because: '%s'", wildCardPath, exception.getMessage());
    }
  }
Пример #15
0
  public void clearDirectories() {
    try {
      SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
      final String sessionDate = df.format(new Date());

      Path startPath = Paths.get(ABS_OUTPUT_DIR);
      logger.info("Начинаем очистку каталога " + ABS_OUTPUT_DIR);
      Files.walkFileTree(
          startPath,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
              logger.info("Обнаружили файл" + file);
              if (file.toString().endsWith(".zip")) {
                Path fileTo =
                    Paths.get(ABS_ARCH_OUT_DIR + "/" + sessionDate + "-" + file.getFileName());
                Files.move(file, fileTo);
                logger.info("Скопирован файл " + file + " -> " + fileTo);
              } else {
                logger.info("Удаляем файл " + file);
                Files.delete(file);
              }
              return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
              logger.info("Обнаружили каталог" + dir);
              if (!dir.toString().equals(ABS_OUTPUT_DIR)) {
                logger.info("Удаляем каталог " + dir);
                Files.delete(dir);
              }
              return FileVisitResult.CONTINUE;
            }
          });
      logger.info("Закончили очистку каталога " + ABS_OUTPUT_DIR);
    } catch (Exception e) {
      logger.error(e.getMessage());
      e.printStackTrace();
    }
  }
  private void deleteDirectory(Path path) throws IOException {
    Files.walkFileTree(
        path,
        new SimpleFileVisitor<Path>() {

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc == null) {
              Files.delete(dir);
              return FileVisitResult.CONTINUE;
            } else {
              throw exc;
            }
          }

          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
          }
        });
  }
Пример #17
0
  /**
   * 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;
  }
  public void unpack(String artifact, File destination) {
    File archiveFile = getAsFile(artifact);

    if (archiveFile == null || !archiveFile.exists()) return;
    if (archiveFile.isDirectory()) {
      try {
        Files.walkFileTree(archiveFile.toPath(), new CopyDirVisitor(archiveFile, destination));
      } catch (Exception e) {
        OutputBouble.reportError(e);
      }

    } else if (archiveFile.getName().endsWith("jar")) {
      destination.mkdirs();
      try {
        java.util.jar.JarFile jar = new java.util.jar.JarFile(archiveFile);
        java.util.Enumeration jarEnum = jar.entries();
        while (jarEnum.hasMoreElements()) {
          java.util.jar.JarEntry file = (java.util.jar.JarEntry) jarEnum.nextElement();
          java.io.File f = new java.io.File(destination + java.io.File.separator + file.getName());
          if (file.isDirectory()) { // if its a directory, create it
            f.mkdir();
            continue;
          }
          java.io.InputStream is = jar.getInputStream(file); // get the input stream
          java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
          while (is.available() > 0) { // write contents of 'is' to 'fos'
            fos.write(is.read());
          }
          fos.close();
          is.close();
        }
        jar.close();
      } catch (Exception e) {

      }
    }
  }
Пример #19
0
  public static void generate(final Path input, final Path output) {
    final SimpleCache cache = new SimpleCache(output);
    try {
      Files.walkFileTree(
          input,
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
                throws IOException {
              if (!cache.contains(file) || isModified(file)) {
                try {
                  SourceFile sourceFile = new SourceFile(file, input, output);
                  ContextFactory factory = ContextFactoryScanner.get(sourceFile.getInputPath());
                  write(factory, sourceFile);
                  cache.add(file, new CacheEntry(file.toFile().lastModified(), true));
                } catch (Throwable t) {
                  cache.add(file, new CacheEntry(file.toFile().lastModified(), false));
                }
              }
              return FileVisitResult.CONTINUE;
            }

            private boolean isModified(Path file) throws IOException {
              return !Files.getLastModifiedTime(file).equals(cache.get(file).getLastModifiedTime());
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
              cache.add(file, new CacheEntry(file.toFile().lastModified(), false));
              return FileVisitResult.CONTINUE;
            }
          });
    } catch (IOException e) {
      throw new CodeGeneratorException(e);
    }
  }
Пример #20
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;
            }
          });
    }
  }
 /**
  * Applies the given function to all files belonging to the configuration directory.
  *
  * @param consumer the function instance that should be applied to
  * @throws Exception if an error occurs
  */
 protected synchronized void eachFile(final Consumer<Path> consumer) throws Exception {
   Files.walkFileTree(path, new Walker(consumer));
 }
Пример #22
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());
    }
  }
Пример #23
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;
      }
    }
  }