aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 8ba30adf6cc92de334fa46a31c9bdec2ed01004b (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
package main

import (
	"bufio"
	"os"
	"reflect"
	"sync"

	"go.lindenii.runxiyu.org/lindenii-common/misc"
	"go.lindenii.runxiyu.org/lindenii-common/scfg"
)

var config struct {
	Server_name string `scfg:"server_name"`
	Inbox_path  string `scfg:"inbox_path"`
}
var config_mutex sync.RWMutex

func load_config(path string) error {
	config_file, err := os.Open(path)
	if err != nil {
		return err
	}
	decoder := scfg.NewDecoder(bufio.NewReader(config_file))
	if func() error {
		config_mutex.Lock()
		defer config_mutex.Unlock()
		err = decoder.Decode(&config)
		if err != nil {
			return err
		}
		return nil
	}() != nil {
		return err
	}
	return nil
}

var directories struct {
	inbox misc.Dir_t
}
var directories_mutex sync.RWMutex

func prepare_dirs() error {
	directories_mutex.Lock()
	defer directories_mutex.Unlock()
	var err error
	directories.inbox, err = misc.Open_directory_readonly(config.Inbox_path)
	if err != nil {
		return err
	}
	return nil
}

// clean_up_dirs closes all directories. It should only be called before
// program exit.
func clean_up_dirs() {
	directories_mutex.Lock()
	defer directories_mutex.Unlock()
	directories_rv := reflect.ValueOf(directories)
	directories_rt := directories_rv.Type()
	for i := range directories_rt.NumField() {
		dir, ok := directories_rv.Field(i).Interface().(misc.Dir_t)
		if !ok {
			panic("directories contains a field that's not misc.Dir_t")
		}
		dir.Close()
	}
}