@@ -299,7 +299,7 @@ func TestWithCapSysAdmFanotifyFileOrDirectoryCreated(t *testing.T) {
299299 watchDir := t .TempDir ()
300300
301301 t .Logf ("Watch Directory: %s" , watchDir )
302- action := FileOrDirCreated
302+ action := FileOrDirectoryCreated
303303 l .AddWatch (watchDir , action )
304304 go l .Start ()
305305 defer l .Stop ()
@@ -345,7 +345,7 @@ func TestWithCapSysAdmFanotifyFileDeleted(t *testing.T) {
345345 }
346346}
347347
348- func TestWithCapSysAdmFanotifyFileOrDirDeleted (t * testing.T ) {
348+ func TestWithCapSysAdmFanotifyFileOrDirectoryDeleted (t * testing.T ) {
349349
350350 l , err := NewListener ("/" , 4096 , true )
351351 assert .Nil (t , err )
@@ -357,7 +357,7 @@ func TestWithCapSysAdmFanotifyFileOrDirDeleted(t *testing.T) {
357357 assert .Nil (t , err )
358358
359359 t .Logf ("Watch Directory: %s" , watchDir )
360- action := FileOrDirDeleted
360+ action := FileOrDirectoryDeleted
361361 l .AddWatch (watchDir , action )
362362 go l .Start ()
363363 defer l .Stop ()
@@ -390,7 +390,7 @@ func TestMultipleEvents(t *testing.T) {
390390 defer l .Stop ()
391391
392392 watchDir := t .TempDir ()
393- actions := FileOrDirCreated .Or (FileModified .Or (FileDeleted ))
393+ actions := FileOrDirectoryCreated .Or (FileModified .Or (FileDeleted ))
394394 l .AddWatch (watchDir , actions )
395395 testFile := fmt .Sprintf ("%s/test.txt" , watchDir )
396396 pid , err := runAsCmd ("touch" , testFile ) // create file
@@ -437,3 +437,102 @@ func TestMultipleEvents(t *testing.T) {
437437 t .Logf ("Received: (%s)" , event )
438438 }
439439}
440+
441+ // FileCreated and FileClosed combination does not raise any events
442+ func TestWithCapSysAdmMarkCreateCloseBug (t * testing.T ) {
443+ l , err := NewListener ("/" , 4096 , true )
444+ assert .Nil (t , err )
445+ assert .NotNil (t , l )
446+ go l .Start ()
447+ defer l .Stop ()
448+
449+ watchDir := t .TempDir ()
450+ actions := FileCreated .Or (FileClosed )
451+ l .AddWatch (watchDir , actions )
452+ testFile := fmt .Sprintf ("%s/test.txt" , watchDir )
453+ pid , err := runAsCmd ("touch" , testFile ) // create file
454+ assert .Nil (t , err )
455+ select {
456+ case <- time .After (100 * time .Millisecond ):
457+ assert .Failf (t , "BUG: no events after file create" , "confirmed that no events are raised if mark contains unix.FAN_CREATE|unix.FAN_CLOSE" )
458+ case event := <- l .Events :
459+ assert .Equal (t , fmt .Sprintf ("%s/%s" , event .Path , event .FileName ), testFile )
460+ assert .Equal (t , event .Pid , pid )
461+ t .Logf ("Received: (%s)" , event )
462+ }
463+
464+ // cat the file to simulate close after read
465+ pid , err = runAsCmd ("cat" , testFile )
466+ assert .Nil (t , err )
467+ select {
468+ case <- time .After (100 * time .Millisecond ):
469+ assert .Failf (t , "BUG: no events after file close" , "confirmed that no events are raised if mark contains unix.FAN_CREATE|unix.FAN_CLOSE" )
470+ case event := <- l .Events :
471+ assert .Equal (t , fmt .Sprintf ("%s/%s" , event .Path , event .FileName ), testFile )
472+ assert .Equal (t , event .Pid , pid )
473+ t .Logf ("Received: (%s)" , event )
474+ }
475+ }
476+
477+ // FileCreated and FileClosed combination does not raise any events
478+ func TestWithCapSysAdmMarkFileOrDirectoryOpenedBug (t * testing.T ) {
479+ // setup the file for modification
480+ watchDir := t .TempDir ()
481+ testFile := fmt .Sprintf ("%s/test.txt" , watchDir )
482+ _ , err := runAsCmd ("touch" , testFile ) // create file
483+ assert .Nil (t , err )
484+
485+ // start the listener
486+ l , err := NewListener ("/" , 4096 , true )
487+ assert .Nil (t , err )
488+ assert .NotNil (t , l )
489+ go l .Start ()
490+ defer l .Stop ()
491+ var actions Action
492+ actions =
493+ FileAccessed |
494+ FileOrDirectoryAccessed |
495+ FileModified |
496+ FileOpenedForExec |
497+ FileAttribChanged |
498+ FileOrDirectoryAttribChanged |
499+ FileCreated |
500+ FileOrDirectoryCreated |
501+ FileDeleted |
502+ FileOrDirectoryDeleted |
503+ WatchedFileDeleted |
504+ WatchedFileOrDirectoryDeleted |
505+ FileMovedFrom |
506+ FileOrDirectoryMovedFrom |
507+ FileMovedTo |
508+ FileOrDirectoryMovedTo |
509+ WatchedFileMoved |
510+ WatchedFileOrDirectoryMoved |
511+ FileOrDirectoryOpened
512+ l .AddWatch (watchDir , actions )
513+
514+ // cat the file to simulate close after read
515+ _ , err = runAsCmd ("cat" , testFile )
516+ assert .Nil (t , err )
517+ n := 0
518+ for len (l .Events ) > 0 {
519+ e := <- l .Events
520+ t .Logf ("drain-event: %v" , e )
521+ n ++
522+ }
523+ t .Logf ("BUG: received %d events; while cat would otherwise raise 1 FileAccessed event" , n )
524+
525+ // BUG more duplicate flood events or no further events are received past this
526+ // attribute change file
527+ pid , err := runAsCmd ("chmod" , "+x" , testFile )
528+ assert .Nil (t , err )
529+ select {
530+ case <- time .After (100 * time .Millisecond ):
531+ assert .Failf (t , "BUG: no events after chmod" , "no events after the duplicate event flood" )
532+ case event := <- l .Events :
533+ assert .Equal (t , fmt .Sprintf ("%s/%s" , event .Path , event .FileName ), testFile )
534+ assert .Equal (t , event .Pid , pid )
535+ assert .True (t , event .Actions .Has (FileAttribChanged ))
536+ t .Logf ("Received: (%s)" , event )
537+ }
538+ }
0 commit comments