示例#1
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
}
示例#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

	}
}
示例#3
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
}
示例#4
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)
	}
}
示例#5
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
}
示例#6
0
文件: cache.go 项目: wantedly/risu
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
}
示例#7
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
}
示例#8
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
}
示例#9
0
文件: main.go 项目: 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
}
示例#10
0
文件: libocit.go 项目: 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
}
示例#11
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
}
示例#12
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
}
示例#13
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)
		}
	}
}
示例#14
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
}
示例#15
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")
}
示例#16
0
文件: tar.go 项目: ianwoolf/golib
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
}
示例#17
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)
		}
	}
}
示例#18
0
文件: targz.go 项目: 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
}
示例#19
0
文件: tar.go 项目: ClaudeXin/leanote
// 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)
		}
	}
}
示例#20
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
}
示例#21
0
文件: targzip.go 项目: CafeChair/com
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
}
示例#22
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)
}
示例#23
0
文件: util.go 项目: pmorie/geard
func writeTar(tw *tar.Writer, path string, relative string, fi os.FileInfo) error {
	fr, err := os.Open(path)
	if err != nil {
		return err
	}
	defer fr.Close()

	h := new(tar.Header)
	h.Name = strings.Replace(path, relative, ".", 1)
	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)
	return err
}
示例#24
0
文件: util.go 项目: smaccona/envy
func tarGzip(target io.Writer, path string) {
	gw := gzip.NewWriter(target)
	defer gw.Close()
	tw := tar.NewWriter(gw)
	defer tw.Close()
	tarFile := func(_path string, tw *tar.Writer, fi os.FileInfo) {
		fr, err := os.Open(_path)
		assert(err)
		defer fr.Close()
		h := new(tar.Header)
		h.Name = _path[len(path):]
		h.Size = fi.Size()
		h.Mode = int64(fi.Mode())
		h.ModTime = fi.ModTime()
		err = tw.WriteHeader(h)
		assert(err)
		_, err = io.Copy(tw, fr)
		assert(err)
	}
	tarDir(path, tw, tarFile)
}
示例#25
0
文件: main.go 项目: Mic92/nix2aci
func addPath(writer *tar.Writer, rootPath string, prefixLen int, dereferenceEtc bool) error {
	walkFn := func(source string, info os.FileInfo, err error) error {
		if err != nil {
			return fmt.Errorf("cannot read directory '%s': %v", rootPath, err)
		}
		header := tar.Header{}
		relPath := source[(prefixLen):]
		header.Name = path.Join("rootfs", relPath)
		header.ModTime = info.ModTime()
		header.Mode = int64(info.Mode().Perm())

		if info.Mode().IsRegular() ||
			(dereferenceEtc && info.Mode()&os.ModeSymlink != 0 && strings.HasPrefix(relPath, "etc/")) {
			header.Typeflag = tar.TypeReg
			header.Size = info.Size()
			if err := writer.WriteHeader(&header); err != nil {
				return fmt.Errorf("cannot write aci image entry header '%s': %v", source, err)
			}
			return addFile(writer, source)
		} else {
			if info.Mode()&os.ModeSymlink != 0 {
				target, err := os.Readlink(source)
				if err != nil {
					return fmt.Errorf("cannot read resolve symlink '%s', %v", target, err)
				}
				header.Linkname = target
				header.Typeflag = tar.TypeSymlink
			} else if info.Mode().IsDir() {
				header.Typeflag = tar.TypeDir
			} else {
				return fmt.Errorf("not implemented")
			}
			if err := writer.WriteHeader(&header); err != nil {
				return fmt.Errorf("cannot write aci image entry '%s': %v", source, err)
			}
		}
		return nil
	}
	return filepath.Walk(rootPath, walkFn)
}
示例#26
0
// fills tar.Header missing information (uid/gid, username/groupname, times ...)
func FillHeader(hdr *tar.Header) {
	var cuname string
	cuid := os.Getuid()
	if curr, err := user.LookupId(fmt.Sprintf("%d", cuid)); err == nil {
		cuname = curr.Username
	}
	if hdr.Uid == 0 {
		if hdr.Uname == "" {
			hdr.Uid = cuid
			hdr.Uname = cuname
		} else {
			if usr, err := user.Lookup(hdr.Uname); err == nil {
				if i, err := fmt.Sscanf("%d", usr.Uid); err == nil {
					hdr.Uid = i
					hdr.Uname = usr.Username
				}
			}
		}
	}
	if hdr.Gid == 0 {
		if hdr.Gname == "" {
			if hdr.Uid != 0 {
				if usr, err := user.LookupId(fmt.Sprintf("%d", hdr.Uid)); err == nil {
					if i, err := fmt.Sscanf("%d", usr.Gid); err == nil {
						hdr.Gid = i
					}
				}
			}
		}
	}
	if hdr.ModTime.IsZero() {
		hdr.ModTime = time.Now()
	}
	if hdr.AccessTime.IsZero() {
		hdr.AccessTime = hdr.ModTime
	}
	if hdr.ChangeTime.IsZero() {
		hdr.ChangeTime = hdr.ModTime
	}
}
示例#27
0
文件: tar.go 项目: ianwoolf/golib
func (t *Tar) IoTar() {
	defer sendBoolChan(t.CloseDone, true)
	for {
		var done bool = false
		select {
		case content := <-t.Ch:
			header := new(tar.Header)
			header.Name = content.FileName
			header.Size = int64(len(string(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 {
			// t.Gw.Close()
			t.Tw.Close()
			break
		}
	}
	return
}
示例#28
0
func addFile(tw *tar.Writer, prefix string, unit archiveWorkUnit) error {
	file, err := os.Open(unit.path)
	if err != nil {
		return err
	}
	defer file.Close()
	// now lets create the header as needed for this file within the tarball
	header := new(tar.Header)
	header.Name = filepath.Join(prefix, unit.path)
	header.Size = unit.stat.Size()
	header.Mode = int64(unit.stat.Mode())
	header.ModTime = unit.stat.ModTime()
	// write the header to the tarball archive
	if err := tw.WriteHeader(header); err != nil {
		return err
	}
	// copy the file data to the tarball
	if _, err := io.Copy(tw, file); err != nil {
		return err
	}

	// fmt.Println("DEBUG: added %s to archive", header.Name)
	return nil
}
示例#29
0
文件: archive.go 项目: go-nut/share
// Add file to archive stream
func tarWrite(path string, tw *tar.Writer) error {
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	fi, err := file.Stat()
	if err != nil {
		return err
	}

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

	if err := tw.WriteHeader(h); err != nil {
		return err
	}
	a, err := io.Copy(tw, file)

	if fi.Size() != a {
		shareLog.Printf("%s did not complete", fi.Name())
	}
	if err != nil {
		if err == io.ErrShortWrite {
			shareLog.Printf("%s wrote %d out of %d - %s", fi.Name(), a,
				fi.Size(), err.Error())
		} else {
			return err
		}
	}
	return nil
}
示例#30
0
func writeTar(workOut chan *MetricData, wg *sync.WaitGroup) {
	tw := tar.NewWriter(os.Stdout)
	for work := range workOut {
		log.Printf("Writing %s...", work.Name)
		th := new(tar.Header)
		th.Name = metrics.MetricToRelative(work.Name)
		th.Size = work.Size
		th.Mode = work.Mode
		th.ModTime = time.Unix(work.ModTime, 0)

		err := tw.WriteHeader(th)
		if err != nil {
			log.Fatal("Error writing tar: %s", err)
		}
		_, err = tw.Write(work.Data)
	}

	err := tw.Close()
	if err != nil {
		log.Fatal("Error closing tar archive: %s", err)
	}

	wg.Done()
}