Lockfile moved to device

This commit is contained in:
Matthias Fulz 2022-06-27 02:08:38 +02:00
parent a08904ff33
commit 0c6a88cb60
1 changed files with 5 additions and 24 deletions

View File

@ -102,23 +102,6 @@ func initInfoType() {
}
}
func lock() error {
for {
if _, err := os.Stat(viper.GetString("lockfile")); err == nil {
slog.LOG_DEBUGLN("Waiting for lock")
time.Sleep(time.Second * 1)
continue
} else if os.IsNotExist(err) {
file, err := os.Create(viper.GetString("lockfile"))
if err != nil {
return err
}
file.Close()
return nil
}
}
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "eqmk",
@ -133,17 +116,15 @@ to decrypt encrypted logins and passwords transparent for browsers.`,
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if err := lock(); err != nil {
slog.LOG_ERRORLN(err)
os.Exit(-1)
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
go func() {
<-sigChan
slog.LOG_DEBUGLN("Removing lockfile")
os.Remove(viper.GetString("lockfile"))
slog.LOG_DEBUGLN("Closing device")
if qdev != nil {
qdev.Close()
}
}()
slog.LOG_DEBUGLN("Root Pre run started")
@ -173,7 +154,7 @@ to decrypt encrypted logins and passwords transparent for browsers.`,
os.Exit(-1)
}
}
qdev = qmkenc.QEncNewDevice(actDev, uint8(viper.GetInt("device.blocksize")), viper.GetInt("device.timeout"), viper.GetInt("device.retry_timeout"), viper.GetInt("device.retry_wait"))
qdev = qmkenc.QEncNewDevice(actDev, uint8(viper.GetInt("device.blocksize")), viper.GetInt("device.timeout"), viper.GetInt("device.retry_timeout"), viper.GetInt("device.retry_wait"), viper.GetString("lockfile"))
if err := qdev.Open(); err != nil {
slog.LOG_ERRORLN(err)
os.Exit(-1)