File: configfile.cc

package info (click to toggle)
xapian-omega 1.0.7-3+lenny2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,424 kB
  • ctags: 744
  • sloc: sh: 9,112; cpp: 7,954; makefile: 245; perl: 119
file content (125 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download
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
/* configfile.cc: Read a config file for omega.
 *
 * Copyright 2001 Lemur Consulting Ltd.
 * Copyright 2002 Ananova Ltd
 * Copyright 2003,2005,2007 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>

#include <fstream>
#include <iostream>

#include <sys/types.h>
#include "safesysstat.h"
#include "safeunistd.h"
#include <stdlib.h>

#include "configfile.h"

using namespace std;

static const char configfile_local[] = "omega.conf";
static const char configfile_system[] = CONFIGFILE_SYSTEM;
static const char configfile_envvar[] = "OMEGA_CONFIG_FILE";

string database_dir = "/var/lib/omega/data/";
string template_dir = "/var/lib/omega/templates/";
string log_dir = "/var/log/omega/";
string cdb_dir = "/var/lib/omega/cdb/";

/** Return true if the file fname exists.
 */
static bool
file_exists(const char * fname)
{
    struct stat sbuf;
    // exists && is a regular file or symlink
    return stat(fname, &sbuf) >= 0 && !S_ISDIR(sbuf.st_mode);
}

static bool
try_read_config_file(const char * cfile)
{
    ifstream in(cfile);
    if (!in) {
	if (file_exists(cfile))
	    throw string("Can't open configuration file `") + cfile + "'";
	return false;
    }

    while (in) {
	char line[1024];
	in.getline(line, sizeof(line));

	char *p = line;
	while (isspace((unsigned char)*p)) ++p;
	if (!*p || *p == '#') continue; // Ignore blank line and comments

	char *q = p;
	while (*q && !isspace((unsigned char)*q)) ++q;
	string name(p, q - p);

	p = q;
	while (isspace((unsigned char)*p)) ++p;
	q = p;
	while (*q && !isspace((unsigned char)*q)) ++q;
	string value(p, q - p);

	while (*q && isspace((unsigned char)*q)) ++q;
	if (value.empty() || *q) {
	    throw string("Bad line in configuration file `") + cfile + "'";
	}

	if (name == "database_dir") {
	    database_dir = value + "/";
	} else if (name == "template_dir") {
	    template_dir = value + "/";
	} else if (name == "log_dir") {
	    log_dir = value + "/";
	} else if (name == "cdb_dir") {
	    cdb_dir = value + "/";
	}
    }

    return true;
}

void
read_config_file()
{
    // Tries to find the config file in various ways.  If a config file
    // is found in a particular location but is not readable, an error
    // is thrown.  If a config file isn't found in a particular location,
    // the next candidate is tried.

    // First check if a location is specified in the environment variable.
    const char * cfile = getenv(configfile_envvar);
    if (cfile != NULL && cfile[0] != '\0') {
        if (try_read_config_file(cfile)) return;
    }

    // Next, try reading the local config file.
    if (try_read_config_file(configfile_local)) return;

    // Finally read the system config file.
    if (try_read_config_file(configfile_system)) return;

    // Just use the default configuration.
    return;
}