| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | package root
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("root", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							|  |  |  | 	config := dnsserver.GetConfig(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.Error("root", c.ArgErr())
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		config.Root = c.Val()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check if root path exists
 | 
					
						
							|  |  |  | 	_, err := os.Stat(config.Root)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		if os.IsNotExist(err) {
 | 
					
						
							|  |  |  | 			// Allow this, because the folder might appear later.
 | 
					
						
							|  |  |  | 			// But make sure the user knows!
 | 
					
						
							|  |  |  | 			log.Printf("[WARNING] Root path does not exist: %s", config.Root)
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.Error("root", c.Errf("unable to access root path '%s': %v", config.Root, err))
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |