Пример #1
0
func renderSumForHeader(v Version, h *tar.Header, data []byte) (string, error) {
	buf := bytes.NewBuffer(nil)
	// first build our test tar
	tw := tar.NewWriter(buf)
	if err := tw.WriteHeader(h); err != nil {
		return "", err
	}
	if _, err := tw.Write(data); err != nil {
		return "", err
	}
	tw.Close()

	ts, err := newTarSum(buf, true, v)
	if err != nil {
		return "", err
	}

	tr := tar.NewReader(ts)
	for {
		hdr, err := tr.Next()
		if hdr == nil || err == io.EOF {
			break
		}
		if err != nil {
			return "", err
		}
		if _, err = io.Copy(ioutil.Discard, tr); err != nil {
			return "", err
		}
	}
	return ts.Sum(nil), nil
}
Пример #2
0
// make a tar:
// * num is the number of files the tar should have
// * size is the bytes per file
// * isRand is whether the contents of the files should be a random chunk (otherwise it's all zeros)
// * realFile will write to a TempFile, instead of an in memory buffer
func sizedTar(opts sizedOptions) io.Reader {
	var (
		fh  io.ReadWriter
		err error
	)
	if opts.realFile {
		fh, err = ioutil.TempFile("", "tarsum")
		if err != nil {
			return nil
		}
	} else {
		fh = bytes.NewBuffer([]byte{})
	}
	tarW := tar.NewWriter(fh)
	defer tarW.Close()
	for i := int64(0); i < opts.num; i++ {
		entrySize := opts.size
		if opts.varySize {
			entrySize = mathRand.Int63() % opts.size
		}
		err := tarW.WriteHeader(&tar.Header{
			Name: fmt.Sprintf("/testdata%d", i),
			Mode: 0755,
			Uid:  0,
			Gid:  0,
			Size: entrySize,
		})
		if err != nil {
			return nil
		}
		var rBuf []byte
		if opts.isRand {
			rBuf = make([]byte, 8)
			_, err = rand.Read(rBuf)
			if err != nil {
				return nil
			}
		} else {
			rBuf = []byte{0, 0, 0, 0, 0, 0, 0, 0}
		}

		toWrite := entrySize
		for toWrite > 0 {
			if int64(len(rBuf)) > toWrite {
				rBuf = rBuf[:toWrite]
			}
			n, _ := tarW.Write(rBuf)
			toWrite -= int64(n)
		}
	}
	return fh
}
Пример #3
0
func emptyTarSum(gzip bool) (*tarSum, error) {
	reader, writer := io.Pipe()
	tarWriter := tar.NewWriter(writer)

	// Immediately close tarWriter and write-end of the
	// Pipe in a separate goroutine so we don't block.
	go func() {
		tarWriter.Close()
		writer.Close()
	}()

	return newTarSum(reader, !gzip, Version0)
}
Пример #4
0
func (ts *tarSum) initTarSum() error {
	ts.bufTar = bytes.NewBuffer([]byte{})
	ts.bufWriter = bytes.NewBuffer([]byte{})
	ts.tarR = tar.NewReader(ts.Reader)
	ts.tarW = tar.NewWriter(ts.bufTar)
	if !ts.DisableCompression {
		ts.writer = gzip.NewWriter(ts.bufWriter)
	} else {
		ts.writer = &nopCloseFlusher{Writer: ts.bufWriter}
	}
	if ts.th == nil {
		ts.th = defaultTHash
	}
	ts.h = ts.th.Hash()
	ts.h.Reset()
	ts.first = true
	ts.sums = fileInfoSums{}
	return nil
}
Пример #5
0
func renderDigestSumForHeader(v Version, h *tar.Header, data []byte) (string, error) {
	// First, create the digester.
	ts, err := NewDigest(v)
	if err != nil {
		return "", err
	}

	// Then build our test tar, writing to the digest.
	tw := tar.NewWriter(ts)
	if err := tw.WriteHeader(h); err != nil {
		return "", err
	}
	if _, err := tw.Write(data); err != nil {
		return "", err
	}
	tw.Close()

	// Ensure that we finished.
	if !ts.Finished() {
		return "", errors.New("tarSum digest is not finished when it shoud be!")
	}

	return fmt.Sprintf("%s:%x", ts.Label(), ts.Sum(nil)), nil
}