func handleExport(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) { who := cmd.GetExportCommand().Who // Figure out the correct store: var st *store.Store if who == "" { st = d.Repo.OwnStore } else { whoID, err := id.Cast(who) if err != nil { return nil, err } st, err = d.Repo.Store(whoID) if err != nil { return nil, err } } pbStore, err := st.Export() if err != nil { return nil, err } data, err := proto.Marshal(pbStore) if err != nil { return nil, err } return &wire.Response{ ExportResp: &wire.Response_ExportResp{ Data: data, }, }, nil }
func handleSync(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) { syncCmd := cmd.GetSyncCommand() who, err := id.Cast(syncCmd.Who) if err != nil { return nil, fmt.Errorf("Bad id `%s`: %v", syncCmd.Who, err) } if !d.MetaHost.IsInOnlineMode() { return nil, fmt.Errorf("Metadata Host is not online.") } client, err := d.MetaHost.DialID(who) if err != nil { return nil, err } // This might create a new, empty store if it does not exist yet: remoteStore, err := d.Repo.Store(who) if err != nil { return nil, err } if err := client.Fetch(remoteStore); err != nil { return nil, err } if err := d.Repo.OwnStore.SyncWith(remoteStore); err != nil { return nil, err } return nil, nil }
func handleRemoteLocate(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) { locateCmd := cmd.GetRemoteLocateCommand() idString, peerLimit := locateCmd.Id, int(locateCmd.PeerLimit) timeout := time.Duration(locateCmd.TimeoutMs) * time.Millisecond if timeout <= 0 { timeout = 5 * time.Second } id, err := id.Cast(idString) if err != nil { return nil, err } peers, err := ipfsutil.Locate(d.Repo.IPFS, id.Hash(), peerLimit, timeout) if err != nil { return nil, err } resp := &wire.Response_RemoteLocateResp{} for _, peer := range peers { resp.Hashes = append(resp.Hashes, peer.ID) } return &wire.Response{ RemoteLocateResp: resp, }, nil }
func TestRegister(t *testing.T) { testwith.WithIpfs(t, func(node *ipfsutil.Node) { if err := node.Online(); err != nil { t.Errorf("Could not go online: %v", err) return } alice, err := id.Cast("[email protected]/laptop") if err != nil { t.Errorf("Casting dummy id failed: %v", err) return } if err := alice.Register(node); err != nil { t.Errorf("Could not register `%s`: %v", alice, err) return } time.Sleep(2 * time.Second) if err := alice.Register(node); err != id.ErrAlreadyRegistered { t.Errorf("Could register `%s` twice? (%v)", alice, err) return } }) }
func handleDebugExport(ctx *cli.Context, client *daemon.Client) error { who := ctx.Args().First() var whoID id.ID if who != "" { validID, err := id.Cast(who) if err != nil { return err } whoID = validID } data, err := client.Export(whoID) if err != nil { return err } if _, err := os.Stdout.Write(data); err != nil { return err } return nil }
func handleRemoteRemove(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) { idString := cmd.GetRemoteRemoveCommand().Id id, err := id.Cast(idString) if err != nil { return nil, err } if err := d.Repo.Remotes.Remove(id); err != nil { return nil, err } return nil, nil }
func (cp *Checkpoint) FromProto(msg *wire.Checkpoint) error { cp.hash = &Hash{msg.Hash} cp.change = ChangeType(msg.Change) cp.idLink = msg.IdLink cp.index = msg.Index ID, err := id.Cast(msg.Author) if err != nil { log.Warningf("Bad author-id `%s` in proto-checkpoint: %v", msg.Author, err) return err } cp.author = ID return nil }
func (a *Author) FromProto(pa *wire.Author) error { ident, err := id.Cast(pa.Name) if err != nil { return err } mh, err := multihash.FromB58String(pa.Hash) if err != nil { return err } a.ident = ident a.hash = &Hash{mh} return nil }
func (mg *Merge) FromProto(protoMerge *wire.Merge) error { ID, err := id.Cast(protoMerge.With) if err != nil { return err } hash, err := multihash.Cast(protoMerge.Hash) if err != nil { return err } mg.With = ID mg.Hash = &Hash{hash} return nil }
func handleRemoteAdd(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) { remoteAddCmd := cmd.GetRemoteAddCommand() idString, peerHash := remoteAddCmd.Id, remoteAddCmd.Hash id, err := id.Cast(idString) if err != nil { return nil, err } remote := repo.NewRemote(id, peerHash) if err := d.Repo.Remotes.Insert(remote); err != nil { return nil, err } return nil, nil }
func lookupID(configPath string) (id.ID, error) { cfg, err := config.LoadConfig(configPath) if err != nil { return "", fmt.Errorf("Could not load config: %v", err) } idString, err := cfg.String("repository.id") if err != nil { return "", fmt.Errorf("No ID in config: %v", err) } ID, err := id.Cast(idString) if err != nil { return "", err } return ID, nil }
func handleSyncSingle(ctx *cli.Context, client *daemon.Client, idStr string) error { ID, err := id.Cast(idStr) if err != nil { return ExitCode{ BadArgs, fmt.Sprintf("Bad ID: %v", err), } } log.Infof("Syncing with %s", ID) if err := client.Sync(ID); err != nil { log.Errorf("Failed: %v", err) return ExitCode{UnknownError, err.Error()} } return nil }
func TestValidity(t *testing.T) { for _, test := range validityTests { valid := id.IsValid(test.id) if valid != test.ok { t.Errorf("valid(`%s`) was `%t`, should be `%t`", test.id, valid, test.ok) continue } if !valid { continue } id, err := id.Cast(test.id) if err != nil { t.Errorf("Casting `%s` failed: %v", test.id, err) continue } if id.User() != test.user { t.Errorf( "User differs; wanted `%s`; got `%s`", test.user, id.User(), ) continue } if id.Domain() != test.domain { t.Errorf( "Domain differs; wanted `%s`; got `%s`", test.domain, id.Domain(), ) continue } if id.Resource() != test.resource { t.Errorf( "Resource differs; wanted `%s`; got `%s`", test.resource, id.Resource(), ) continue } } }
func handleRemoteRemove(ctx *cli.Context, client *daemon.Client) error { idString := ctx.Args()[0] id, err := id.Cast(idString) if err != nil { return ExitCode{ BadArgs, fmt.Sprintf("Invalid ID: %v", err), } } if client.RemoteRemove(id) != nil { return ExitCode{ UnknownError, fmt.Sprintf("Unable to remove remote: %v", err), } } return nil }
func handleRemoteAdd(ctx *cli.Context, client *daemon.Client) error { idString, hash := ctx.Args()[0], ctx.Args()[1] id, err := id.Cast(idString) if err != nil { return ExitCode{ BadArgs, fmt.Sprintf("Invalid ID: %v", err), } } if err := client.RemoteAdd(id, hash); err != nil { return ExitCode{ UnknownError, fmt.Sprintf("Unable to add remote: %v", err), } } return nil }
func handleRemoteLocate(ctx *cli.Context, client *daemon.Client) error { id, err := id.Cast(ctx.Args()[0]) if err != nil { return ExitCode{ BadArgs, fmt.Sprintf("Invalid ID: %v", err), } } hashes, err := client.RemoteLocate(id, 10, 50000) if err != nil { return ExitCode{ UnknownError, fmt.Sprintf("Unable to locate ipfs peers: %v", err), } } for _, hash := range hashes { fmt.Println(hash) } return nil }
// Owner returns the owner of the store (name + hash) func (st *Store) Owner() (*Author, error) { bid, err := st.fs.MetadataGet("id") if err != nil { return nil, err } bhash, err := st.fs.MetadataGet("hash") if err != nil { return nil, err } ident, err := id.Cast(string(bid)) if err != nil { return nil, err } hash, err := multihash.FromB58String(string(bhash)) if err != nil { return nil, err } return &Author{ident, &Hash{hash}}, nil }
// loadRepository load a brig repository from a given folder. func loadRepository(pwd, folder string) (*Repository, error) { absFolderPath, err := filepath.Abs(folder) if err != nil { return nil, err } brigPath := filepath.Join(absFolderPath, ".brig") cfg, err := config.LoadConfig(filepath.Join(brigPath, "config")) if err != nil { return nil, err } configValues := map[string]string{ "repository.id": "", } for key := range configValues { configValues[key], err = cfg.String(key) if err != nil { return nil, err } } idString, err := cfg.String("repository.id") if err != nil { return nil, err } ID, err := id.Cast(idString) if err != nil { return nil, err } remoteStore, err := NewYAMLRemotes(filepath.Join(brigPath, "remotes.yml")) if err != nil { return nil, err } ipfsSwarmPort, err := cfg.Int("ipfs.swarmport") if err != nil { return nil, err } ipfsLayer := ipfsutil.NewWithPort( filepath.Join(brigPath, "ipfs"), ipfsSwarmPort, ) hash, err := ipfsLayer.Identity() if err != nil { return nil, err } ownStore, err := store.Open(brigPath, id.NewPeer(ID, hash), ipfsLayer) if err != nil { return nil, err } allStores := make(map[id.ID]*store.Store) allStores[ID] = ownStore repo := Repository{ ID: ID, Folder: absFolderPath, Remotes: remoteStore, InternalFolder: brigPath, Config: cfg, OwnStore: ownStore, Password: pwd, IPFS: ipfsLayer, allStores: make(map[id.ID]*store.Store), } return &repo, nil }
func handleInit(ctx *cli.Context) error { ID, err := id.Cast(ctx.Args().First()) if err != nil { return ExitCode{ BadArgs, fmt.Sprintf("Bad ID: %v", err), } } // Extract the folder from the resource name by default: folder := ctx.GlobalString("path") fmt.Println("Folder:", folder) if folder == "." { folder = ID.AsPath() } pwd := ctx.GlobalString("password") fmt.Println(pwd) if pwd == "" { // TODO: Lower this or make it at least configurable pwdBytes, err := pwdutil.PromptNewPassword(40.0) if err != nil { return ExitCode{BadPassword, err.Error()} } pwd = string(pwdBytes) } log.Warning("============================================") log.Warning("Please make sure to remember the passphrase!") log.Warning("Your data will not be accessible without it.") log.Warning("============================================") repo, err := repo.NewRepository(string(ID), pwd, folder) if err != nil { return ExitCode{UnknownError, err.Error()} } if err := repo.Close(); err != nil { return ExitCode{ UnknownError, fmt.Sprintf("close: %v", err), } } if !ctx.GlobalBool("nodaemon") { port, err := repo.Config.Int("daemon.port") if err != nil { return ExitCode{UnknownError, "Unable to find out port"} } if _, err := daemon.Reach(string(pwd), folder, port); err != nil { return ExitCode{ DaemonNotResponding, fmt.Sprintf("Unable to start daemon: %v", err), } } } return nil }