summaryrefslogtreecommitdiffstats
path: root/fikenauth/lock_unix.go
diff options
context:
space:
mode:
authorSimen A. W. Olsen <hello@simenandre.no>2026-11-03 22:08:49 +0100
committerSimen A. W. Olsen <hello@simenandre.no>2026-11-03 22:08:49 +0100
commitbef3ccf4521d4ed431f3cdfc66e618c619ce28e4 (patch)
treeef8e6ff90fed4b27d4f2cfd082106f2efdb38ca5 /fikenauth/lock_unix.go
parenta5491c9dccdf4678bf3b4380d208cf2562afeee4 (diff)
downloadfiken-bef3ccf4521d4ed431f3cdfc66e618c619ce28e4.tar.gz
fiken-bef3ccf4521d4ed431f3cdfc66e618c619ce28e4.zip
Share one Fiken login between programsHEADv0.4.0main
fikenconfig keeps an OAuth app and a grant per account in ~/.config/fiken, and finds the account that reaches a company. Renewing a grant takes a lock, so programs sharing it take turns.
Diffstat (limited to 'fikenauth/lock_unix.go')
-rw-r--r--fikenauth/lock_unix.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/fikenauth/lock_unix.go b/fikenauth/lock_unix.go
new file mode 100644
index 0000000..e29472d
--- /dev/null
+++ b/fikenauth/lock_unix.go
@@ -0,0 +1,31 @@
+//go:build unix
+
+package fikenauth
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "syscall"
+)
+
+// lock takes an exclusive lock beside the token file, held until unlock is
+// called. It is advisory: it only orders programs that also take it, which is
+// every program using this package.
+func (s *Store) lock() (unlock func(), err error) {
+ if err := os.MkdirAll(filepath.Dir(s.path), 0o700); err != nil {
+ return nil, fmt.Errorf("create %s: %w", filepath.Dir(s.path), err)
+ }
+ f, err := os.OpenFile(s.path+".lock", os.O_CREATE|os.O_RDWR, 0o600)
+ if err != nil {
+ return nil, fmt.Errorf("lock %s: %w", s.path, err)
+ }
+ if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
+ f.Close()
+ return nil, fmt.Errorf("lock %s: %w", s.path, err)
+ }
+ return func() {
+ syscall.Flock(int(f.Fd()), syscall.LOCK_UN) //nolint:errcheck // closing releases it anyway
+ f.Close()
+ }, nil
+}