Пример #1
0
  /**
   * Creates a package descriptor from the specified control file and adds the <tt>Date</tt>,
   * <tt>Distribution</tt> and <tt>Urgency</tt> fields if missing. The <tt>Installed-Size</tt> field
   * is also initialized to the actual size of the package. The <tt>Maintainer</tt> field is
   * overridden by the <tt>DEBEMAIL</tt> and <tt>DEBFULLNAME</tt> environment variables if defined.
   *
   * @param file the control file
   * @param pDataSize the size of the installed package
   */
  private PackageDescriptor createPackageDescriptor(File file, BigInteger pDataSize)
      throws IOException, ParseException {
    PackageDescriptor packageDescriptor =
        new PackageDescriptor(new FileInputStream(file), resolver);

    if (packageDescriptor.get("Date") == null) {
      // Mon, 26 Mar 2007 11:44:04 +0200 (RFC 2822)
      SimpleDateFormat fmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
      packageDescriptor.set("Date", fmt.format(new Date()));
    }

    if (packageDescriptor.get("Distribution") == null) {
      packageDescriptor.set("Distribution", "unknown");
    }

    if (packageDescriptor.get("Urgency") == null) {
      packageDescriptor.set("Urgency", "low");
    }

    packageDescriptor.set("Installed-Size", pDataSize.divide(BigInteger.valueOf(1024)).toString());

    // override the Version if the DEBVERSION environment variable is defined
    final String debVersion = System.getenv("DEBVERSION");
    if (debVersion != null) {
      packageDescriptor.set("Version", debVersion);
      console.info("Using version'" + debVersion + "' from the environment variables.");
    }

    // override the Maintainer field if the DEBFULLNAME and DEBEMAIL environment variables are
    // defined
    final String debFullName = System.getenv("DEBFULLNAME");
    final String debEmail = System.getenv("DEBEMAIL");

    if (debFullName != null && debEmail != null) {
      final String maintainer = debFullName + " <" + debEmail + ">";
      packageDescriptor.set("Maintainer", maintainer);
      console.info("Using maintainer '" + maintainer + "' from the environment variables.");
    }

    return packageDescriptor;
  }
Пример #2
0
  /**
   * Build control archive of the deb
   *
   * @param pControlFiles
   * @param pDataSize
   * @param pChecksums
   * @param pOutput
   * @return
   * @throws FileNotFoundException
   * @throws IOException
   * @throws ParseException
   */
  private PackageDescriptor buildControl(
      final File[] pControlFiles,
      final BigInteger pDataSize,
      final StringBuffer pChecksums,
      final File pOutput)
      throws IOException, ParseException {

    PackageDescriptor packageDescriptor = null;

    final TarOutputStream outputStream =
        new TarOutputStream(new GZIPOutputStream(new FileOutputStream(pOutput)));
    outputStream.setLongFileMode(TarOutputStream.LONGFILE_GNU);

    for (int i = 0; i < pControlFiles.length; i++) {
      final File file = pControlFiles[i];

      if (file.isDirectory()) {
        continue;
      }

      final TarEntry entry = new TarEntry(file);

      final String name = file.getName();

      entry.setName("./" + name);
      entry.setNames("root", "root");
      entry.setMode(PermMapper.toMode("755"));

      if ("control".equals(name)) {
        packageDescriptor = new PackageDescriptor(new FileInputStream(file), resolver);

        if (packageDescriptor.get("Date") == null) {
          SimpleDateFormat fmt =
              new SimpleDateFormat(
                  "EEE, d MMM yyyy HH:mm:ss Z",
                  Locale.ENGLISH); // Mon, 26 Mar 2007 11:44:04 +0200 (RFC 2822)
          // FIXME Is this field allowed in package descriptors ?
          packageDescriptor.set("Date", fmt.format(new Date()));
        }

        if (packageDescriptor.get("Distribution") == null) {
          packageDescriptor.set("Distribution", "unknown");
        }

        if (packageDescriptor.get("Urgency") == null) {
          packageDescriptor.set("Urgency", "low");
        }

        final String debFullName = System.getenv("DEBFULLNAME");
        final String debEmail = System.getenv("DEBEMAIL");

        if (debFullName != null && debEmail != null) {
          packageDescriptor.set("Maintainer", debFullName + " <" + debEmail + ">");
          console.println("Using maintainer from the environment variables.");
        }

        continue;
      }

      final InputStream inputStream = new FileInputStream(file);

      outputStream.putNextEntry(entry);

      Utils.copy(inputStream, outputStream);

      outputStream.closeEntry();

      inputStream.close();
    }

    if (packageDescriptor == null) {
      throw new FileNotFoundException("No control file in " + Arrays.toString(pControlFiles));
    }

    packageDescriptor.set("Installed-Size", pDataSize.divide(BigInteger.valueOf(1024)).toString());

    addEntry("control", packageDescriptor.toString(), outputStream);

    addEntry("md5sums", pChecksums.toString(), outputStream);

    outputStream.close();

    return packageDescriptor;
  }
