-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcodex_sqlite.go
More file actions
87 lines (80 loc) · 2.01 KB
/
Copy pathcodex_sqlite.go
File metadata and controls
87 lines (80 loc) · 2.01 KB
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
84
85
86
87
package main
import (
"os"
"path/filepath"
"sort"
"strings"
)
func codexSQLiteHome(home string) string {
if value := strings.TrimSpace(os.Getenv("CODEX_SQLITE_HOME")); value != "" {
return filepath.Clean(os.ExpandEnv(value))
}
return filepath.Clean(home)
}
func codexSessionDBPaths(home string) []string {
sqliteHome := codexSQLiteHome(home)
paths := make([]string, 0)
seen := map[string]bool{}
add := func(path string) {
path = filepath.Clean(path)
if path == "" || seen[path] || !fileExists(path) {
return
}
seen[path] = true
paths = append(paths, path)
}
sqliteDir := filepath.Join(sqliteHome, "sqlite")
if entries, err := os.ReadDir(sqliteDir); err == nil {
for _, entry := range entries {
if entry.IsDir() {
continue
}
lower := strings.ToLower(entry.Name())
if strings.HasSuffix(lower, ".db") || strings.HasSuffix(lower, ".sqlite") {
add(filepath.Join(sqliteDir, entry.Name()))
}
}
}
sort.Strings(paths)
add(filepath.Join(sqliteHome, "state_5.sqlite"))
return paths
}
func codexPreferredSessionDBPath(home string) string {
paths := codexSessionDBPaths(home)
for _, path := range paths {
if sqlitePathHasTable(path, "threads") {
return path
}
}
return filepath.Join(codexSQLiteHome(home), "state_5.sqlite")
}
func sqlitePathHasTable(path, table string) bool {
if !fileExists(path) {
return false
}
db, err := openSQLite(path)
if err != nil {
return false
}
defer db.Close()
columns, err := sqliteTableColumns(db, table)
return err == nil && len(columns) > 0
}
func codexLogsDBPath(home string) string {
return filepath.Join(codexSQLiteHome(home), "logs_2.sqlite")
}
func pathWithin(root, candidate string) bool {
rootAbs, err := filepath.Abs(root)
if err != nil {
return false
}
candidateAbs, err := filepath.Abs(candidate)
if err != nil {
return false
}
relative, err := filepath.Rel(rootAbs, candidateAbs)
if err != nil {
return false
}
return relative == "." || relative != ".." && !strings.HasPrefix(relative, ".."+string(os.PathSeparator))
}