2017-06-08 18:43:11 +01:00
|
|
|
package file
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
2022-02-14 08:24:21 -08:00
|
|
|
"path/filepath"
|
2017-09-20 17:28:23 +01:00
|
|
|
"time"
|
2020-09-24 11:30:39 -07:00
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin/transfer"
|
2017-06-08 18:43:11 +01:00
|
|
|
)
|
|
|
|
|
|
2021-06-05 14:06:25 +01:00
|
|
|
// Reload reloads a zone when it is changed on disk. If z.ReloadInterval is zero, no reloading will be done.
|
2020-09-24 11:30:39 -07:00
|
|
|
func (z *Zone) Reload(t *transfer.Transfer) error {
|
2018-09-29 17:50:49 +02:00
|
|
|
if z.ReloadInterval == 0 {
|
2017-06-08 18:43:11 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2019-07-04 06:56:37 +01:00
|
|
|
tick := time.NewTicker(z.ReloadInterval)
|
2017-06-08 18:43:11 +01:00
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
2017-09-20 17:28:23 +01:00
|
|
|
case <-tick.C:
|
2018-07-03 06:20:39 -04:00
|
|
|
zFile := z.File()
|
2022-02-14 08:24:21 -08:00
|
|
|
reader, err := os.Open(filepath.Clean(zFile))
|
2017-09-20 17:28:23 +01:00
|
|
|
if err != nil {
|
2018-07-03 06:20:39 -04:00
|
|
|
log.Errorf("Failed to open zone %q in %q: %v", z.origin, zFile, err)
|
2017-09-20 17:28:23 +01:00
|
|
|
continue
|
|
|
|
|
}
|
2017-06-08 18:43:11 +01:00
|
|
|
|
2017-09-20 17:28:23 +01:00
|
|
|
serial := z.SOASerialIfDefined()
|
2018-07-03 06:20:39 -04:00
|
|
|
zone, err := Parse(reader, z.origin, zFile, serial)
|
2019-08-26 08:25:02 +00:00
|
|
|
reader.Close()
|
2017-09-20 17:28:23 +01:00
|
|
|
if err != nil {
|
|
|
|
|
if _, ok := err.(*serialErr); !ok {
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Errorf("Parsing zone %q: %v", z.origin, err)
|
2017-06-08 18:43:11 +01:00
|
|
|
}
|
2017-09-20 17:28:23 +01:00
|
|
|
continue
|
|
|
|
|
}
|
2017-06-08 18:43:11 +01:00
|
|
|
|
2017-09-20 17:28:23 +01:00
|
|
|
// copy elements we need
|
2019-07-23 18:32:44 +00:00
|
|
|
z.Lock()
|
2017-09-20 17:28:23 +01:00
|
|
|
z.Apex = zone.Apex
|
|
|
|
|
z.Tree = zone.Tree
|
2019-07-23 18:32:44 +00:00
|
|
|
z.Unlock()
|
2017-09-20 17:28:23 +01:00
|
|
|
|
2019-08-03 09:31:01 +00:00
|
|
|
log.Infof("Successfully reloaded zone %q in %q with %d SOA serial", z.origin, zFile, z.Apex.SOA.Serial)
|
2020-09-24 11:30:39 -07:00
|
|
|
if t != nil {
|
|
|
|
|
if err := t.Notify(z.origin); err != nil {
|
|
|
|
|
log.Warningf("Failed sending notifies: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-08 18:43:11 +01:00
|
|
|
|
2018-02-28 18:19:37 -08:00
|
|
|
case <-z.reloadShutdown:
|
2017-09-20 17:28:23 +01:00
|
|
|
tick.Stop()
|
2017-06-08 18:43:11 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 18:32:44 +00:00
|
|
|
// SOASerialIfDefined returns the SOA's serial if the zone has a SOA record in the Apex, or -1 otherwise.
|
2017-06-08 18:43:11 +01:00
|
|
|
func (z *Zone) SOASerialIfDefined() int64 {
|
2019-07-23 18:32:44 +00:00
|
|
|
z.RLock()
|
|
|
|
|
defer z.RUnlock()
|
2017-06-08 18:43:11 +01:00
|
|
|
if z.Apex.SOA != nil {
|
|
|
|
|
return int64(z.Apex.SOA.Serial)
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|