Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swarm filters save config (fix #2878) #2880

Merged
merged 4 commits into from
Jun 21, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"sort"

cmds "github.com/ipfs/go-ipfs/commands"
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
"github.com/ipfs/go-ipfs/repo/fsrepo"
iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
pstore "gx/ipfs/QmXHUpFsnpCmanRnacqYkFoLoFfEq5yS2nUgGkAjJ1Nj9j/go-libp2p-peerstore"
swarm "gx/ipfs/QmdBpVuSYuTGDA8Kn66CbKvEThXqKUh2nTANZEhzSxqrmJ/go-libp2p/p2p/net/swarm"
Expand Down Expand Up @@ -458,6 +461,23 @@ add your filters to the ipfs config file.
return
}

r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

if len(req.Arguments()) == 0 {
res.SetError(errors.New("no filters to add"), cmds.ErrClient)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this before opening the repo

}

for _, arg := range req.Arguments() {
mask, err := mafilter.NewMask(arg)
if err != nil {
Expand All @@ -467,7 +487,20 @@ add your filters to the ipfs config file.

snet.Filters.AddDialFilter(mask)
}

added, err := filtersAdd(r, cfg, req.Arguments())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return

}

res.SetOutput(&stringList{added})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: stringListMarshaler,
},
Type: stringList{},
}

var swarmFiltersRmCmd = &cmds.Command{
Expand Down Expand Up @@ -500,11 +533,32 @@ remove your filters from the ipfs config file.
return
}

r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
defer r.Close()
cfg, err := r.Config()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

if req.Arguments()[0] == "all" || req.Arguments()[0] == "*" {
fs := snet.Filters.Filters()
for _, f := range fs {
snet.Filters.Remove(f)
}

removed, err := filtersRemoveAll(r, cfg)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

res.SetOutput(&stringList{removed})

return
}

Expand All @@ -517,5 +571,89 @@ remove your filters from the ipfs config file.

snet.Filters.Remove(mask)
}

removed, err := filtersRemove(r, cfg, req.Arguments())

res.SetOutput(&stringList{removed})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: stringListMarshaler,
},
Type: stringList{},
}

func filtersAdd(r repo.Repo, cfg *config.Config, filters []string) ([]string, error) {
addedMap := map[string]struct{}{}
addedList := make([]string, 0, len(filters))

// re-add cfg swarm filters to rm dupes
oldFilters := cfg.Swarm.AddrFilters
cfg.Swarm.AddrFilters = nil

// add new filters
for _, filter := range filters {
if _, found := addedMap[filter]; found {
continue
}

cfg.Swarm.AddrFilters = append(cfg.Swarm.AddrFilters, filter)
addedList = append(addedList, filter)
addedMap[filter] = struct{}{}
}

// add back original filters. in this order so that we output them.
for _, filter := range oldFilters {
if _, found := addedMap[filter]; found {
continue
}

cfg.Swarm.AddrFilters = append(cfg.Swarm.AddrFilters, filter)
addedMap[filter] = struct{}{}
}

if err := r.SetConfig(cfg); err != nil {
return nil, err
}

return addedList, nil
}

func filtersRemoveAll(r repo.Repo, cfg *config.Config) ([]string, error) {
removed := cfg.Swarm.AddrFilters
cfg.Swarm.AddrFilters = nil

if err := r.SetConfig(cfg); err != nil {
return nil, err
}

return removed, nil
}

func filtersRemove(r repo.Repo, cfg *config.Config, toRemoveFilters []string) ([]string, error) {
removed := make([]string, 0, len(toRemoveFilters))
keep := make([]string, 0, len(cfg.Swarm.AddrFilters))

oldFilters := cfg.Swarm.AddrFilters

for _, oldFilter := range oldFilters {
found := false
for _, toRemoveFilter := range toRemoveFilters {
if oldFilter == toRemoveFilter {
found = true
removed = append(removed, toRemoveFilter)
break
}
}

if !found {
keep = append(keep, oldFilter)
}
}
cfg.Swarm.AddrFilters = keep

if err := r.SetConfig(cfg); err != nil {
return nil, err
}

return removed, nil
}