コード例 #1
0
ファイル: build.go プロジェクト: pombredanne/snappy-1
func hashForFile(buildDir, path string, info os.FileInfo) (h *fileHash, err error) {
	sha512sum := ""
	// pointer so that omitempty works (we don't want size for
	// directories or symlinks)
	var size *int64
	if info.Mode().IsRegular() {
		sha512sum, err = helpers.Sha512sum(path)
		if err != nil {
			return nil, err
		}
		fsize := info.Size()
		size = &fsize
	}

	// major/minor handling
	device := ""
	major, minor, err := helpers.MajorMinor(info)
	if err == nil {
		device = fmt.Sprintf("%v,%v", major, minor)
	}

	if buildDir != "" {
		path = path[len(buildDir)+1:]
	}

	return &fileHash{
		Name:   path,
		Size:   size,
		Sha512: sha512sum,
		Device: device,
		// FIXME: not portable, this output is different on
		//        windows, macos
		Mode: newYamlFileMode(info.Mode()),
	}, nil
}
コード例 #2
0
ファイル: build.go プロジェクト: pombredanne/snappy-1
func writeHashes(buildDir, dataTar string) error {

	debianDir := filepath.Join(buildDir, "DEBIAN")
	os.MkdirAll(debianDir, 0755)

	hashes := hashesYaml{}
	sha512, err := helpers.Sha512sum(dataTar)
	if err != nil {
		return err
	}
	hashes.ArchiveSha512 = sha512

	err = filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
		// path will always start with buildDir...
		if path[len(buildDir):] == "/DEBIAN" {
			return filepath.SkipDir
		}
		// ...so if path's length is == buildDir, it's buildDir
		if len(path) == len(buildDir) {
			return nil
		}

		hash, err := hashForFile(buildDir, path, info)
		if err != nil {
			return err
		}
		hashes.Files = append(hashes.Files, hash)

		return nil
	})
	if err != nil {
		return err
	}

	content, err := yaml.Marshal(hashes)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filepath.Join(debianDir, "hashes.yaml"), []byte(content), 0644)
}