Пример #3
0
  /**
   * Create the debian archive with from the provided control files and data producers.
   *
   * @param pControlFiles
   * @param pData
   * @param pOutput
   * @param compression the compression method used for the data file (gzip, bzip2 or anything else
   *     for no compression)
   * @return PackageDescriptor
   * @throws PackagingException
   */
  public PackageDescriptor createDeb(
      final File[] pControlFiles,
      final DataProducer[] pData,
      final File pOutput,
      String compression)
      throws PackagingException, InvalidDescriptorException {

    File tempData = null;
    File tempControl = null;

    try {
      tempData = File.createTempFile("deb", "data");
      tempControl = File.createTempFile("deb", "control");

      console.println("Building data");
      final StringBuffer md5s = new StringBuffer();
      final BigInteger size = buildData(pData, tempData, md5s, compression);

      console.println("Building control");
      final PackageDescriptor packageDescriptor =
          buildControl(pControlFiles, size, md5s, tempControl);

      if (!packageDescriptor.isValid()) {
        throw new InvalidDescriptorException(packageDescriptor);
      }

      pOutput.getParentFile().mkdirs();
      final InformationOutputStream md5output =
          new InformationOutputStream(
              new FileOutputStream(pOutput), MessageDigest.getInstance("MD5"));
      // Add chain of filters in order to calculate sha1 and sha256 for 1.8 format
      final InformationOutputStream sha1output =
          new InformationOutputStream(md5output, MessageDigest.getInstance("SHA1"));
      final InformationOutputStream sha256output =
          new InformationOutputStream(sha1output, MessageDigest.getInstance("SHA-256"));

      final ArArchiveOutputStream ar = new ArArchiveOutputStream(sha256output);

      addTo(ar, "debian-binary", "2.0\n");
      addTo(ar, "control.tar.gz", tempControl);
      addTo(ar, "data.tar" + getExtension(compression), tempData);

      ar.close();

      // intermediate values
      packageDescriptor.set("MD5", md5output.getHexDigest());
      packageDescriptor.set("SHA1", sha1output.getHexDigest());
      packageDescriptor.set("SHA256", sha256output.getHexDigest());
      packageDescriptor.set("Size", "" + md5output.getSize());
      packageDescriptor.set("File", pOutput.getName());

      return packageDescriptor;

    } catch (InvalidDescriptorException e) {
      throw e;
    } catch (Exception e) {
      throw new PackagingException("Could not create deb package", e);
    } finally {
      if (tempData != null) {
        if (!tempData.delete()) {
          throw new PackagingException("Could not delete " + tempData);
        }
      }
      if (tempControl != null) {
        if (!tempControl.delete()) {
          throw new PackagingException("Could not delete " + tempControl);
        }
      }
    }
  }
