summaryrefslogtreecommitdiff
path: root/gui/internal/model/mainconf.go
blob: 327451ec892493f8f792649e2dfe9d6a60fd9c5d (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-License-Identifier: GPL-3.0-or-later

package model

import (
	"crypto/sha256"
	"fmt"
	"os"
	"strings"

	"krino/internal/config"
	"krino/internal/engine"
	"krino/internal/sexp"
)

// MainSettings are the settings krino.conf carries for every directory,
// inside its (defaults ...) form, in the order the window shows them.
var MainSettings = []string{
	"case", "fold", "recursive", "min-age", "max-read", "max-size",
	"busy", "on-conflict",
}

// MainConf is krino.conf open for editing. Only its (defaults ...) block
// and the log path are edited here - the includes and the excludes are the
// file's own business, and the Text tab is where those are written (GUI
// design §5).
type MainConf struct {
	File string
	Text string

	e     *engine.Engine
	saved string
	stamp [32]byte
}

// OpenMain reads krino.conf.
func OpenMain(e *engine.Engine) (*MainConf, error) {
	text, err := os.ReadFile(e.MainFile)
	if err != nil {
		return nil, err
	}
	return &MainConf{File: e.MainFile, Text: string(text), e: e,
		saved: string(text), stamp: sha256.Sum256(text)}, nil
}

// Modified reports whether the text differs from what is on disk.
func (m *MainConf) Modified() bool { return m.Text != m.saved }

// Check loads the whole configuration with this text in place of the file
// and reports what is wrong with it.
func (m *MainConf) Check() []*config.Diag {
	over := map[string][]byte{m.File: []byte(m.Text)}
	_, diags := engine.LoadWith(m.File, over)
	return diags
}

// Setting reads one default as it is written: "log" from the top level,
// everything else from inside (defaults ...).
func (m *MainConf) Setting(head string) (args string, set bool, err error) {
	if head != "log" && !isMainSetting(head) {
		return "", false, fmt.Errorf("model: %s is not a setting of krino.conf", head)
	}
	node, err := m.node(head)
	if err != nil || node == nil {
		return "", false, err
	}
	return argsOf(node, m.Text), true, nil
}

// SetSetting writes one default, or takes it out when args is empty. A
// setting that is not in the file yet is written into (defaults ...), which
// is created if the file has none.
func (m *MainConf) SetSetting(head, args string) error {
	if head != "log" && !isMainSetting(head) {
		return fmt.Errorf("model: %s is not a setting of krino.conf", head)
	}
	args = strings.TrimSpace(args)
	node, err := m.node(head)
	if err != nil {
		return err
	}
	switch {
	case node != nil && args == "":
		start, end := lineStart(m.Text, node.Pos.Offset), lineEnd(m.Text, node.End.Offset)
		m.Text = join(m.Text[:start], m.Text[end:])
	case node != nil:
		m.Text = m.Text[:node.Pos.Offset] + "(" + head + " " + args + ")" + m.Text[node.End.Offset:]
	case head == "log":
		m.Text = join(m.Text, "\n(log "+args+")\n")
	default:
		if err := m.setInDefaults(head, args); err != nil {
			return err
		}
	}
	return nil
}

// setInDefaults writes a setting inside (defaults ...), adding the form
// when the file has none.
func (m *MainConf) setInDefaults(head, args string) error {
	defaults, err := m.topLevel("defaults")
	if err != nil {
		return err
	}
	if defaults == nil {
		m.Text = join(m.Text, "\n(defaults\n  ("+head+" "+args+"))\n")
		return nil
	}
	// Just inside the closing paren, on a line of its own.
	at := defaults.End.Offset - 1
	m.Text = m.Text[:at] + "\n  (" + head + " " + args + ")" + m.Text[at:]
	return nil
}

// node is the form for a setting: (log ...) at the top level, the rest
// inside (defaults ...).
func (m *MainConf) node(head string) (*sexp.Node, error) {
	if head == "log" {
		return m.topLevel("log")
	}
	defaults, err := m.topLevel("defaults")
	if err != nil || defaults == nil {
		return nil, err
	}
	for _, n := range defaults.Args() {
		if n.Kind == sexp.List && n.Head() == head {
			return n, nil
		}
	}
	return nil, nil
}

// topLevel is the top-level form with that head, or nil.
func (m *MainConf) topLevel(head string) (*sexp.Node, error) {
	nodes, err := sexp.Parse(m.File, []byte(m.Text))
	if err != nil {
		return nil, err
	}
	for _, n := range nodes {
		if n.Kind == sexp.List && n.Head() == head {
			return n, nil
		}
	}
	return nil, nil
}

// Save writes krino.conf, refusing a file that will not load and one that
// changed on disk since it was opened - the rules the directory files are
// saved by (GUI design §5.3). The previous text is kept as krino.conf.bak.
func (m *MainConf) Save() error {
	if diags := m.Check(); len(diags) > 0 {
		return fmt.Errorf("%s", diags[0])
	}
	on, err := os.ReadFile(m.File)
	if err != nil {
		return err
	}
	if sha256.Sum256(on) != m.stamp {
		return ErrChangedOnDisk
	}
	mode := os.FileMode(0o600)
	if fi, err := os.Stat(m.File); err == nil {
		mode = fi.Mode().Perm()
	}
	if err := os.WriteFile(m.File+".bak", on, mode); err != nil {
		return err
	}
	text := []byte(m.Text)
	if err := config.Replace(m.File, text); err != nil {
		return err
	}
	m.saved, m.stamp = m.Text, sha256.Sum256(text)
	return nil
}

// isMainSetting reports whether head is one of the defaults.
func isMainSetting(head string) bool {
	for _, s := range MainSettings {
		if s == head {
			return true
		}
	}
	return false
}