Ejemplo n.º 1
0
// fetchAndStore reads the contents of path and stores them into a, returning
// the digest of the contents.  The path to digest mapping is cached so that
// repeated uses of the same file will avoid redundant work.
func (e *Extractor) fetchAndStore(ctx context.Context, path string, a *indexpack.Archive) (string, error) {
	if digest, ok := e.fmap[path]; ok {
		return digest, nil
	}
	data, err := vfs.ReadFile(ctx, path)
	if err != nil {
		// If there's an alternative installation path, and this is a path that
		// could potentially be there, try that.
		if i := strings.Index(path, "/pkg/"); i >= 0 && e.AltInstallPath != "" {
			alt := e.AltInstallPath + path[i:]
			data, err = vfs.ReadFile(ctx, alt)
			// fall through to the recheck below
		}
	}
	if err != nil {
		return "", err
	}
	name, err := a.WriteFile(ctx, data)
	if err != nil {
		return "", err
	}
	digest := strings.TrimSuffix(name, filepath.Ext(name))
	if e.fmap == nil {
		e.fmap = map[string]string{path: digest}
	}
	return digest, err
}
Ejemplo n.º 2
0
func emitPath(path string, info os.FileInfo, err error) error {
	if info.IsDir() || !(*emitIrregular || info.Mode().IsRegular()) {
		return nil
	}
	for _, re := range excludes {
		if re.MatchString(path) {
			return nil
		}
	}

	if *verbose {
		log.Printf("Reading/emitting %s", path)
	}
	contents, err := vfs.ReadFile(context.Background(), path)
	if err != nil {
		return err
	}
	vName := fileRules.ApplyDefault(path, new(spb.VName))

	digest := sha256.Sum256(contents)
	vName.Signature = hex.EncodeToString(digest[:])

	if vName.Language == "" {
		vName.Language = sourceLanguage(path)
	}
	if vName.Path == "" {
		vName.Path = path
	}

	if err := emitEntry(vName, kindLabel, fileKind); err != nil {
		return err
	}
	return emitEntry(vName, textLabel, contents)
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()

	if *exclude != "" {
		for _, pattern := range strings.Split(*exclude, ",") {
			excludes = append(excludes, regexp.MustCompile(pattern))
		}
	}

	if data, err := vfs.ReadFile(context.Background(), *vnamesConfigPath); err != nil {
		log.Fatalf("Unable to read VNames config file %q: %v", *vnamesConfigPath, err)
	} else if rules, err := vnameutil.ParseRules(data); err != nil {
		log.Fatalf("Invalid VName rules: %v", err)
	} else {
		fileRules = rules
	}

	dirs := flag.Args()
	if len(dirs) == 0 {
		dirs = []string{"."}
	}

	for _, dir := range dirs {
		if err := filepath.Walk(dir, emitPath); err != nil {
			log.Fatalf("Error walking %s: %v", dir, err)
		}
	}
}