-
Notifications
You must be signed in to change notification settings - Fork 608
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 <[email protected]>
- Loading branch information
1 parent
c4986e7
commit 0baf073
Showing
12 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
Empty file.
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,88 @@ | ||
package hostagent | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
|
||
guestagentapi "github.com/lima-vm/lima/pkg/guestagent/api" | ||
"github.com/lima-vm/lima/pkg/localpathutil" | ||
"github.com/rjeczalik/notify" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
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) | ||
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") | ||
} | ||
|
||
err = client.Inotify(ctx, guestagentapi.InotifyEvent{Location: watchEvent.Path(), Mode: stat.Mode()}) | ||
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.All) | ||
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