// Build the DPN bag by fetching the APTrust files, writing manifests, etc. func (packager *DPNPackager) assembleFilesAndManifests(manifest *models.DPNIngestManifest) { packager.Context.MessageLog.Info("Assembling %s", manifest.IntellectualObject.Identifier) manifest.NsqMessage.Touch() // A little problem with BagBuilder here is that it creates a directory // with the bag name under the directory you give it. So if LocalDir // is /mnt/dpn/staging/test.edu/bag1, the BagBuilder starts putting files // into /mnt/dpn/staging/test.edu/bag1/bag1. That behavior makes sense in // other contexts, but causes problems here. So we have to remove the // bag name from the path. pathParts := strings.Split(manifest.LocalDir, string(os.PathSeparator)) builderDir := strings.Join(pathParts[0:len(pathParts)-1], string(os.PathSeparator)) packager.Context.MessageLog.Info("BuilderDir is %s", builderDir) builder, err := dpn_util.NewBagBuilder( builderDir, // should be absolute path manifest.IntellectualObject, packager.Context.Config.DPN.DefaultMetadata) if err != nil { manifest.PackageSummary.AddError("Cannot create BagBuilder: %v", err) return } packager.fetchAllFiles(manifest) for _, gf := range manifest.IntellectualObject.GenericFiles { localPath := filepath.Join(manifest.LocalDir, gf.OriginalPath()) var err error if strings.HasPrefix(gf.OriginalPath(), "data/") { pathMinusDataPrefix := strings.Replace(gf.OriginalPath(), "data/", "", 1) packager.Context.MessageLog.Info("Adding %s as data file at %s", localPath, pathMinusDataPrefix) err = builder.Bag.AddFile(localPath, pathMinusDataPrefix) } else { // Using Sprintf instead of filepath.Join because, for our purposes, // tar file paths are supposed to contain forward slashes only. // See https://tools.ietf.org/html/draft-kunze-bagit-14, sections // 2.1.3 and 7.2. The GNU tar file standard also states that // directory names are separated by slashes (not backslashes). // http://www.gnu.org/software/tar/manual/html_node/Standard.html targetPath := fmt.Sprintf("aptrust-tags/%s", gf.OriginalPath()) localPath = filepath.Join(manifest.LocalDir, "aptrust-tags", gf.OriginalPath()) packager.Context.MessageLog.Info("Adding %s as tag file at %s", localPath, targetPath) err = builder.Bag.AddCustomTagfile(localPath, targetPath, true) } if err != nil { manifest.PackageSummary.AddError("Cannot add %s to bag: %v", localPath, err) return } } manifest.NsqMessage.Touch() // Write tag files and manifests errors := builder.Bag.Save() if errors != nil && len(errors) > 0 { for _, err = range errors { manifest.PackageSummary.AddError("Bagging error: %v", err) } } manifest.NsqMessage.Touch() }
func createBagBuilder(t *testing.T) (builder *util.BagBuilder) { obj := intelObj(t) configFile := filepath.Join("config", "test.json") config, err := models.LoadConfigFile(configFile) require.Nil(t, err) builder, err = util.NewBagBuilder(testBagPath(), obj, config.DPN.DefaultMetadata) if err != nil { tearDown() t.Errorf("Could not create bag builder: %s", err.Error()) return nil } builder.Bag.Save() return builder }