From c5eeea64ac62bd1b5bb1ab40329c7d343abeeb43 Mon Sep 17 00:00:00 2001
From: the-y <the-y@riseup.net>
Date: Fri, 17 Feb 2023 14:44:41 +0100
Subject: [PATCH] Fix cache files overwriting each other if using senpai
with
different config paths. We solve this problem by creating a hash of the
configPath and prepend this to -lastbuffer.txt and -laststamp.txt
---
cmd/senpai/main.go | 43 +++++++++++++++++++++++++++----------------
1 file changed, 27 insertions(+), 16 deletions(-)
diff --git a/cmd/senpai/main.go b/cmd/senpai/main.go
index 6434182..b84c7c5 100644
--- a/cmd/senpai/main.go
+++ b/cmd/senpai/main.go
@@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
+ "hash/fnv"
"io/ioutil"
"math/rand"
"os"
@@ -35,6 +36,8 @@ func main() {
configPath = path.Join(configDir, "senpai",
"senpai.scfg")
}
+ var configPathHash = configPathHash(configPath)
+
cfg, err := senpai.LoadConfigFile(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load the required
configuration file at %q: %s\n", configPath, err)
@@ -48,9 +51,9 @@ func main() {
panic(err)
}
- lastNetID, lastBuffer := getLastBuffer()
+ lastNetID, lastBuffer := getLastBuffer(configPathHash)
app.SwitchToBuffer(lastNetID, lastBuffer)
- app.SetLastClose(getLastStamp())
+ app.SetLastClose(getLastStamp(configPathHash))
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM,
syscall.SIGHUP)
@@ -62,8 +65,14 @@ func main() {
app.Run()
app.Close()
- writeLastBuffer(app)
- writeLastStamp(app)
+ writeLastBuffer(configPathHash, app)
+ writeLastStamp(configPathHash, app)
+}
+
+func configPathHash(configPath string) string {
+ h := fnv.New32a()
+ h.Write([]byte(configPath))
+ return fmt.Sprint(h.Sum32())
}
func cachePath() string {
@@ -79,12 +88,13 @@ func cachePath() string {
return cache
}
-func lastBufferPath() string {
- return path.Join(cachePath(), "lastbuffer.txt")
+func lastBufferPath(configPathHash string) string {
+ var filename = fmt.Sprintf("lastbuffer-%s.txt", configPathHash)
+ return path.Join(cachePath(), filename)
}
-func getLastBuffer() (netID, buffer string) {
- buf, err := ioutil.ReadFile(lastBufferPath())
+func getLastBuffer(configPathHash string) (netID, buffer string) {
+ buf, err := ioutil.ReadFile(lastBufferPath(configPathHash))
if err != nil {
return "", ""
}
@@ -97,8 +107,8 @@ func getLastBuffer() (netID, buffer string) {
return fields[0], fields[1]
}
-func writeLastBuffer(app *senpai.App) {
- lastBufferPath := lastBufferPath()
+func writeLastBuffer(configPathHash string, app *senpai.App) {
+ lastBufferPath := lastBufferPath(configPathHash)
lastNetID, lastBuffer := app.CurrentBuffer()
err := os.WriteFile(lastBufferPath, []byte(fmt.Sprintf("%s %s",
lastNetID, lastBuffer)), 0666)
if err != nil {
@@ -106,12 +116,13 @@ func writeLastBuffer(app *senpai.App) {
}
}
-func lastStampPath() string {
- return path.Join(cachePath(), "laststamp.txt")
+func lastStampPath(configPathHash string) string {
+ var filename = fmt.Sprintf("laststamp-%s.txt", configPathHash)
+ return path.Join(cachePath(), filename)
}
-func getLastStamp() time.Time {
- buf, err := ioutil.ReadFile(lastStampPath())
+func getLastStamp(configPathHash string) time.Time {
+ buf, err := ioutil.ReadFile(lastStampPath(configPathHash))
if err != nil {
return time.Time{}
}
@@ -124,8 +135,8 @@ func getLastStamp() time.Time {
return t
}
-func writeLastStamp(app *senpai.App) {
- lastStampPath := lastStampPath()
+func writeLastStamp(configPathHash string, app *senpai.App) {
+ lastStampPath := lastStampPath(configPathHash)
last := app.LastMessageTime()
if last.IsZero() {
return
--
2.39.2