Пример #4
0
  /**
   * Build control archive of the deb
   *
   * @param pControlFiles
   * @param pDataSize
   * @param pChecksums
   * @param pOutput
   * @return
   * @throws FileNotFoundException
   * @throws IOException
   * @throws ParseException
   */
  private PackageDescriptor buildControl(
      final File[] pControlFiles,
      final BigInteger pDataSize,
      final StringBuilder pChecksums,
      final File pOutput)
      throws IOException, ParseException {

    final File dir = pOutput.getParentFile();
    if (dir != null && (!dir.exists() || !dir.isDirectory())) {
      throw new IOException("Cannot write control file at '" + pOutput.getAbsolutePath() + "'");
    }

    final TarArchiveOutputStream outputStream =
        new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(pOutput)));
    outputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    List<FilteredConfigurationFile> configurationFiles = new ArrayList<FilteredConfigurationFile>();

    // create a descriptor out of the "control" file, copy all other files, ignore directories
    PackageDescriptor packageDescriptor = null;
    for (File file : pControlFiles) {
      if (file.isDirectory()) {
        // warn about the misplaced directory, except for directories ignored by default (.svn, cvs,
        // etc)
        if (!isDefaultExcludes(file)) {
          console.info(
              "Found directory '"
                  + file
                  + "' in the control directory. Maybe you are pointing to wrong dir?");
        }
        continue;
      }

      if (CONFIGURATION_FILENAMES.contains(file.getName())
          || MAINTAINER_SCRIPTS.contains(file.getName())) {
        FilteredConfigurationFile configurationFile =
            new FilteredConfigurationFile(file.getName(), new FileInputStream(file), resolver);
        configurationFiles.add(configurationFile);

      } else if ("control".equals(file.getName())) {
        packageDescriptor = createPackageDescriptor(file, pDataSize);

      } else {
        // initialize the information stream to guess the type of the file
        InformationInputStream infoStream = new InformationInputStream(new FileInputStream(file));
        Utils.copy(infoStream, NullOutputStream.NULL_OUTPUT_STREAM);
        infoStream.close();

        // fix line endings for shell scripts
        InputStream in = new FileInputStream(file);
        if (infoStream.isShell() && !infoStream.hasUnixLineEndings()) {
          byte[] buf = Utils.toUnixLineEndings(in);
          in = new ByteArrayInputStream(buf);
        }

        addControlEntry(file.getName(), IOUtils.toString(in), outputStream);

        in.close();
      }
    }

    if (packageDescriptor == null) {
      throw new FileNotFoundException(
          "No 'control' file found in " + Arrays.toString(pControlFiles));
    }

    for (FilteredConfigurationFile configurationFile : configurationFiles) {
      addControlEntry(configurationFile.getName(), configurationFile.toString(), outputStream);
    }
    addControlEntry("control", packageDescriptor.toString(), outputStream);
    addControlEntry("md5sums", pChecksums.toString(), outputStream);

    outputStream.close();

    return packageDescriptor;
  }
Пример #5
0
  /**
   * Create the debian archive with from the provided control files and data producers.
   *
   * @param pControlFiles
   * @param pData
   * @param pOutput
   * @param compression the compression method used for the data file
   * @return PackageDescriptor
   * @throws PackagingException
   */
  public PackageDescriptor createDeb(
      final File[] pControlFiles,
      final DataProducer[] pData,
      final File pOutput,
      Compression compression)
      throws PackagingException {

    File tempData = null;
    File tempControl = null;

    try {
      tempData = File.createTempFile("deb", "data");
      tempControl = File.createTempFile("deb", "control");

      console.info("Building data");
      final StringBuilder md5s = new StringBuilder();
      final BigInteger size = buildData(pData, tempData, md5s, compression);

      console.info("Building control");
      final PackageDescriptor packageDescriptor =
          buildControl(pControlFiles, size, md5s, tempControl);

      if (!packageDescriptor.isValid()) {
        throw new PackagingException(
            "Control file descriptor keys are invalid "
                + packageDescriptor.invalidKeys()
                + ". The following keys are mandatory "
                + Arrays.toString(PackageDescriptor.MANDATORY_KEYS)
                + ". Please check your pom.xml/build.xml and your control file.");
      }

      pOutput.getParentFile().mkdirs();

      // pass through stream chain to calculate all the different digests
      final InformationOutputStream md5output =
          new InformationOutputStream(
              new FileOutputStream(pOutput), MessageDigest.getInstance("MD5"));
      final InformationOutputStream sha1output =
          new InformationOutputStream(md5output, MessageDigest.getInstance("SHA1"));
      final InformationOutputStream sha256output =
          new InformationOutputStream(sha1output, MessageDigest.getInstance("SHA-256"));
      final ArArchiveOutputStream ar = new ArArchiveOutputStream(sha256output);

      addTo(ar, "debian-binary", "2.0\n");
      addTo(ar, "control.tar.gz", tempControl);
      addTo(ar, "data.tar" + compression.getExtension(), tempData);

      ar.close();

      // intermediate values
      packageDescriptor.set("MD5", md5output.getHexDigest());
      packageDescriptor.set("SHA1", sha1output.getHexDigest());
      packageDescriptor.set("SHA256", sha256output.getHexDigest());
      packageDescriptor.set("Size", "" + md5output.getSize());
      packageDescriptor.set("File", pOutput.getName());

      return packageDescriptor;

    } catch (Exception e) {
      throw new PackagingException("Could not create deb package", e);
    } finally {
      if (tempData != null) {
        if (!tempData.delete()) {
          console.warn("Could not delete the temporary file " + tempData);
        }
      }
      if (tempControl != null) {
        if (!tempControl.delete()) {
          console.warn("Could not delete the temporary file " + tempControl);
        }
      }
    }
  }