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 (s *WriterSuite) TestHashingWriterHexSum(c *gc.C) { var buf bytes.Buffer hasher := fakeHasher{sum: []byte("spam")} w := hash.NewHashingWriter(&buf, &hasher) rawhash := w.HexSum() c.Check(rawhash, gc.Equals, "7370616d") }
func (s *WriterSuite) TestHashingWriterSum(c *gc.C) { var buf bytes.Buffer hasher := fakeHasher{sum: []byte("spam")} w := hash.NewHashingWriter(&buf, &hasher) b64hash := string(w.Sum()) c.Check(b64hash, gc.Equals, "spam") }
func (s *WriterSuite) TestHashingWriterBase64Sum(c *gc.C) { var buf bytes.Buffer hasher := fakeHasher{sum: []byte("spam")} w := hash.NewHashingWriter(&buf, &hasher) b64hash := w.Base64Sum() c.Check(b64hash, gc.Equals, "c3BhbQ==") }
func (s *WriterSuite) TestHashingWriterBase64Sum(c *gc.C) { s.hash.ReturnSum = []byte("spam") w := hash.NewHashingWriter(s.writer, s.hash) b64sum := w.Base64Sum() s.stub.CheckCallNames(c, "Sum") c.Check(b64sum, gc.Equals, "c3BhbQ==") }
func (s *WriterSuite) TestHashingWriterWriteFileError(c *gc.C) { file := errorWriter{err: errors.New("failed!")} hasher := fakeHasher{} w := hash.NewHashingWriter(&file, &hasher) _, err := w.Write([]byte("spam")) c.Check(err, gc.ErrorMatches, "failed!") }
func (s *WriterSuite) TestHashingWriterWriteFileError(c *gc.C) { w := hash.NewHashingWriter(s.writer, s.hash) failure := errors.New("<failed>") s.stub.SetErrors(failure) _, err := w.Write([]byte("spam")) s.stub.CheckCallNames(c, "Write") c.Check(errors.Cause(err), gc.Equals, failure) }
func (s *WriterSuite) TestHashingWriterWriteSmall(c *gc.C) { w := hash.NewHashingWriter(s.writer, s.hash) n, err := w.Write([]byte("spam")) c.Assert(err, jc.ErrorIsNil) s.stub.CheckCallNames(c, "Write", "Write") c.Check(n, gc.Equals, 4) c.Check(s.wBuffer.String(), gc.Equals, "spam") c.Check(s.hBuffer.String(), gc.Equals, "spam") }
func (s *WriterSuite) TestHashingWriterWriteEmpty(c *gc.C) { w := hash.NewHashingWriter(s.writer, s.hash) n, err := w.Write(nil) c.Assert(err, jc.ErrorIsNil) s.stub.CheckCallNames(c, "Write", "Write") c.Check(n, gc.Equals, 0) c.Check(s.wBuffer.String(), gc.Equals, "") c.Check(s.hBuffer.String(), gc.Equals, "") }
func (s *WriterSuite) TestHashingWriterWriteSmall(c *gc.C) { var buf bytes.Buffer hasher := fakeHasher{} w := hash.NewHashingWriter(&buf, &hasher) n, err := w.Write([]byte("spam")) c.Check(err, gc.IsNil) c.Check(n, gc.Equals, 4) c.Check(buf.String(), gc.Equals, "spam") c.Check(hasher.String(), gc.Equals, "spam") }
func (s *WriterSuite) TestHashingWriterWriteEmpty(c *gc.C) { var buf bytes.Buffer hasher := fakeHasher{} w := hash.NewHashingWriter(&buf, &hasher) n, err := w.Write(nil) c.Check(err, gc.IsNil) c.Check(n, gc.Equals, 0) c.Check(buf.String(), gc.Equals, "") c.Check(hasher.String(), gc.Equals, "") }
func (b *builder) buildArchiveAndChecksum() error { logger.Infof("building archive file (%s)", b.archive.Filename) if b.archiveFile == nil { return errors.New("missing archiveFile") } // 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(b.archiveFile, sha1.New()) if err := b.buildArchive(hasher); err != nil { return errors.Trace(err) } // Save 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. b.checksum = hasher.Base64Sum() return nil }