Skip to content

Commit

Permalink
lock around map access
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar authored and MiguelMoll committed Nov 10, 2016
1 parent 79e6a8a commit 5e6bcdb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 19 additions & 2 deletions changes/watch_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "C"

import (
"math/rand"
"sync"
"time"
"unsafe"
)
Expand All @@ -30,14 +31,18 @@ var (
chans = make(map[string](chan string))
interval = 700 * time.Millisecond
now = C.FSEventStreamEventId((1 << 64) - 1)

lock sync.Mutex
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

func startScanner(dir string) {
lock.Lock()
chans[dir] = make(chan string)
lock.Unlock()

cpaths := C.fswatch_make_mutable_array()
defer C.free(unsafe.Pointer(cpaths))
Expand Down Expand Up @@ -65,8 +70,16 @@ func waitForNextScan(dir string) {
fired := false

for {
lock.Lock()
ch, ok := chans[dir]
lock.Unlock()

if !ok {
return
}

select {
case <-chans[dir]:
case <-ch:
fired = true
case <-tick:
if fired {
Expand All @@ -80,7 +93,11 @@ func waitForNextScan(dir string) {
func callback(stream C.FSEventStreamRef, info unsafe.Pointer, count C.size_t, paths **C.char, flags *C.FSEventStreamEventFlags, ids *C.FSEventStreamEventId) {
dir := C.GoString((*C.char)(info))

if ch, ok := chans[dir]; ok {
lock.Lock()
ch, ok := chans[dir]
lock.Unlock()

if ok {
ch <- ""
}
}
12 changes: 10 additions & 2 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ func (s *Sync) syncIncomingAdds(adds []changes.Change, st Stream) {

local := filepath.Join(s.Local, rel)

s.lock.Lock()
s.outgoingBlocks[rel] += 1

os.MkdirAll(filepath.Dir(local), 0755)
s.lock.Unlock()

tmpfile, err := ioutil.TempFile("", filepath.Base(rel))
if err != nil {
Expand All @@ -211,6 +211,12 @@ func (s *Sync) syncIncomingAdds(adds []changes.Change, st Stream) {
return
}

err = os.MkdirAll(filepath.Dir(local), 0755)
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

err = os.Rename(tmpfile.Name(), local)
if err != nil {
st <- fmt.Sprintf("error: %s", err)
Expand Down Expand Up @@ -264,7 +270,9 @@ func (s *Sync) syncOutgoingAdds(adds []changes.Change, st Stream) {

remote := filepath.Join(s.Remote, a.Path)

s.lock.Lock()
s.incomingBlocks[a.Path] += 1
s.lock.Unlock()

tgz.WriteHeader(&tar.Header{
Name: remote,
Expand Down

0 comments on commit 5e6bcdb

Please sign in to comment.