mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
bbs -config FILE config is screens on the local terminal for bbs.conf: its sections and keys, each with the sample's comment on it, its default and examples, and whether it takes effect at once or at a restart. The same screens are the sysop's tools' Configuration (F), on text terminals and 3270s. Keys are edited in place, unset, or taken from the sample's examples; sections made (uncommented where the file has them commented out), renamed and deleted (commented out). The file is checked as the BBS would read it, written whole with its permissions and owner kept and the old one as bbs.conf.bak, and not over a file changed since it was read. config get/set/unset do a key at a time, for scripts. internal/confedit edits the file as a sysop would by hand: every comment kept, a new key below the sample's example of it. Its catalog is read from bbs.conf.sample (embedded); a test keeps every key the loader reads in the sample, which gained comments where keys had none. SIGHUP (ExecReload in bbs.service.sample), or writing on the sysop's screen, reads the file again and puts in force what may change while callers are on: [general]'s names, texts, limits, timeouts, GeoIP and levels, [level], [menu], [strings], [chan] (but dir) and the forum's options; the rest is logged as waiting for a restart. A file the BBS would not start with changes nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package confedit
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
)
|
|
|
|
// WriteFile puts data in path whole or not at all: a file beside it
|
|
// renamed over it, with the old one's permissions and owner; the old one
|
|
// kept as path.bak (absent: there was none).
|
|
func WriteFile(path string, data, old []byte, absent bool) error {
|
|
mode := os.FileMode(0o640)
|
|
uid, gid := -1, -1
|
|
if fi, err := os.Stat(path); err == nil {
|
|
mode = fi.Mode().Perm()
|
|
if st, ok := fi.Sys().(*syscall.Stat_t); ok {
|
|
uid, gid = int(st.Uid), int(st.Gid)
|
|
}
|
|
}
|
|
if !absent {
|
|
if err := os.WriteFile(path+".bak", old, mode); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
f, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(f.Name())
|
|
if _, err := f.Write(data); err != nil {
|
|
f.Close()
|
|
return err
|
|
}
|
|
if err := f.Chmod(mode); err != nil {
|
|
f.Close()
|
|
return err
|
|
}
|
|
if uid >= 0 && os.Geteuid() == 0 {
|
|
f.Chown(uid, gid)
|
|
}
|
|
if err := f.Sync(); err != nil {
|
|
f.Close()
|
|
return err
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(f.Name(), path)
|
|
}
|