public boolean archiveDirectory()

in genie-common-internal/src/main/java/com/netflix/genie/common/internal/services/impl/FileSystemJobArchiverImpl.java [54:93]


    public boolean archiveDirectory(
        final Path directory,
        final List<File> filesList,
        final URI target
    ) throws JobArchiveException {
        if (!target.getScheme().equalsIgnoreCase(FILE_SCHEME)) {
            return false;
        }

        final Path targetDirectoryPath = Paths.get(target.getPath());

        // If the source doesn't exist or isn't a directory, then throw an exception
        if (!Files.exists(directory) || !Files.isDirectory(directory)) {
            throw new JobArchiveException(directory + " doesn't exist or isn't a directory. Unable to copy");
        }

        // If the destination base directory exists and isn't a directory, then throw an exception
        if (Files.exists(targetDirectoryPath) && !Files.isDirectory(targetDirectoryPath)) {
            throw new JobArchiveException(targetDirectoryPath + " exist and isn't a directory. Unable to copy");
        }

        for (final File file : filesList) {
            final Path sourceFilePath = file.toPath();
            final Path sourceFileRelativePath = directory.relativize(sourceFilePath);
            final Path destinationFilePath = targetDirectoryPath.resolve(sourceFileRelativePath);
            try {
                final Path parentDirectory = destinationFilePath.getParent();
                if (parentDirectory != null) {
                    log.info("Creating parent directory for {}", destinationFilePath);
                    Files.createDirectories(parentDirectory);
                }
                log.info("Copying {} to {}", sourceFilePath, destinationFilePath);
                Files.copy(sourceFilePath, destinationFilePath, COPY_OPTIONS);
            } catch (IOException e) {
                log.warn("Failed to archive file {} to {}: {}", sourceFilePath, destinationFilePath, e.getMessage(), e);
            }
        }

        return true;
    }