1 Star 0 Fork 1

mysnapcore/mysnapd

forked from tupelo-shen/mysnapd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
inhibit.go 4.15 KB
一键复制 编辑 原始数据 按行查看 历史
tupelo-shen 提交于 2022-11-08 22:37 . fix: cmd commit
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"context"
"fmt"
"os"
"time"
"gitee.com/mysnapcore/mysnapd/cmd/snaplock/runinhibit"
"gitee.com/mysnapcore/mysnapd/i18n"
"gitee.com/mysnapcore/mysnapd/progress"
"gitee.com/mysnapcore/mysnapd/usersession/client"
)
func waitWhileInhibited(snapName string) error {
hint, err := runinhibit.IsLocked(snapName)
if err != nil {
return err
}
if hint == runinhibit.HintNotInhibited {
return nil
}
// wait for HintInhibitedForRefresh set by gate-auto-refresh hook handler
// when it has finished; the hook starts with HintInhibitedGateRefresh lock
// and then either unlocks it or changes to HintInhibitedForRefresh (see
// gateAutoRefreshHookHandler in hooks.go).
// waitInhibitUnlock will return also on HintNotInhibited.
notInhibited, err := waitInhibitUnlock(snapName, runinhibit.HintInhibitedForRefresh)
if err != nil {
return err
}
if notInhibited {
return nil
}
if isGraphicalSession() {
return graphicalSessionFlow(snapName, hint)
}
// terminal and headless
return textFlow(snapName, hint)
}
func inhibitMessage(snapName string, hint runinhibit.Hint) string {
switch hint {
case runinhibit.HintInhibitedForRefresh:
return fmt.Sprintf(i18n.G("snap package %q is being refreshed, please wait"), snapName)
default:
return fmt.Sprintf(i18n.G("snap package cannot be used now: %s"), string(hint))
}
}
var isGraphicalSession = func() bool {
return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != ""
}
var pendingRefreshNotification = func(refreshInfo *client.PendingSnapRefreshInfo) error {
userclient := client.NewForUids(os.Getuid())
if err := userclient.PendingRefreshNotification(context.TODO(), refreshInfo); err != nil {
return err
}
return nil
}
var finishRefreshNotification = func(refreshInfo *client.FinishedSnapRefreshInfo) error {
userclient := client.NewForUids(os.Getuid())
if err := userclient.FinishRefreshNotification(context.TODO(), refreshInfo); err != nil {
return err
}
return nil
}
func graphicalSessionFlow(snapName string, hint runinhibit.Hint) error {
refreshInfo := client.PendingSnapRefreshInfo{
InstanceName: snapName,
// Remaining time = 0 results in "Snap .. is refreshing now" message from
// usersession agent.
TimeRemaining: 0,
}
if err := pendingRefreshNotification(&refreshInfo); err != nil {
return err
}
if _, err := waitInhibitUnlock(snapName, runinhibit.HintNotInhibited); err != nil {
return err
}
finishRefreshInfo := client.FinishedSnapRefreshInfo{InstanceName: snapName}
return finishRefreshNotification(&finishRefreshInfo)
}
func textFlow(snapName string, hint runinhibit.Hint) error {
fmt.Fprintf(Stdout, "%s\n", inhibitMessage(snapName, hint))
pb := progress.MakeProgressBar(Stdout)
pb.Spin(i18n.G("please wait..."))
_, err := waitInhibitUnlock(snapName, runinhibit.HintNotInhibited)
pb.Finished()
return err
}
var isLocked = runinhibit.IsLocked
// waitInhibitUnlock waits until the runinhibit lock hint has a specific waitFor value
// or isn't inhibited anymore.
var waitInhibitUnlock = func(snapName string, waitFor runinhibit.Hint) (notInhibited bool, err error) {
// Every 0.5s check if the inhibition file is still present.
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Half a second has elapsed, let's check again.
hint, err := isLocked(snapName)
if err != nil {
return false, err
}
if hint == runinhibit.HintNotInhibited {
return true, nil
}
if hint == waitFor {
return false, nil
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mysnapcore/mysnapd.git
git@gitee.com:mysnapcore/mysnapd.git
mysnapcore
mysnapd
mysnapd
v0.0.1

搜索帮助