Ejemplo n.º 1
0
  @Override
  public Bag makeBagInPlace(
      Version version,
      boolean retainBaseDirectory,
      boolean keepEmptyDirectories,
      Completer completer) {
    log.info(MessageFormat.format("Making a bag in place at {0}", this.dir));
    File dataDir = new File(this.dir, this.bagFactory.getBagConstants(version).getDataDirectory());
    log.trace("Data directory is " + dataDir);
    try {
      // If there is no data direct
      if (!dataDir.exists()) {
        log.trace("Data directory does not exist");
        // If retainBaseDirectory
        File moveToDir = dataDir;
        if (retainBaseDirectory) {
          log.trace("Retaining base directory");
          // Create new base directory in data directory
          moveToDir = new File(dataDir, this.dir.getName());
          // Move contents of base directory to new base directory
        }
        log.debug(
            MessageFormat.format(
                "Data directory does not exist so moving files to {0}", moveToDir));
        for (File file : FileHelper.normalizeForm(this.dir.listFiles())) {
          if (!(file.equals(dataDir)
              || (file.isDirectory() && this.ignoreDirs.contains(file.getName())))) {
            log.trace(MessageFormat.format("Moving {0} to {1}", file, moveToDir));
            FileUtils.moveToDirectory(file, moveToDir, true);
          } else {
            log.trace(MessageFormat.format("Not moving {0}", file));
          }
        }

      } else {
        if (!dataDir.isDirectory())
          throw new RuntimeException(MessageFormat.format("{0} is not a directory", dataDir));
        // Look for additional, non-ignored files
        for (File file : FileHelper.normalizeForm(this.dir.listFiles())) {
          // If there is a directory that isn't the data dir and isn't ignored and pre v0.97 then
          // exception
          if (file.isDirectory()
              && (!file.equals(dataDir))
              && !this.ignoreDirs.contains(file.getName())
              && (Version.V0_93 == version
                  || Version.V0_94 == version
                  || Version.V0_95 == version
                  || Version.V0_96 == version)) {
            throw new RuntimeException(
                "Found additional directories in addition to existing data directory.");
          }
        }
      }
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }

    // Handle empty directories
    if (keepEmptyDirectories) {
      log.debug("Adding .keep files to empty directories");
      this.addKeep(dataDir);
    } else {
      log.trace("Not adding .keep files to empty directories");
    }

    // Copy the tags
    log.debug("Copying tag files");
    for (File tagFile : this.tagFiles) {
      log.trace(MessageFormat.format("Copying tag file {0} to {1}", tagFile, this.dir));
      try {
        FileUtils.copyFileToDirectory(tagFile, this.dir);
      } catch (IOException ex) {
        throw new RuntimeException(ex);
      }
    }

    // Create a bag
    log.debug(MessageFormat.format("Creating bag by payload files at {0}", this.dir));
    Bag bag = this.bagFactory.createBagByPayloadFiles(this.dir, version, this.ignoreDirs);
    // Complete the bag
    log.debug("Making complete");
    bag = bag.makeComplete(completer);
    // Write the bag
    log.debug("Writing");
    return bag.write(new FileSystemWriter(this.bagFactory), this.dir);
  }
Ejemplo n.º 2
0
  /**
   * Creates an AP Trust compliant bag
   *
   * @param destinationDir the directory into which the bag will be serialized
   * @param tar if true, a tar file will be the ultimate output, if false a simple directory
   *     structure (note: the current implementation writes out a directory structure first and tars
   *     it second)
   * @return a BagSummary referencing the the file that represents the root of the bag (either the
   *     tar file or the bag directory) and a checksum if the bag is a tar file
   * @throws Exception
   */
  public BagSummary serializeAPTrustBag(File destinationDir, boolean tar) throws Exception {
    long payloadSize = 0;
    if (!destinationDir.exists()) {
      destinationDir.mkdirs();
    } else {
      if (!destinationDir.isDirectory()) {
        throw new IllegalArgumentException(destinationDir + " is not a directory!");
      }
    }
    File file = new File(destinationDir, getAptrustBagName());
    BagFactory f = new BagFactory();
    final Bag b = f.createBag();
    final Bag.BagPartFactory partFactory = f.getBagPartFactory();

    final List<File> payload = getPayloadFiles();

    // write the bagit.txt
    b.putBagFile(partFactory.createBagItTxt());

    // write the manifest
    final StringBuffer manifestCopy = new StringBuffer();
    final File manifestFile = new File("manifest-md5.txt");
    final FileOutputStream manifestOS = new FileOutputStream(manifestFile);
    try {
      final ManifestWriter manifestWriter = f.getBagPartFactory().createManifestWriter(manifestOS);
      for (File payloadFile : payload) {
        payloadSize += payloadFile.length();
        final String filename = "data/" + payloadFile.getName();
        final String fixity =
            MessageDigestHelper.generateFixity(payloadFile, Manifest.Algorithm.MD5);
        manifestCopy.append(fixity + "  " + filename + "\n");
        manifestWriter.write(filename, fixity);
      }
      manifestWriter.close();
      b.addFileAsTag(manifestFile);
    } finally {
      manifestOS.close();
    }

    // write the bag-info.txt
    final File bagInfoFile = new File("bag-info.txt");
    final FileOutputStream bagInfoOS = new FileOutputStream(bagInfoFile);
    try {
      final BagInfoTxtWriter bagInfoWriter =
          f.getBagPartFactory().createBagInfoTxtWriter(bagInfoOS, "UTF-8");
      bagInfo.write(bagInfoWriter);
      bagInfoOS.close();
      b.addFileAsTag(bagInfoFile);
    } finally {
      bagInfoOS.close();
    }

    // write the aptrust-info.txt
    final File aptrustInfoFile = new File("aptrust-info.txt");
    final FileOutputStream aptrustInfoOS = new FileOutputStream(aptrustInfoFile);
    try {
      final BagItTxtWriter aptrustInfoWriter =
          f.getBagPartFactory().createBagItTxtWriter(aptrustInfoOS, "UTF-8");
      aptrustInfoWriter.write("Title", aptrustInfo.getTitle());
      aptrustInfoWriter.write("Access", aptrustInfo.getAccess());
      aptrustInfoOS.close();
      b.addFileAsTag(aptrustInfoFile);
    } finally {
      aptrustInfoOS.close();
    }

    b.addFilesToPayload(payload);
    b.write(new FileSystemWriter(f), file);

    for (File payloadFile : payload) {
      freePayloadFile(payloadFile);
    }
    manifestFile.delete();
    aptrustInfoFile.delete();
    bagInfoFile.delete();

    if (tar) {
      final BagSummary result = tarDirectory(file, manifestCopy.toString(), payloadSize);
      FileUtils.deleteDirectory(file);
      return result;
    } else {
      return new BagSummary(file, null, manifestCopy.toString(), payloadSize);
    }
  }