func createBundle(name, outdir, contentdir, root string) (string, error) { archive, err := os.Create(filepath.Join(outdir, name)) if err != nil { return "", errors.Annotate(err, "error opening archive file") } defer archive.Close() // Build the tarball, writing out to both the archive file and a // SHA1 hash. The hash will correspond to the gzipped file rather // than to the uncompressed contents of the tarball. This is so // that users can compare the published checksum against the // checksum of the file without having to decompress it first. hasher := hash.NewHashingWriter(archive, sha1.New()) err = func() error { tarball := gzip.NewWriter(hasher) defer tarball.Close() _, err := tar.TarFiles([]string{contentdir}, tarball, root) return err }() if err != nil { return "", errors.Annotate(err, "error bundling final archive") } // Return the SHA1 checksum. // Gzip writers may buffer what they're writing so we must call // Close() on the writer *before* getting the checksum from the // hasher. return hasher.Base64Sum(), nil }
func (b *builder) buildArchive(outFile io.Writer) error { tarball := gzip.NewWriter(outFile) defer tarball.Close() // We add a trailing slash (or whatever) to root so that everything // in the path up to and including that slash is stripped off when // each file is added to the tar file. stripPrefix := b.archive.UnpackedRootDir + string(os.PathSeparator) filenames := []string{b.archive.ContentDir()} if _, err := tar.TarFiles(filenames, tarball, stripPrefix); err != nil { return errors.Annotate(err, "while bundling final archive") } return nil }
func (b *builder) buildFilesBundle() error { logger.Infof("dumping juju state-related files") if len(b.filesToBackUp) == 0 { return errors.New("missing list of files to back up") } if b.bundleFile == nil { return errors.New("missing bundleFile") } stripPrefix := string(os.PathSeparator) _, err := tar.TarFiles(b.filesToBackUp, b.bundleFile, stripPrefix) if err != nil { return errors.Annotate(err, "while bundling state-critical files") } return nil }
func dumpFiles(dumpdir string) error { tarFile, err := os.Create(filepath.Join(dumpdir, "root.tar")) if err != nil { return errors.Annotate(err, "error while opening initial archive") } defer tarFile.Close() backupFiles, err := getFilesToBackup("") if err != nil { return errors.Annotate(err, "cannot determine files to backup") } sep := string(os.PathSeparator) _, err = tar.TarFiles(backupFiles, tarFile, sep) if err != nil { return errors.Annotate(err, "cannot backup configuration files") } return nil }