| 302 | } |
| 303 | |
| 304 | func (e *Engine) applyEvents(re rootEvent) { |
| 305 | e.mu.Lock() |
| 306 | defer e.mu.Unlock() |
| 307 | rs, exists := e.roots[re.root] |
| 308 | if !exists { |
| 309 | return |
| 310 | } |
| 311 | changed := false |
| 312 | for _, ev := range re.events { |
| 313 | relPath, err := filepath.Rel(rs.root, ev.Path) |
| 314 | if err != nil { |
| 315 | continue |
| 316 | } |
| 317 | relPath = filepath.ToSlash(relPath) |
| 318 | switch ev.Op { |
| 319 | case OpCreate: |
| 320 | if rs.index.Has(relPath) { |
| 321 | continue |
| 322 | } |
| 323 | var flags uint16 |
| 324 | if ev.IsDir { |
| 325 | flags = uint16(FlagDir) |
| 326 | } |
| 327 | rs.index.Add(relPath, flags) |
| 328 | changed = true |
| 329 | case OpRemove, OpRename: |
| 330 | if rs.index.Remove(relPath) { |
| 331 | changed = true |
| 332 | } |
| 333 | if ev.IsDir || ev.Op == OpRename { |
| 334 | prefix := strings.ToLower(filepath.ToSlash(relPath)) + "/" |
| 335 | for path := range rs.index.byPath { |
| 336 | if strings.HasPrefix(path, prefix) { |
| 337 | rs.index.Remove(path) |
| 338 | changed = true |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | case OpModify: |
| 343 | } |
| 344 | } |
| 345 | if changed { |
| 346 | e.publishSnapshot() |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // publishSnapshot builds and atomically publishes a new snapshot. |
| 351 | // Must be called with e.mu held. |