Lockfile moved to device

This commit is contained in:
Matthias Fulz 2022-06-27 02:08:38 +02:00
parent a08904ff33
commit 0c6a88cb60

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 // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "eqmk", Use: "eqmk",
@ -133,17 +116,15 @@ to decrypt encrypted logins and passwords transparent for browsers.`,
// has an action associated with it: // has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { }, // Run: func(cmd *cobra.Command, args []string) { },
PersistentPreRun: 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) sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
go func() { go func() {
<-sigChan <-sigChan
slog.LOG_DEBUGLN("Removing lockfile") slog.LOG_DEBUGLN("Closing device")
os.Remove(viper.GetString("lockfile")) if qdev != nil {
qdev.Close()
}
}() }()
slog.LOG_DEBUGLN("Root Pre run started") slog.LOG_DEBUGLN("Root Pre run started")
@ -173,7 +154,7 @@ to decrypt encrypted logins and passwords transparent for browsers.`,
os.Exit(-1) 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 { if err := qdev.Open(); err != nil {
slog.LOG_ERRORLN(err) slog.LOG_ERRORLN(err)
os.Exit(-1) os.Exit(-1)