fix(clients): сохранять авторизацию при ошибке записи базы

This commit is contained in:
samosvalishe committed 2026-09-12 17:33:46 +03:00
1 parent 6570cd9dd7
commit 061e3b882b
2 files changed
+55 -6

No files matched your search

+11 -6
View File
@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"maps"
"net" "net"
"os" "os"
"sync" "sync"
@@ -61,16 +62,18 @@ func (db *DB) Add(clientID, comment string) error {
db.mu.Lock() db.mu.Lock()
defer db.mu.Unlock() defer db.mu.Unlock()
db.data.Clients[clientID] = ClientInfo{Comment: comment} next := Data{Clients: maps.Clone(db.data.Clients)}
return db.save() next.Clients[clientID] = ClientInfo{Comment: comment}
return db.save(next)
} }
func (db *DB) Remove(clientID string) error { func (db *DB) Remove(clientID string) error {
db.mu.Lock() db.mu.Lock()
defer db.mu.Unlock() defer db.mu.Unlock()
delete(db.data.Clients, clientID) next := Data{Clients: maps.Clone(db.data.Clients)}
return db.save() delete(next.Clients, clientID)
return db.save(next)
} }
func (db *DB) List() map[string]ClientInfo { func (db *DB) List() map[string]ClientInfo {
@@ -126,8 +129,8 @@ func (db *DB) loadIfModified() {
} }
} }
func (db *DB) save() error { func (db *DB) save(next Data) error {
b, err := json.MarshalIndent(db.data, "", " ") b, err := json.MarshalIndent(next, "", " ")
if err != nil { if err != nil {
return err return err
} }
@@ -138,6 +141,8 @@ func (db *DB) save() error {
err = os.Rename(tmpFile, db.path) err = os.Rename(tmpFile, db.path)
} }
if err == nil { if err == nil {
// Авторизация меняется только после успешной замены файла.
db.data = next
stat, _ := os.Stat(db.path) stat, _ := os.Stat(db.path)
if stat != nil { if stat != nil {
db.lastModified = stat.ModTime() db.lastModified = stat.ModTime()
+44
View File
@@ -2,11 +2,55 @@ package clientsdb
import ( import (
"net" "net"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
) )
func TestFailedSavePreservesAuthorization(t *testing.T) {
path := filepath.Join(t.TempDir(), "clients.json")
db, err := New(path)
if err != nil {
t.Fatal(err)
}
if err := db.Add("existing", "original"); err != nil {
t.Fatal(err)
}
// Каталог вместо временного файла воспроизводит отказ записи без прав root.
if err := os.Mkdir(path+".tmp", 0o700); err != nil {
t.Fatal(err)
}
operations := []struct {
name string
run func() error
}{
{"add", func() error { return db.Add("new", "new") }},
{"update", func() error { return db.Add("existing", "changed") }},
{"remove", func() error { return db.Remove("existing") }},
}
for _, op := range operations {
t.Run(op.name, func(t *testing.T) {
if err := op.run(); err == nil {
t.Fatal("save unexpectedly succeeded")
}
if !db.IsAuthorized("existing") || db.IsAuthorized("new") {
t.Fatal("failed save changed authorization")
}
if got := db.List(); len(got) != 1 || got["existing"].Comment != "original" {
t.Fatalf("failed save changed clients: %+v", got)
}
})
}
loaded, err := New(path)
if err != nil {
t.Fatal(err)
}
if got := loaded.List(); len(got) != 1 || got["existing"].Comment != "original" {
t.Fatalf("disk changed: %+v", got)
}
}
func TestClientsDB(t *testing.T) { func TestClientsDB(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "clients.json") dbPath := filepath.Join(tmpDir, "clients.json")