| 1 | // Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail |
| 2 | // Copyright (c) 2015 HPE Software Inc. All rights reserved. |
| 3 | // Copyright (c) 2013 ActiveState Software Inc. All rights reserved. |
| 4 | |
| 5 | package watchscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 10 | "path/filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/" |
| 11 | |
| 12 | "github.com/nxadm/tail/utilscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/util`/" |
| 13 | |
| 14 | "github.com/fsnotify/fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/" |
| 15 | "gopkg.in/tomb.v1scip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/" |
| 16 | ) |
| 17 | |
| 18 | // InotifyFileWatcher uses inotify to monitor file changes. |
| 19 | type InotifyFileWatcherscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher# struct { |
| 20 | Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename. string |
| 21 | Sizescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Size. int64 |
| 22 | } |
| 23 | |
| 24 | func NewInotifyFileWatcherscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/NewInotifyFileWatcher().(filename string) *InotifyFileWatcherscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher# { |
| 25 | fw := &InotifyFileWatcherscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#{filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/Clean().(filename), 0} |
| 26 | return fw |
| 27 | } |
| 28 | |
| 29 | func (fw *InotifyFileWatcherscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#) BlockUntilExistsscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#BlockUntilExists().(t *tombscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/.Tombscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/Tomb#) error { |
| 30 | err := WatchCreatescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/WatchCreate().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | defer RemoveWatchCreatescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/RemoveWatchCreate().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 35 | |
| 36 | // Do a real check now as the file might have been created before |
| 37 | // calling `WatchFlags` above. |
| 38 | if _, err = osscip-go gomod github.com/golang/go/src go1.13 os/.Statscip-go gomod github.com/golang/go/src go1.13 os/Stat().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.); !osscip-go gomod github.com/golang/go/src go1.13 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.13 os/IsNotExist().(err) { |
| 39 | // file exists, or stat returned an error. |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | events := Eventsscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/Events().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 44 | |
| 45 | for { |
| 46 | select { |
| 47 | case evt, ok := <-events: |
| 48 | if !ok { |
| 49 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("inotify watcher has been closed") |
| 50 | } |
| 51 | evtName, err := filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/Abs().(evt.Namescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Event#Name.) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | fwFilename, err := filepathscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.13 `path/filepath`/Abs().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if evtName == fwFilename { |
| 60 | return nil |
| 61 | } |
| 62 | case <-t.Dyingscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/Tomb#Dying().(): |
| 63 | return tombscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/.ErrDyingscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/ErrDying. |
| 64 | } |
| 65 | } |
| 66 | panic("unreachable") |
| 67 | } |
| 68 | |
| 69 | func (fw *InotifyFileWatcherscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#) ChangeEventsscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#ChangeEvents().(t *tombscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/.Tombscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/Tomb#, pos int64) (*FileChangesscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/FileChanges#, error) { |
| 70 | err := Watchscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/Watch().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | changes := NewFileChangesscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/NewFileChanges().() |
| 76 | fw.Sizescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Size. = pos |
| 77 | |
| 78 | go func() { |
| 79 | |
| 80 | events := Eventsscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/Events().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 81 | |
| 82 | for { |
| 83 | prevSize := fw.Sizescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Size. |
| 84 | |
| 85 | var evt fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Eventscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Event# |
| 86 | var ok bool |
| 87 | |
| 88 | select { |
| 89 | case evt, ok = <-events: |
| 90 | if !ok { |
| 91 | RemoveWatchscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/RemoveWatch().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 92 | return |
| 93 | } |
| 94 | case <-t.Dyingscip-go gomod gopkg.in/tomb.v1 dd632973f1e7 `gopkg.in/tomb.v1`/Tomb#Dying().(): |
| 95 | RemoveWatchscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/RemoveWatch().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 96 | return |
| 97 | } |
| 98 | |
| 99 | switch { |
| 100 | case evt.Opscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Event#Op.&fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Removescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Remove. == fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Removescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Remove.: |
| 101 | fallthrough |
| 102 | |
| 103 | case evt.Opscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Event#Op.&fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Renamescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Rename. == fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Renamescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Rename.: |
| 104 | RemoveWatchscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/RemoveWatch().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 105 | changes.NotifyDeletedscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/FileChanges#NotifyDeleted().() |
| 106 | return |
| 107 | |
| 108 | //With an open fd, unlink(fd) - inotify returns IN_ATTRIB (==fsnotify.Chmod) |
| 109 | case evt.Opscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Event#Op.&fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Chmodscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Chmod. == fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Chmodscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Chmod.: |
| 110 | fallthrough |
| 111 | |
| 112 | case evt.Opscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Event#Op.&fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Writescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Write. == fsnotifyscip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/.Writescip-go gomod github.com/fsnotify/fsnotify v1.6.0 `github.com/fsnotify/fsnotify`/Write.: |
| 113 | fi, err := osscip-go gomod github.com/golang/go/src go1.13 os/.Statscip-go gomod github.com/golang/go/src go1.13 os/Stat().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 114 | if err != nil { |
| 115 | if osscip-go gomod github.com/golang/go/src go1.13 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.13 os/IsNotExist().(err) { |
| 116 | RemoveWatchscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/RemoveWatch().(fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename.) |
| 117 | changes.NotifyDeletedscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/FileChanges#NotifyDeleted().() |
| 118 | return |
| 119 | } |
| 120 | // XXX: report this error back to the user |
| 121 | utilscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/util`/.Fatalscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/util`/Fatal().("Failed to stat file %v: %v", fw.Filenamescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Filename., err) |
| 122 | } |
| 123 | fw.Sizescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Size. = fi.Sizescip-go gomod github.com/golang/go/src go1.13 `io/fs`/FileInfo#Size().() |
| 124 | |
| 125 | if prevSize > 0 && prevSize > fw.Sizescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Size. { |
| 126 | changes.NotifyTruncatedscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/FileChanges#NotifyTruncated().() |
| 127 | } else { |
| 128 | changes.NotifyModifiedscip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/FileChanges#NotifyModified().() |
| 129 | } |
| 130 | prevSize = fw.Sizescip-go gomod github.com/nxadm/tail . `github.com/nxadm/tail/watch`/InotifyFileWatcher#Size. |
| 131 | } |
| 132 | } |
| 133 | }() |
| 134 | |
| 135 | return changes, nil |
| 136 | } |