aboutsummaryrefslogtreecommitdiff
path: root/plugin/auto/walk.go
blob: a50c5d85c31e1753f5d64fff277b1647afa73eff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package auto

import (
	"os"
	"path/filepath"
	"regexp"

	"github.com/coredns/coredns/plugin/file"

	"github.com/miekg/dns"
)

// Walk will recursively walk of the file under l.directory and adds the one that match l.re.
func (a Auto) Walk() error {
	// TODO(miek): should add something so that we don't stomp on each other.

	toDelete := make(map[string]bool)
	for _, n := range a.Zones.Names() {
		toDelete[n] = true
	}

	filepath.Walk(a.loader.directory, func(path string, info os.FileInfo, _ error) error {
		if info == nil || info.IsDir() {
			return nil
		}

		match, origin := matches(a.loader.re, info.Name(), a.loader.template)
		if !match {
			return nil
		}

		if z, ok := a.Zones.Z[origin]; ok {
			// we already have this zone
			toDelete[origin] = false
			z.SetFile(path)
			return nil
		}

		reader, err := os.Open(filepath.Clean(path))
		if err != nil {
			log.Warningf("Opening %s failed: %s", path, err)
			return nil
		}
		defer reader.Close()

		// Serial for loading a zone is 0, because it is a new zone.
		zo, err := file.Parse(reader, origin, path, 0)
		if err != nil {
			log.Warningf("Parse zone `%s': %v", origin, err)
			return nil
		}

		zo.ReloadInterval = a.loader.ReloadInterval
		zo.Upstream = a.loader.upstream

		a.Zones.Add(zo, origin, a.transfer)

		if a.metrics != nil {
			a.metrics.AddZone(origin)
		}

		a.transfer.Notify(origin)

		log.Infof("Inserting zone `%s' from: %s", origin, path)

		toDelete[origin] = false

		return nil
	})

	for origin, ok := range toDelete {
		if !ok {
			continue
		}

		if a.metrics != nil {
			a.metrics.RemoveZone(origin)
		}

		a.Zones.Remove(origin)

		log.Infof("Deleting zone `%s'", origin)
	}

	return nil
}

// matches re to filename, if it is a match, the subexpression will be used to expand
// template to an origin. When match is true that origin is returned. Origin is fully qualified.
func matches(re *regexp.Regexp, filename, template string) (match bool, origin string) {
	base := filepath.Base(filename)

	matches := re.FindStringSubmatchIndex(base)
	if matches == nil {
		return false, ""
	}

	by := re.ExpandString(nil, template, base, matches)
	if by == nil {
		return false, ""
	}

	origin = dns.Fqdn(string(by))

	return true, origin
}