Ejemplo n.º 1
0
func toCache(root, node, id string, content []byte,
	mods *service.CacheMods) error {
	// Write deps to filesystem.
	thisDep := service.CacheDep{Node: node, Cache: id}
	if mods != nil {
		for _, dep := range mods.Deps {
			err := appendRdeps(root, dep, []service.CacheDep{thisDep})
			if err != nil {
				return fmt.Errorf("Could not write rdeps: %v", err)
			}
		}
	}

	// Write cache to filesystem.
	nodePath := filepath.Join(root, node[1:])
	path := filepath.Join(nodePath, ".data", filepath.Base(id))
	if err := os.MkdirAll(filepath.Dir(path), 0770); err != nil {
		return fmt.Errorf("Could not create node cache directory: %v", err)
	}
	if mods != nil {
		mods.Deps = nil
	}
	var raw bytes.Buffer
	enc := gob.NewEncoder(&raw)
	data := cacheData{Data: content, CacheMods: mods}
	if err := enc.Encode(&data); err != nil {
		return fmt.Errorf("Could not encode cache data: %v", err)
	}
	if err := ioutil.WriteFile(path, raw.Bytes(), 0660); err != nil {
		return fmt.Errorf("Could not write node cache: %v", err)
	}

	return nil
}