Skip to content

Commit

Permalink
Move around tests a bit, split pwr.StateConsumer into its own package…
Browse files Browse the repository at this point in the history
…, display total coverage via gocovmerge
  • Loading branch information
fasterthanlime committed Oct 5, 2016
1 parent 53c74d8 commit a6b8ae4
Show file tree
Hide file tree
Showing 31 changed files with 568 additions and 469 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.coverprofile
coverage.txt
/src
/pkg
/bin
10 changes: 5 additions & 5 deletions archiver/archiver.go
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/go-errors/errors"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/pwr"
"github.com/itchio/wharf/state"
)

const (
Expand All @@ -32,7 +32,7 @@ type CompressResult struct {
CompressedSize int64
}

func ExtractPath(archive string, destPath string, consumer *pwr.StateConsumer) (*ExtractResult, error) {
func ExtractPath(archive string, destPath string, consumer *state.Consumer) (*ExtractResult, error) {
var result *ExtractResult
var err error

Expand Down Expand Up @@ -60,7 +60,7 @@ func ExtractPath(archive string, destPath string, consumer *pwr.StateConsumer) (
return result, nil
}

func Extract(readerAt io.ReaderAt, size int64, destPath string, consumer *pwr.StateConsumer) (*ExtractResult, error) {
func Extract(readerAt io.ReaderAt, size int64, destPath string, consumer *state.Consumer) (*ExtractResult, error) {
result, err := ExtractZip(readerAt, size, destPath, consumer)
if err != nil {
return nil, errors.Wrap(err, 1)
Expand Down Expand Up @@ -96,7 +96,7 @@ func Mkdir(dstpath string) error {
return nil
}

func Symlink(linkname string, filename string, consumer *pwr.StateConsumer) error {
func Symlink(linkname string, filename string, consumer *state.Consumer) error {
consumer.Debugf("ln -s %s %s", linkname, filename)

err := os.RemoveAll(filename)
Expand All @@ -117,7 +117,7 @@ func Symlink(linkname string, filename string, consumer *pwr.StateConsumer) erro
return nil
}

func CopyFile(filename string, mode os.FileMode, fileReader io.Reader, consumer *pwr.StateConsumer) error {
func CopyFile(filename string, mode os.FileMode, fileReader io.Reader, consumer *state.Consumer) error {
consumer.Debugf("extract %s", filename)
err := os.RemoveAll(filename)
if err != nil {
Expand Down
100 changes: 100 additions & 0 deletions archiver/containerarchiver/zip.go
@@ -0,0 +1,100 @@
package containerarchiver

import (
"archive/zip"
"io"
"os"
"time"

"github.com/go-errors/errors"
"github.com/itchio/wharf/archiver"
"github.com/itchio/wharf/counter"
"github.com/itchio/wharf/state"
"github.com/itchio/wharf/tlc"
"github.com/itchio/wharf/wsync"
)

func CompressZip(archiveWriter io.Writer, container *tlc.Container, pool wsync.Pool, consumer *state.Consumer) (*archiver.CompressResult, error) {
var err error
var uncompressedSize int64
var compressedSize int64

archiveCounter := counter.NewWriter(archiveWriter)

zipWriter := zip.NewWriter(archiveCounter)
defer zipWriter.Close()
defer func() {
if zipWriter != nil {
if zErr := zipWriter.Close(); err == nil && zErr != nil {
err = errors.Wrap(zErr, 1)
}
}
}()

for _, dir := range container.Dirs {
fh := zip.FileHeader{
Name: dir.Path + "/",
}
fh.SetMode(os.FileMode(dir.Mode))
fh.SetModTime(time.Now())

_, hErr := zipWriter.CreateHeader(&fh)
if hErr != nil {
return nil, errors.Wrap(hErr, 1)
}
}

for fileIndex, file := range container.Files {
fh := zip.FileHeader{
Name: file.Path,
UncompressedSize64: uint64(file.Size),
Method: zip.Deflate,
}
fh.SetMode(os.FileMode(file.Mode))
fh.SetModTime(time.Now())

entryWriter, eErr := zipWriter.CreateHeader(&fh)
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
}

entryReader, eErr := pool.GetReader(int64(fileIndex))
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
}

copiedBytes, eErr := io.Copy(entryWriter, entryReader)
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
}

uncompressedSize += copiedBytes
}

for _, symlink := range container.Symlinks {
fh := zip.FileHeader{
Name: symlink.Path,
}
fh.SetMode(os.FileMode(symlink.Mode))

entryWriter, eErr := zipWriter.CreateHeader(&fh)
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
}

entryWriter.Write([]byte(symlink.Dest))
}

err = zipWriter.Close()
if err != nil {
return nil, errors.Wrap(err, 1)
}
zipWriter = nil

compressedSize = archiveCounter.Count()

return &archiver.CompressResult{
UncompressedSize: uncompressedSize,
CompressedSize: compressedSize,
}, nil
}
4 changes: 2 additions & 2 deletions archiver/tar.go
Expand Up @@ -10,11 +10,11 @@ import (

"github.com/go-errors/errors"
"github.com/itchio/wharf/eos"
"github.com/itchio/wharf/pwr"
"github.com/itchio/wharf/state"
)

// Does not preserve users, nor permission, except the executable bit
func ExtractTar(archive string, dir string, consumer *pwr.StateConsumer) (*ExtractResult, error) {
func ExtractTar(archive string, dir string, consumer *state.Consumer) (*ExtractResult, error) {
consumer.Infof("Extracting %s to %s", archive, dir)

dirCount := 0
Expand Down
88 changes: 42 additions & 46 deletions archiver/zip.go
Expand Up @@ -7,16 +7,13 @@ import (
"os"
"path"
"path/filepath"
"time"

"github.com/go-errors/errors"
"github.com/itchio/wharf/counter"
"github.com/itchio/wharf/pwr"
"github.com/itchio/wharf/tlc"
"github.com/itchio/wharf/wsync"
"github.com/itchio/wharf/state"
)

func ExtractZip(readerAt io.ReaderAt, size int64, dir string, consumer *pwr.StateConsumer) (*ExtractResult, error) {
func ExtractZip(readerAt io.ReaderAt, size int64, dir string, consumer *state.Consumer) (*ExtractResult, error) {
dirCount := 0
regCount := 0
symlinkCount := 0
Expand Down Expand Up @@ -90,7 +87,7 @@ func ExtractZip(readerAt io.ReaderAt, size int64, dir string, consumer *pwr.Stat
}, nil
}

func CompressZip(archiveWriter io.Writer, container *tlc.Container, pool wsync.Pool, consumer *pwr.StateConsumer) (*CompressResult, error) {
func CompressZip(archiveWriter io.Writer, dir string, consumer *state.Consumer) (*CompressResult, error) {
var err error
var uncompressedSize int64
var compressedSize int64
Expand All @@ -107,59 +104,58 @@ func CompressZip(archiveWriter io.Writer, container *tlc.Container, pool wsync.P
}
}()

for _, dir := range container.Dirs {
fh := zip.FileHeader{
Name: dir.Path + "/",
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
name, wErr := filepath.Rel(dir, path)
if wErr != nil {
return wErr
}
fh.SetMode(os.FileMode(dir.Mode))
fh.SetModTime(time.Now())

_, hErr := zipWriter.CreateHeader(&fh)
if hErr != nil {
return nil, errors.Wrap(hErr, 1)
if name == "." {
// don't add '.' to zip
return nil
}
}

for fileIndex, file := range container.Files {
fh := zip.FileHeader{
Name: file.Path,
UncompressedSize64: uint64(file.Size),
Method: zip.Deflate,
fh, wErr := zip.FileInfoHeader(info)
if wErr != nil {
return wErr
}
fh.SetMode(os.FileMode(file.Mode))
fh.SetModTime(time.Now())

entryWriter, eErr := zipWriter.CreateHeader(&fh)
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
}
fh.Name = name

entryReader, eErr := pool.GetReader(int64(fileIndex))
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
writer, wErr := zipWriter.CreateHeader(fh)
if wErr != nil {
return wErr
}

copiedBytes, eErr := io.Copy(entryWriter, entryReader)
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
}
if info.IsDir() {
// good!
} else if info.Mode()&os.ModeSymlink > 0 {
dest, wErr := os.Readlink(path)
if wErr != nil {
return wErr
}

uncompressedSize += copiedBytes
}
_, wErr = writer.Write([]byte(dest))
if wErr != nil {
return wErr
}
} else if info.Mode().IsRegular() {
reader, wErr := os.Open(path)
if wErr != nil {
return wErr
}
defer reader.Close()

for _, symlink := range container.Symlinks {
fh := zip.FileHeader{
Name: symlink.Path,
}
fh.SetMode(os.FileMode(symlink.Mode))
copiedBytes, wErr := io.Copy(writer, reader)
if wErr != nil {
return wErr
}

entryWriter, eErr := zipWriter.CreateHeader(&fh)
if eErr != nil {
return nil, errors.Wrap(eErr, 1)
uncompressedSize += copiedBytes
}

entryWriter.Write([]byte(symlink.Dest))
}
return nil
})

err = zipWriter.Close()
if err != nil {
Expand All @@ -172,5 +168,5 @@ func CompressZip(archiveWriter io.Writer, container *tlc.Container, pool wsync.P
return &CompressResult{
UncompressedSize: uncompressedSize,
CompressedSize: compressedSize,
}, nil
}, err
}

0 comments on commit a6b8ae4

Please sign in to comment.