func runRestore(cmd *Command) error { f, err := os.OpenFile(cmd.Globals.File, os.O_RDONLY, 0666) if err != nil { return fmt.Errorf("Unable to open file: %s", err) } r := journal.NewReader(f) for { entry, err := r.ReadEntry() if err == io.EOF { break } if err != nil { return err } switch entry.Op { case journal.OpSet: _, err = cmd.Conn.Set(entry.Path, -1, entry.Value) if err != nil { return fmt.Errorf("Error setting '%s' to '%s'", entry.Path, string(entry.Value)) } case journal.OpDel: err = cmd.Conn.Del(entry.Path, -1) if err != nil { return fmt.Errorf("Error deleting '%s'", entry.Path) } default: return fmt.Errorf("Unknown operation %s", entry.Op) } } return nil }
func runCompress(cmd *Command) error { if len(cmd.Args) != 1 { cmd.Usage() } tree := map[string]*journal.Entry{} uncompressed, err := os.Open(cmd.Globals.File) if err != nil { return fmt.Errorf("file open failed: %s", err) } defer uncompressed.Close() u := journal.NewReader(uncompressed) for { entry, err := u.ReadEntry() if err == io.EOF { break } if err != nil { return err } switch entry.Op { case journal.OpSet: tree[entry.Path] = entry case journal.OpDel: delete(tree, entry.Path) default: return fmt.Errorf("unknown operation %s", entry.Op) } } compressed, err := os.Create(cmd.Args[0]) if err != nil { return fmt.Errorf("file create failed: %s", err) } defer compressed.Close() var ( c = journal.New(compressed) rev int64 ) for _, entry := range tree { err := c.Append(journal.NewEntry(rev, journal.OpSet, entry.Path, entry.Value)) if err != nil { return fmt.Errorf("append failed: %s", err) } rev++ } err = compressed.Sync() if err != nil { return fmt.Errorf("file sync failed: %s", err) } return nil }