Skip to content

Commit 84a31bc

Browse files
committed
Fix cross platform build with locking
1 parent 4054ef7 commit 84a31bc

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/getlantern/systray v1.2.2
1010
github.com/jeromehadorn/vss v0.1.0
1111
github.com/klauspost/compress v1.17.4
12+
github.com/rodolfoag/gow32 v0.0.0-20230512144032-1e896a3c51aa
1213
github.com/tawesoft/golib/v2 v2.10.0
1314
golang.org/x/net v0.19.0
1415
)
@@ -27,7 +28,6 @@ require (
2728
github.com/godbus/dbus/v5 v5.1.0 // indirect
2829
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
2930
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
30-
github.com/rodolfoag/gow32 v0.0.0-20230512144032-1e896a3c51aa // indirect
3131
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
3232
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a // indirect
3333
golang.org/x/sys v0.15.0 // indirect

main.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ import (
1111
"os"
1212
"runtime"
1313
"sync/atomic"
14-
"syscall"
1514
"time"
1615

1716
"github.com/cornelk/hashmap"
1817
"github.com/gen2brain/beeep"
1918
"github.com/getlantern/systray"
2019
"github.com/tawesoft/golib/v2/dialog"
21-
"github.com/rodolfoag/gow32"
2220
)
2321

24-
const MutexName = "proxmoxbackupclient_go"
22+
2523

2624
var defaultMailSubjectTemplate = "Backup {{.Status}}"
2725
var defaultMailBodyTemplate = `{{if .Success}}Backup complete ({{.FromattedDuration}})
@@ -78,27 +76,26 @@ func main() {
7876
os.Exit(1)
7977
}
8078

81-
if runtime.GOOS == "windows" {
82-
mutexid, err := gow32.CreateMutex(MutexName)
83-
if (err != nil) {
84-
if exitcode := int(err.(syscall.Errno)); exitcode == gow32.ERROR_ALREADY_EXISTS {
85-
dialog.Error("Backup jobs need to run exclusively, please wait until the previous job has finished")
86-
os.Exit(2)
87-
}
88-
// this should never happen
89-
panic(err)
90-
}
91-
defer gow32.ReleaseMutex(mutexid)
79+
L := Locking{}
9280

93-
go systray.Run(func() {
94-
systray.SetIcon(ICON)
95-
systray.SetTooltip("PBSGO Backup running")
96-
beeep.Notify("Proxmox Backup Go", "Backup started", "")
97-
},
98-
func() {
99-
100-
})
81+
82+
lock_ok := L.AcquireProcessLock()
83+
if !lock_ok {
84+
85+
dialog.Error("Backup jobs need to run exclusively, please wait until the previous job has finished")
86+
os.Exit(2)
10187
}
88+
defer L.ReleaseProcessLock()
89+
90+
go systray.Run(func() {
91+
systray.SetIcon(ICON)
92+
systray.SetTooltip("PBSGO Backup running")
93+
beeep.Notify("Proxmox Backup Go", "Backup started", "")
94+
},
95+
func() {
96+
97+
})
98+
10299

103100
insecure := cfg.CertFingerprint != ""
104101

stub_locking.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build linux darwin freebsd openbsd
2+
package main
3+
4+
type Locking struct {
5+
mutexid uintptr
6+
}
7+
8+
func (l *Locking) AcquireProcessLock() bool {
9+
return true
10+
}
11+
12+
func (l * Locking) ReleaseProcessLock() {
13+
14+
}

win_locking.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build windows
2+
// +build windows
3+
package main
4+
5+
import "github.com/rodolfoag/gow32"
6+
import "syscall"
7+
8+
const MutexName = "proxmoxbackupclient_go"
9+
10+
11+
type Locking struct {
12+
mutexid uintptr
13+
}
14+
15+
func (l *Locking) AcquireProcessLock() bool {
16+
mutexid , err := gow32.CreateMutex(MutexName)
17+
if err != nil {
18+
if exitcode := int(err.(syscall.Errno)); exitcode == gow32.ERROR_ALREADY_EXISTS {
19+
return false
20+
}
21+
panic(err)
22+
}
23+
l.mutexid = mutexid
24+
return true
25+
}
26+
27+
func (l * Locking) ReleaseProcessLock() {
28+
gow32.ReleaseMutex(l.mutexid)
29+
}

0 commit comments

Comments
 (0)