github.com/Cloud-Foundations/Dominator@v0.3.4/lib/fsutil/watchFile_windows.go (about)

     1  package fsutil
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  )
    10  
    11  var stopped bool
    12  
    13  func watchFile(pathname string, logger log.Logger) <-chan io.ReadCloser {
    14  	channel := make(chan io.ReadCloser, 1)
    15  	go watchFileForever(pathname, channel, logger)
    16  	return channel
    17  }
    18  
    19  func watchFileForever(pathname string, channel chan<- io.ReadCloser,
    20  	logger log.Logger) {
    21  	var lastModTime time.Time
    22  	for ; !stopped; time.Sleep(time.Second) {
    23  		fileInfo, err := os.Stat(pathname)
    24  		if err != nil {
    25  			if logger != nil {
    26  				logger.Printf("Error stating file: %s: %s\n", pathname, err)
    27  			}
    28  			continue
    29  		}
    30  		if fileInfo.ModTime() != lastModTime {
    31  			if file, err := os.Open(pathname); err != nil {
    32  				if logger != nil {
    33  					logger.Printf("Error opening file: %s: %s\n", pathname, err)
    34  				}
    35  				continue
    36  			} else {
    37  				channel <- file
    38  				lastModTime = fileInfo.ModTime()
    39  			}
    40  		}
    41  	}
    42  	close(channel)
    43  }
    44  
    45  func watchFileStop() {
    46  	stopped = true
    47  }