aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/id_alloc.go
blob: 3a3c973065bb9e88c50ebf935eb4248b6843ea0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package admin

import (
	"os"
	"path/filepath"
	"strconv"
)

// nextRoomID allocates a free room ID for a new room that will live in the same
// directory as fromRoomID (so new IDs cluster near their neighbors). Unlike the
// old per-subdir scan, the candidate is checked against EVERY room ID on disk
// (via listRoomIDs, which walks the whole data/rooms tree), so it can never
// collide with an ID that lives in a different subdirectory. The starting point
// is the local subdir's minimum existing ID (clustering), then the first
// globally-free ID scanning upward.
func (s *AdminServer) nextRoomID(fromRoomID int) (int, string, error) {
	base := filepath.Join(s.dataDir, "rooms")
	subdir, err := findRoomSubdir(base, fromRoomID)
	if err != nil {
		subdir = base
	}

	localUsed := map[int]bool{}
	scanDir(subdir, localUsed)
	start := 1
	if len(localUsed) > 0 {
		minID := 1<<31 - 1
		for id := range localUsed {
			if id < minID {
				minID = id
			}
		}
		start = minID
	}

	globalUsed, _ := listRoomIDs(s.dataDir)
	used := make(map[int]bool, len(globalUsed))
	for _, id := range globalUsed {
		used[id] = true
	}

	id := start
	if id < 1 {
		id = 1
	}
	for used[id] {
		id++
	}
	return id, subdir, nil
}

func findRoomSubdir(base string, roomID int) (string, error) {
	fileName := strconv.Itoa(roomID) + ".yaml"
	var found string
	filepath.WalkDir(base, func(path string, d os.DirEntry, err error) error {
		if err != nil || found != "" {
			return nil
		}
		if !d.IsDir() && d.Name() == fileName {
			found = filepath.Dir(path)
		}
		return nil
	})
	if found == "" {
		return "", os.ErrNotExist
	}
	return found, nil
}

func scanDir(dir string, used map[int]bool) {
	entries, err := os.ReadDir(dir)
	if err != nil {
		return
	}
	for _, e := range entries {
		if !e.IsDir() && filepath.Ext(e.Name()) == ".yaml" {
			idStr := e.Name()[:len(e.Name())-5]
			if id, err := strconv.Atoi(idStr); err == nil && id > 0 {
				used[id] = true
			}
		}
	}
}