forked from qt-creator/qt-creator
CmdBridge: Fix concurrent map access
Change-Id: I180eb315c4e253550fcdda5506a30e95ff62fbee Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/fxamacker/cbor/v2"
|
"github.com/fxamacker/cbor/v2"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
|
|
||||||
type WatcherHandler struct {
|
type WatcherHandler struct {
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
|
mutex sync.Mutex
|
||||||
watchList map[int]string
|
watchList map[int]string
|
||||||
watchRefs map[string]int
|
watchRefs map[string]int
|
||||||
}
|
}
|
||||||
@@ -33,13 +35,16 @@ type watchevent struct {
|
|||||||
EventType int
|
EventType int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (watchHandler WatcherHandler) start(out chan<- []byte) {
|
func (watchHandler *WatcherHandler) start(out chan<- []byte) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event, ok := <-watchHandler.watcher.Events:
|
case event, ok := <-watchHandler.watcher.Events:
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
watchHandler.mutex.Lock()
|
||||||
|
defer watchHandler.mutex.Unlock()
|
||||||
|
|
||||||
// Find which watchList entries correspond to the event
|
// Find which watchList entries correspond to the event
|
||||||
for id, path := range watchHandler.watchList {
|
for id, path := range watchHandler.watchList {
|
||||||
// See if the event path is a subpath of the watch path
|
// See if the event path is a subpath of the watch path
|
||||||
@@ -79,13 +84,17 @@ type addwatchresult struct {
|
|||||||
Result bool
|
Result bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (watchHandler WatcherHandler) processAdd(cmd command, out chan<- []byte) {
|
func (watchHandler *WatcherHandler) processAdd(cmd command, out chan<- []byte) {
|
||||||
// TODO: Resolve links
|
// TODO: Resolve links
|
||||||
err := watchHandler.watcher.Add(cmd.Path)
|
err := watchHandler.watcher.Add(cmd.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendError(out, cmd, err)
|
sendError(out, cmd, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
watchHandler.mutex.Lock()
|
||||||
|
defer watchHandler.mutex.Unlock()
|
||||||
|
|
||||||
watchHandler.watchList[cmd.Id] = cmd.Path
|
watchHandler.watchList[cmd.Id] = cmd.Path
|
||||||
watchHandler.watchRefs[cmd.Path]++
|
watchHandler.watchRefs[cmd.Path]++
|
||||||
data, _ := cbor.Marshal(addwatchresult{
|
data, _ := cbor.Marshal(addwatchresult{
|
||||||
@@ -97,7 +106,10 @@ func (watchHandler WatcherHandler) processAdd(cmd command, out chan<- []byte) {
|
|||||||
out <- data
|
out <- data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (watchHandler WatcherHandler) processStop(cmd command, out chan<- []byte) {
|
func (watchHandler *WatcherHandler) processStop(cmd command, out chan<- []byte) {
|
||||||
|
watchHandler.mutex.Lock()
|
||||||
|
defer watchHandler.mutex.Unlock()
|
||||||
|
|
||||||
path, ok := watchHandler.watchList[cmd.Id]
|
path, ok := watchHandler.watchList[cmd.Id]
|
||||||
if !ok {
|
if !ok {
|
||||||
sendError(out, cmd, &watchnotfounderror{})
|
sendError(out, cmd, &watchnotfounderror{})
|
||||||
@@ -140,7 +152,10 @@ func (e *watchnotfounderror) Error() string {
|
|||||||
return "Watch not found"
|
return "Watch not found"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (watchHandler WatcherHandler) processRemove(cmd command, out chan<- []byte) {
|
func (watchHandler *WatcherHandler) processRemove(cmd command, out chan<- []byte) {
|
||||||
|
watchHandler.mutex.Lock()
|
||||||
|
defer watchHandler.mutex.Unlock()
|
||||||
|
|
||||||
if _, ok := watchHandler.watchList[cmd.Id]; !ok {
|
if _, ok := watchHandler.watchList[cmd.Id]; !ok {
|
||||||
sendError(out, cmd, &watchnotfounderror{})
|
sendError(out, cmd, &watchnotfounderror{})
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user