2016-09-10 4 views
1

그래서 파일의 변경 사항을 반복적으로 모니터링하기 위해 filepath.Walk 함수를 사용하고 있습니다. fsnotify은 상자 밖으로 재귀 적으로 수행 할 수 없습니다. 변경을 모니터링하기 위해 Goroutine을 설정 한 다음 Walk() 함수에서 감시자에게 경로를 추가합니다.Golang 파일/디렉토리 워커가 여러 번 파일을 추가하는 중

func (w Watch) walkDirectories(fp string) { 
    error := filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { 
     // skip files 
     if info == nil { 
      log.Fatalf("wrong watcher package: %s", path) 
     } 
     if !info.IsDir() { 
      return nil 
     } 
     if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") { 
      return filepath.SkipDir 
     } 
     log.Println("filepath: ", filepath) 
     w.W.Add(path) 
     return err 
    }) 
    log.Println("error: ", error) 
} 

감시자를 보유하는 사용자 지정 구조체가 있으므로 시계 경로를 쉽게 추가 할 수 있습니다. 여기에 사용 된 것을 볼 수 있습니다 : w.W.Add(path). 최상위 디렉토리의 파일이 감시자에게 두 번 추가되는 것, 또는 적어도 내 가설은 "최상위 레벨 아래의 디렉토리 레벨만큼 많은 시간"이라는 것만 제외하면 모두 훌륭합니다. 내 디렉토리 구조는 다음과 같습니다 :

. 
├── README.md 
├── languages.go 
├── languages.json 
├── librarymonitor.go 
├── telemetryClient 
└── testfiles 
    ├── test.go 
    ├── test.c 
    ├── test.java 

testfiles 디렉토리의 파일을 변경하면 감시인에게 "알림"하나가 표시됩니다. 루트에서 파일을 변경하면 두 개가 생깁니다. 누구든지 이것에 대해 비추어 줄 수 있습니까?
감사

기본 코드를 확인
+0

에 문제를 재현 할 수 없습니다 내 기계. 내 마음에 거래는 당신이 제공하지 않은 오타 또는 w.W.Add (경로)입니다. w.W. 내 컴퓨터에 코드 출력을 올바르게 추가하십시오. – Uvelichitel

답변

1

, 이것은 잘 작동 (The Go Playground 시도) :

package main 

import (
    "fmt" 
    "os" 
    "path/filepath" 
    "reflect" 
    "time" 
) 

func main() { 
    rootDir := ".." 
    pattern := "*" 
    dirs, err := GetDirectories(rootDir, pattern) 
    if err != nil { 
     panic(err) 
    } 
    ticker := time.NewTicker(1 * time.Second) 
    for i := 1; i < 10; i++ { 
     <-ticker.C 
     dirs2, err := GetDirectories(rootDir, pattern) 
     //fmt.Println(dirs2) 
     if err != nil { 
      panic(err) 
     } 
     if !reflect.DeepEqual(dirs, dirs2) { 
      fmt.Println("Dir Changed: ", len(dirs), len(dirs2)) 
      dirs = dirs2 
     } 
    } 
    ticker.Stop() 
    fmt.Println("Done") 
} 

// Returns the names of the subdirectories (including their paths) 
// that match the specified search pattern in the specified directory. 
func GetDirectories(root, pattern string) ([]string, error) { 
    dirs := make([]string, 0, 144) 
    return dirs, filepath.Walk(root, func(path string, fi os.FileInfo, err error) error { 
     if !fi.IsDir() { 
      return nil 
     } 
     matched, err := filepath.Match(pattern, fi.Name()) 
     if err != nil { 
      return err 
     } 
     if !matched { 
      return nil 
     } 
     dirs = append(dirs, path) 
     return nil 
    }) 
} 

샘플 출력을 (하나의 새로운 디렉토리로) :

Dir Changed: 16 17 
Done