Esempio n. 1
0
func (f *File) WriteTo(w io.Writer) error {
	tw := tar.NewWriter(w)
	hdr := new(tar.Header)
	hdr.Name = "file.qvm"
	hdr.Size = 32
	if f.QvmFile.Header.Magic == qvm.VM_MAGIC_VER2 {
		hdr.Size += 4
	}
	hdr.Size += int64(f.QvmFile.Header.CodeLength + f.QvmFile.Header.DataLength + f.QvmFile.Header.LitLength)
	if err := tw.WriteHeader(hdr); err != nil {
		return err
	}
	buf := new(Rab)
	binary.Write(buf, binary.LittleEndian, &f.QvmFile.Header)
	if _, err := tw.Write([]byte(*buf)[0:32]); err != nil {
		return err
	}
	if f.QvmFile.Header.Magic == qvm.VM_MAGIC_VER2 {
		if _, err := tw.Write([]byte(*buf)[32:]); err != nil {
			return err
		}
	}

	if _, err := tw.Write(f.QvmFile.Code); err != nil {
		return err
	}
	if _, err := tw.Write(f.QvmFile.Data); err != nil {
		return err
	}
	if _, err := tw.Write(f.QvmFile.Lit); err != nil {
		return err
	}

	hdr.Name = "comments.csv"
	hdr.Size = int64(len(f.CommentsFile.Data))
	if err := tw.WriteHeader(hdr); err != nil {
		return err
	}
	if _, err := tw.Write(f.CommentsFile.Data); err != nil {
		return err
	}

	hdr.Name = "syscalls.asm"
	hdr.Size = int64(len(f.SyscallsFile.Data))
	if err := tw.WriteHeader(hdr); err != nil {
		return err
	}
	if _, err := tw.Write(f.SyscallsFile.Data); err != nil {
		return err
	}
	if err := tw.Flush(); err != nil {
		return err
	}

	return nil
}
Esempio n. 2
0
// Deal with files
func tarGzFile(srcFile string, recPath string, tw *tar.Writer, fi os.FileInfo) error {
	//转换字符集
	recPath = Utf8ToGBK(recPath)

	if fi.IsDir() {
		// Create tar header
		hdr := new(tar.Header)
		// if last character of header name is '/' it also can be directory
		// but if you don't set Typeflag, error will occur when you untargz
		hdr.Name = recPath + "/"
		hdr.Typeflag = tar.TypeDir
		hdr.Size = 0
		//hdr.Mode = 0755 | c_ISDIR
		hdr.Mode = int64(fi.Mode())
		hdr.ModTime = fi.ModTime()

		// Write hander
		err := tw.WriteHeader(hdr)

		return err

	} else {
		// File reader
		fr, err := os.Open(srcFile)
		if err != nil {
			return err
		}
		defer fr.Close()

		// Create tar header
		hdr := new(tar.Header)
		hdr.Name = recPath
		hdr.Size = fi.Size()
		hdr.Mode = int64(fi.Mode())
		hdr.ModTime = fi.ModTime()

		// Write hander
		err = tw.WriteHeader(hdr)
		if err != nil {
			return err
		}

		// Write file data
		_, err = io.Copy(tw, fr)

		return err

	}
}
Esempio n. 3
0
func sparseCompress() {
	err := os.Mkdir("sparse", 0777)
	tarfile, err := os.Create("sparse/sparse.tar")
	if err != nil {
		log.Fatalln(err)
	}
	defer tarfile.Close()
	var fileWriter io.WriteCloser = tarfile
	tfw := tar.NewWriter(fileWriter)
	defer tfw.Close()
	file, err := os.Open("sparse.img")
	defer file.Close()
	fileInfo, err := file.Stat()
	header := new(tar.Header)
	header.Name = file.Name()
	header.Size = fileInfo.Size()
	header.Mode = int64(fileInfo.Mode())
	header.ModTime = fileInfo.ModTime()
	header.Typeflag = tar.TypeGNUSparse

	err = tfw.WriteHeader(header)
	if err != nil {
		log.Fatalln(err)
	}
	_, err = io.Copy(tfw, file)

	if err != nil {
		log.Fatalln(err)
	}
}
Esempio n. 4
0
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
}
Esempio n. 5
0
// Write the files into the tar dir
func writeTar(path string, tw *tar.Writer, fi os.FileInfo) error {
	fr, err := os.Open(path)
	if err != nil {
		return err
	}

	defer fr.Close()

	h := new(tar.Header)
	h.Name = fi.Name()
	h.Size = fi.Size()
	h.Mode = int64(fi.Mode())
	h.ModTime = fi.ModTime()

	err = tw.WriteHeader(h)
	if err != nil {
		return err
	}

	_, err = io.Copy(tw, fr)
	if err != nil {
		return err
	}

	return err
}
Esempio n. 6
0
func addFileToArchive(filePath string, addPath string, tarWriter *tar.Writer, fileInfo os.FileInfo) error {
	addPath = filepath.ToSlash(addPath)

	if isSymlink(fileInfo) {
		link, err := filepath.EvalSymlinks(filePath)
		if err != nil {
			return err
		}
		filePath = link
		fileInfo, err = os.Lstat(filePath)
		if err != nil {
			return err
		}
	}

	header := new(tar.Header)
	header.Name = addPath
	header.Size = fileInfo.Size()
	header.Mode = int64(fileInfo.Mode())
	header.ModTime = fileInfo.ModTime()

	fileReader, err := os.Open(filePath)
	if err != nil {
		return err
	}
	defer fileReader.Close()
	err = tarWriter.WriteHeader(header)
	fail.Handle(err)
	_, err = io.Copy(tarWriter, fileReader)
	return err
}
Esempio n. 7
0
File: libocit.go Progetto: hqhq/oct
// file name filelist is like this: './source/file'
func TarFilelist(filelist []string, case_dir string, object_name string) (tar_url string) {
	tar_url = path.Join(case_dir, object_name) + ".tar.gz"
	fw, err := os.Create(tar_url)
	if err != nil {
		fmt.Println("Failed in create tar file ", err)
		return tar_url
	}
	defer fw.Close()
	gw := gzip.NewWriter(fw)
	defer gw.Close()
	tw := tar.NewWriter(gw)
	defer tw.Close()

	for index := 0; index < len(filelist); index++ {
		source_file := filelist[index]
		fi, err := os.Stat(path.Join(case_dir, source_file))
		if err != nil {
			fmt.Println(err)
			continue
		}
		fr, err := os.Open(path.Join(case_dir, source_file))
		if err != nil {
			fmt.Println(err)
			continue
		}
		h := new(tar.Header)
		h.Name = source_file
		h.Size = fi.Size()
		h.Mode = int64(fi.Mode())
		h.ModTime = fi.ModTime()
		err = tw.WriteHeader(h)
		_, err = io.Copy(tw, fr)
	}
	return tar_url
}
Esempio n. 8
0
File: main.go Progetto: jonfk/gotar
func addFile(path string, fileInfo os.FileInfo, tarFileWriter *tar.Writer) error {
	if fileInfo.IsDir() {
		return nil
	}

	file, err := os.Open(path)
	if err != nil {
		return err
	}

	defer file.Close()

	header := new(tar.Header)
	header.Name = file.Name()
	header.Size = fileInfo.Size()
	header.Mode = int64(fileInfo.Mode())
	header.ModTime = fileInfo.ModTime()

	err = tarFileWriter.WriteHeader(header)
	if err != nil {
		return err
	}

	_, err = io.Copy(tarFileWriter, file)
	if err != nil {
		return err
	}

	return nil
}
Esempio n. 9
0
// Write a single file to TarGz
func TarGzWrite(item ArchiveItem, tw *tar.Writer, fi os.FileInfo) (err error) {
	if item.FileSystemPath != "" {
		fr, err := os.Open(item.FileSystemPath)
		if err == nil {
			defer fr.Close()

			h := new(tar.Header)
			h.Name = item.ArchivePath
			h.Size = fi.Size()
			h.Mode = int64(fi.Mode())
			h.ModTime = fi.ModTime()

			err = tw.WriteHeader(h)

			if err == nil {
				_, err = io.Copy(tw, fr)
			}
		}
	} else {
		h := new(tar.Header)
		//backslash-only paths
		h.Name = strings.Replace(item.ArchivePath, "\\", "/", -1)
		h.Size = int64(len(item.Data))
		h.Mode = int64(0644) //? is this ok?
		h.ModTime = time.Now()
		err = tw.WriteHeader(h)
		if err == nil {
			_, err = tw.Write(item.Data)
		}
	}
	return err
}
Esempio n. 10
0
File: main.go Progetto: kr/hpush
func entar(w io.Writer, dir string, ww io.Writer) error {
	msg.Write(ww, msg.User, []byte("entar\n"))
	tw := tar.NewWriter(w)
	err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		hdr := new(tar.Header)
		hdr.Name = "./app" + path[len(dir):]
		hdr.Mode = int64(fi.Mode() & os.ModePerm)
		if fi.IsDir() {
			hdr.Typeflag = tar.TypeDir
		} else {
			hdr.Typeflag = tar.TypeReg
			hdr.Size = fi.Size()
		}
		if err = tw.WriteHeader(hdr); err != nil {
			return err
		}
		if !fi.IsDir() {
			var f *os.File
			f, err = os.Open(path)
			if err != nil {
				return err
			}
			_, err = io.Copy(tw, f)
		}
		return err
	})
	if err != nil {
		return err
	}
	return tw.Close()
}
Esempio n. 11
0
func (t *Tarball) addFile(file *File) error {
	// grab a handle on the file to read it into the buffer
	fr, err := os.Open(file.path)
	if err != nil {
		return err
	}
	defer fr.Close()
	if stat, err := fr.Stat(); err == nil {
		// create the tarball header for this file
		header := new(tar.Header)
		header.Name = file.path
		header.Size = stat.Size()
		header.Mode = int64(stat.Mode())
		header.ModTime = stat.ModTime()
		// write the header to the current tarball
		if err := t.tw.WriteHeader(header); err != nil {
			return err
		}
		// copy the file data over now that the header is set
		if _, err := io.Copy(t.tw, fr); err == nil {
			t.size += stat.Size()
		} else {
			return err
		}
	} else {
		return err
	}
	return nil
}
Esempio n. 12
0
// 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
}
Esempio n. 13
0
func (c AdminData) Download(createdTime string) revel.Result {
	backup, ok := configService.GetBackup(createdTime)
	if !ok {
		return c.RenderText("")
	}

	dbname, _ := revel.Config.String("db.dbname")
	path := backup["path"] + "/" + dbname
	allFiles := ListDir(path)

	filename := "backup_" + dbname + "_" + backup["createdTime"] + ".tar.gz"

	// file write
	fw, err := os.Create(revel.BasePath + "/files/" + filename)
	if err != nil {
		return c.RenderText("")
	}
	// defer fw.Close() // 不需要关闭, 还要读取给用户下载
	// gzip write
	gw := gzip.NewWriter(fw)
	defer gw.Close()

	// tar write
	tw := tar.NewWriter(gw)
	defer tw.Close()

	// 遍历文件列表
	for _, file := range allFiles {
		fn := path + "/" + file
		fr, err := os.Open(fn)
		fileInfo, _ := fr.Stat()
		if err != nil {
			return c.RenderText("")
		}
		defer fr.Close()

		// 信息头
		h := new(tar.Header)
		h.Name = file
		h.Size = fileInfo.Size()
		h.Mode = int64(fileInfo.Mode())
		h.ModTime = fileInfo.ModTime()

		// 写信息头
		err = tw.WriteHeader(h)
		if err != nil {
			panic(err)
		}

		// 写文件
		_, err = io.Copy(tw, fr)
		if err != nil {
			panic(err)
		}
	} // for

	return c.RenderBinary(fw, filename, revel.Attachment, time.Now()) // revel.Attachm
}
Esempio n. 14
0
func MakeTarball(tarname string, fnames []string) error {
	log.Printf("tarring %d entrires to %s ...", len(fnames), tarname)
	tarfile, err := os.Create(tarname)
	if err != nil {
		return err
	}
	defer tarfile.Close()
	var tarwriter *tar.Writer
	if strings.HasSuffix(tarname, ".gz") {
		zipper := gzip.NewWriter(tarfile)
		defer zipper.Close()
		tarwriter = tar.NewWriter(zipper)
		/*
			} else if strings.HasSuffix(tarname, ".xz") {
				p := xz.WriterDefaults
				p.DictCap = 1 << 24
				zipper, err := xz.NewWriterParams(tarfile, &p) //xz.NewWriter(tarfile)
				if err != nil {
					return err
				}
				defer zipper.Close()
				tarwriter = tar.NewWriter(zipper)
		*/
	} else {
		tarwriter = tar.NewWriter(tarfile)
	}
	defer tarwriter.Close()

	for _, fname := range fnames {
		realm, ts, good := util.Parse_FName(fname)
		if !good {
			log.Printf("warning: skip ill-named file '%s'", fname)
			continue // skip
		}
		data, err := util.Load(fname)
		if err != nil {
			return err
		}

		hdr := new(tar.Header)
		hdr.Name = util.Make_FName(realm, ts, false)
		hdr.Size = int64(len(data))
		hdr.ModTime = ts
		hdr.Mode = 0644
		err = tarwriter.WriteHeader(hdr)
		if err != nil {
			return err
		}
		log.Printf("tar %d bytes for file %s", hdr.Size, hdr.Name)
		_, err = tarwriter.Write(data)
		if err != nil {
			return err
		}
	}
	log.Printf("%s tarred without errors", tarname)
	return nil
}
Esempio n. 15
0
// Deal with files
func tarGzFile(srcFile string, recPath string, tw *tar.Writer, fi os.FileInfo) {
	if fi.IsDir() {
		// Create tar header
		hdr := new(tar.Header)
		hdr.Name = recPath + "/"
		hdr.Typeflag = tar.TypeDir
		hdr.Size = 0
		//hdr.Mode = 0755 | c_ISDIR
		hdr.Mode = int64(fi.Mode())
		hdr.ModTime = fi.ModTime()

		// Write hander
		err := tw.WriteHeader(hdr)
		if err != nil {
			panic(err)
		}
	} else {
		// File reader
		fr, err := os.Open(srcFile)
		if err != nil {
			panic(err)
		}
		defer fr.Close()

		// Create tar header
		hdr := new(tar.Header)
		hdr.Name = recPath
		hdr.Size = fi.Size()
		hdr.Mode = int64(fi.Mode())
		hdr.ModTime = fi.ModTime()

		// Write hander
		err = tw.WriteHeader(hdr)
		if err != nil {
			panic(err)
		}

		// Write file data
		_, err = io.Copy(tw, fr)
		if err != nil {
			panic(err)
		}
	}
}
Esempio n. 16
0
func TarCompress(source, target string) error {
	fw, err := os.Create(target)
	if err != nil {
		return err
	}
	defer fw.Close()

	// gzip write
	gw := gzip.NewWriter(fw)
	defer gw.Close()

	// tar write
	tw := tar.NewWriter(gw)
	defer tw.Close()

	dir, err := os.Open(source)
	if err != nil {
		return err
	}
	defer dir.Close()

	fis, err := dir.Readdir(0)
	if err != nil {
		return err
	}

	for _, fi := range fis {
		if fi.IsDir() {
			continue
		}

		fmt.Println(fi.Name())
		fr, err := os.Open(dir.Name() + "/" + fi.Name())
		if err != nil {
			return err
		}
		defer fr.Close()

		h := new(tar.Header)
		h.Name = fi.Name()
		h.Size = fi.Size()
		h.Mode = int64(fi.Mode())
		h.ModTime = fi.ModTime()

		err = tw.WriteHeader(h)
		if err != nil {
			return err
		}

		_, err = io.Copy(tw, fr)
		if err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 17
0
func (t *Tar) ChanTar() {
	defer sendBoolChan(t.CloseDone, true)
	fw, err := os.Create(t.Dest)
	if err != nil {
		return
	}
	defer fw.Close()
	// if t.Gz {
	t.Gw = gzip.NewWriter(fw)
	defer t.Gw.Close()

	t.Tw = tar.NewWriter(t.Gw)
	// }
	defer t.Tw.Close()

	dir, err := os.Open(t.OriPath)
	if err != nil {
		return
	}
	defer dir.Close()
	t.Prepare.Done()

	for {
		var done bool = false
		select {
		case content := <-t.Ch:
			header := new(tar.Header)
			header.Name = content.FileName
			header.Size = int64(len(content.Text))
			header.Mode = content.Mode
			if year, _, _ := content.ModTime.Date(); year == 1 {
				header.ModTime = time.Now()
			}
			if err := t.Tw.WriteHeader(header); err != nil {
				// todo error
				fmt.Println(err.Error())
				return
				// break
			}
			if _, err := t.Tw.Write(content.Text); err != nil {
				fmt.Println(err.Error())
				return
				// break
			}
			t.Job.Done()
		case <-t.JobDone:
			done = true
			break
		}
		if done {
			break
		}
	}
	return
}
Esempio n. 18
0
func TestUntarCreatesDeeperPathsIfNotMentioned(t *testing.T) {
	StartTest(t)
	defer FinishTest(t)

	// create a buffer and tar.Writer
	buffer := bytes.NewBufferString("")
	archive := tar.NewWriter(buffer)

	writeFile := func(name, contents string) {
		b := []byte(contents)
		header := new(tar.Header)
		header.Name = name
		header.Typeflag = tar.TypeReg
		header.Mode = 0644
		header.Mode |= c_ISREG
		header.ModTime = time.Now()
		header.Size = int64(len(b))

		TestExpectSuccess(t, archive.WriteHeader(header))
		_, err := archive.Write(b)
		TestExpectSuccess(t, err)
		TestExpectSuccess(t, archive.Flush())
	}

	// generate the mock tar... this will write to a file in a directory that
	// isn't already created within the tar
	writeFile("./a_directory/file", "foo")
	archive.Close()

	// create temp folder to extract to
	tempDir := TempDir(t)
	extractionPath := path.Join(tempDir, "pkg")
	err := os.MkdirAll(extractionPath, 0755)
	TestExpectSuccess(t, err)

	// extract
	r := bytes.NewReader(buffer.Bytes())
	u := NewUntar(r, extractionPath)
	u.AbsoluteRoot = tempDir
	TestExpectSuccess(t, u.Extract())

	fileExists := func(name string) {
		_, err := os.Stat(path.Join(tempDir, name))
		TestExpectSuccess(t, err)
	}

	fileContents := func(name, contents string) {
		b, err := ioutil.ReadFile(path.Join(tempDir, name))
		TestExpectSuccess(t, err)
		TestEqual(t, string(b), contents)
	}

	fileExists("./pkg/a_directory/file")
	fileContents("./pkg/a_directory/file", "foo")
}
Esempio n. 19
0
func addAllFile(dirPath string, tw *tar.Writer) {
	//打开文件夹
	dir, err := os.Open(dirPath)
	if err != nil {
		panic(err)
	}
	defer dir.Close()

	//读取文件列表
	fis, err := dir.Readdir(0) //n<=0 读取所有 n>0 读取n个
	if err != nil {
		panic(err)
	}

	//遍历文件列表
	for _, fi := range fis {
		if fi.IsDir() {
			addAllFile(dirPath+fi.Name()+"/", tw)
			continue
		}

		//打印文件名称
		fmt.Println(dirPath + fi.Name())

		//打开文件
		fr, err := os.Open(dir.Name() + "/" + fi.Name())
		if err != nil {
			panic(err)
		}
		defer fr.Close()

		//信息头
		h := new(tar.Header)
		h.Name = dirPath + fi.Name()
		h.Size = fi.Size()
		h.Mode = int64(fi.Mode())
		h.ModTime = fi.ModTime()

		//写信息头
		err = tw.WriteHeader(h)
		if err != nil {
			panic(err)
		}

		//写文件
		_, err = io.Copy(tw, fr)
		if err != nil {
			panic(err)
		}
	}
}
Esempio n. 20
0
File: targz.go Progetto: get3w/get3w
// Pack tar and gzip files
func Pack(gzPath, dirPath string, pathMap map[string]int) error {
	fw, err := os.Create(gzPath)
	if err != nil {
		return err
	}
	defer fw.Close()

	gw := gzip.NewWriter(fw)
	defer gw.Close()

	tw := tar.NewWriter(gw)
	defer tw.Close()

	for path, val := range pathMap {
		if val < 0 {
			continue
		}
		f, err := os.Stat(filepath.Join(dirPath, path))
		if err != nil {
			continue
		}
		if f.IsDir() {
			continue
		}

		fr, err := os.Open(filepath.Join(dirPath, path))
		if err != nil {
			return err
		}
		defer fr.Close()

		h := new(tar.Header)
		h.Name = path
		h.Size = f.Size()
		h.Mode = int64(f.Mode())
		h.ModTime = f.ModTime()

		err = tw.WriteHeader(h)
		if err != nil {
			return err
		}

		_, err = io.Copy(tw, fr)
		if err != nil {
			return err
		}
	}

	return nil
}
Esempio n. 21
0
// Deal with files
func tarGzFile(srcFile string, recPath string, tw *tar.Writer, fi os.FileInfo) {
	if fi.IsDir() {
		//    	fmt.Println("??")
		// Create tar header
		hdr := new(tar.Header)
		// if last character of header name is '/' it also can be directory
		// but if you don't set Typeflag, error will occur when you untargz
		hdr.Name = recPath // + "/"
		//        fmt.Println(hdr.Name)
		hdr.Typeflag = tar.TypeDir
		//        hdr.Size = 0
		//hdr.Mode = 0755 | c_ISDIR
		//        hdr.Mode = int64(fi.Mode()) // 加这个会有错误!!!
		//        hdr.ModTime = fi.ModTime() // 加这个会有错误!!

		// Write hander
		err := tw.WriteHeader(hdr)
		if err != nil {
			panic(err)
		}
	} else {
		// File reader
		fr, err := os.Open(srcFile)
		if err != nil {
			panic(err)
		}
		defer fr.Close()

		// Create tar header
		hdr := new(tar.Header)
		hdr.Name = recPath
		//        fmt.Println(hdr.Name)
		hdr.Size = fi.Size()
		hdr.Mode = int64(fi.Mode())
		hdr.ModTime = fi.ModTime()

		// Write hander
		err = tw.WriteHeader(hdr)
		if err != nil {
			panic(err)
		}

		// Write file data
		_, err = io.Copy(tw, fr)
		if err != nil {
			panic(err)
		}
	}
}
Esempio n. 22
0
func writeTarEntry(tw *tar.Writer, name string, data []byte) error {
	h := new(tar.Header)
	h.Name = name
	h.Uid = 1000
	h.Gid = 1000
	h.Mode = 0755
	h.Size = int64(len(data))
	if err := tw.WriteHeader(h); err != nil {
		return err
	}
	if _, err := tw.Write(data); err != nil {
		return err
	}
	return nil
}
Esempio n. 23
0
// FIXME: this is duplicated from graph_test.go in the docker package.
func fakeTar() (io.Reader, error) {
	content := []byte("Hello world!\n")
	buf := new(bytes.Buffer)
	tw := tar.NewWriter(buf)
	for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
		hdr := new(tar.Header)
		hdr.Size = int64(len(content))
		hdr.Name = name
		if err := tw.WriteHeader(hdr); err != nil {
			return nil, err
		}
		tw.Write([]byte(content))
	}
	tw.Close()
	return buf, nil
}
Esempio n. 24
0
func writeManifest(tw *tar.Writer, manifest map[string]interface{}) error {
	buf, err := json.Marshal(manifest)
	if err != nil {
		return fmt.Errorf("cannot serialize manifest: %v", err)
	}
	header := tar.Header{}
	header.Name = "manifest"
	header.Mode = 0644
	header.Size = int64(len(buf))
	header.Typeflag = tar.TypeReg
	tw.WriteHeader(&header)
	if _, err := tw.Write(buf); err != nil {
		return fmt.Errorf("cannot write manifest: %v", err)
	}
	return nil
}
Esempio n. 25
0
// file name filelist is like this: './source/file'
func TarFileList(filelist []string, caseDir string, objectName string) (tarURL string, found bool) {
	logrus.Debugf("Tar file %v in %v", filelist, caseDir)
	tarURL = path.Join(caseDir, objectName) + ".tar.gz"
	fw, err := os.Create(tarURL)
	if err != nil {
		logrus.Warn(err)
		return tarURL, false
	}
	defer fw.Close()
	gw := gzip.NewWriter(fw)
	defer gw.Close()
	tw := tar.NewWriter(gw)
	defer tw.Close()

	found = false
	for index := 0; index < len(filelist); index++ {
		sourceFile := filelist[index]
		logrus.Debugf("Tar file %v", sourceFile)
		if len(sourceFile) == 0 {
			logrus.Debugf("empty sourceFile found")
			continue
		}
		fi, err := os.Stat(path.Join(caseDir, sourceFile))
		if err != nil {
			logrus.Warn(err)
			continue
		}
		fr, err := os.Open(path.Join(caseDir, sourceFile))
		if err != nil {
			logrus.Warn(err)
			continue
		}

		h := new(tar.Header)
		h.Name = sourceFile
		h.Size = fi.Size()
		h.Mode = int64(fi.Mode())
		h.ModTime = fi.ModTime()
		err = tw.WriteHeader(h)
		_, err = io.Copy(tw, fr)
		found = true
	}
	if !found {
		os.Remove(tarURL)
	}
	return tarURL, found
}
Esempio n. 26
0
func tarFile(path string, tw *tar.Writer, fi os.FileInfo) {
	fr, err := os.Open(path)
	handleErr(err)
	defer fr.Close()

	hdr := new(tar.Header)
	hdr.Name = path
	hdr.Size = fi.Size()
	hdr.Mode = int64(fi.Mode())
	hdr.ModTime = fi.ModTime()

	err = tw.WriteHeader(hdr)
	handleErr(err)

	_, err = io.Copy(tw, fr)
	handleErr(err)
	return
}
Esempio n. 27
0
// WriteToTarStream writes a byte array of data into a tar stream
func WriteToTarStream(stream *tar.Writer, data []byte, header tar.Header) error {
	if header.Mode == 0 {
		header.Mode = 0644
	}
	if header.Size == 0 {
		header.Size = int64(len(data))
	}
	if header.Typeflag == 0 {
		header.Typeflag = tar.TypeReg
	}
	if err := stream.WriteHeader(&header); err != nil {
		return err
	}
	if _, err := stream.Write(data); err != nil {
		return err
	}
	return nil
}
Esempio n. 28
0
func TarGzWrite(_path string, tw *tar.Writer, fi os.FileInfo) {
	fr, err := os.Open(_path)
	handleError(err)
	defer fr.Close()

	h := new(tar.Header)
	h.Name = _path[len(ChallengesPath+"/"):]
	h.Size = fi.Size()
	h.Mode = int64(fi.Mode())
	h.ModTime = fi.ModTime()
	h.Typeflag = tar.TypeReg

	err = tw.WriteHeader(h)
	handleError(err)

	_, err = io.Copy(tw, fr)
	handleError(err)
}
Esempio n. 29
0
// differOnlyByFileSize returns true iff the headers differ only by size, but
// that differences is less than within bytes.
func differOnlyByFileSize(a, b *tar.Header, within int64) bool {
	if a == nil || b == nil {
		return false
	}
	if a.Size == b.Size {
		return false
	}

	diff := a.Size - b.Size
	if diff < 0 {
		diff = diff * -1
	}
	if diff < within && a.Size != 0 && b.Size != 0 {
		a.Size = b.Size
		if reflect.DeepEqual(a, b) {
			return true
		}
	}
	return false
}
Esempio n. 30
0
func writeHosts(tw *tar.Writer) error {
	etcheader := tar.Header{}
	etcheader.Name = "rootfs/etc/"
	etcheader.Mode = 0644
	etcheader.Typeflag = tar.TypeDir
	tw.WriteHeader(&etcheader)

	content := []byte("127.0.0.1 localhost\n::1 localhost\n")
	header := tar.Header{}
	header.Name = "rootfs/etc/hosts"
	header.Mode = 0644
	header.Typeflag = tar.TypeReg
	header.Size = int64(len(content))

	tw.WriteHeader(&header)
	if _, err := tw.Write(content); err != nil {
		return fmt.Errorf("cannot write manifest: %v", err)
	}
	return nil
}