-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for inotify in mounted directories
Signed-off-by: Balaji Vijayakumar <kuttibalaji.v6@gmail.com>
- Loading branch information
1 parent
c4986e7
commit 21058b0
Showing
11 changed files
with
200 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package hostagent | ||
|
||
import ( | ||
"context" | ||
guestagentapi "github.com/lima-vm/lima/pkg/guestagent/api" | ||
"github.com/lima-vm/lima/pkg/localpathutil" | ||
"github.com/rjeczalik/notify" | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
"path" | ||
) | ||
|
||
const CacheSize = 10000 | ||
|
||
var ( | ||
inotifyCache = make(map[string]string) | ||
) | ||
|
||
func (a *HostAgent) startInotify(ctx context.Context, guestSocketAddr string, localUnix string, remoteUnix string) error { | ||
mountWatchCh := make(chan notify.EventInfo, 128) | ||
err := a.setupWatchers(mountWatchCh) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
select { | ||
|
||
case <-ctx.Done(): | ||
return nil | ||
case watchEvent := <-mountWatchCh: | ||
stat, err := os.Stat(watchEvent.Path()) | ||
if err != nil { | ||
logrus.Warn("ignore inotify event", watchEvent.Path()) | ||
continue | ||
} | ||
|
||
if filterEvents(watchEvent) { | ||
logrus.Warn("ignore inotify event", watchEvent.Path()) | ||
continue | ||
} | ||
|
||
client, err := a.createClient(ctx, guestSocketAddr, localUnix, remoteUnix) | ||
if err != nil { | ||
logrus.WithError(err).Warn("failed to create guestagent for inotify") | ||
continue | ||
} | ||
|
||
event := guestagentapi.InotifyEvent{Location: watchEvent.Path(), Time: stat.ModTime().UTC()} | ||
err = client.Inotify(ctx, event) | ||
if err != nil { | ||
logrus.WithError(err).Warn("failed to send inotify to guestagent") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (a *HostAgent) setupWatchers(events chan notify.EventInfo) error { | ||
for _, m := range a.y.Mounts { | ||
if *m.Writable { | ||
location, err := localpathutil.Expand(m.Location) | ||
if err != nil { | ||
return err | ||
} | ||
err = notify.Watch(path.Join(location, "..."), events, notify.Create|notify.Write) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func filterEvents(event notify.EventInfo) bool { | ||
eventPath := event.Path() | ||
_, ok := inotifyCache[eventPath] | ||
if ok { | ||
//Ignore the duplicate inotify on mounted directories, so always remove a entry if already present | ||
delete(inotifyCache, eventPath) | ||
return true | ||
} | ||
inotifyCache[eventPath] = "" | ||
|
||
if len(inotifyCache) >= CacheSize { | ||
clear(inotifyCache) | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters