Exemple #1
0
func untar(buff []byte) ([]tarItem, error) {
	tr := tar.NewReader(bytes.NewReader(buff))

	var items []tarItem

	buff = make([]byte, 32*1024)
	for {
		hdr, err := tr.Next()
		if err == io.EOF {
			break
		}

		item := tarItem{
			header: hdr,
			body:   []byte{},
		}

		if err != nil {
			return []tarItem{}, err
		}

		nr, err := tr.Read(buff)
		if err != nil && err != io.EOF {
			return []tarItem{}, err
		}

		if nr > 0 {
			item.body = buff[0:nr]
		}

		items = append(items, item)
	}

	return items, nil
}
Exemple #2
0
func (s *TarGzTestSuite) TestCreateTar(c *C) {
	buff, err := createTar("fixtures/")
	c.Assert(err, IsNil)
	c.Assert(len(buff) > 0, Equals, true)

	rdr := bytes.NewReader(buff)
	tr := tar.NewReader(rdr)

	var obtained []*tar.Header
	for {
		hdr, err := tr.Next()
		if err == io.EOF {
			break
		}

		if err != nil {
			panic(err)
		}

		obtained = append(obtained, hdr)
	}

	expected := []*tar.Header{
		&tar.Header{
			Name:     "fixtures/",
			Typeflag: tar.TypeDir,
		},
		&tar.Header{
			Name:     "fixtures/dir",
			Typeflag: tar.TypeDir,
		},
		&tar.Header{
			Name:     "fixtures/dir/file.txt",
			Typeflag: tar.TypeReg,
		},
		&tar.Header{
			Name:     "fixtures/dir/hardlink",
			Typeflag: tar.TypeReg,
		},
		&tar.Header{
			Name:     "fixtures/dir/symlink",
			Typeflag: tar.TypeSymlink,
		},
	}

	c.Assert(len(obtained), Equals, len(expected))
	for i, hdr := range obtained {
		c.Assert(hdr.Name, Equals, expected[i].Name)
		c.Assert(hdr.Typeflag, Equals, expected[i].Typeflag)
	}
}
Exemple #3
0
func (ts *TarSum) Read(buf []byte) (int, error) {
	if ts.gz == nil {
		ts.bufTar = bytes.NewBuffer([]byte{})
		ts.bufGz = bytes.NewBuffer([]byte{})
		ts.tarR = tar.NewReader(ts.Reader)
		ts.tarW = tar.NewWriter(ts.bufTar)
		ts.gz = gzip.NewWriter(ts.bufGz)
		ts.h = sha256.New()
		//		ts.h = verboseHash{sha256.New()}
		ts.h.Reset()
		ts.first = true
	}

	if ts.finished {
		return ts.bufGz.Read(buf)
	}
	buf2 := make([]byte, len(buf), cap(buf))

	n, err := ts.tarR.Read(buf2)
	if err != nil {
		if err == io.EOF {
			if _, err := ts.h.Write(buf2[:n]); err != nil {
				return 0, err
			}
			if !ts.first {
				ts.sums = append(ts.sums, hex.EncodeToString(ts.h.Sum(nil)))
				ts.h.Reset()
			} else {
				ts.first = false
			}

			currentHeader, err := ts.tarR.Next()
			if err != nil {
				if err == io.EOF {
					if err := ts.gz.Close(); err != nil {
						return 0, err
					}
					ts.finished = true
					return n, nil
				}
				return n, err
			}
			if err := ts.encodeHeader(currentHeader); err != nil {
				return 0, err
			}
			if err := ts.tarW.WriteHeader(currentHeader); err != nil {
				return 0, err
			}
			if _, err := ts.tarW.Write(buf2[:n]); err != nil {
				return 0, err
			}
			ts.tarW.Flush()
			if _, err := io.Copy(ts.gz, ts.bufTar); err != nil {
				return 0, err
			}
			ts.gz.Flush()

			return ts.bufGz.Read(buf)
		}
		return n, err
	}

	// Filling the hash buffer
	if _, err = ts.h.Write(buf2[:n]); err != nil {
		return 0, err
	}

	// Filling the tar writter
	if _, err = ts.tarW.Write(buf2[:n]); err != nil {
		return 0, err
	}
	ts.tarW.Flush()

	// Filling the gz writter
	if _, err = io.Copy(ts.gz, ts.bufTar); err != nil {
		return 0, err
	}
	ts.gz.Flush()

	return ts.bufGz.Read(buf)
}