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 (f *fileTarReceiver) OnEntry(tarHeader *tar.Header, tarFileReader io.Reader) error { fullDestPath := f.file parentDir := filepath.Dir(fullDestPath) err := os.MkdirAll(parentDir, os.FileMode(tarHeader.Mode)) if err != nil { return fmt.Errorf("Unable to MkDirAll '%s', error: %s", parentDir, err.Error()) } file, err := os.OpenFile(fullDestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(tarHeader.Mode)) if err != nil { return fmt.Errorf("Unable to open file '%s', error: %s", fullDestPath, err.Error()) } defer func() { file.Close() os.Chtimes(fullDestPath, tarHeader.AccessTime, tarHeader.ModTime) }() _, err = io_throttler.CopyThrottled(io_throttler.DefaultIOThrottlingBandwidth, file, tarFileReader) if err != nil { return fmt.Errorf("Unable to copy stream to file '%s', error: %s", fullDestPath, err.Error()) } return nil }
func (w *writerTarReceiver) OnEntry(tarHeader *tar.Header, tarFileReader io.Reader) error { if w.alreadyHaveFile { return fmt.Errorf("Memory TarReceiver can only process a single file") } w.alreadyHaveFile = true _, err := io_throttler.CopyThrottled(io_throttler.DefaultIOThrottlingBandwidth, w.writer, tarFileReader) if err != nil { return fmt.Errorf("Unable to copy stream to buffer, error: %s", err.Error()) } return nil }
func (d *dirTarReceiver) OnEntry(tarHeader *tar.Header, tarFileReader io.Reader) error { relativePath := tarHeader.Name if tarHeader.FileInfo().IsDir() { fullDestPath := filepath.Join(d.dir, relativePath) defer os.Chtimes(fullDestPath, tarHeader.AccessTime, tarHeader.ModTime) return os.MkdirAll(fullDestPath, os.FileMode(tarHeader.Mode)) } else { fullDestPath := filepath.Join(d.dir, relativePath) if val, ok := tarHeader.Xattrs["SINGLE_FILE_ONLY"]; ok && val == "1" { fullDestPath = d.dir } parentDir := filepath.Dir(fullDestPath) err := os.MkdirAll(parentDir, os.FileMode(tarHeader.Mode)) if err != nil { return fmt.Errorf("Unable to MkDirAll '%s', error: %s", parentDir, err.Error()) } file, err := os.OpenFile(fullDestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(tarHeader.Mode)) if err != nil { return fmt.Errorf("Unable to open file '%s', error: %s", fullDestPath, err.Error()) } defer func() { file.Close() os.Chtimes(fullDestPath, tarHeader.AccessTime, tarHeader.ModTime) }() _, err = io_throttler.CopyThrottled(io_throttler.DefaultIOThrottlingBandwidth, file, tarFileReader) if err != nil { return fmt.Errorf("Unable to copy stream to file '%s', error: %s", fullDestPath, err.Error()) } return nil } }
func (s *sendTarHandler) Read(reader io.Reader) error { _, err := io_throttler.CopyThrottled(io_throttler.DefaultIOThrottlingBandwidth, s.writer, reader) return err }