/* handle - Pointer to a Tar file being built rootPath - Path to read the contents from relPath - Path to write these files into */ func writeFile(handle *tar.Writer, rootPath, relPath string) error { var file *os.File var stat os.FileInfo var buffer []byte var err error if file, err = os.OpenFile(rootPath, os.O_RDONLY, os.ModePerm); err != nil { return err } if stat, err = file.Stat(); err != nil { return err } file.Close() header := &tar.Header{ Name: relPath, Size: stat.Size(), } if err := handle.WriteHeader(header); err != nil { return err } if buffer, err = ioutil.ReadFile(rootPath); err != nil { return err } if _, err := handle.Write(buffer); err != nil { return err } return nil }
func (c *PackCmd) packLib(writer *tar.Writer, dir string) error { // these should be of the form $GOOS_$GOARCH name := fmt.Sprintf("pkg/%s/%s%s", dir, c.Paths.PkgName, GO_LIB_EXT) fname := os.ExpandEnv("$GOPATH/") + name c.log.Logv("Looking for %s", fname) file, err := os.Open(fname) if err != nil { return err } defer file.Close() body, err := ioutil.ReadAll(file) if err != nil { panic(err) } header := tar.Header{ Name: name, Mode: 0444, Size: int64(len(body)), } writer.WriteHeader(&header) writer.Write(body) return nil }
func appendLink(tw *tar.Writer, tfh io.Seeker, fn string) (pos1 uint64, pos2 uint64, err error) { if !fileIsSymlink(fn) { return appendFile(tw, tfh, fn) } logger.Tracef("adding link %s (%s) to %s", tfh, fn, tw) hdr, e := FileTarHeader(fn) hdr.Size = 0 hdr.Typeflag = tar.TypeSymlink hdr.Linkname = BaseName(FindLinkOrigin(fn, false)) // logger.Printf("fn=%s hdr=%+v tm=%s", fn, hdr, hdr.Typeflag) if e != nil { err = e return } p, e := tfh.Seek(0, 1) if e != nil { err = e return } pos1 = uint64(p) if err = WriteTar(tw, hdr, nil); err != nil { return } _ = tw.Flush() if p, err = tfh.Seek(0, 1); err != nil { return } pos2 = uint64(p) return }
//WriteGopathSrc tars up files under gopath src func WriteGopathSrc(tw *tar.Writer, excludeDir string) error { gopath := os.Getenv("GOPATH") // Only take the first element of GOPATH gopath = filepath.SplitList(gopath)[0] rootDirectory := filepath.Join(gopath, "src") vmLogger.Infof("rootDirectory = %s", rootDirectory) if err := WriteFolderToTarPackage(tw, rootDirectory, excludeDir, includeFileTypes, nil); err != nil { vmLogger.Errorf("Error writing folder to tar package %s", err) return err } // Add the certificates to tar if viper.GetBool("peer.tls.enabled") { err := WriteFileToPackage(viper.GetString("peer.tls.cert.file"), "src/certs/cert.pem", tw) if err != nil { return fmt.Errorf("Error writing cert file to package: %s", err) } } // Write the tar file out if err := tw.Close(); err != nil { return err } //ioutil.WriteFile("/tmp/chaincode_deployment.tar", inputbuf.Bytes(), 0644) return nil }
func writeEndOfTarStreamHeader(tarWriter *tar.Writer) { hdr := &tar.Header{ Name: END_OF_TAR_FILENAME, } err := tarWriter.WriteHeader(hdr) CheckError(err) }
func addFileToTar(fileWriter *os.File, tarWriter *tar.Writer, info os.FileInfo, containingPath string, fullPath string) error { var ( relpath string err error ) if relpath, err = filepath.Rel(containingPath, fullPath); err != nil { return err } if relpath == fileWriter.Name() || relpath == "." || relpath == ".." { return nil } if h, _ := tar.FileInfoHeader(info, fullPath); h != nil { h.Name = relpath if err := tarWriter.WriteHeader(h); err != nil { return err } } if !info.IsDir() { fr, err := os.Open(fullPath) if err != nil { return err } defer fr.Close() if _, err := io.Copy(tarWriter, fr); err != nil { return err } } return nil }
// copyFile copies src to tar file dst. func copyFile(tw *tar.Writer, dst, src string) error { s, err := os.Open(src) if err != nil { return fmt.Errorf("unable to open %v: %v", src, err) } defer s.Close() fi, err := s.Stat() if err != nil { return fmt.Errorf("unable to stat %v: %v", src, err) } hdr, err := tar.FileInfoHeader(fi, dst) if err != nil { return fmt.Errorf("unable to create tar header for %v: %v", dst, err) } hdr.Name = dst if err := tw.WriteHeader(hdr); err != nil { return fmt.Errorf("unable to write header for %v: %v", dst, err) } _, err = io.Copy(tw, s) if err != nil { return fmt.Errorf("unable to copy %v to %v: %v", src, dst, err) } return nil }
func addFileToTarWriter(filePath string, tarWriter *tar.Writer) error { file, err := os.Open(filePath) if err != nil { return errors.New(fmt.Sprintf("Could not open file '%s', got error '%s'", filePath, err.Error())) } defer file.Close() stat, err := file.Stat() if err != nil { return errors.New(fmt.Sprintf("Could not get stat for file '%s', got error '%s'", filePath, err.Error())) } header := &tar.Header{ Name: filePath, Size: stat.Size(), Mode: int64(stat.Mode()), ModTime: stat.ModTime(), } err = tarWriter.WriteHeader(header) if err != nil { return errors.New(fmt.Sprintf("Could not write header for file '%s', got error '%s'", filePath, err.Error())) } _, err = io.Copy(tarWriter, file) if err != nil { return errors.New(fmt.Sprintf("Could not copy the file '%s' data to the tarball, got error '%s'", filePath, err.Error())) } return nil }
func targzWrite(tarWriter *tar.Writer, h *tar.Header, r io.Reader) { err := tarWriter.WriteHeader(h) panicOnError(err, "Failed to write tar entry header") _, err = io.Copy(tarWriter, r) panicOnError(err, "Failed to copy") }
// Writes the given file into tarfn func writeItem(tarfn string, fn string) (pos uint64, err error) { pos = 0 sfh, err := os.Open(fn) defer sfh.Close() if sfi, err := sfh.Stat(); err == nil { hdr := new(tar.Header) hdr.Name = sfi.Name() hdr.Size = sfi.Size() hdr.Mode = int64(sfi.Mode().Perm()) hdr.ModTime = sfi.ModTime() var ( tw *tar.Writer f ReadWriteSeekCloser ) if tw, f, pos, err = OpenForAppend(tarfn); err == nil { defer f.Close() defer tw.Close() //LIFO if err := tw.WriteHeader(hdr); err == nil { if zw, err := gzip.NewWriterLevel(tw, flate.BestCompression); err == nil { defer zw.Close() _, err = io.Copy(zw, sfh) } } } } return }
func targzWalk(dirPath string, tw *tar.Writer) { var walkfunc filepath.WalkFunc walkfunc = func(path string, fi os.FileInfo, err error) error { h, err := tar.FileInfoHeader(fi, "") handleError(err) h.Name = "./app/" + path if fi.Mode()&os.ModeSymlink != 0 { linkPath, err := os.Readlink(path) handleError(err) h.Linkname = linkPath } err = tw.WriteHeader(h) handleError(err) if fi.Mode()&os.ModeDir == 0 && fi.Mode()&os.ModeSymlink == 0 { fr, err := os.Open(path) handleError(err) defer fr.Close() _, err = io.Copy(tw, fr) handleError(err) } return nil } filepath.Walk(dirPath, walkfunc) }
func recursiveTar(base, file string, tw *tar.Writer) error { filepath := path.Join(base, file) stat, err := os.Stat(filepath) if err != nil { return err } if stat.IsDir() { files, err := ioutil.ReadDir(filepath) if err != nil { return err } for _, f := range files { if err := recursiveTar(base, path.Join(file, f.Name()), tw); err != nil { return err } } return nil } hdr, err := tar.FileInfoHeader(stat, filepath) if err != nil { return err } hdr.Name = file if err := tw.WriteHeader(hdr); err != nil { return err } f, err := os.Open(filepath) if err != nil { return err } defer f.Close() _, err = io.Copy(tw, f) return err }
func (c *PackCmd) packSource(writer *tar.Writer, p *utils.Paths) error { base := path.Join(p.SrcDir, p.PkgName) files, err := ioutil.ReadDir(base) if err != nil { return err } for _, file := range files { if !file.IsDir() && strings.HasSuffix(file.Name(), ".go") { fpath := path.Join(base, file.Name()) src, err := ioutil.ReadFile(fpath) if err != nil { return err } src, err = source.Prune(src) header := tar.Header{ Name: fmt.Sprintf("src/%s/%s", p.PkgName, file.Name()), Mode: 0444, Size: int64(len(src)), } writer.WriteHeader(&header) writer.Write(src) } } return nil }
// WritePackage satisfies the platform interface for generating a docker package // that encapsulates the environment for a CAR based chaincode func (carPlatform *Platform) WritePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error { path, err := download(spec.ChaincodeID.Path) if err != nil { return err } var buf []string //let the executable's name be chaincode ID's name buf = append(buf, cutil.GetDockerfileFromConfig("chaincode.car.Dockerfile")) buf = append(buf, "COPY package.car /tmp/package.car") // invoking directly for maximum JRE compatiblity buf = append(buf, fmt.Sprintf("RUN java -jar /usr/local/bin/chaintool buildcar /tmp/package.car -o $GOPATH/bin/%s && rm /tmp/package.car", spec.ChaincodeID.Name)) dockerFileContents := strings.Join(buf, "\n") dockerFileSize := int64(len([]byte(dockerFileContents))) //Make headers identical by using zero time var zeroTime time.Time tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime}) tw.Write([]byte(dockerFileContents)) err = cutil.WriteFileToPackage(path, "package.car", tw) if err != nil { return err } return nil }
func addFile(writer *tar.Writer, filepath string) error { f, err := os.Open(filepath) if err != nil { return err } defer f.Close() fi, err := os.Lstat(filepath) if err != nil { return err } if fi.Mode()&os.ModeSymlink == os.ModeSymlink { target, err := os.Readlink(filepath) if err != nil { return err } return addSymlink(writer, filepath, target) } header, err := tar.FileInfoHeader(fi, "") if err != nil { return err } header.Name = filepath err = writer.WriteHeader(header) if err != nil { return err } n, err := io.Copy(writer, f) if err != nil { return err } if n != fi.Size() { return io.ErrShortWrite } return nil }
func saveWalk(srcBasePath string, tw *tar.Writer, filterset filter.FilterSet, bucket fshash.Bucket, hasherFactory func() hash.Hash) error { preVisit := func(filenode *fs.FilewalkNode) error { if filenode.Err != nil { return filenode.Err } hdr, file := fs.ScanFile(srcBasePath, filenode.Path, filenode.Info) // apply filters. on scans, this is pretty easy, all of em just apply to the stream in memory. hdr = filterset.Apply(hdr) // flaten time to seconds. this tar writer impl doesn't do subsecond precision. // the writer will flatten it internally of course, but we need to do it here as well // so that the hash and the serial form are describing the same thing. hdr.ModTime = hdr.ModTime.Truncate(time.Second) wat := tar.Header(hdr) // this line is... we're not gonna talk about this. tw.WriteHeader(&wat) if file == nil { bucket.Record(hdr, nil) } else { defer file.Close() hasher := hasherFactory() tee := io.MultiWriter(tw, hasher) _, err := io.Copy(tee, file) if err != nil { return err } bucket.Record(hdr, hasher.Sum(nil)) } return nil } return fs.Walk(srcBasePath, preVisit, nil) }
func writeTarGz(filePath, baseDir string, tarGzWriter *tar.Writer, fileInfo os.FileInfo) error { file, err := os.Open(filePath) if err != nil { return err } defer file.Close() relativePath, err := filepath.Rel(baseDir, filePath) if err != nil { return err } header := new(tar.Header) header.Name = relativePath header.Size = fileInfo.Size() header.Mode = int64(fileInfo.Mode()) header.ModTime = fileInfo.ModTime() err = tarGzWriter.WriteHeader(header) if err != nil { return err } _, err = io.Copy(tarGzWriter, file) if err != nil { return err } return nil }
// 因为要在 defer 中关闭文件,所以要单独创建一个函数 func tarFile(srcBase, srcRelative string, tw *tar.Writer, fi os.FileInfo) (err error) { // 获取完整路径 srcFull := srcBase + srcRelative // 写入文件信息 hdr, er := tar.FileInfoHeader(fi, "") if er != nil { return er } hdr.Name = srcRelative if er = tw.WriteHeader(hdr); er != nil { return er } // 打开要打包的文件,准备读取 fr, er := os.Open(srcFull) if er != nil { return er } defer fr.Close() // 将文件数据写入 tw 中 if _, er = io.Copy(tw, fr); er != nil { return er } return nil }
// synthesizeMain generates a new main func and writes it to the tarball. func synthesizeMain(tw *tar.Writer, appFiles []string) error { appMap := make(map[string]bool) for _, f := range appFiles { appMap[f] = true } var f string for i := 0; i < 100; i++ { f = fmt.Sprintf("app_main%d.go", i) if !appMap[filepath.Join(*rootDir, f)] { break } } if appMap[filepath.Join(*rootDir, f)] { return fmt.Errorf("unable to find unique name for %v", f) } hdr := &tar.Header{ Name: f, Mode: 0644, Size: int64(len(newMain)), } if err := tw.WriteHeader(hdr); err != nil { return fmt.Errorf("unable to write header for %v: %v", f, err) } if _, err := tw.Write([]byte(newMain)); err != nil { return fmt.Errorf("unable to write %v to tar file: %v", f, err) } return nil }
//compress the file (if compress to the zip file , using the similar package: zip.FileInfoHeader) func Filecompress(tw *tar.Writer, dir string, fi os.FileInfo) error { //打开文件 open当中是 目录名称/文件名称 构成的组合 log.Println(dir + fi.Name()) fr, err := os.Open(dir + fi.Name()) log.Println(fr.Name()) if err != nil { return err } defer fr.Close() hdr, err := tar.FileInfoHeader(fi, "") hdr.Name = fr.Name() if err = tw.WriteHeader(hdr); err != nil { return err } _, err = io.Copy(tw, fr) if err != nil { return err } //打印文件名称 log.Println("add the file: " + fi.Name()) return nil }
func WriteToTar(tarWriter *tar.Writer, file *TarFile) error { hdr, err := tar.FileInfoHeader(file.Info, "") if err != nil { return fmt.Errorf("Unable to get tar FileInfoHeader of tar file '%s', error: %s", file.FileName, err.Error()) } hdr.Name = file.FileName if hdr.Xattrs == nil { hdr.Xattrs = map[string]string{} } hdr.Xattrs["SIZE"] = fmt.Sprintf("%d", file.Info.Size()) if file.IsOnlyFile { hdr.Xattrs["SINGLE_FILE_ONLY"] = "1" } err = tarWriter.WriteHeader(hdr) if err != nil { return fmt.Errorf("Unable to write tar header for file '%s', error: %s", file.FileName, err.Error()) } if !file.HasContent() { return nil } defer file.ContentReadCloser.Close() _, err = io_throttler.CopyThrottled(io_throttler.DefaultIOThrottlingBandwidth, tarWriter, file.ContentReadCloser) return err }
func addFileToTar(fileWriter *os.File, tarWriter *tar.Writer, info os.FileInfo, relativePath, fullPath string) error { if relativePath == fileWriter.Name() || relativePath == "." || relativePath == ".." { return nil } if h, err := tar.FileInfoHeader(info, fullPath); err == nil { h.Name = relativePath if err := tarWriter.WriteHeader(h); err != nil { return err } } if info.IsDir() { return nil } li, err := os.Lstat(fullPath) if err != nil { return err } if li.Mode()&os.ModeSymlink == os.ModeSymlink { return nil } fr, err := os.Open(fullPath) if err != nil { return err } defer fr.Close() if _, err := io.Copy(tarWriter, fr); err != nil { return err } return nil }
//WriteStreamToPackage writes bytes (from a file reader) to the tarball func WriteStreamToPackage(is io.Reader, localpath string, packagepath string, tw *tar.Writer) error { info, err := os.Stat(localpath) if err != nil { return fmt.Errorf("%s: %s", localpath, err) } header, err := tar.FileInfoHeader(info, localpath) if err != nil { return fmt.Errorf("Error getting FileInfoHeader: %s", err) } //Let's take the variance out of the tar, make headers identical by using zero time oldname := header.Name var zeroTime time.Time header.AccessTime = zeroTime header.ModTime = zeroTime header.ChangeTime = zeroTime header.Name = packagepath if err = tw.WriteHeader(header); err != nil { return fmt.Errorf("Error write header for (path: %s, oldname:%s,newname:%s,sz:%d) : %s", localpath, oldname, packagepath, header.Size, err) } if _, err := io.Copy(tw, is); err != nil { return fmt.Errorf("Error copy (path: %s, oldname:%s,newname:%s,sz:%d) : %s", localpath, oldname, packagepath, header.Size, err) } return nil }
// writeStdioSymlinks adds the /dev/stdin, /dev/stdout, /dev/stderr, and // /dev/fd symlinks expected by Docker to the converted ACIs so apps can find // them as expected func writeStdioSymlinks(tarWriter *tar.Writer, fileMap map[string]struct{}, pwl []string) ([]string, error) { stdioSymlinks := []symlink{ {"/dev/stdin", "/proc/self/fd/0"}, // Docker makes /dev/{stdout,stderr} point to /proc/self/fd/{1,2} but // we point to /dev/console instead in order to support the case when // stdout/stderr is a Unix socket (e.g. for the journal). {"/dev/stdout", "/dev/console"}, {"/dev/stderr", "/dev/console"}, {"/dev/fd", "/proc/self/fd"}, } for _, s := range stdioSymlinks { name := s.linkname target := s.target if _, exists := fileMap[name]; exists { continue } hdr := &tar.Header{ Name: filepath.Join("rootfs", name), Mode: 0777, Typeflag: tar.TypeSymlink, Linkname: target, } if err := tarWriter.WriteHeader(hdr); err != nil { return nil, err } if !util.In(pwl, name) { pwl = append(pwl, name) } } return pwl, nil }
func tarDir(srcBase, srcRelative string, tw *tar.Writer, fi os.FileInfo) (err error) { srcFull := srcBase + srcRelative fis, er := ioutil.ReadDir(srcFull) if er != nil { return er } for _, fi := range fis { if fi.IsDir() { tarDir(srcBase, srcRelative+"/"+fi.Name(), tw, fi) } else { tarFile(srcBase, srcRelative+"/"+fi.Name(), tw, fi) } } if len(srcRelative) > 0 { hdr, er := tar.FileInfoHeader(fi, "") if er != nil { return er } hdr.Name = srcRelative if er = tw.WriteHeader(hdr); er != nil { return er } } return nil }
func addFile(filename string, tw *tar.Writer) error { file, err := os.Open(filename) if err != nil { return fmt.Errorf("failed opening %s: %s", filename, err) } defer file.Close() stat, err := file.Stat() if err != nil { return fmt.Errorf("failed file stat for %s: %s", filename, err) } hdr := &tar.Header{ ModTime: stat.ModTime(), Name: filename, Size: stat.Size(), Mode: int64(stat.Mode().Perm()), } if err := tw.WriteHeader(hdr); err != nil { return fmt.Errorf("failed writing tar header for %s: %s", filename, err) } copied, err := io.Copy(tw, file) if err != nil { return fmt.Errorf("failed writing %s to tar: %s", filename, err) } // Check copied, since we have the file stat with its size if copied < stat.Size() { return fmt.Errorf("wrote %d bytes of %s, expected to write %d", copied, filename, stat.Size()) } return nil }
// appends file to tar func appendFile(tw *tar.Writer, tfh io.Seeker, fn string) (pos1 uint64, pos2 uint64, err error) { logger.Tracef("adding %s (%s) to %s", tfh, fn, tw) hdr, e := FileTarHeader(fn) if e != nil { err = e return } sfh, e := os.Open(fn) if e != nil { err = e return } defer sfh.Close() p, e := tfh.Seek(0, 1) if e != nil { err = e return } pos1 = uint64(p) if err = WriteTar(tw, hdr, sfh); err != nil { return } _ = tw.Flush() if p, err = tfh.Seek(0, 1); err != nil { return } pos2 = uint64(p) return }
func addDir(writer *tar.Writer, dirpath string) error { dir, err := os.Open(dirpath) if err != nil { return err } defer dir.Close() fi, err := dir.Stat() if err != nil { return err } header, err := tar.FileInfoHeader(fi, "") if err != nil { return err } header.Name = dirpath err = writer.WriteHeader(header) if err != nil { return err } fis, err := dir.Readdir(0) if err != nil { return err } for _, fi := range fis { if fi.IsDir() { err = addDir(writer, path.Join(dirpath, fi.Name())) } else { err = addFile(writer, path.Join(dirpath, fi.Name())) } if err != nil { return err } } return nil }
// writeTarHeader writes tar header for given file, returns error if operation fails func (t *stiTar) writeTarHeader(tarWriter *tar.Writer, dir string, path string, info os.FileInfo, includeDirInPath bool) error { var ( link string err error ) if info.Mode()&os.ModeSymlink != 0 { link, err = os.Readlink(path) if err != nil { return err } } header, err := tar.FileInfoHeader(info, link) if err != nil { return err } prefix := dir if includeDirInPath { prefix = filepath.Dir(prefix) } header.Name = filepath.ToSlash(path[1+len(prefix):]) glog.V(5).Infof("Adding to tar: %s as %s", path, header.Name) if err = tarWriter.WriteHeader(header); err != nil { return err } return nil }
//tw is expected to have the chaincode in it from GenerateHashcode. This method //will just package rest of the bytes func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error { var urlLocation string if strings.HasPrefix(spec.ChaincodeID.Path, "http://") { urlLocation = spec.ChaincodeID.Path[7:] } else if strings.HasPrefix(spec.ChaincodeID.Path, "https://") { urlLocation = spec.ChaincodeID.Path[8:] } else { urlLocation = spec.ChaincodeID.Path } newRunLine := fmt.Sprintf("RUN go install %s && cp src/github.com/openblockchain/obc-peer/openchain.yaml $GOPATH/bin", urlLocation) dockerFileContents := fmt.Sprintf("%s\n%s", viper.GetString("chaincode.golang.Dockerfile"), newRunLine) dockerFileSize := int64(len([]byte(dockerFileContents))) //Make headers identical by using zero time var zeroTime time.Time tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime}) tw.Write([]byte(dockerFileContents)) err := writeGopathSrc(tw, urlLocation) if err != nil { return fmt.Errorf("Error writing Chaincode package contents: %s", err) } return nil }