Example #1
0
func LoadDirectory(ref *block.Ref) (entries []*Entry, err error) {
	r, err := block.NewReader(ref)
	if err != nil {
		return
	}
	err = json.NewDecoder(r).Decode(&entries)
	return
}
Example #2
0
func verifyFile(entry *Entry) error {
	r, err := block.NewReader(entry.Ref)
	if err != nil {
		return err
	}
	if _, err := io.Copy(ioutil.Discard, r); err != nil {
		return err
	}
	return nil
}
Example #3
0
func showRef() error {
	if flag.NArg() < 2 || flag.Arg(1) == "" {
		return fmt.Errorf("expecting block ref")
	}
	ref := block.RefFromHex([]byte(flag.Arg(1)))
	if ref == nil {
		return fmt.Errorf("bad ref %s", flag.Arg(1))
	}
	r, err := block.NewReader(ref)
	if err != nil {
		return err
	}
	if _, err := io.Copy(os.Stdout, r); err != nil {
		return err
	}
	return nil
}
Example #4
0
func restoreFile(entry *Entry, outdir string) error {
	var path = filepath.Join(outdir, entry.Name)
	if entry.Mode.IsDir() {
		if err := os.MkdirAll(path, entry.Mode); err != nil {
			return err
		}
	} else {
		r, err := block.NewReader(entry.Ref)
		if err != nil {
			return err
		}
		f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, entry.Mode)
		if err != nil {
			return err
		}
		if _, err := io.Copy(f, r); err != nil {
			f.Close()
			os.Remove(path)
			return err
		}
		if config.FileSync {
			if err := f.Sync(); err != nil {
				f.Close()
				os.Remove(path)
				return err
			}
		}
		if err := f.Close(); err != nil {
			os.Remove(path)
			return err
		}
	}
	if err := os.Chtimes(path, time.Now(), entry.ModTime); err != nil {
		return err
	}
	log.Printf("restored %s", path)
	return nil
}