Exemple #1
0
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
}
Exemple #2
0
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
}
Exemple #3
0
func handlePin(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	pinCmd := cmd.GetPinCommand()
	var isPinned bool

	switch balance := pinCmd.Balance; {
	case balance < 0:
		if err := d.Repo.OwnStore.Unpin(pinCmd.Path); err != nil {
			return nil, err
		}

		isPinned = false
	case balance > 0:
		if err := d.Repo.OwnStore.Pin(pinCmd.Path); err != nil {
			return nil, err
		}

		isPinned = true
	case balance == 0:
		var err error
		if isPinned, err = d.Repo.OwnStore.IsPinned(pinCmd.Path); err != nil {
			return nil, err
		}
	}

	return &wire.Response{
		PinResp: &wire.Response_PinResp{
			IsPinned: isPinned,
		},
	}, nil
}
Exemple #4
0
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
}
Exemple #5
0
func handleMv(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	mvCmd := cmd.GetMvCommand()
	if err := d.Repo.OwnStore.Move(mvCmd.Source, mvCmd.Dest, true); err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #6
0
func handleCommit(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	message := cmd.GetCommitCommand().Message

	if err := d.Repo.OwnStore.MakeCommit(message); err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #7
0
func handleUnmount(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	mountPath := cmd.GetUnmountCommand().MountPoint

	if err := d.Mounts.Unmount(mountPath); err != nil {
		log.Errorf("Unable to unmount `%v`: %v", mountPath, err)
		return nil, err
	}

	return nil, nil
}
Exemple #8
0
func handleRm(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	rmCmd := cmd.GetRmCommand()
	repoPath := rmCmd.RepoPath
	recursive := rmCmd.Recursive

	if err := d.Repo.OwnStore.Remove(repoPath, recursive); err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #9
0
func handleStage(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	filePath := cmd.GetAddCommand().FilePath
	repoPath := cmd.GetAddCommand().RepoPath

	err := d.Repo.OwnStore.Stage(filePath, repoPath)
	if err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #10
0
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
}
Exemple #11
0
func handleCat(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	filePath := cmd.GetCatCommand().FilePath
	fd, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return nil, err
	}

	srcPath := cmd.GetCatCommand().RepoPath
	if err := d.Repo.OwnStore.Cat(srcPath, fd); err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #12
0
func handleImport(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	pbData := cmd.GetImportCommand().Data

	pbStore := storewire.Store{}
	if err := proto.Unmarshal(pbData, &pbStore); err != nil {
		return nil, err
	}

	if err := d.Repo.OwnStore.Import(&pbStore); err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #13
0
func handleList(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	listCmd := cmd.GetListCommand()
	root, depth := listCmd.Root, listCmd.Depth

	entries, err := d.Repo.OwnStore.ListProtoNodes(root, int(depth))
	if err != nil {
		return nil, err
	}

	return &wire.Response{
		ListResp: &wire.Response_ListResp{
			Entries: entries,
		},
	}, nil
}
Exemple #14
0
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
}
Exemple #15
0
func handleOnlineStatus(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	qry := cmd.GetOnlineStatusCommand().Query
	switch qry {
	case wire.OnlineQuery_IS_ONLINE:
		return &wire.Response{
			OnlineStatusResp: &wire.Response_OnlineStatusResp{
				IsOnline: d.IsOnline(),
			},
		}, nil
	case wire.OnlineQuery_GO_ONLINE:
		return nil, d.Connect()
	case wire.OnlineQuery_GO_OFFLINE:
		return nil, d.Disconnect()
	}

	return nil, fmt.Errorf("handleOnlineStatus: Bad query received: %v", qry)
}
Exemple #16
0
func handleMkdir(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	mkdirCmd := cmd.GetMkdirCommand()
	path := mkdirCmd.Path

	var err error
	if mkdirCmd.CreateParents {
		_, err = d.Repo.OwnStore.MkdirAll(path)
	} else {
		_, err = d.Repo.OwnStore.Mkdir(path)
	}

	if err != nil {
		return nil, err
	}

	return nil, nil
}
Exemple #17
0
func handleHistory(d *Server, ctx context.Context, cmd *wire.Command) (*wire.Response, error) {
	repoPath := cmd.GetHistoryCommand().RepoPath

	history, err := d.Repo.OwnStore.History(repoPath)
	if err != nil {
		return nil, err
	}

	histProto, err := history.ToProto()
	if err != nil {
		return nil, err
	}

	return &wire.Response{
		HistoryResp: &wire.Response_HistoryResp{
			History: histProto,
		},
	}, nil
}