1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-01-09 17:55:57 +00:00

Exit if there's an error reading users/aliases files on startup

Today, when starting up, if there's an error reading the users or
aliases files, we only log but do not exit. And then those files will
not be attempted to be read on the periodic reload.

We also treat "file does not exist" as an error for users file, but not
aliases file, resulting in inconsistent behaviour between the two.

All of this makes some classes of problems (like permission errors) more
difficult to spot and troubleshoot. For example,
https://github.com/albertito/chasquid/issues/55.

So this patch makes errors reading users/aliases files on startup a
fatal error, and also unifies the "file does not exist" behaviour to
make it not an error in both cases.

Note that the behaviour on the periodic reload is unchanged: treat these
errors as fatal too. This may be changed in future patches.
This commit is contained in:
Alberto Bertogli
2024-05-10 09:11:35 +01:00
parent 0414af09b4
commit e6a9410377
15 changed files with 99 additions and 25 deletions

View File

@@ -33,6 +33,7 @@ import (
"crypto/subtle"
"errors"
"fmt"
"os"
"sync"
"golang.org/x/crypto/scrypt"
@@ -59,8 +60,8 @@ func New(fname string) *DB {
}
// Load the database from the given file.
// Return the database, and a fatal error if the database could not be
// loaded.
// Return the database, and an error if the database could not be loaded. If
// the file does not exist, that is not considered an error.
func Load(fname string) (*DB, error) {
db := New(fname)
err := protoio.ReadTextMessage(fname, db.db)
@@ -72,6 +73,12 @@ func Load(fname string) (*DB, error) {
db.db = &ProtoDB{Users: map[string]*Password{}}
}
if os.IsNotExist(err) {
// If the file does not exist now, it is not an error, as it might
// exist later and we want to be able to read it.
err = nil
}
return db, err
}