| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | // Package auto implements a on-the-fly loading file backend.
 | 
					
						
							|  |  |  | package auto
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/file"
 | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/transfer"
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | // Zones maps zone names to a *Zone. This keeps track of what zones we have loaded at
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | // any one time.
 | 
					
						
							|  |  |  | type Zones struct {
 | 
					
						
							|  |  |  | 	Z     map[string]*file.Zone // A map mapping zone (origin) to the Zone's data.
 | 
					
						
							|  |  |  | 	names []string              // All the keys from the map Z as a string slice.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	origins []string // Any origins from the server block.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sync.RWMutex
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Names returns the names from z.
 | 
					
						
							|  |  |  | func (z *Zones) Names() []string {
 | 
					
						
							|  |  |  | 	z.RLock()
 | 
					
						
							|  |  |  | 	n := z.names
 | 
					
						
							|  |  |  | 	z.RUnlock()
 | 
					
						
							|  |  |  | 	return n
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Origins returns the origins from z.
 | 
					
						
							|  |  |  | func (z *Zones) Origins() []string {
 | 
					
						
							|  |  |  | 	// doesn't need locking, because there aren't multiple Go routines accessing it.
 | 
					
						
							|  |  |  | 	return z.origins
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Zones returns a zone with origin name from z, nil when not found.
 | 
					
						
							|  |  |  | func (z *Zones) Zones(name string) *file.Zone {
 | 
					
						
							|  |  |  | 	z.RLock()
 | 
					
						
							|  |  |  | 	zo := z.Z[name]
 | 
					
						
							|  |  |  | 	z.RUnlock()
 | 
					
						
							|  |  |  | 	return zo
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | // Add adds a new zone into z. If zo.NoReload is false, the
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | // reload goroutine is started.
 | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | func (z *Zones) Add(zo *file.Zone, name string, t *transfer.Transfer) {
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	z.Lock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if z.Z == nil {
 | 
					
						
							|  |  |  | 		z.Z = make(map[string]*file.Zone)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	z.Z[name] = zo
 | 
					
						
							|  |  |  | 	z.names = append(z.names, name)
 | 
					
						
							| 
									
										
										
										
											2020-09-24 11:30:39 -07:00
										 |  |  | 	zo.Reload(t)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	z.Unlock()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | // Remove removes the zone named name from z. It also stops the zone's reload goroutine.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (z *Zones) Remove(name string) {
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	z.Lock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:19:37 -08:00
										 |  |  | 	if zo, ok := z.Z[name]; ok {
 | 
					
						
							|  |  |  | 		zo.OnShutdown()
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	delete(z.Z, name)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	// TODO(miek): just regenerate Names (might be bad if you have a lot of zones...)
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	z.names = []string{}
 | 
					
						
							|  |  |  | 	for n := range z.Z {
 | 
					
						
							|  |  |  | 		z.names = append(z.names, n)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	z.Unlock()
 | 
					
						
							|  |  |  | }
 |