Example #1
0
// updateLocal updates local index and saves it to cache directory.
func (s *synced) updateLocal() error {
	dataPath := filepath.Join(s.wd, "data")
	cs := s.lidx.Compare(dataPath)

	if len(cs) == 0 {
		return nil
	}

	s.lidx.Apply(dataPath, cs)
	return index.SaveIndex(s.lidx, filepath.Join(s.wd, LocalIndexName))
}
Example #2
0
// loadIdx reads named index from synced working directory. If index file does
// not exist, it is fetched by calling provided `fetchIdx` function and saved to
// provided path.
func (s *synced) loadIdx(name string, fetchIdx idxFunc) (*index.Index, error) {
	path := filepath.Join(s.wd, name)
	f, err := os.Open(path)
	if os.IsNotExist(err) {
		idx, err := fetchIdx()
		if err != nil {
			return nil, err
		}
		return idx, index.SaveIndex(idx, path)
	} else if err != nil {
		return nil, err
	}
	defer f.Close()

	idx := index.NewIndex()
	return idx, json.NewDecoder(f).Decode(idx)
}