Exemple #1
0
  void createArchive(Path projectPath, Path output, Config overrideParams) throws IOException {
    out.println("Creating " + output + "...");

    ProjectArchive project =
        projectLoader.load(projectPath, WorkflowResourceMatcher.defaultMatcher(), overrideParams);
    ArchiveMetadata meta = project.getArchiveMetadata();

    try (TarArchiveOutputStream tar =
        new TarArchiveOutputStream(new GzipCompressorOutputStream(Files.newOutputStream(output)))) {
      // default mode for file names longer than 100 bytes is throwing an exception (LONGFILE_ERROR)
      tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

      project.listFiles(
          (resourceName, absPath) -> {
            if (!Files.isDirectory(absPath)) {
              out.println("  Archiving " + resourceName);

              TarArchiveEntry e = buildTarArchiveEntry(project, absPath, resourceName);
              tar.putArchiveEntry(e);
              if (e.isSymbolicLink()) {
                out.println("    symlink -> " + e.getLinkName());
              } else {
                try (InputStream in = Files.newInputStream(absPath)) {
                  ByteStreams.copy(in, tar);
                }
                tar.closeArchiveEntry();
              }
            }
          });

      // create .digdag.dig
      // TODO set default time zone if not set?
      byte[] metaBody = yamlMapper.toYaml(meta).getBytes(StandardCharsets.UTF_8);
      TarArchiveEntry metaEntry = new TarArchiveEntry(ArchiveMetadata.FILE_NAME);
      metaEntry.setSize(metaBody.length);
      metaEntry.setModTime(new Date());
      tar.putArchiveEntry(metaEntry);
      tar.write(metaBody);
      tar.closeArchiveEntry();
    }

    out.println("Workflows:");
    for (WorkflowDefinition workflow : meta.getWorkflowList().get()) {
      out.println("  " + workflow.getName());
    }
    out.println("");
  }
Exemple #2
0
  /**
   * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the
   * <i>skipTopFolder</i> is set, then don't assume that the archive contains one single folder and
   * unpack the content of that folder, not including the folder itself.
   *
   * @param source The input source. Must be in <i>TAR</i> format.
   * @param targetFolder The destination folder for the unpack. Not used when a <tt>fileCatcher</tt>
   *     is provided
   * @param skipTopFolder Set to <code>true</code> to unpack beneath the top folder of the archive.
   *     The archive must consist of one single folder and nothing else in order for this to work.
   * @param fileCatcher Used when specific files should be picked from the archive without writing
   *     them to disk. Can be <tt>null</tt>.
   * @throws IOException
   */
  public static void unpack(
      InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher)
      throws IOException {
    String topFolderName = null;
    Map<File, Map<Integer, List<String>>> chmodMap =
        new HashMap<File, Map<Integer, List<String>>>();
    TarArchiveInputStream in = new TarArchiveInputStream(source);
    try {
      TarArchiveEntry te = in.getNextTarEntry();
      if (te == null) {
        throw new IOException("No entry in the tar file");
      }
      do {
        if (te.isGlobalPaxHeader()) continue;

        String name = te.getName();
        if (name.startsWith("./._"))
          // MacOS specific extended attributes addition. Just skip it
          continue;

        if (skipTopFolder) {
          int firstSlash = name.indexOf('/');
          if (firstSlash < 0) throw new IOException("Archive doesn't contain one single folder");

          String tfName = name.substring(0, firstSlash);
          if (topFolderName == null) topFolderName = tfName;
          else if (!tfName.equals(topFolderName))
            throw new IOException("Archive doesn't contain one single folder");
          name = name.substring(firstSlash + 1);
        }
        if (name.length() == 0) continue;

        String linkName = te.getLinkName();
        if (linkName != null) {
          if (linkName.trim().equals("")) linkName = null;
        }

        if (fileCatcher != null) {
          if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) {
            if (fileCatcher.catchData(name, in))
              // We're done here
              return;
          }
          continue;
        }

        File outFile = new File(targetFolder, name);
        if (linkName != null) {
          if (!OsUtil.link(targetFolder, name, te.getLinkName()))
            throw new IOException(
                "Archive contains links but they are not supported on this platform");
        } else {
          if (te.isDirectory()) {
            outFile.mkdirs();
          } else {
            outFile.getParentFile().mkdirs();
            OutputStream target = new FileOutputStream(outFile);
            StreamUtil.copy(in, target);
            target.close();
            outFile.setLastModified(te.getModTime().getTime());
          }
          registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name);
        }
      } while ((te = in.getNextTarEntry()) != null);
    } finally {
      StreamUtil.close(in);
    }
    chmod(chmodMap);
  }