~ubuntu-branches/ubuntu/oneiric/strigi/oneiric

« back to all changes in this revision

Viewing changes to strigidaemon/bin/daemon/configupdater.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2011-09-20 08:50:25 UTC
  • mto: (1.1.20 upstream) (5.1.6 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20110920085025-wszfu6x8rshrjq0e
ImportĀ upstreamĀ versionĀ 0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Strigi Desktop Search
 
2
 *
 
3
 * Copyright (C) 2006 Flavio Castelli <flavio.castelli@gmail.com>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public License
 
16
 * along with this library; see the file COPYING.LIB.  If not, write to
 
17
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
#include <algorithm>
 
22
#include <iostream>
 
23
#include <fstream>
 
24
#include <getopt.h>
 
25
#include <set>
 
26
#include <string>
 
27
 
 
28
#include "daemonconfigurator.h"
 
29
#include "filters.h"
 
30
#include "strigilogging.h"
 
31
 
 
32
using namespace std;
 
33
 
 
34
/**
 
35
* Check if log4cxx file exists, otherwise creates a default one
 
36
**/
 
37
void
 
38
checkLogConf(const string& filename) {
 
39
    std::fstream confFile;
 
40
    confFile.open(filename.c_str(), std::ios::in);
 
41
    if (!confFile.is_open()){
 
42
        /*create the default configuration file*/
 
43
        confFile.open(filename.c_str(), std::ios::out);
 
44
        confFile << "# Set root logger level to DEBUG and its only appender to A1.\n";
 
45
        confFile << "log4j.rootLogger=DEBUG, A1\n\n";
 
46
        confFile << "# A1 is set to be a ConsoleAppender.\n";
 
47
        confFile << "log4j.appender.A1=org.apache.log4j.ConsoleAppender\n";
 
48
        confFile << "# A1 uses PatternLayout.\n";
 
49
        confFile << "log4j.appender.A1.layout=org.apache.log4j.PatternLayout\n";
 
50
        confFile << "log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n\n";
 
51
    }
 
52
    confFile.close();
 
53
}
 
54
 
 
55
void loadConfFile(string file, set<string>& entries, string name)
 
56
{
 
57
    fstream confFile;
 
58
    string line;
 
59
    char buffer [500];
 
60
    unsigned int counter = 0;
 
61
 
 
62
    confFile.open(file.c_str(), ios::in);
 
63
 
 
64
    if (confFile.is_open())
 
65
    {
 
66
        // read filter rules
 
67
        while (!confFile.eof())
 
68
        {
 
69
            confFile.getline(buffer, 500);
 
70
            line = buffer;
 
71
 
 
72
            if (line.size() > 0)
 
73
            {
 
74
                set<string>::iterator iter;
 
75
                iter = find(entries.begin(), entries.end(), line);
 
76
 
 
77
                if (iter == entries.end())
 
78
                {
 
79
                    // it's a new entry
 
80
                    counter++;
 
81
                    entries.insert (line);
 
82
                }
 
83
            }
 
84
        }
 
85
 
 
86
        confFile.close();
 
87
    }
 
88
 
 
89
    printf ("added %i new %s\n",counter, name.c_str());
 
90
}
 
91
 
 
92
void help()
 
93
{
 
94
    printf ("Usage: strigiconfupdaterman [options]\n");
 
95
    printf ("\t--merge-filters\tmerge old filters with new ones\n");
 
96
    printf ("\t--merge-path\tmerge old paths with new ones\n");
 
97
    printf ("\t-s\t\tsimulate, print to stdout\n");
 
98
    printf ("\t-o filename\tsave new conf to filename\n");
 
99
}
 
100
 
 
101
int main(int argc,char *argv[])
 
102
{
 
103
    if (argc == 1)
 
104
    {
 
105
        help();
 
106
        return 1;
 
107
    }
 
108
 
 
109
    string homedir = getenv("HOME");
 
110
    string daemondir = homedir+"/.strigi";
 
111
    string dirsfile = daemondir+"/dirstoindex";
 
112
    string pathfilterfile = daemondir+"/pathfilter.conf";
 
113
    string patternfilterfile = daemondir+"/patternfilter.conf";
 
114
    string conffile = daemondir+"/daemon.conf";
 
115
    string logconffile = daemondir+"/log.conf";
 
116
    string confout = conffile;
 
117
    bool   mergeFilters = false;
 
118
    bool   mergePath = false;
 
119
    bool   save = true;
 
120
    set<string> entries;
 
121
 
 
122
    // init logging
 
123
    checkLogConf(logconffile);
 
124
    STRIGI_LOG_INIT(logconffile);
 
125
 
 
126
    while (1)
 
127
     {
 
128
        int option_index = 0;
 
129
        int c;
 
130
 
 
131
        static struct option long_options[] = {
 
132
            {"merge-filters", 0, 0, 1},
 
133
            {"merge-path", 0, 0, 2},
 
134
            {"help", 0, 0, 'h'},
 
135
            {0, 0, 0, 0}
 
136
        };
 
137
 
 
138
        c = getopt_long (argc, argv, "h:o:s",
 
139
                 long_options, &option_index);
 
140
        if (c == -1)
 
141
            break;
 
142
 
 
143
        switch (c)
 
144
        {
 
145
            case 1:
 
146
                //merge filters
 
147
                mergeFilters = true;
 
148
                break;
 
149
            case 2:
 
150
                //merge path
 
151
                mergePath = true;
 
152
                break;
 
153
            case 'o':
 
154
                confout = string(optarg);
 
155
                break;
 
156
            case 's':
 
157
                save = false;
 
158
                break;
 
159
            case 'h':
 
160
                help();
 
161
                return 0;
 
162
                break;
 
163
            default:
 
164
                help();
 
165
                return 1;
 
166
        }
 
167
    }
 
168
 
 
169
    // init daemon configurator
 
170
    DaemonConfigurator config (conffile);
 
171
 
 
172
    // load path filtering rules
 
173
 
 
174
    if (mergeFilters) // read existing rules, avoid duplicate rules creation
 
175
    {
 
176
        printf ("merging ");
 
177
        entries = config.readFilteringRules();
 
178
    }
 
179
    else
 
180
        printf ("restoring ");
 
181
    printf ("path filtering rules\n");
 
182
 
 
183
    loadConfFile (pathfilterfile, entries, "path filtering rules");
 
184
    config.saveFilteringRules(entries, PathFilter::RTTI, false);
 
185
 
 
186
    entries.clear();
 
187
 
 
188
    // load pattern filtering rules
 
189
    printf ("restoring pattern filtering rules\n");
 
190
    loadConfFile (patternfilterfile, entries, "pattern filtering rules");
 
191
    config.saveFilteringRules(entries, PatternFilter::RTTI, true);
 
192
 
 
193
    entries.clear();
 
194
 
 
195
    // load dirs to index
 
196
    if (mergePath)
 
197
    {
 
198
        entries = config.getIndexedDirectories();
 
199
        printf ("merging ");
 
200
    }
 
201
    else
 
202
        printf ("restoring ");
 
203
    printf ("indexed directories paths\n");
 
204
 
 
205
    loadConfFile (dirsfile, entries, "indexed dirs paths");
 
206
    config.setIndexedDirectories( entries, "localhost", mergePath);
 
207
 
 
208
    if (save)
 
209
    {
 
210
        printf ("writing new configuration file\n");
 
211
        config.save (confout.c_str());
 
212
    }
 
213
    else
 
214
        cout << endl << endl << config;
 
215
 
 
216
    return 0;
 
217
}