go - bug using golang io.pipe to tar files -


i have been testing code using io.pipe tar , gunzip files tar ball , unzipping using tar utility. follow code passes, untaring process keeps getting

error: tar: truncated input file (needed 1050624 bytes, 0 available) tar: error exit delayed previous errors.

this issue driving me crazy. has been 2 weeks. need debugging.

thanks.

development enviroment: go version go1.9 darwin/amd64

package main  import (     "archive/tar"     "compress/gzip"     "fmt"     "io"     "log"     "os"     "path/filepath"     "testing" )  func testtargzippipe2(t *testing.t) {     src := "/path/to/file/folder"      pr, pw := io.pipe()     gzipwriter := gzip.newwriter(pw)     defer gzipwriter.close()     tarwriter := tar.newwriter(gzipwriter)     defer tarwriter.close()      status := make(chan bool)      go func() {         defer pr.close()         // tar local disk         tarfile, err := os.openfile("/path/to/tar/ball/test.tar.gz", os.o_rdwr|os.o_create, 0755)         if err != nil {             log.fatal(err)         }         defer tarfile.close()         if _, err := io.copy(tarfile, pr); err != nil {             log.fatal(err)         }          status <- true     }()      err := filepath.walk(src, func(path string, info os.fileinfo, err error) error {         if err != nil {             return err         }          header, err := tar.fileinfoheader(info, info.name())         if err != nil {             return err         }          // header.name = strings.trimprefix(strings.replace(path, src, "", -1), string(filepath.separator))          if err := tarwriter.writeheader(header); err != nil {             return err         }          if info.mode().isdir() {             return nil         }          fmt.println(path)         f, err := os.open(path)         if err != nil {             return err         }         defer f.close()          if _, err := io.copy(tarwriter, f); err != nil {             return err         }          return nil     })      if err != nil {         log.fatal(err)     }      pw.close()     <-status } 

you closing pipe before deferred close calls on gzipwriter , tarwriter. there's no error, because you're not checking error on either of close calls. need close tarwriter, gzipwriter, pipewriter, in order.

however, there's no reason pipe @ in code, , can remove goroutine , associated coordination altogether if write directly file.

tarfile, err := os.openfile("/tmp/test.tar.gz", os.o_rdwr|os.o_create, 0644) if err != nil {     log.fatal(err) } defer tarfile.close()  gzipwriter := gzip.newwriter(tarfile) defer gzipwriter.close() tarwriter := tar.newwriter(gzipwriter) defer tarwriter.close() 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -