"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "go/pkg/astset/astset.go" between
Netspoc-6.032.tar.gz and Netspoc-6.033.tar.gz

About: NetSPoC is a network security policy compiler (using its own description language) to manage all the packet filter devices inside your network topology.

astset.go  (Netspoc-6.032):astset.go  (Netspoc-6.033)
package astset package astset
import ( import (
"fmt" "fmt"
"github.com/hknutzen/Netspoc/go/pkg/ast" "github.com/hknutzen/Netspoc/go/pkg/ast"
"github.com/hknutzen/Netspoc/go/pkg/fileop" "github.com/hknutzen/Netspoc/go/pkg/fileop"
"github.com/hknutzen/Netspoc/go/pkg/filetree" "github.com/hknutzen/Netspoc/go/pkg/filetree"
"github.com/hknutzen/Netspoc/go/pkg/parser" "github.com/hknutzen/Netspoc/go/pkg/parser"
"github.com/hknutzen/Netspoc/go/pkg/printer" "github.com/hknutzen/Netspoc/go/pkg/printer"
"os"
"path"
"strings" "strings"
) )
type State struct { type State struct {
astFiles []*ast.File astFiles []*ast.File
base string
files []string files []string
changed map[string]bool changed map[string]bool
} }
func Read(netspocPath string) (*State, error) { func Read(netspocBase string) (*State, error) {
s := new(State) s := new(State)
s.changed = make(map[string]bool) s.changed = make(map[string]bool)
err := filetree.Walk(netspocPath, func(input *filetree.Context) error { s.base = netspocBase
err := filetree.Walk(netspocBase, func(input *filetree.Context) error {
source := []byte(input.Data) source := []byte(input.Data)
path := input.Path path := input.Path
aF, err := parser.ParseFile(source, path, parser.ParseComments) aF, err := parser.ParseFile(source, path, parser.ParseComments)
if err != nil { if err != nil {
return err return err
} }
s.astFiles = append(s.astFiles, aF) s.astFiles = append(s.astFiles, aF)
s.files = append(s.files, path) s.files = append(s.files, path)
return nil return nil
}) })
skipping to change at line 59 skipping to change at line 63
p := printer.File(s.astFiles[i]) p := printer.File(s.astFiles[i])
err := fileop.Overwrite(path, p) err := fileop.Overwrite(path, p)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
} }
} }
func (s *State) getFileIndex(file string) int { func (s *State) getFileIndex(file string) int {
file = path.Clean(file)
// Prevent dangerous filenames, especially starting with "../".
if file == "" || file[0] == '.' {
panic(fmt.Errorf("Invalid filename %v", file))
}
file = path.Join(s.base, file)
idx := -1 idx := -1
for i, f := range s.files { for i, f := range s.files {
if f == file { if f == file {
idx = i idx = i
} }
} }
// New file is added.
if idx == -1 { if idx == -1 {
// Create missing sub directory.
d := path.Dir(file)
if err := os.MkdirAll(d, os.ModePerm); err != nil {
panic(err)
}
idx = len(s.files) idx = len(s.files)
s.files = append(s.files, file) s.files = append(s.files, file)
s.astFiles = append(s.astFiles, new(ast.File)) s.astFiles = append(s.astFiles, new(ast.File))
} }
return idx return idx
} }
func (s *State) GetFileNodes(file string) []ast.Toplevel {
idx := s.getFileIndex(file)
return s.astFiles[idx].Nodes
}
func (s *State) Modify(f func(ast.Toplevel) bool) bool { func (s *State) Modify(f func(ast.Toplevel) bool) bool {
someModified := false someModified := false
for i, aF := range s.astFiles { for i, aF := range s.astFiles {
modified := false modified := false
for _, n := range aF.Nodes { for _, n := range aF.Nodes {
if f(n) { if f(n) {
modified = true modified = true
} }
} }
if modified { if modified {
s.changed[s.files[i]] = true s.changed[s.files[i]] = true
someModified = true someModified = true
} }
} }
return someModified return someModified
} }
func (s *State) ModifyObj(name string, f func(ast.Toplevel)) error { func (s *State) ModifyObj(name string, f func(ast.Toplevel) error) error {
var err error
found := s.Modify(func(toplevel ast.Toplevel) bool { found := s.Modify(func(toplevel ast.Toplevel) bool {
if name == toplevel.GetName() { if name == toplevel.GetName() {
f(toplevel) if err2 := f(toplevel); err2 != nil {
err = err2
}
return true return true
} }
return false return false
}) })
if !found { if !found {
return fmt.Errorf("Can't find %s", name) return fmt.Errorf("Can't find %s", name)
} }
return nil return err
} }
func (s *State) CreateToplevel(file string, n ast.Toplevel) { func (s *State) CreateToplevel(file string, n ast.Toplevel) {
idx := s.getFileIndex(file) idx := s.getFileIndex(file)
aF := s.astFiles[idx] aF := s.astFiles[idx]
cp := make([]ast.Toplevel, 0, len(aF.Nodes)+1) cp := make([]ast.Toplevel, 0, len(aF.Nodes)+1)
inserted := false inserted := false
typ, name := getTypeName(n.GetName()) typ, name := getTypeName(n.GetName())
nLower := strings.ToLower(name) nLower := strings.ToLower(name)
for i, toplevel := range aF.Nodes { for i, toplevel := range aF.Nodes {
skipping to change at line 130 skipping to change at line 144
cp = append(cp, aF.Nodes[i:]...) cp = append(cp, aF.Nodes[i:]...)
inserted = true inserted = true
break break
} }
cp = append(cp, toplevel) cp = append(cp, toplevel)
} }
if !inserted { if !inserted {
cp = append(cp, n) cp = append(cp, n)
} }
s.astFiles[idx].Nodes = cp s.astFiles[idx].Nodes = cp
s.changed[file] = true s.changed[s.files[idx]] = true
} }
func (s *State) DeleteToplevel(name string) error { func (s *State) DeleteToplevel(name string) error {
found := false found := false
for i, aF := range s.astFiles { for i, aF := range s.astFiles {
cp := make([]ast.Toplevel, 0, len(aF.Nodes)) cp := make([]ast.Toplevel, 0, len(aF.Nodes))
for _, toplevel := range aF.Nodes { for _, toplevel := range aF.Nodes {
if name == toplevel.GetName() { if name == toplevel.GetName() {
found = true found = true
} else { } else {
skipping to change at line 154 skipping to change at line 168
if found { if found {
s.astFiles[i].Nodes = cp s.astFiles[i].Nodes = cp
s.changed[s.files[i]] = true s.changed[s.files[i]] = true
return nil return nil
} }
} }
return fmt.Errorf("Can't find %s", name) return fmt.Errorf("Can't find %s", name)
} }
func getTypeName(v string) (string, string) { func getTypeName(v string) (string, string) {
parts := strings.SplitN(v, ":", 2) i := strings.Index(v, ":")
if len(parts) != 2 { return v[:i], v[i+1:]
return v, ""
}
return parts[0], parts[1]
} }
 End of changes. 13 change blocks. 
16 lines changed or deleted 27 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)